Skip to content

Commit e0c6134

Browse files
ronfroydunglas
authored andcommitted
improve exeption message (#2041)
* improve exeption message * add test
1 parent 808a453 commit e0c6134

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface
115115
$routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM);
116116

117117
try {
118-
$identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->getIdentifiersFromItem($item));
118+
$identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->getIdentifiersFromItem($item), $resourceClass);
119119

120120
return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType);
121121
} catch (RuntimeException $e) {
@@ -170,12 +170,22 @@ public function getSubresourceIriFromResourceClass(string $resourceClass, array
170170
/**
171171
* Generate the identifier url.
172172
*
173-
* @param array $identifiers
173+
* @param array $identifiers
174+
* @param string $resourceClass
175+
*
176+
* @throws InvalidArgumentException
174177
*
175178
* @return string[]
176179
*/
177-
private function generateIdentifiersUrl(array $identifiers): array
180+
private function generateIdentifiersUrl(array $identifiers, string $resourceClass): array
178181
{
182+
if (0 === \count($identifiers)) {
183+
throw new InvalidArgumentException(sprintf(
184+
'No identifiers defined for resource of type "%s"',
185+
$resourceClass
186+
));
187+
}
188+
179189
if (1 === \count($identifiers)) {
180190
return [rawurlencode((string) array_values($identifiers)[0])];
181191
}

tests/Bridge/Symfony/Routing/IriConverterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ public function testGetItemFromIriBadIdentifierException()
284284
$this->assertEquals($converter->getItemFromIri('/users/3', ['fetch_data' => true]), $item);
285285
}
286286

287+
public function testNoIdentifiersException()
288+
{
289+
$this->expectException(InvalidArgumentException::class);
290+
$this->expectExceptionMessage('No identifiers defined for resource of type "\App\Entity\Sample"');
291+
292+
$routeNameResolverProphecy = $this->prophesize(RouteNameResolverInterface::class);
293+
$routerProphecy = $this->prophesize(RouterInterface::class);
294+
295+
$converter = $this->getIriConverter($routerProphecy, $routeNameResolverProphecy, null);
296+
297+
$method = new \ReflectionMethod(IriConverter::class, 'generateIdentifiersUrl');
298+
$method->setAccessible(true);
299+
$method->invoke($converter, [], '\App\Entity\Sample');
300+
}
301+
287302
/**
288303
* @group legacy
289304
* @expectedDeprecation Not injecting "ApiPlatform\Core\Api\IdentifiersExtractorInterface" is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3

0 commit comments

Comments
 (0)