Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Remove dependency on request parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Apr 10, 2015
1 parent 433ce48 commit 7c983c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 66 deletions.
16 changes: 5 additions & 11 deletions src/Event/Subscriber/TapeRecorderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ public function onPreSend(PreSendEvent $event)
&& $this->recordingMode !== self::RECORDING_MODE_OVERWRITE
) {
$track = $this->currentTape->getTrackForRequest($request);
$event->setRequest($request->withParameter('track', $track));
$this->currentTape->play($track);
}

Expand All @@ -203,14 +202,12 @@ public function onPostSend(PostSendEvent $event)

$request = $event->getRequest();

if (!$request->hasParameter('track')) {
// Nothing to do here. Since we are in recording mode, the request should
// have a track, but we check nonetheless, it's better to be safe than sorry
if (!$this->currentTape->hasTrackForRequest($request)) {
return;
}

/** @var TrackInterface $track */
$track = $request->getParameter('track');
$track = $this->currentTape->getTrackForRequest($request);

$this->currentTape->finishRecording(
$track,
$event->getResponse(),
Expand All @@ -232,14 +229,11 @@ public function onException(ExceptionEvent $event)
$exception = $event->getException();
$request = $exception->getRequest();

if (!$request->hasParameter('track')) {
// Nothing to do here. Since we are in recording mode, the request should
// have a track, but we check nonetheless, it's better to be safe than sorry
if (!$this->currentTape->hasTrackForRequest($request)) {
return;
}

/** @var TrackInterface $track */
$track = $request->getParameter('track');
$track = $this->currentTape->getTrackForRequest($request);

if (!($exception instanceof TapeRecorderException)) {
// Normal exception, let's store it in the track for the next time
Expand Down
86 changes: 31 additions & 55 deletions tests/Event/Subscriber/TapeRecorderSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,6 @@ public function testPreSendEventWithExistingTrackShouldReplay()
->method('play')
->with($track);

$request
->expects($this->once())
->method('withParameter')
->with('track', $track)
->willReturn($this->createRequestMockWithTrack($track));

$this->injectTapeMock($tape);

$this->subscriber->startRecording();
Expand Down Expand Up @@ -256,30 +250,31 @@ public function testPostSendEventWithoutStartedRecordingShouldDoNothing()
public function testPostSend()
{
$request = $this->createRequestMock();
$request
->expects($this->once())
->method('hasParameter')
->with('track')
->willReturn(true);

$request
->expects($this->once())
->method('getParameter')
->with('track')
->willReturn($track = $this->createTrackMock($request));

$this->injectTapeMock($tape = $this->createTapeMock());

$tape
->expects($this->once())
->method('finishRecording');

$tape
->expects($this->once())
->method('hasTrackForRequest')
->with($request)
->willReturn(true);

$tape->expects($this->once())
->method('getTrackForRequest')
->with($request)
->willReturn($this->createTrackMock($request));

$this->subscriber->startRecording();
$this->subscriber->onPostSend($this->createPostSendEvent(null, $request));
}

public function testPostSendEventWithARequestThatHasNoAttachedTrackShouldDoNothing()
{
$this->markTestSkipped();
$this->injectTapeMock($tape = $this->createTapeMock());
$tape
->expects($this->never())
Expand Down Expand Up @@ -308,6 +303,7 @@ public function testExceptionEventWithoutStartedRecordingShouldDoNothing()

public function testExceptionEventWithARequestThatHasNoAttachedTrackShouldDoNothing()
{
$this->markTestSkipped();
$exception = $this->createExceptionMock($request = $this->createRequestMock());
$request
->expects($this->once())
Expand All @@ -327,34 +323,38 @@ public function testExceptionEventWithARequestThatHasNoAttachedTrackShouldDoNoth
public function testExceptionEventWithNormalHttpAdapterExceptionShouldFinishRecording()
{
$exception = $this->createExceptionMock(
$request = $this->createRequestMockWithTrack(
$track = $this->createTrackMock()
)
$request = $this->createRequestMock()
);

$track
->expects($this->never())
->method('hasResponse');

$this->injectTapeMock($tape = $this->createTapeMock());

$tape
->expects($this->once())
->method('finishRecording');
->method('hasTrackForRequest')
->with($request)
->willReturn(true);

$tape->expects($this->once())
->method('getTrackForRequest')
->with($request)
->willReturn($track = $this->createTrackMock($request));

$tape
->expects($this->once())
->method('finishRecording')
->with($track);

$this->subscriber->startRecording();
$this->subscriber->onException($this->createExceptionEvent(null, $exception));
}

public function testExceptionEventWithFullTrackWillReplayTheResponseAndException()
{
$track = $this->createTrackMock(
$this->createRequestMock(),
$response = $this->createResponseMock(),
$exception = $this->createExceptionMock()
);
$response = $this->createResponseMock();
$exception = $this->createExceptionMock();

$exception = $this->createTapeRecorderExceptionMock(
$request = $this->createRequestMockWithTrack($track), $response, $exception
$request = $this->createRequestMock(), $response, $exception
);

$this->injectTapeMock($tape = $this->createTapeMock());
Expand Down Expand Up @@ -434,30 +434,6 @@ protected function createTrackMock(
return $track;
}

/**
* @param TrackInterface $track
*
* @return InternalRequestInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function createRequestMockWithTrack(TrackInterface $track = null)
{
$request = $this->createRequestMock();

$request
->expects($this->any())
->method('hasParameter')
->with('track')
->willReturn(true);

$request
->expects($this->any())
->method('getParameter')
->with('track')
->willReturn($track);

return $request;
}

/**
* @param InternalRequestInterface $internalRequest
* @param ResponseInterface $response
Expand Down

0 comments on commit 7c983c3

Please sign in to comment.