|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\JsonApi\Tests\Serializer; |
| 15 | + |
| 16 | +use ApiPlatform\JsonApi\Serializer\ItemDenormalizer; |
| 17 | +use ApiPlatform\JsonApi\Serializer\ItemNormalizer; |
| 18 | +use ApiPlatform\JsonApi\Tests\Fixtures\Dummy; |
| 19 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 20 | +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 21 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 22 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 23 | +use PHPUnit\Framework\Attributes\Group; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 26 | +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 27 | + |
| 28 | +class ItemDenormalizerTest extends TestCase |
| 29 | +{ |
| 30 | + use ProphecyTrait; |
| 31 | + |
| 32 | + public function testSupportsDenormalizationOnlyForJsonApiFormat(): void |
| 33 | + { |
| 34 | + $dummy = new Dummy(); |
| 35 | + |
| 36 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 37 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 38 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 39 | + |
| 40 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 41 | + $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true); |
| 42 | + |
| 43 | + $denormalizer = new ItemDenormalizer( |
| 44 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 45 | + $propertyMetadataFactoryProphecy->reveal(), |
| 46 | + $iriConverterProphecy->reveal(), |
| 47 | + $resourceClassResolverProphecy->reveal() |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertFalse($denormalizer->supportsNormalization($dummy, ItemNormalizer::FORMAT)); |
| 51 | + $this->assertTrue($denormalizer->supportsDenormalization($dummy, Dummy::class, ItemNormalizer::FORMAT)); |
| 52 | + $this->assertFalse($denormalizer->supportsDenormalization($dummy, Dummy::class, 'jsonld')); |
| 53 | + } |
| 54 | + |
| 55 | + #[Group('legacy')] |
| 56 | + public function testDenormalizeOnLegacyItemNormalizerIsDeprecated(): void |
| 57 | + { |
| 58 | + $this->expectUserDeprecationMessage('Since api-platform/core 4.4: Calling "denormalize()" on "ApiPlatform\JsonApi\Serializer\ItemNormalizer" is deprecated, use "ApiPlatform\JsonApi\Serializer\ItemDenormalizer" instead.'); |
| 59 | + $this->expectException(NotNormalizableValueException::class); |
| 60 | + |
| 61 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 62 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 63 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 64 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 65 | + |
| 66 | + $normalizer = new ItemNormalizer( |
| 67 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 68 | + $propertyMetadataFactoryProphecy->reveal(), |
| 69 | + $iriConverterProphecy->reveal(), |
| 70 | + $resourceClassResolverProphecy->reveal() |
| 71 | + ); |
| 72 | + |
| 73 | + $normalizer->denormalize( |
| 74 | + ['data' => ['id' => '/dummies/1']], |
| 75 | + Dummy::class, |
| 76 | + ItemNormalizer::FORMAT, |
| 77 | + ['api_allow_update' => false] |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments