Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

php-vcr

> 测试质量
Open source

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

1.2K stars0 likes0 views
WebsiteGitHub

About

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

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. Full documentation lives at php-vcr.github.io/php-vcr — or browse the raw Markdown in docs/.

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 — see Supported HTTP libraries below
  • 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)

Usage example

⚠️ Turn VCR on as soon as possible — right after Composer's autoloader, before any code that calls curl_* or uses SoapClient gets loaded. That call is what registers the interception; turn it back off right afterwards (VCR::turnOn(); VCR::turnOff(); in your bootstrap file) if you don't want hooks live for the whole run — each test can cheaply call turnOn() again only when it actually wants a cassette. Details: How VCR works.

Using static method calls:

…

Forgetting to insert a cassette throws immediately, instead of silently hitting the network:

public function testShouldThrowExceptionIfNoCasettePresent()
{
    $this->expectException(\BadMethodCallException::class);
    $this->expectExceptionMessage(
        "Invalid http request. No cassette inserted. Please make sure to insert "
        . "a cassette in your unit test using VCR::insertCassette('name');"
    );
    \VCR\VCR::turnOn();
    file_get_contents('http://example.com');
}

Supported HTTP libraries

All three hooks (stream_wrapper, curl, soap) are enabled by default when you call VCR::turnOn(). Full interception details and how to enable only specific hooks: Library Hooks.

Record modes

The record mode controls how VCR behaves when a cassette is inserted: new_episodes (default), once, none, all. Full behaviour per mode: Record Modes.

Recording identical requests

By default php-vcr records identical requests separately and replays them in the same order they were made. Details and how to change this: Cassettes → identical requests.

Installation

Simply run the following command:

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

Dependencies

PHP-VCR depends on PHP 8 and the curl extension, plus a few Composer packages Composer installs for you. Full requirements and the tested HTTP library matrix: Requirements.

Documentation

Full documentation — searchable, versioned, dark mode — lives at php-vcr.github.io/php-vcr. Or browse the raw Markdown directly in docs/:

  • Getting Started · How VCR works
  • Cassettes · Record Modes · Request Matching
  • How-to: PHPUnit · Codeception · Filter sensitive data · SOAP · Storage factory / custom storage
  • Reference: Configuration · Events · Storage backends

Contributing

Bug reports, feature requests and pull requests are welcome — see CONTRIBUTING.md for how this repository is set up, the pre-push checks, and why documentation is part of every change.

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-2026 Adrian Philipp. Released under the terms of the MIT license. See LICENSE for details. Contributors

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPaccurate-testsapi-testapi-testingdeterministic

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.