Skip to content

Commit 999443d

Browse files
authored
Merge pull request #2476 from api-platform/2.3
merge 2.3 in 2.4
2 parents 43a2b6c + 8adbb31 commit 999443d

File tree

4 files changed

+28
-54
lines changed

4 files changed

+28
-54
lines changed

src/EventListener/RespondListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
4545
$controllerResult = $event->getControllerResult();
4646
$request = $event->getRequest();
4747

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

src/EventListener/SerializeListener.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
4949
$controllerResult = $event->getControllerResult();
5050
$request = $event->getRequest();
5151

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

@@ -107,10 +107,6 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
107107
*/
108108
private function serializeRawData(GetResponseForControllerResultEvent $event, Request $request, $controllerResult)
109109
{
110-
if (!$request->attributes->get('_api_respond')) {
111-
return;
112-
}
113-
114110
if (\is_object($controllerResult)) {
115111
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $request->attributes->get('_api_normalization_context', [])));
116112

tests/EventListener/RespondListenerTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2020
use PHPUnit\Framework\TestCase;
21+
use Prophecy\Argument;
2122
use Symfony\Component\HttpFoundation\Request;
2223
use Symfony\Component\HttpFoundation\Response;
2324
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
@@ -31,11 +32,24 @@ class RespondListenerTest extends TestCase
3132
public function testDoNotHandleResponse()
3233
{
3334
$request = new Request();
34-
$request->setRequestFormat('xml');
3535

3636
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
37-
$eventProphecy->getControllerResult()->willReturn(new Response())->shouldBeCalled();
38-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
37+
$eventProphecy->getControllerResult()->willReturn(new Response());
38+
$eventProphecy->getRequest()->willReturn($request);
39+
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();
40+
41+
$listener = new RespondListener();
42+
$listener->onKernelView($eventProphecy->reveal());
43+
}
44+
45+
public function testDoNotHandleWhenRespondFlagIsFalse()
46+
{
47+
$request = new Request([], [], ['_api_respond' => false]);
48+
49+
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
50+
$eventProphecy->getControllerResult()->willReturn('foo');
51+
$eventProphecy->getRequest()->willReturn($request);
52+
$eventProphecy->setResponse(Argument::any())->shouldNotBeCalled();
3953

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

tests/EventListener/SerializeListenerTest.php

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SerializeListenerTest extends TestCase
3232
public function testDoNotSerializeResponse()
3333
{
3434
$serializerProphecy = $this->prophesize(SerializerInterface::class);
35-
$serializerProphecy->serialize()->shouldNotBeCalled();
35+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
3636

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

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

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

51-
public function testDoNotSerializeWhenFormatNotSet()
51+
public function testDoNotSerializeWhenRespondFlagIsFalse()
5252
{
5353
$serializerProphecy = $this->prophesize(SerializerInterface::class);
54-
$serializerProphecy->serialize()->shouldNotBeCalled();
55-
56-
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
57-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
58-
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
59-
60-
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
61-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
62-
63-
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
64-
$listener->onKernelView($eventProphecy->reveal());
65-
}
66-
67-
public function testDoNotSerializeWhenResourceClassNotSet()
68-
{
69-
$serializerProphecy = $this->prophesize(SerializerInterface::class);
70-
$serializerProphecy->serialize()->shouldNotBeCalled();
71-
72-
$request = new Request([], [], ['_api_collection_operation_name' => 'get']);
73-
$request->setRequestFormat('xml');
74-
75-
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
76-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
77-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
54+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
7855

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

82-
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
83-
$listener->onKernelView($eventProphecy->reveal());
84-
}
85-
86-
public function testDoNotSerializeWhenOperationNotSet()
87-
{
88-
$serializerProphecy = $this->prophesize(SerializerInterface::class);
89-
$serializerProphecy->serialize()->shouldNotBeCalled();
90-
91-
$request = new Request([], [], ['_api_resource_class' => 'Foo']);
92-
$request->setRequestFormat('xml');
58+
$request = new Request([], [], ['_api_respond' => false]);
9359

9460
$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
95-
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
96-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
97-
98-
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
99-
$serializerContextBuilderProphecy->createFromRequest()->shouldNotBeCalled();
61+
$eventProphecy->getControllerResult()->willReturn(new \stdClass());
62+
$eventProphecy->getRequest()->willReturn($request);
63+
$eventProphecy->setControllerResult(Argument::any())->shouldNotBeCalled();
10064

10165
$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
10266
$listener->onKernelView($eventProphecy->reveal());
@@ -198,7 +162,7 @@ public function testEncode()
198162
$serializerProphecy = $this->prophesize(SerializerInterface::class);
199163
$serializerProphecy->willImplement(EncoderInterface::class);
200164
$serializerProphecy->encode(Argument::any(), 'xml')->willReturn('bar')->shouldBeCalled();
201-
$serializerProphecy->serialize()->shouldNotBeCalled();
165+
$serializerProphecy->serialize(Argument::cetera())->shouldNotBeCalled();
202166

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

0 commit comments

Comments
 (0)