Giter VIP home page Giter VIP logo

vcrbundle's Introduction

PHP-VCR

Continuous Integration Code Coverage Scrutinizer Quality Score

This is a port of the VCR Ruby library to PHP.

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.

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

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:

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

        // 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');
    }
}

You can use annotations in PHPUnit by using phpunit-testlistener-vcr:

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:

$ composer require --dev php-vcr/php-vcr

Dependencies

PHP-VCR depends on:

Composer installs all dependencies except extensions like curl.

Run tests

In order to run all tests you need to get development dependencies using composer:

composer install
composer test

Changelog

The changelog has moved to the PHP-VCR releases page.

Old changelog entries

Copyright

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

vcrbundle's People

Contributors

higidi avatar janvernieuwe avatar k-phoen avatar simonhard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vcrbundle's Issues

Symfony 4

The package is currently not symfony 4 compatible.

$ composer require php-vcr/vcr-bundle --dev
Using version ^1.4 for php-vcr/vcr-bundle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for php-vcr/vcr-bundle ^1.4 -> satisfiable by php-vcr/vcr-bundle[1.4.0].
    - Conclusion: remove symfony/framework-bundle v4.0.3
    - Conclusion: don't install symfony/framework-bundle v4.0.3
    - php-vcr/vcr-bundle 1.4.0 requires symfony/framework-bundle ~2.6||~3.0 -> satisfiable by symfony/framework-bundle[2.6.x-dev, 2.7.x-dev, 2.8.x-dev, 3.0.x-dev, 3.1.x-dev, 3.2.x-dev, 3.3.x-dev, 3.4.x-dev].
    - Can only install one of: symfony/framework-bundle[2.7.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[2.8.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[3.0.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[3.1.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[3.2.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[3.3.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[3.4.x-dev, v4.0.3].
    - Can only install one of: symfony/framework-bundle[2.6.x-dev, v4.0.3].
    - Installation request for symfony/framework-bundle (locked at v4.0.3, required as ^4.0) -> satisfiable by symfony/framework-bundle[v4.0.3].


Installation failed, reverting ./composer.json to its original content.
´´´

Register custom request matcher when using bundle?

hi everyone!

first of all i want to say "thank you!" for providing such a useful bundle!

we have integrated php-vcr in our phpunit testsuite and take advantage of it, when external services will be requested during our integration tests.

to match the recorded request with the requests generated during our tests, i had to provide a custom request matcher callback, as the generated requests currently contain randomly generated parameters like a token in a "back"-url.

see this gist how we use it in our phpunit bootstrap.php

https://gist.github.com/Headd2k/62f6d271a2b7890fd59a76254bcfafc9

question: how would i register such a custom request matcher when using the bundle? i would like to enable php-vcr via the bundle for our behat testsuite but i think i would need to register my custom handler to get it working with our cases.

as a workaround i think i could setup php-vcr in my app_test.php but this would make the bundle obsolete.

any ideas?

thanks in advance!

kai

Missing ResetInterface method

PHP Fatal error: Class VCR\VCRBundle\DataCollector\VCRDataCollector contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Contracts\Service\ResetInterface::reset) in /home/project/src/vendor/php-vcr/vcr-bundle/DataCollector/VCRDataCollector.php on line 61

Use KernelEvents for TurnOn and TurnOff recodings

Hi. I've got idea about testing Behat scenarios with php-vcr...

Why should we put logic in AppKernel.php like this and in app_test.php like this? Maybe use Kernel Events for this?

  1. On KernelEvents::REQUEST with high priority we do all what we do on AppKernel.php
  2. On KernelEvents::TERMINATE with lowest priority we "eject" cassette and "stop recording"

Conclusions:

  1. we have clean AppKernel.php
  2. we could change dynamic behavior via EventSubscriber logic.

PS: Maybe It would be nice to create Extension for Behat?

Proper testing

Would be really good to have:
Unit tests
Travis building against the same versions as PHP-VCR
Test agains symfony 2.3 to 3.1 using composer require in travis matrix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.