php vcr
1.8.0
这是 VCR Ruby 库到 PHP 的移植。
记录测试套件的 HTTP 交互,并在未来的测试运行期间重放它们,以实现快速、确定性、准确的测试。可以在 php-vcr 网站上找到一些文档。
免责声明:在 PHP 中执行此操作并不像在支持猴子修补的编程语言中那么容易(我正在看着你,Ruby)
$http_response_header
,参见#96)VCRVCR::turnOn();
在你的tests/bootstrap.php
VCRVCR::turnOn();
在你的tests/bootstrap.php
使用静态方法调用:
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 取决于:
Composer 安装除curl 等扩展之外的所有依赖项。
为了运行所有测试,您需要使用 Composer 获取开发依赖项:
composer install
composer test
变更日志已移至 PHP-VCR 发布页面。
旧的变更日志条目
版权所有 (c) 2013-2023 阿德里安·菲利普。根据 MIT 许可条款发布。有关详细信息,请参阅许可证。贡献者