Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/EventListener/RespondListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();

if ($controllerResult instanceof Response || !$request->attributes->get('_api_respond')) {
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
return;
}

Expand Down
6 changes: 1 addition & 5 deletions src/EventListener/SerializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();

if ($controllerResult instanceof Response) {
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
return;
}

Expand Down Expand Up @@ -80,10 +80,6 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
*/
private function serializeRawData(GetResponseForControllerResultEvent $event, Request $request, $controllerResult)
{
if (!$request->attributes->get('_api_respond')) {
return;
}

if (\is_object($controllerResult)) {
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $request->attributes->get('_api_normalization_context', [])));

Expand Down
20 changes: 17 additions & 3 deletions tests/EventListener/RespondListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\EventListener\RespondListener;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
Expand All @@ -28,11 +29,24 @@ class RespondListenerTest extends TestCase
public function testDoNotHandleResponse()
{
$request = new Request();
$request->setRequestFormat('xml');

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn(new Response())->shouldBeCalled();
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
$eventProphecy->getControllerResult()->willReturn(new Response());
$eventProphecy->getRequest()->willReturn($request);
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();

$listener = new RespondListener();
$listener->onKernelView($eventProphecy->reveal());
}

public function testDoNotHandleWhenRespondFlagIsFalse()
{
$request = new Request([], [], ['_api_respond' => false]);

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn('foo');
$eventProphecy->getRequest()->willReturn($request);
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();

$listener = new RespondListener();
$listener->onKernelView($eventProphecy->reveal());
Expand Down
54 changes: 9 additions & 45 deletions tests/EventListener/SerializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SerializeListenerTest extends TestCase
public function testDoNotSerializeResponse()
{
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize()->shouldNotBeCalled();
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();

$request = new Request();
$request->setRequestFormat('xml');
Expand All @@ -42,61 +42,25 @@ public function testDoNotSerializeResponse()
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
$serializerContextBuilderProphecy->createFromRequest(Argument::cetera())->shouldNotBeCalled();

$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
$listener->onKernelView($eventProphecy->reveal());
}

public function testDoNotSerializeWhenFormatNotSet()
public function testDoNotSerializeWhenRespondFlagIsFalse()
{
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize()->shouldNotBeCalled();

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();

$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
$listener->onKernelView($eventProphecy->reveal());
}

public function testDoNotSerializeWhenResourceClassNotSet()
{
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize()->shouldNotBeCalled();

$request = new Request([], [], ['_api_collection_operation_name' => 'get']);
$request->setRequestFormat('xml');

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();

$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
$listener->onKernelView($eventProphecy->reveal());
}

public function testDoNotSerializeWhenOperationNotSet()
{
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize()->shouldNotBeCalled();

$request = new Request([], [], ['_api_resource_class' => 'Foo']);
$request->setRequestFormat('xml');
$request = new Request([], [], ['_api_respond' => false]);

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
$eventProphecy->getControllerResult()->willReturn(new \stdClass());
$eventProphecy->getRequest()->willReturn($request);
$eventProphecy->setControllerResult(Argument::any())->shouldNotBeCalled();

$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
$listener->onKernelView($eventProphecy->reveal());
Expand Down Expand Up @@ -178,7 +142,7 @@ public function testEncode()
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(EncoderInterface::class);
$serializerProphecy->encode(Argument::any(), 'xml')->willReturn('bar')->shouldBeCalled();
$serializerProphecy->serialize()->shouldNotBeCalled();
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();

$request = new Request([], [], ['_api_respond' => true]);
$request->setRequestFormat('xml');
Expand Down