|
| 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\GraphQl\Tests\Serializer; |
| 15 | + |
| 16 | +use ApiPlatform\GraphQl\Serializer\ItemDenormalizer; |
| 17 | +use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\Dummy; |
| 18 | +use ApiPlatform\Metadata\ApiProperty; |
| 19 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 20 | +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 21 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 22 | +use ApiPlatform\Metadata\Property\PropertyNameCollection; |
| 23 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Prophecy\Argument; |
| 26 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 27 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 28 | +use Symfony\Component\Serializer\SerializerInterface; |
| 29 | + |
| 30 | +class ItemDenormalizerTest extends TestCase |
| 31 | +{ |
| 32 | + use ProphecyTrait; |
| 33 | + |
| 34 | + public function testSupportsDenormalizationOnlyForGraphQlFormat(): void |
| 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(new Dummy(), ItemDenormalizer::FORMAT)); |
| 51 | + $this->assertTrue($denormalizer->supportsDenormalization([], Dummy::class, ItemDenormalizer::FORMAT)); |
| 52 | + $this->assertFalse($denormalizer->supportsDenormalization([], Dummy::class, 'jsonld')); |
| 53 | + } |
| 54 | + |
| 55 | + public function testDenormalize(): void |
| 56 | + { |
| 57 | + $context = ['resource_class' => Dummy::class, 'api_allow_update' => true]; |
| 58 | + |
| 59 | + $propertyNameCollection = new PropertyNameCollection(['name']); |
| 60 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 61 | + $propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::type('array'))->willReturn($propertyNameCollection)->shouldBeCalled(); |
| 62 | + |
| 63 | + $propertyMetadata = (new ApiProperty())->withWritable(true)->withReadable(true); |
| 64 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 65 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn($propertyMetadata)->shouldBeCalled(); |
| 66 | + |
| 67 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 68 | + |
| 69 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 70 | + $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class); |
| 71 | + $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true); |
| 72 | + |
| 73 | + $serializerProphecy = $this->prophesize(SerializerInterface::class); |
| 74 | + $serializerProphecy->willImplement(DenormalizerInterface::class); |
| 75 | + |
| 76 | + $denormalizer = new ItemDenormalizer( |
| 77 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 78 | + $propertyMetadataFactoryProphecy->reveal(), |
| 79 | + $iriConverterProphecy->reveal(), |
| 80 | + $resourceClassResolverProphecy->reveal() |
| 81 | + ); |
| 82 | + $denormalizer->setSerializer($serializerProphecy->reveal()); |
| 83 | + |
| 84 | + $this->assertInstanceOf(Dummy::class, $denormalizer->denormalize(['name' => 'hello'], Dummy::class, ItemDenormalizer::FORMAT, $context)); |
| 85 | + } |
| 86 | +} |
0 commit comments