|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Metadata\Tests; |
15 | 15 |
|
| 16 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
16 | 17 | use ApiPlatform\Metadata\Get; |
17 | 18 | use ApiPlatform\Metadata\HttpOperation; |
18 | 19 | use ApiPlatform\Metadata\IdentifiersExtractor; |
|
24 | 25 | use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Dummy; |
25 | 26 | use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\DummyWithEnumIdentifier; |
26 | 27 | use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\RelationMultiple; |
| 28 | +use PHPUnit\Framework\Attributes\DataProvider; |
27 | 29 | use PHPUnit\Framework\TestCase; |
28 | 30 | use Prophecy\Argument; |
29 | 31 | use Prophecy\PhpUnit\ProphecyTrait; |
@@ -174,6 +176,46 @@ public function testGetIdentifiersFromItemWithToProperty(): void |
174 | 176 | ); |
175 | 177 | } |
176 | 178 |
|
| 179 | + public static function unreadableToPropertyProvider(): iterable |
| 180 | + { |
| 181 | + yield 'missing property' => ['missing']; |
| 182 | + yield 'null relation' => ['first']; |
| 183 | + } |
| 184 | + |
| 185 | + #[DataProvider('unreadableToPropertyProvider')] |
| 186 | + public function testGetIdentifiersFromItemWithUnreadableToPropertyThrowsRuntimeException(string $toProperty): void |
| 187 | + { |
| 188 | + $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); |
| 189 | + $resourceClassResolver->method('isResourceClass')->willReturn(true); |
| 190 | + $resourceClassResolver->method('getResourceClass')->willReturn(RelationMultiple::class); |
| 191 | + |
| 192 | + $identifiersExtractor = new IdentifiersExtractor( |
| 193 | + $this->createStub(ResourceMetadataCollectionFactoryInterface::class), |
| 194 | + $resourceClassResolver, |
| 195 | + $this->createStub(PropertyNameCollectionFactoryInterface::class), |
| 196 | + $this->createStub(PropertyMetadataFactoryInterface::class) |
| 197 | + ); |
| 198 | + |
| 199 | + $operation = (new Get()) |
| 200 | + ->withUriTemplate('/resources/{firstId}') |
| 201 | + ->withUriVariables([ |
| 202 | + 'firstId' => (new Link()) |
| 203 | + ->withFromClass(Dummy::class) |
| 204 | + ->withToProperty($toProperty) |
| 205 | + ->withIdentifiers(['id']) |
| 206 | + ->withParameterName('firstId'), |
| 207 | + ]) |
| 208 | + ->withClass(RelationMultiple::class); |
| 209 | + |
| 210 | + $item = new RelationMultiple(); |
| 211 | + $item->id = 1; |
| 212 | + |
| 213 | + $this->expectException(RuntimeException::class); |
| 214 | + $this->expectExceptionMessage('Not able to retrieve identifiers.'); |
| 215 | + |
| 216 | + $identifiersExtractor->getIdentifiersFromItem($item, $operation); |
| 217 | + } |
| 218 | + |
177 | 219 | public function testGetIdentifierWithEnumValues(): void |
178 | 220 | { |
179 | 221 | $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
|
0 commit comments