This is a port of VCR for ruby.
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
- Automatically records and replays your HTTP(s) interactions with minimal setup/configuration code.
- Supports common http functions and extensions
- everyting using streamWrapper: fopen(), fread(), file_get_contents(), ... without any modification
- SoapClient using your own wrapper class
- curl(), using runkit extension and
runkit.internal_override=1in your php.ini - Todo: curl_multi()
- The same request can receive different responses in different tests--just use different cassettes.
- Disables all HTTP requests that you don't explicitly allow (except SoapClient if not configured).
- Request matching is configurable based on HTTP method, URI, host, path, body and headers, or you can easily implement a custom request matcher to handle any need.
- The recorded requests and responses are stored on disk in a serialization format of your choice (currently YAML and JSON are built in, and you can easily implement your own custom serializer)
- Todo: Recorded requests and responses can easily be inspected and edited.
- Todo: Supports PHPUnit annotations.
- Todo: Automatically filters confidential or private information like passwords, auth tokens and emails.
- Todo: Automatically re-records cassettes on a configurable regular interval to keep them fresh and current.
Using annotations:
class VCRTest extends \PHPUnit_Framework_TestCase { public function setUp() { // Initialize VCR VCR::init(); } public function testNoCassetteUsed() { // Now all HTTP requests will be intercepted, an exception is thrown // if you don't provide a @VCR:useCassette($name) annotation, example: $this->setExpectedException('\BadMethodCallException'); file_get_contents('http://example.com'); } /** * You can use a test method annotation... * @VCR:useCassette('example') */ public function testUsingAnnotation() { // Following request will be recorded once and replayed in furture test runs $result = file_get_contents('http://example.com'); $this->assertNotEmpty($result); } }Using inline method calls:
class VCRTest extends \PHPUnit_Framework_TestCase { public function setUp() { // Initialize VCR VCR::init(); } public function testUsingInlineMethodCall() { // .... or use an inline method call VCR::useCassette('example'); // Following request will be recorded once and replayed in furture test runs $result = file_get_contents('http://example.com'); $this->assertNotEmpty($result); } public function tearDown() { // When using inline method calls, make sure to clean up after every test // This is not needed when using annotations VCR::eject(); } }There is no release yet, sorry.
git clone git@github.com:adri/php-vcr cd php-vcr composer install --dev phpunit testsPHP-VCR depends on:
- PHP 5.3+
- Curl extension
- HTTP library Guzzle
- symfony/yaml
- beberlei/assert
- (optional) runkit extension with
runkit.internal_override=1in php.ini if you want to intercept curl
Composer installs all depenencies except extensions like curl or runkit.
In order to run all tests you need to get development dependencies using composer:
composer install -dev phpunit ./tests- 2013-02-22 Added YAML support
- 2013-02-21 Added custom request matcher
- 2013-02-21 Added JSON storage which uses less memory
- 2013-02-21 Added support for binary data
- 2013-02-20 Added Soap support
- 2013-02-19 Curl hook fixes, more tests
- 2013-02-18 First prototype
Copyright (c) 2013 Adrian Philipp. Released under the terms of the MIT license. See LICENSE for details.
