Skip to content

Commit 5c8abe3

Browse files
committed
Fix review
And add some unit test
1 parent 1691273 commit 5c8abe3

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

features/main/crud_abstract.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ Feature: Create-Retrieve-Update-Delete on abstract resource
9393
And the response should be in JSON
9494
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
9595
And the header "Content-Location" should be equal to "/concrete_dummies/1"
96-
And the header "Location" should be equal to "/concrete_dummies/1"
9796
And the JSON should be equal to:
9897
"""
9998
{

features/main/uuid.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Feature: Using uuid identifier on resource
9191
And the response should be in JSON
9292
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
9393
And the header "Content-Location" should be equal to "/custom_generated_identifiers/foo"
94+
And the header "Location" should be equal to "/custom_generated_identifiers/foo"
9495
And the JSON should be equal to:
9596
"""
9697
{

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152

153153
<service id="api_platform.listener.view.write" class="ApiPlatform\Core\EventListener\WriteListener">
154154
<argument type="service" id="api_platform.data_persister" />
155+
<argument type="service" id="api_platform.iri_converter" on-invalid="null" />
155156

156157
<tag name="kernel.event_listener" event="kernel.view" method="onKernelView" priority="32" />
157158
</service>

src/EventListener/RespondListener.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
4949
'X-Frame-Options' => 'deny',
5050
];
5151

