PHP code example of kreait / tape-recorder-subscriber

1. Go to this page and download the library: Download kreait/tape-recorder-subscriber library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kreait / tape-recorder-subscriber example snippets


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(...);

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(...);
    }
}