Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP-VCR Lightningtalk

PHP-VCR Lightningtalk

From a internal presentation at Liip AG in Zürich.

Record HTTP interactions in your tests and replay them during future test runs for fast, deterministic and accurate tests. This is a PHP fork of the fabulous VCR for ruby library.

More info: http://php-vcr.github.io
Github: https://github.com/php-vcr/php-vcr

Avatar for Adrian Philipp

Adrian Philipp

April 15, 2014

More Decks by Adrian Philipp

Other Decks in Programming

Transcript

  1. Record HTTP interactions in your tests ...and replay them during

    future test runs for fast, deterministic and accurate tests. Port of the VCR library for ruby
  2. Why? Much faster tests Independent of API Inspect HTTP requests

    Deterministic tests (no rate limits, external errors) See if external APIs change (version control of recordings) See when unexpected HTTP requests happen No manual mocking
  3. Usage ! // start interception VCR::turnOn(); ! // start recording

    VCR::insertCassette('unittest_example.yml'); ! // do a request file_get_contents('http://example.com'); ! // stop recording VCR::eject(); ! // stop interception VCR::turnOff(); Example usage with plain PHP:
  4. PHPUnit ! /** * @vcr unittest_example.yml */ public function testShouldCrawlWebsite()

    { // do a request $crawler = new Crawler(); $crawler->crawl('http://example.com'); } Example PHPUnit integration
  5. Record request: method: GET url: 'http://example.com' headers: Host: example.com User-Agent:

    'Guzzle/3.8.1 curl/7.30.0 PHP/5.4.16' response: status: 302 headers: Accept-Ranges: bytes Cache-Control: max-age=604800 Content-Type: text/html Date: 'Wed, 26 Feb 2014 21:50:54 GMT' Etag: '"359670651"' Expires: 'Wed, 05 Mar 2014 21:50:54 GMT' Last-Modified: 'Fri, 09 Aug 2013 23:54:35 GMT' Server: 'EOS (lax004/45BF)' Content-Length: '1270' body: "<!doctype html><html><head>…" Example recording in YAML format:
  6. Disclaimer Not so easy in PHP (no monkey patching) How

    it works: Replacing function calls on the fly using a stream wrapper filter when including PHP files.