52-
if($request->isMethod('POST') || $request->isMethod('PUT')){
53-
$headers['Content-Location'] = $request->attributes->get('_iri_item');
54-
}
52+
if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('PATCH')) {
53+
if ($request->attributes->has('_api_write_item_iri')) {
54+
$headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
5555

56-
if($request->isMethod('POST')){
57-
$headers['Location'] = $request->attributes->get('_iri_item');
56+
if ($request->isMethod('POST')) {
57+
$headers['Location'] = $request->attributes->get('_api_write_item_iri');
58+
}
59+
} else {
60+
@trigger_error(sprintf('No request attribute from `_api_write_item_iri` key is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an string should always be returned. see deprecated into %s constructor for more details.', WriteListener::class), E_USER_DEPRECATED);
61+
}
5862
}
5963

6064
$event->setResponse(new Response(

src/EventListener/WriteListener.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class WriteListener
2828
private $dataPersister;
2929
private $iriConverter;
3030

31-
public function __construct(DataPersisterInterface $dataPersister, IriConverterInterface $iriConverter)
31+
public function __construct(DataPersisterInterface $dataPersister, IriConverterInterface $iriConverter = null)
3232
{
3333
$this->dataPersister = $dataPersister;
3434
$this->iriConverter = $iriConverter;
35+
36+
if (null === $iriConverter) {
37+
@trigger_error(sprintf('Class %s will have a second `IriConverterInterface $iriConverter` argument in version 3.0. Not defining it is deprecated since 2.3.', __CLASS__), E_USER_DEPRECATED);
38+
}
3539
}
3640

3741
/**
@@ -60,7 +64,10 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
6064
}
6165

6266
$event->setControllerResult($persistResult ?? $controllerResult);
63-
$request->attributes->set('_iri_item', $this->iriConverter->getIriFromItem($controllerResult));
67+
68+
if (null !== $this->iriConverter) {
69+
$request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromItem($controllerResult));
70+
}
6471
break;
6572
case 'DELETE':
6673
$this->dataPersister->remove($controllerResult);

tests/EventListener/RespondListenerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testCreate201Response()
6767
{
6868
$kernelProphecy = $this->prophesize(HttpKernelInterface::class);
6969

70-
$request = new Request([], [], ['_api_respond' => true]);
70+
$request = new Request([], [], ['_api_respond' => true, '_api_write_item_iri' => '/dummy_entities/1']);
7171
$request->setMethod('POST');
7272
$request->setRequestFormat('xml');
7373

@@ -88,6 +88,8 @@ public function testCreate201Response()
8888
$this->assertEquals('Accept', $response->headers->get('Vary'));
8989
$this->assertEquals('nosniff', $response->headers->get('X-Content-Type-Options'));
9090
$this->assertEquals('deny', $response->headers->get('X-Frame-Options'));
91+
$this->assertEquals('/dummy_entities/1', $response->headers->get('Location'));
92+
$this->assertEquals('/dummy_entities/1', $response->headers->get('Content-Location'));
9193
}
9294

9395
public function testCreate204Response()

tests/EventListener/WriteListenerTest.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Tests\EventListener;
1515

16+
use ApiPlatform\Core\Api\IriConverterInterface;
1617
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
1718
use ApiPlatform\Core\EventListener\WriteListener;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
@@ -35,6 +36,9 @@ public function testOnKernelViewWithControllerResultAndPersist()
3536
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
3637
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
3738

39+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
40+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
41+
3842
$request = new Request();
3943
$request->attributes->set('_api_resource_class', Dummy::class);
4044

@@ -48,8 +52,9 @@ public function testOnKernelViewWithControllerResultAndPersist()
4852
foreach (['PATCH', 'PUT', 'POST'] as $httpMethod) {
4953
$request->setMethod($httpMethod);
5054

51-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
55+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
5256
$this->assertSame($dummy, $event->getControllerResult());
57+
$this->assertEquals('/dummy/1', $request->attributes->get('_api_write_item_iri'));
5358
}
5459
}
5560

@@ -98,6 +103,9 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
98103
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
99104
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
100105

106+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
107+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
108+
101109
$dataPersisterProphecy
102110
->persist($dummy)
103111
->willReturn($dummy2) // Persist is not mutating $dummy, but return a brand new technically unrelated object instead
@@ -117,9 +125,10 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
117125

118126
$request->setMethod($httpMethod);
119127

120-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
128+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
121129

122130
$this->assertSame($dummy2, $event->getControllerResult());
131+
$this->assertEquals('/dummy/1', $request->attributes->get('_api_write_item_iri'));
123132
}
124133
}
125134

@@ -132,6 +141,9 @@ public function testOnKernelViewWithControllerResultAndRemove()
132141
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
133142
$dataPersisterProphecy->remove($dummy)->shouldBeCalled();
134143

144+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
145+
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
146+
135147
$request = new Request();
136148
$request->setMethod('DELETE');
137149
$request->attributes->set('_api_resource_class', Dummy::class);
@@ -143,7 +155,7 @@ public function testOnKernelViewWithControllerResultAndRemove()
143155
$dummy
144156
);
145157

146-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
158+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
147159
}
148160

149161
public function testOnKernelViewWithSafeMethod()
@@ -156,6 +168,9 @@ public function testOnKernelViewWithSafeMethod()
156168
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
157169
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
158170

171+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
172+
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
173+
159174
$request = new Request();
160175
$request->setMethod('HEAD');
161176
$request->attributes->set('_api_resource_class', Dummy::class);
@@ -167,7 +182,7 @@ public function testOnKernelViewWithSafeMethod()
167182
$dummy
168183
);
169184

170-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
185+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
171186
}
172187

173188
public function testOnKernelViewWithNoResourceClass()
@@ -180,6 +195,9 @@ public function testOnKernelViewWithNoResourceClass()
180195
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
181196
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
182197

198+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
199+
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
200+
183201
$request = new Request();
184202
$request->setMethod('POST');
185203

@@ -190,7 +208,7 @@ public function testOnKernelViewWithNoResourceClass()
190208
$dummy
191209
);
192210

193-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
211+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
194212
}
195213

196214
public function testOnKernelViewWithNoDataPersisterSupport()
@@ -203,6 +221,9 @@ public function testOnKernelViewWithNoDataPersisterSupport()
203221
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
204222
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
205223

224+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
225+
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
226+
206227
$request = new Request();
207228
$request->setMethod('POST');
208229
$request->attributes->set('_api_resource_class', 'Dummy');
@@ -214,6 +235,6 @@ public function testOnKernelViewWithNoDataPersisterSupport()
214235
$dummy
215236
);
216237

217-
(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
238+
(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
218239
}
219240
}

0 commit comments

Comments
 (0)