承接 kreait/tape-recorder-subscriber 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kreait/tape-recorder-subscriber

最新稳定版本:3.0.2

Composer 安装命令:

composer require kreait/tape-recorder-subscriber

包简介

Record HTTP interactions to replay them later with the Tape Recorder Subscriber for the Ivory HTTP Adapter

README 文档

README

The Tape Recorder Subscriber for the Ivory HTTP Adapter works similarly to the php-vcr and also uses similar wordings :).

With it, it is possible to record the HTTP interactions, e.g. in Unit Tests, store them as Fixture files and replay them in future runs.

An example fixture (actually used for such a test) can be found here: Example fixture.

Usage

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
Recording modes
$recorder->setRecordingMode(...);

The following recording modes can be set when using the Tape Recorder:

Mode Description
RECORDING_MODE_ONCE (default) Performs a real request and stores it to a fixture, unless a fixture already exists.
RECORDING_MODE_OVERWRITE Always performs a real request and overwrites the fixture.
RECORDING_MODE_NEVER Always performs a real request and does not write a fixture.
Usage example in Unit Tests
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(...);
    }
}

统计信息

  • 总下载量: 672
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-23