Skip to content

Commit

Permalink
Merge pull request #39 from Payum/interactive-request-listener-set-co…
Browse files Browse the repository at this point in the history
…rrect-http-status

[interactive request] set correct status from response, now it always se...
  • Loading branch information
makasim committed Aug 12, 2013
2 parents abd4893 + 28a102a commit 763d75b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
17 changes: 8 additions & 9 deletions EventListener/InteractiveRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ public function onKernelException(GetResponseForExceptionEvent $event)

if ($interactiveRequest instanceof ResponseInteractiveRequest) {
$event->setResponse($interactiveRequest->getResponse());
$event->stopPropagation();

return;
}

if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) {
} else if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) {
$event->setResponse(new RedirectResponse($interactiveRequest->getUrl()));
$event->stopPropagation();

}

if ($event->getResponse()) {
if (false == $event->getResponse()->headers->has('X-Status-Code')) {
$event->getResponse()->headers->set('X-Status-Code', $event->getResponse()->getStatusCode());
}

return;
}

$ro = new \ReflectionObject($interactiveRequest);

$event->setException(new LogicException(
sprintf('Cannot convert interactive request %s to symfony response.', $ro->getShortName()),
null,
Expand Down
86 changes: 83 additions & 3 deletions Tests/EventListener/InteractiveRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ public function shouldSetRedirectResponseIfExceptionInstanceOfRedirectUrlInterac
$this->assertSame($interactiveRequest, $event->getException());
}

/**
* @test
*/
public function shouldSetXStatusCodeWhenExceptionInstanceOfRedirectUrlInteractiveRequest()
{
$interactiveRequest = new RedirectUrlInteractiveRequest('/foo/bar');

$event = new GetResponseForExceptionEvent(
$this->createHttpKernelMock(),
new Request,
'requestType',
$interactiveRequest
);

$listener = new InteractiveRequestListener;

$listener->onKernelException($event);

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $event->getResponse());
$this->assertTrue($event->getResponse()->headers->has('X-Status-Code'));
$this->assertEquals(302, $event->getResponse()->headers->get('X-Status-Code'));
}

/**
* @test
*/
Expand Down Expand Up @@ -96,11 +119,43 @@ public function shouldSetResponseIfExceptionInstanceOfResponseInteractiveRequest
/**
* @test
*/
public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported()
public function shouldSetXStatusCodeWhenExceptionInstanceOfResponseInteractiveRequest()
{
$expectedResponse = new Response('foobar');
$expectedStatus = 555;

$response = new Response('foobar', $expectedStatus);

$interactiveRequest = new ResponseInteractiveRequest($response);

$event = new GetResponseForExceptionEvent(
$this->createHttpKernelMock(),
new Request,
'requestType',
$interactiveRequest
);

$listener = new InteractiveRequestListener;

$listener->onKernelException($event);

//guard
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $event->getResponse());

$this->assertTrue($event->getResponse()->headers->has('X-Status-Code'));
$this->assertEquals(555, $event->getResponse()->headers->get('X-Status-Code'));
}

/**
* @test
*/
public function shouldNotSetXStatusCodeIfAlreadySetWhenExceptionInstanceOfResponseInteractiveRequest()
{
$expectedStatus = 555;

$response = new Response('foobar', $expectedStatus);
$response->headers->set('X-Status-Code', 666);

$interactiveRequest = $this->getMock('Payum\Request\BaseInteractiveRequest');
$interactiveRequest = new ResponseInteractiveRequest($response);

$event = new GetResponseForExceptionEvent(
$this->createHttpKernelMock(),
Expand All @@ -113,6 +168,31 @@ public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported()

$listener->onKernelException($event);

//guard
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $event->getResponse());

$this->assertTrue($event->getResponse()->headers->has('X-Status-Code'));
$this->assertEquals(666, $event->getResponse()->headers->get('X-Status-Code'));
}

/**
* @test
*/
public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported()
{
$notSupportedInteractiveRequest = $this->getMock('Payum\Request\BaseInteractiveRequest');

$event = new GetResponseForExceptionEvent(
$this->createHttpKernelMock(),
new Request,
'requestType',
$notSupportedInteractiveRequest
);

$listener = new InteractiveRequestListener;

$listener->onKernelException($event);

$this->assertNull($event->getResponse());
$this->assertInstanceOf('Payum\Exception\LogicException', $event->getException());
$this->assertStringStartsWith(
Expand Down

0 comments on commit 763d75b

Please sign in to comment.