|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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 | +namespace ApiPlatform\Core\tests\Hydra; |
| 13 | + |
| 14 | +use ApiPlatform\Core\Api\IriConverterInterface; |
| 15 | +use ApiPlatform\Core\Api\ResourceClassResolverInterface; |
| 16 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
| 17 | +use ApiPlatform\Core\JsonLd\ContextBuilderInterface; |
| 18 | +use ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer; |
| 19 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 20 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 21 | +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
| 22 | +use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; |
| 23 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 24 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
| 25 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 26 | +use Prophecy\Argument; |
| 27 | +use Symfony\Component\PropertyInfo\Type; |
| 28 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 29 | +use Symfony\Component\Serializer\SerializerInterface; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Kévin Dunglas <[email protected]> |
| 33 | + */ |
| 34 | +class ItemNormalizerTest extends \PHPUnit_Framework_TestCase |
| 35 | +{ |
| 36 | + public function testDontSupportDenormalization() |
| 37 | + { |
| 38 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 39 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 40 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 41 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 42 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 43 | + $contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class); |
| 44 | + $resourceClassResolverProphecy->getResourceClass(['dummy'], 'Dummy')->willReturn(Dummy::class); |
| 45 | + $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name' => 'name'])); |
| 46 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(new PropertyMetadata())->shouldBeCalled(1); |
| 47 | + |
| 48 | + $normalizer = new ItemNormalizer($resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $contextBuilderProphecy->reveal()); |
| 49 | + |
| 50 | + $this->assertFalse($normalizer->supportsDenormalization('foo', ItemNormalizer::FORMAT)); |
| 51 | + $normalizer->denormalize(['foo'], Dummy::class, 'jsonld', ['jsonld_has_context' => true, 'jsonld_sub_level' => true, 'resource_class' => Dummy::class]); |
| 52 | + } |
| 53 | + |
| 54 | + public function testSupportNormalization() |
| 55 | + { |
| 56 | + $std = new \stdClass(); |
| 57 | + $dummy = new Dummy(); |
| 58 | + $dummy->setDescription('hello'); |
| 59 | + |
| 60 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 61 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 62 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 63 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 64 | + $contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class); |
| 65 | + |
| 66 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 67 | + $resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class)->shouldBeCalled(); |
| 68 | + $resourceClassResolverProphecy->getResourceClass($std)->willThrow(new InvalidArgumentException())->shouldBeCalled(); |
| 69 | + |
| 70 | + $normalizer = new ItemNormalizer( |
| 71 | + $resourceMetadataFactoryProphecy->reveal(), |
| 72 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 73 | + $propertyMetadataFactoryProphecy->reveal(), |
| 74 | + $iriConverterProphecy->reveal(), |
| 75 | + $resourceClassResolverProphecy->reveal(), |
| 76 | + $contextBuilderProphecy->reveal() |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertTrue($normalizer->supportsNormalization($dummy, 'jsonld')); |
| 80 | + $this->assertFalse($normalizer->supportsNormalization($dummy, 'xml')); |
| 81 | + $this->assertFalse($normalizer->supportsNormalization($std, 'jsonld')); |
| 82 | + } |
| 83 | + |
| 84 | + public function testNormalize() |
| 85 | + { |
| 86 | + $dummy = new Dummy(); |
| 87 | + $dummy->setName('hello'); |
| 88 | + |
| 89 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 90 | + $resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('Dummy')); |
| 91 | + $propertyNameCollection = new PropertyNameCollection(['name']); |
| 92 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 93 | + $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled(); |
| 94 | + |
| 95 | + $propertyMetadataFactory = new PropertyMetadata(null, null, true); |
| 96 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 97 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled(); |
| 98 | + |
| 99 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 100 | + $iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1988')->shouldBeCalled(); |
| 101 | + |
| 102 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 103 | + $resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled(); |
| 104 | + |
| 105 | + $serializerProphecy = $this->prophesize(SerializerInterface::class); |
| 106 | + $serializerProphecy->willImplement(NormalizerInterface::class); |
| 107 | + $serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled(); |
| 108 | + $contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class); |
| 109 | + $contextBuilderProphecy->getResourceContextUri(Dummy::class)->willReturn('/contexts/Dummy'); |
| 110 | + |
| 111 | + $normalizer = new ItemNormalizer( |
| 112 | + $resourceMetadataFactoryProphecy->reveal(), |
| 113 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 114 | + $propertyMetadataFactoryProphecy->reveal(), |
| 115 | + $iriConverterProphecy->reveal(), |
| 116 | + $resourceClassResolverProphecy->reveal(), |
| 117 | + $contextBuilderProphecy->reveal() |
| 118 | + ); |
| 119 | + $normalizer->setSerializer($serializerProphecy->reveal()); |
| 120 | + |
| 121 | + $expected = ['@context' => '/contexts/Dummy', |
| 122 | + '@id' => '/dummies/1988', |
| 123 | + '@type' => 'Dummy', |
| 124 | + 'name' => 'hello', |
| 125 | + |
| 126 | + |
| 127 | + ]; |
| 128 | + $this->assertEquals($expected, $normalizer->normalize($dummy)); |
| 129 | + } |
| 130 | +} |
0 commit comments