Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/VCR/Cassette.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use VCR\Storage\Storage;
use VCR\Util\Assertion;
use VCR\Event\BeforePlaybackEvent;

/**
* A Cassette records and plays back pairs of Requests and Responses in a Storage.
Expand Down Expand Up @@ -56,7 +57,7 @@ public function __construct($name, Configuration $config, Storage $storage)
*/
public function hasResponse(Request $request)
{
return $this->playback($request) !== null;
return $this->getResponse($request, true) !== null;
}

/**
Expand All @@ -68,8 +69,24 @@ public function hasResponse(Request $request)
*/
public function playback(Request $request)
{
return $this->getResponse($request);
}

/**
* Returns a response for given request or null if not found.
* Optionally dispatches VCR_BEFORE_PLAYBACK event.
*
* @param Request $request Request to check if it was recorded.
* @param boolean $dispatchEvents Whether to dispatch events or not. Default is false.
*
* @return Response|null Response for specified request.
*/
protected function getResponse(Request $request, $dispatchEvents = false) {
foreach ($this->storage as $recording) {
$storedRequest = Request::fromArray($recording['request']);
if ($dispatchEvents) {
$this->config->dispatch(VCREvents::VCR_BEFORE_PLAYBACK, new BeforePlaybackEvent($storedRequest, $this));
}
if ($storedRequest->matches($request, $this->getRequestMatchers())) {
return Response::fromArray($recording['response']);
}
Expand Down
39 changes: 39 additions & 0 deletions src/VCR/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace VCR;

use VCR\Util\Assertion;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;

/**
* Configuration stores a Videorecorders configuration options.
Expand Down Expand Up @@ -143,6 +146,11 @@ class Configuration
VCR::MODE_NONE,
);

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* Returns the current blacklist.
*
Expand Down Expand Up @@ -385,4 +393,35 @@ private function assertValidCassettePath($cassettePath)
. "\\VCR\\VCR::configure()->setCassettePath('directory')."
);
}

/**
* @param EventDispatcherInterface $dispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $dispatcher)
{
$this->eventDispatcher = $dispatcher;
}

/**
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
if (!$this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
}
return $this->eventDispatcher;
}

/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch.
* @param Event $event The event to pass to the event handlers/listeners.
* @return Event
*/
public function dispatch($eventName, Event $event = null)
{
return $this->getEventDispatcher()->dispatch($eventName, $event);
}
}
49 changes: 4 additions & 45 deletions src/VCR/Videorecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
use VCR\Event\AfterHttpRequestEvent;
use VCR\Event\AfterPlaybackEvent;
use VCR\Event\BeforeHttpRequestEvent;
use VCR\Event\BeforePlaybackEvent;
use VCR\Event\BeforeRecordEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;

/**
* A videorecorder records requests on a cassette.
Expand Down Expand Up @@ -54,11 +50,6 @@ class Videorecorder
*/
protected $isOn = false;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* Creates a videorecorder instance.
*
Expand All @@ -73,37 +64,6 @@ public function __construct(Configuration $config, HttpClient $client, VCRFactor
$this->factory = $factory;
}

/**
* @param EventDispatcherInterface $dispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $dispatcher)
{
$this->eventDispatcher = $dispatcher;
}

/**
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
if (!$this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
}
return $this->eventDispatcher;
}

/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch.
* @param Event $event The event to pass to the event handlers/listeners.
* @return Event
*/
private function dispatch($eventName, Event $event = null)
{
return $this->getEventDispatcher()->dispatch($eventName, $event);
}

/**
* Turns on this videorecorder.
*
Expand Down Expand Up @@ -217,9 +177,8 @@ public function handleRequest(Request $request)
}

if ($this->cassette->hasResponse($request)) {
$this->dispatch(VCREvents::VCR_BEFORE_PLAYBACK, new BeforePlaybackEvent($request, $this->cassette));
$response = $this->cassette->playback($request);
$this->dispatch(VCREvents::VCR_AFTER_PLAYBACK, new AfterPlaybackEvent($request, $response, $this->cassette));
$this->config->dispatch(VCREvents::VCR_AFTER_PLAYBACK, new AfterPlaybackEvent($request, $response, $this->cassette));
return $response;
}

Expand All @@ -232,11 +191,11 @@ public function handleRequest(Request $request)

$this->disableLibraryHooks();

$this->dispatch(VCREvents::VCR_BEFORE_HTTP_REQUEST, new BeforeHttpRequestEvent($request));
$this->config->dispatch(VCREvents::VCR_BEFORE_HTTP_REQUEST, new BeforeHttpRequestEvent($request));
$response = $this->client->send($request);
$this->dispatch(VCREvents::VCR_AFTER_HTTP_REQUEST, new AfterHttpRequestEvent($request, $response));
$this->config->dispatch(VCREvents::VCR_AFTER_HTTP_REQUEST, new AfterHttpRequestEvent($request, $response));

$this->dispatch(VCREvents::VCR_BEFORE_RECORD, new BeforeRecordEvent($request, $response, $this->cassette));
$this->config->dispatch(VCREvents::VCR_BEFORE_RECORD, new BeforeRecordEvent($request, $response, $this->cassette));
$this->cassette->record($request, $response);
$this->enableLibraryHooks();

Expand Down
39 changes: 36 additions & 3 deletions tests/VCR/VCRTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
*/
class VCRTest extends \PHPUnit_Framework_TestCase
{
public static function resetCassettePath() {
VCR::configure()->setCassettePath('tests/fixtures') ;
}

public static function setupBeforeClass()
{
VCR::configure()->setCassettePath('tests/fixtures') ;
self::resetCassettePath();
}

public function setUp() {
$this->events = array();
}

public function testUseStaticCallsNotInitialized()
Expand Down Expand Up @@ -142,7 +149,6 @@ public function testShouldDispatchBeforeAndAfterPlaybackWhenCassetteHasResponse(
);
VCR::eject();
VCR::turnOff();

}

public function testShouldDispatchBeforeAfterHttpRequestAndBeforeRecordWhenCassetteHasNoResponse()
Expand All @@ -163,6 +169,33 @@ public function testShouldDispatchBeforeAfterHttpRequestAndBeforeRecordWhenCasse
);
VCR::eject();
VCR::turnOff();
self::resetCassettePath();
}

