Skip to content

Commit 9943760

Browse files
authored
Merge pull request #1819 from Simperfit/bug/throw-when-an-subresources-id-is-not-found
bugfix: throw when a subresources id is not found
2 parents 0faba60 + a65e09e commit 9943760

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

features/main/subresource.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Feature: Subresource support
2424
}
2525
"""
2626

27+
Scenario: Get a non existant subresource
28+
Given there is an answer "42" to the question "What's the answer to the Ultimate Question of Life, the Universe and Everything?"
29+
When I send a "GET" request to "/questions/999999/answer"
30+
And the response status code should be 404
31+
And the response should be in JSON
32+
2733
Scenario: Get subresource one to one relation
2834
When I send a "GET" request to "/questions/1/answer/related_questions"
2935
And the response status code should be 200

src/EventListener/ReadListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ private function getSubresourceData(Request $request, array $attributes, array $
147147

148148
$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']);
149149

150+
if (null === $data) {
151+
throw new NotFoundHttpException('Not Found.');
152+
}
153+
150154
return $data;
151155
}
152156
}

tests/EventListener/ReadListenerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ public function testRetrieveSubresource()
160160
$this->assertSame($data, $request->attributes->get('data'));
161161
}
162162

163+
/**
164+
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
165+
*/
166+
public function testRetrieveSubresourceNotFound()
167+
{
168+
$collectionDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
169+
$collectionDataProvider->getCollection()->shouldNotBeCalled();
170+
171+
$itemDataProvider = $this->prophesize(ItemDataProviderInterface::class);
172+
$itemDataProvider->getItem()->shouldNotBeCalled();
173+
174+
$subresourceDataProvider = $this->prophesize(SubresourceDataProviderInterface::class);
175+
$subresourceDataProvider->getSubresource('Foo', ['id' => 1], ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar'], 'get')->willReturn(null)->shouldBeCalled();
176+
177+
$request = new Request([], [], ['id' => 1, '_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json', '_api_subresource_context' => ['identifiers' => [['id', 'Bar', true]], 'property' => 'bar']]);
178+
$request->setMethod('GET');
179+
180+
$event = $this->prophesize(GetResponseEvent::class);
181+
$event->getRequest()->willReturn($request)->shouldBeCalled();
182+
183+
$listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal());
184+
$listener->onKernelRequest($event->reveal());
185+
}
186+
163187
/**
164188
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
165189
*/

0 commit comments

Comments
 (0)