|
23 | 23 | use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
|
24 | 24 | use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
|
25 | 25 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
| 26 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyForAdditionalFields; |
| 27 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyForAdditionalFieldsInput; |
26 | 28 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
|
27 | 29 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
|
28 | 30 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
|
@@ -290,6 +292,48 @@ public function testDenormalize()
|
290 | 292 | ], Dummy::class);
|
291 | 293 | }
|
292 | 294 |
|
| 295 | + public function testCanDenormalizeInputClassWithDifferentFieldsThanResourceClass() |
| 296 | + { |
| 297 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 298 | + $propertyNameCollectionFactoryProphecy->create(DummyForAdditionalFieldsInput::class, [])->willReturn( |
| 299 | + new PropertyNameCollection(['dummyName']) |
| 300 | + ); |
| 301 | + $propertyNameCollectionFactoryProphecy->create(DummyForAdditionalFields::class, [])->willReturn( |
| 302 | + new PropertyNameCollection(['id', 'name', 'slug']) |
| 303 | + ); |
| 304 | + |
| 305 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 306 | + // Create DummyForAdditionalFieldsInput mocks |
| 307 | + $propertyMetadataFactoryProphecy->create(DummyForAdditionalFieldsInput::class, 'dummyName', [])->willReturn( |
| 308 | + (new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true) |
| 309 | + ); |
| 310 | + // Create DummyForAdditionalFields mocks |
| 311 | + $propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'id', [])->willReturn( |
| 312 | + (new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), '', true, false))->withInitializable(false) |
| 313 | + ); |
| 314 | + $propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'name', [])->willReturn( |
| 315 | + (new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true) |
| 316 | + ); |
| 317 | + $propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'slug', [])->willReturn( |
| 318 | + (new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true) |
| 319 | + ); |
| 320 | + |
| 321 | + $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $this->prophesize(IriConverterInterface::class)->reveal(), $this->prophesize(ResourceClassResolverInterface::class)->reveal()) extends AbstractItemNormalizer { |
| 322 | + }; |
| 323 | + |
| 324 | + /** @var DummyForAdditionalFieldsInput $res */ |
| 325 | + $res = $normalizer->denormalize([ |
| 326 | + 'dummyName' => 'Dummy Name', |
| 327 | + ], DummyForAdditionalFieldsInput::class, 'json', [ |
| 328 | + 'resource_class' => DummyForAdditionalFields::class, |
| 329 | + 'input_class' => DummyForAdditionalFieldsInput::class, |
| 330 | + 'output_class' => DummyForAdditionalFields::class, |
| 331 | + ]); |
| 332 | + |
| 333 | + $this->assertInstanceOf(DummyForAdditionalFieldsInput::class, $res); |
| 334 | + $this->assertEquals('Dummy Name', $res->getDummyName()); |
| 335 | + } |
| 336 | + |
293 | 337 | public function testDenormalizeWritableLinks()
|
294 | 338 | {
|
295 | 339 | $relatedDummy1 = new RelatedDummy();
|
|
0 commit comments