|
15 | 15 |
|
16 | 16 | use ApiPlatform\Api\IriConverterInterface;
|
17 | 17 | use ApiPlatform\Api\ResourceClassResolverInterface;
|
| 18 | +use ApiPlatform\Api\UrlGeneratorInterface; |
| 19 | +use ApiPlatform\Exception\InvalidArgumentException; |
18 | 20 | use ApiPlatform\Metadata\ApiProperty;
|
| 21 | +use ApiPlatform\Metadata\ApiResource; |
| 22 | +use ApiPlatform\Metadata\Get; |
| 23 | +use ApiPlatform\Metadata\Link; |
| 24 | +use ApiPlatform\Metadata\Operations; |
19 | 25 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
20 | 26 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
|
21 | 27 | use ApiPlatform\Metadata\Property\PropertyNameCollection;
|
| 28 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 29 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
22 | 30 | use ApiPlatform\Serializer\ItemNormalizer;
|
23 | 31 | use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
|
24 | 32 | use PHPUnit\Framework\TestCase;
|
@@ -173,6 +181,69 @@ public function testDenormalizeWithIri(): void
|
173 | 181 | $this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context));
|
174 | 182 | }
|
175 | 183 |
|
| 184 | + public function testDenormalizeGuessingUriVariables(): void |
| 185 | + { |
| 186 | + $context = ['resource_class' => Dummy::class, 'api_allow_update' => true, 'uri_variables' => [ |
| 187 | + 'parent_resource' => '1', |
| 188 | + 'resource' => '1', |
| 189 | + ]]; |
| 190 | + |
| 191 | + $propertyNameCollection = new PropertyNameCollection(['name']); |
| 192 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 193 | + $propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::cetera())->willReturn($propertyNameCollection)->shouldBeCalled(); |
| 194 | + |
| 195 | + $propertyMetadata = (new ApiProperty())->withReadable(true)->withWritable(true); |
| 196 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 197 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::cetera())->willReturn($propertyMetadata)->shouldBeCalled(); |
| 198 | + |
| 199 | + $uriVariables = [ |
| 200 | + 'parent_resource' => new Link('parent_resource', identifiers: ['id']), |
| 201 | + 'resource' => new Link('resource', identifiers: ['id']), |
| 202 | + 'sub_resource' => new Link('sub_resource', identifiers: ['id']), |
| 203 | + ]; |
| 204 | + $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 205 | + $resourceMetadataCollectionFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [ |
| 206 | + (new ApiResource())->withShortName('Dummy')->withOperations(new Operations([ |
| 207 | + 'sub_resource' => (new Get(uriVariables: $uriVariables))->withShortName('Dummy'), |
| 208 | + ])), |
| 209 | + ])); |
| 210 | + |
| 211 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 212 | + $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class); |
| 213 | + $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true); |
| 214 | + |
| 215 | + $serializerProphecy = $this->prophesize(SerializerInterface::class); |
| 216 | + $serializerProphecy->willImplement(DenormalizerInterface::class); |
| 217 | + |
| 218 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 219 | + $iriConverterProphecy->getResourceFromIri(Argument::is('12'), Argument::cetera())->willThrow(InvalidArgumentException::class); |
| 220 | + $iriConverterProphecy |
| 221 | + ->getIriFromResource( |
| 222 | + Dummy::class, |
| 223 | + UrlGeneratorInterface::ABS_PATH, |
| 224 | + Argument::type(Get::class), |
| 225 | + Argument::withEntry('uri_variables', Argument::allOf( |
| 226 | + Argument::withEntry('parent_resource', '1'), |
| 227 | + Argument::withEntry('resource', '1'), |
| 228 | + Argument::withEntry('sub_resource', '12') |
| 229 | + )) |
| 230 | + ) |
| 231 | + ->willReturn('parent_resource/1/resource/1/sub_resource/2') |
| 232 | + ->shouldBeCalledOnce(); |
| 233 | + $iriConverterProphecy->getResourceFromIri('parent_resource/1/resource/1/sub_resource/2', ['fetch_data' => true])->shouldBeCalledOnce(); |
| 234 | + |
| 235 | + $normalizer = new ItemNormalizer( |
| 236 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 237 | + $propertyMetadataFactoryProphecy->reveal(), |
| 238 | + $iriConverterProphecy->reveal(), |
| 239 | + $resourceClassResolverProphecy->reveal(), |
| 240 | + resourceMetadataFactory: $resourceMetadataCollectionFactoryProphecy->reveal(), |
| 241 | + ); |
| 242 | + $normalizer->setSerializer($serializerProphecy->reveal()); |
| 243 | + |
| 244 | + $this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '12', 'name' => 'hello'], Dummy::class, null, $context)); |
| 245 | + } |
| 246 | + |
176 | 247 | public function testDenormalizeWithIdAndUpdateNotAllowed(): void
|
177 | 248 | {
|
178 | 249 | $this->expectException(NotNormalizableValueException::class);
|
|
0 commit comments