kreait / tape-recorder-subscriber
该软件包已被废弃且不再维护。未建议替代包。
将HTTP交互记录下来,以便以后使用Ivory HTTP适配器的Tape Recorder订阅者重新播放
3.0.2
2016-07-16 08:46 UTC
Requires
- egeloen/http-adapter: ^0.8|^1.0
- symfony/event-dispatcher: ^2.6|^3.0
- symfony/yaml: ^2.6|^3.0
Requires (Dev)
- fabpot/php-cs-fixer: ^1.6
- phpunit/phpunit: ^4.6
This package is not auto-updated.
Last update: 2022-02-01 12:44:51 UTC
README
Ivory HTTP适配器的Tape Recorder订阅者与php-vcr类似,也使用相似的表述 :).
使用它,可以记录HTTP交互,例如在单元测试中,将它们存储为固定文件,并在未来的运行中重新播放。
一个示例固定文件(实际上用于此类测试)可以在以下位置找到:示例固定文件.
使用方法
use Ivory\HttpAdapter\EventDispatcherHttpAdapter; use Ivory\HttpAdapter\HttpAdapterFactory; use Kreait\Ivory\HttpAdapter\Event\Subscriber\TapeRecorderSubscriber; use Symfony\Component\EventDispatcher\EventDispatcher; $recorder = new TapeRecorderSubscriber(__DIR__.'/fixtures'); $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber($this->recorder); $http = new EventDispatcherHttpAdapter( HttpAdapterFactory::guess(), $eventDispatcher ); $recorder->insertTape('my_tape'); $recorder->startRecording(); $httpAdapter->get(...); // This interaction will be stored as a track. $recorder->stopRecording; $httpAdapter->get(...); // This interaction will not be stored. $recorder->eject(); // Stores the tape to the file system
记录模式
$recorder->setRecordingMode(...);
在使用Tape Recorder时,可以设置以下记录模式
模式 | 描述 |
---|---|
RECORDING_MODE_ONCE | (默认) 执行实际请求并将其存储到固定文件中,除非已存在固定文件。 |
RECORDING_MODE_OVERWRITE | 始终执行实际请求并覆盖固定文件。 |
RECORDING_MODE_NEVER | 始终执行实际请求但不写入固定文件。 |
单元测试中的使用示例
namespace My\Application\Tests; use Ivory\HttpAdapter\EventDispatcherHttpAdapter; use Ivory\HttpAdapter\HttpAdapterFactory; use Kreait\Ivory\HttpAdapter\Event\Subscriber\TapeRecorderSubscriber; use Symfony\Component\EventDispatcher\EventDispatcher; class MyTest extends extends \PHPUnit_Framework_TestCase { /** @var \Ivory\HttpAdapter\HttpAdapterInterface **/ protected $http; /** @var TapeRecorderSubscriber */ protected $recorder; protected function setUp() { $this->recorder = new TapeRecorderSubscriber(__DIR__.'/fixtures'); $http = HttpAdapterFactory::guess(); $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber($this->recorder); $this->http = new EventDispatcherHttpAdapter( $http, $eventDispatcher ); } protected function tearDown() { $this->recorder->eject(); } protected function testRequest() { // This will result in the file 'fixtures/testRequest.yml' $this->recorder->insertTape(__FUNCTION__); $this->recorder->startRecording(); $this->http->get(...); } }