public function testBeforePlaybackIsDispatchedForEachCassetteInteraction() {
$counts = array();
VCR::configure()
->setMode('none')
->enableLibraryHooks(array('curl'))
->getEventDispatcher()->addListener(VCREvents::VCR_BEFORE_PLAYBACK, function(Event $event) use (&$counts) {
$eventName = $event->getName();
if (isset($counts[$eventName])) {
$counts[$eventName] += 1;
} else {
$counts[$eventName] = 1;
}
});
VCR::turnOn();
VCR::insertCassette('unittest_event_test');

$this->doCurlGetRequest('http://google.com/');

$this->assertEquals(
array(VCREvents::VCR_BEFORE_PLAYBACK => 2),
$counts
);
VCR::eject();
VCR::turnOff();
}

private function recordAllEvents()
Expand All @@ -175,7 +208,7 @@ private function recordAllEvents()
VCREvents::VCR_BEFORE_RECORD,
);
foreach ($allEventsToListen as $eventToListen) {
VCR::getEventDispatcher()->addListener($eventToListen, array($this, 'recordEvent'));
VCR::configure()->getEventDispatcher()->addListener($eventToListen, array($this, 'recordEvent'));
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/fixtures/unittest_event_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

-
request:
method: GET
url: 'http://google2.com/'
headers:
Host: google.com
Content-Length: 0
response:
status: 301
headers:
Location: 'http://www.google.com/'
Content-Type: 'text/html; charset=UTF-8'
Date: 'Mon, 13 May 2013 08:15:58 GMT'
Expires: 'Wed, 12 Jun 2013 08:15:58 GMT'
Cache-Control: 'public, max-age=2592000'
Server: gws
Content-Length: '219'
X-XSS-Protection: '1; mode=block'
X-Frame-Options: SAMEORIGIN
body: "This is a curl test dummy."
-
request:
method: GET
url: 'http://google.com/'
headers:
Host: google.com
Content-Length: 0
response:
status: 301
headers:
Location: 'http://www.google.com/'
Content-Type: 'text/html; charset=UTF-8'
Date: 'Mon, 13 May 2013 08:15:58 GMT'
Expires: 'Wed, 12 Jun 2013 08:15:58 GMT'
Cache-Control: 'public, max-age=2592000'
Server: gws
Content-Length: '219'
X-XSS-Protection: '1; mode=block'
X-Frame-Options: SAMEORIGIN
body: "This is a curl test dummy."