2017 © Pedro Peláez
 

library php-vcr

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

image

php-vcr/php-vcr

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  • Wednesday, July 25, 2018
  • by adri
  • Repository
  • 31 Watchers
  • 731 Stars
  • 346,736 Installations
  • PHP
  • 86 Dependents
  • 0 Suggesters
  • 107 Forks
  • 74 Open issues
  • 51 Versions
  • 9 % Grown

The README.md

PHP-VCR, (*1)

Continuous Integration Code Coverage Scrutinizer Quality Score, (*2)

This is a port of the VCR Ruby library to PHP., (*3)

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests. A bit of documentation can be found on the php-vcr website., (*4)

Disclaimer: Doing this in PHP is not as easy as in programming languages which support monkey patching (I'm looking at you, Ruby), (*5)

Features

  • Automatically records and replays your HTTP(s) interactions with minimal setup/configuration code.
  • Supports common http functions and extensions
    • everything using streamWrapper: fopen(), fread(), file_get_contents(), ... without any modification (except $http_response_header see #96)
    • SoapClient by adding \VCR\VCR::turnOn(); in your tests/bootstrap.php
    • curl(), by adding \VCR\VCR::turnOn(); in your tests/bootstrap.php
  • The same request can receive different responses in different tests -- just use different cassettes.
  • Disables all HTTP requests that you don't explicitly allow by setting the record mode
  • 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)
  • Supports PHPUnit annotations.

Usage example

Using static method calls:, (*6)

``` php class VCRTest extends TestCase { public function testShouldInterceptStreamWrapper() { // After turning on the VCR will intercept all requests \VCR\VCR::turnOn();, (*7)

    // Record requests and responses in cassette file 'example'
    \VCR\VCR::insertCassette('example');

    // Following request will be recorded once and replayed in future test runs
    $result = file_get_contents('http://example.com');
    $this->assertNotEmpty($result);

    // To stop recording requests, eject the cassette
    \VCR\VCR::eject();

    // Turn off VCR to stop intercepting requests
    \VCR\VCR::turnOff();
}

public function testShouldThrowExceptionIfNoCasettePresent()
{
    $this->setExpectedException(
        'BadMethodCallException',
        "Invalid http request. No cassette inserted. Please make sure to insert "
        . "a cassette in your unit test using VCR::insertCassette('name');"
    );
    \VCR\VCR::turnOn();
    // If there is no cassette inserted, a request throws an exception
    file_get_contents('http://example.com');
}

}, (*8)


You can use annotations in PHPUnit by using [phpunit-testlistener-vcr](https://github.com/php-vcr/phpunit-testlistener-vcr): ``` php class VCRTest extends TestCase { /** * @vcr unittest_annotation_test */ public function testInterceptsWithAnnotations() { // Requests are intercepted and stored into tests/fixtures/unittest_annotation_test. $result = file_get_contents('http://google.com'); $this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using annotations).'); // VCR is automatically turned on and off. } }

Installation

Simply run the following command:, (*9)

``` bash $ composer require --dev php-vcr/php-vcr, (*10)


## Dependencies PHP-VCR depends on: * PHP 8 * Curl extension * [symfony/event-dispatcher](https://github.com/symfony/event-dispatcher) * [symfony/yaml](https://github.com/symfony/yaml) * [beberlei/assert](https://github.com/beberlei/assert) Composer installs all dependencies except extensions like curl. ## Run tests In order to run all tests you need to get development dependencies using composer: ``` php composer install composer test

Changelog

The changelog has moved to the PHP-VCR releases page., (*11)

Old changelog entries, (*12)

Copyright (c) 2013-2023 Adrian Philipp. Released under the terms of the MIT license. See LICENSE for details. Contributors, (*13)

The Versions

25/07 2018

dev-master

9999999-dev

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

25/07 2018

dev-fix/integration-tests

dev-fix/integration-tests

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

30/05 2018

1.4.4

1.4.4.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

30/05 2018

1.4.3

1.4.3.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

08/12 2017

1.4.2

1.4.2.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

20/10 2017

dev-fix/is_writable_nfs

dev-fix/is_writable_nfs

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

18/10 2017

1.4.1

1.4.1.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

18/10 2017

1.4.0

1.4.0.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

18/10 2017

1.3.x-dev

1.3.9999999.9999999-dev

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

18/10 2017

1.3.4

1.3.4.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

18/10 2017

1.3.3

1.3.3.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

23/12 2016

dev-feature/storage-refactoring

dev-feature/storage-refactoring

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

23/12 2016

1.3.2

1.3.2.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

27/11 2016

dev-feature/pullrequest-template

dev-feature/pullrequest-template

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

27/10 2016

dev-feature/composer-test

dev-feature/composer-test

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

26/10 2016

1.3.1

1.3.1.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

19/10 2016

1.3.0

1.3.0.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

25/01 2016

1.2.8

1.2.8.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

07/01 2016

1.2.7

1.2.7.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

29/09 2015

1.2.6

1.2.6.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

28/06 2015

dev-requests-integration

dev-requests-integration

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

19/06 2015

1.2.5

1.2.5.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

02/03 2015

1.2.4

1.2.4.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

27/02 2015

dev-fix-replace-across-bucket-borders

dev-fix-replace-across-bucket-borders

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

24/02 2015

1.2.3

1.2.3.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

21/02 2015

1.2.2

1.2.2.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

19/02 2015

1.2.1

1.2.1.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

24/01 2015

1.2

1.2.0.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

11/01 2015

1.1.8

1.1.8.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

26/11 2014

1.1.7

1.1.7.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

23/10 2014

1.1.6

1.1.6.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

11/09 2014

1.1.5

1.1.5.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

25/04 2014

1.1.4

1.1.4.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

12/04 2014

1.1.3

1.1.3.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

12/03 2014

1.1.2

1.1.2.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

27/02 2014

1.1.1

1.1.1.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

22/02 2014

1.1.0

1.1.0.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

19/02 2014

1.0.7

1.0.7.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

28/01 2014

dev-remove-runkit-dependency

dev-remove-runkit-dependency

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

23/01 2014

dev-fix-stat-error

dev-fix-stat-error

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

12/01 2014

1.0.6

1.0.6.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

12/01 2014

dev-dependency-update

dev-dependency-update

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

22/10 2013

dev-f-stream-processor-tests

dev-f-stream-processor-tests

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

22/10 2013

dev-move-soap-client-test

dev-move-soap-client-test

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

13/10 2013

1.0.5

1.0.5.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

13/10 2013

dev-f-soap

dev-f-soap

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

22/07 2013

1.0.4

1.0.4.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

05/06 2013

1.0.3

1.0.3.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

21/05 2013

1.0.2

1.0.2.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

21/05 2013

1.0.1

1.0.1.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp

15/05 2013

1.0.0

1.0.0.0

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adrian Philipp