php-vcr / php-vcr
记录测试套件的HTTP交互,并在未来的测试运行中重新播放,以实现快速、确定性和准确的测试。
1.7.0
2024-02-23 22:40 UTC
Requires
- php: ^8,<8.2|>=8.2.9,<8.4
- ext-curl: *
- beberlei/assert: ^3.2.5
- symfony/event-dispatcher: ^4|^5|^6
- symfony/event-dispatcher-contracts: ^1|^2|^3
- symfony/yaml: ^3|^4|^5|^6
Requires (Dev)
- ext-soap: *
- editorconfig-checker/editorconfig-checker: ^10.3
- friendsofphp/php-cs-fixer: ^3.0
- guzzlehttp/guzzle: ^7
- mikey179/vfsstream: ^1.6.10
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1
- phpstan/phpstan-beberlei-assert: ^1
- phpstan/phpstan-phpunit: ^1
- phpunit/phpunit: ^9.5.10
- thecodingmachine/phpstan-strict-rules: ^1
- dev-master
- 1.7.x-dev
- 1.7.0
- 1.6.x-dev
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.x-dev
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.5.0alpha1
- 1.5.0alpha0
- 1.4.x-dev
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.x-dev
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-383-backport-fix-code-transformation-on-streams-which-are-divided-into-several-chunks
- dev-feature/storage-refactoring
- dev-f-stream-processor-tests
This package is auto-updated.
Last update: 2024-08-25 11:43:41 UTC
README
这是Ruby库VCR到PHP的移植。
记录测试套件的HTTP交互,并在未来的测试运行中重新播放,以实现快速、确定性和准确的测试。有关文档,请访问php-vcr网站。
免责声明:在PHP中这样做并不像支持猴子补丁的语言(比如Ruby)那样简单。
功能
- 自动记录和重新播放您的HTTP(s)交互,无需设置/配置代码。
- 支持常见的http函数和扩展
- 使用streamWrapper的任何东西:fopen()、fread()、file_get_contents()等,无需任何修改(除了http_response_header,请参阅#96)
- 通过在您的
tests/bootstrap.php
中添加\VCR\VCR::turnOn();
来支持SoapClient - 通过在您的
tests/bootstrap.php
中添加\VCR\VCR::turnOn();
来支持curl()
- 同一请求可以在不同的测试中接收到不同的响应--只需使用不同的磁带。
- 通过设置记录模式禁用所有您没有明确允许的HTTP请求
- 基于HTTP方法、URI、主机、路径、正文和头部的请求匹配是可配置的,或者您可以轻松实现自定义请求匹配器来处理任何需求。
- 记录的请求和响应以您选择的序列化格式存储在磁盘上(目前内置YAML和JSON,您可以轻松实现自己的自定义序列化器)
- 支持PHPUnit注解。
使用示例
使用静态方法调用
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'); } }
您可以使用phpunit-testlistener-vcr在PHPUnit中使用注解
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. } }
安装
只需运行以下命令
$ composer require --dev php-vcr/php-vcr
依赖关系
PHP-VCR依赖于
- PHP 8
- Curl扩展
- symfony/event-dispatcher
- symfony/yaml
- beberlei/assert
Composer安装所有依赖项,除了curl之类的扩展。
运行测试
要运行所有测试,您需要使用composer获取开发依赖项
composer install composer test
更改日志
更改日志已移动到PHP-VCR发布页面。
版权
版权(c)2013-2023 Adrian Philipp。在MIT许可下发布。有关详细信息,请参阅LICENSE。 贡献者