korchasa / php-vhs
此包已被 废弃 并不再维护。未建议替换包。
PHP 的 HTTP 请求/响应记录和模拟库
0.5.0
2019-09-22 23:56 UTC
Requires
- php: >=7.1
- ext-json: *
- korchasa/matcho: ^1.1
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- infection/infection: ^0.12
- phpstan/phpstan: ^0.10
- phpunit/phpunit: ^6.0 || ^7.0
- rappasoft/laravel-helpers: dev-master
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.9.2 || ^3.3.1
This package is auto-updated.
Last update: 2022-09-23 18:03:51 UTC
README
安装
composer require --dev korchasa/php-vhs
客户端测试
1. 使用 VhsTestCase
trait 编写测试。将客户端调用包围在 assertVhs()
中。
<?php declare(strict_types=1); namespace korchasa\Vhs\Tests; use korchasa\Vhs\VhsTestCase; use PHPUnit\Framework\TestCase; class AwesomeClientOfflineTest extends TestCase { use VhsTestCase; /** @var AwesomeClient */ private $packagistClient; public function setUp() { $client = new AwesomeClient("bla-bla-bla-bla-bla-bla.commmm"); $client->setGuzzle($this->connectVhs($client->getGuzzle(), $offline = true)); $this->packagistClient = $client; } public function testSuccessStory(): void { $this->assertVhs(function () { $packageName = $this->packagistClient->getFirstTagName(); $this->assertEquals('korchasa/php-vhs', $packageName); }); } public function testWithFail(): void { $this->assertVhs(function () { $resp = $this->packagistClient->auth(); $this->assertEquals(403, $resp); }); } }
2. 运行测试以记录磁带(测试将不完整)
磁带 tests/vhs_cassettes/AwesomeClientOfflineTest_testSuccessStory.json
内容
[ { "request": { "uri": "https:\/\/httpbin.org\/anything?name=korchasa\/php-vhs", "method": "GET", "headers": { "X-Foo": [ "Bar" ], "Host": [ "httpbin.org" ] }, "body": "", "body_format": "raw" }, "response": { "status": 200, "headers": { "Content-Type": [ "application\/json" ] }, "body": { "args": { "name": "korchasa\/php-vhs" }, "data": "", "files": {}, "form": {}, "headers": { "Host": "httpbin.org", "User-Agent": "***", "X-Foo": "Bar" }, "json": null, "method": "GET", "origin": "***", "url": "https:\/\/httpbin.org\/anything?name=korchasa%2Fphp-vhs" }, "body_format": "json" } } ]
3. 再次运行测试
如果磁带已存在,我们将检查请求并替换响应为磁带中记录的响应。