|
14 | 14 | namespace ApiPlatform\Core\Tests\Hydra\Serializer;
|
15 | 15 |
|
16 | 16 | use ApiPlatform\Core\Api\FilterCollection;
|
| 17 | +use ApiPlatform\Core\Api\OperationType; |
17 | 18 | use ApiPlatform\Core\Api\ResourceClassResolverInterface;
|
18 | 19 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\FilterInterface;
|
19 | 20 | use ApiPlatform\Core\Exception\InvalidArgumentException;
|
20 | 21 | use ApiPlatform\Core\Hydra\Serializer\CollectionFiltersNormalizer;
|
| 22 | +use ApiPlatform\Core\Hydra\Serializer\CollectionNormalizer; |
21 | 23 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
|
22 | 24 | use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
|
| 25 | +use ApiPlatform\Core\Tests\Fixtures\Foo; |
| 26 | +use ApiPlatform\Core\Tests\Fixtures\NotAResource; |
23 | 27 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
24 | 28 | use PHPUnit\Framework\TestCase;
|
| 29 | +use Prophecy\Argument; |
25 | 30 | use Psr\Container\ContainerInterface;
|
26 | 31 | use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
|
27 | 32 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
@@ -49,24 +54,141 @@ public function testSupportsNormalization()
|
49 | 54 | $this->assertTrue($normalizer->hasCacheableSupportsMethod());
|
50 | 55 | }
|
51 | 56 |
|
52 |
| - public function testDoNothingIfSubLevel() |
| 57 | + public function testNormalizeNonResourceCollection() |
53 | 58 | {
|
54 |
| - $dummy = new Dummy(); |
| 59 | + $notAResourceA = new NotAResource('A', 'buzz'); |
| 60 | + $notAResourceB = new NotAResource('B', 'bzzt'); |
| 61 | + |
| 62 | + $data = [$notAResourceA, $notAResourceB]; |
| 63 | + |
| 64 | + $normalizedNotAResourceA = [ |
| 65 | + 'foo' => 'A', |
| 66 | + 'bar' => 'buzz', |
| 67 | + ]; |
| 68 | + |
| 69 | + $normalizedNotAResourceB = [ |
| 70 | + 'foo' => 'B', |
| 71 | + 'bar' => 'bzzt', |
| 72 | + ]; |
55 | 73 |
|
56 | 74 | $decoratedProphecy = $this->prophesize(NormalizerInterface::class);
|
57 |
| - $decoratedProphecy->normalize($dummy, null, ['api_sub_level' => true])->willReturn(['name' => 'foo'])->shouldBeCalled(); |
| 75 | + $decoratedProphecy->normalize($data, CollectionNormalizer::FORMAT, Argument::any())->willReturn([ |
| 76 | + $normalizedNotAResourceA, |
| 77 | + $normalizedNotAResourceB, |
| 78 | + ]); |
| 79 | + |
| 80 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
58 | 81 |
|
59 | 82 | $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
|
60 |
| - $resourceClassResolverProphecy->getResourceClass()->shouldNotBeCalled(); |
| 83 | + $resourceClassResolverProphecy->getResourceClass($data, null, true)->willThrow(InvalidArgumentException::class); |
61 | 84 |
|
62 |
| - $normalizer = new CollectionFiltersNormalizer( |
63 |
| - $decoratedProphecy->reveal(), |
64 |
| - $this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), |
65 |
| - $resourceClassResolverProphecy->reveal(), |
66 |
| - $this->prophesize(ContainerInterface::class)->reveal() |
67 |
| - ); |
| 85 | + $filterLocatorProphecy = $this->prophesize(ContainerInterface::class); |
| 86 | + |
| 87 | + $normalizer = new CollectionFiltersNormalizer($decoratedProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $filterLocatorProphecy->reveal()); |
68 | 88 |
|
69 |
| - $this->assertEquals(['name' => 'foo'], $normalizer->normalize($dummy, null, ['api_sub_level' => true])); |
| 89 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 90 | + ]); |
| 91 | + |
| 92 | + $this->assertEquals([ |
| 93 | + $normalizedNotAResourceA, |
| 94 | + $normalizedNotAResourceB, |
| 95 | + ], $actual); |
| 96 | + } |
| 97 | + |
| 98 | + public function testNormalizeSubLevelResourceCollection() |
| 99 | + { |
| 100 | + $fooOne = new Foo(); |
| 101 | + $fooOne->id = 1; |
| 102 | + $fooOne->bar = 'baz'; |
| 103 | + |
| 104 | + $fooThree = new Foo(); |
| 105 | + $fooThree->id = 3; |
| 106 | + $fooThree->bar = 'bzz'; |
| 107 | + |
| 108 | + $data = [$fooOne, $fooThree]; |
| 109 | + |
| 110 | + $normalizedFooOne = [ |
| 111 | + '@id' => '/foos/1', |
| 112 | + '@type' => 'Foo', |
| 113 | + 'bar' => 'baz', |
| 114 | + ]; |
| 115 | + |
| 116 | + $normalizedFooThree = [ |
| 117 | + '@id' => '/foos/3', |
| 118 | + '@type' => 'Foo', |
| 119 | + 'bar' => 'bzz', |
| 120 | + ]; |
| 121 | + |
| 122 | + $decoratedProphecy = $this->prophesize(NormalizerInterface::class); |
| 123 | + $decoratedProphecy->normalize($data, CollectionNormalizer::FORMAT, Argument::allOf( |
| 124 | + Argument::withEntry('resource_class', Foo::class), |
| 125 | + Argument::withEntry('api_sub_level', true) |
| 126 | + ))->willReturn([ |
| 127 | + $normalizedFooOne, |
| 128 | + $normalizedFooThree, |
| 129 | + ]); |
| 130 | + |
| 131 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 132 | + |
| 133 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 134 | + |
| 135 | + $filterLocatorProphecy = $this->prophesize(ContainerInterface::class); |
| 136 | + |
| 137 | + $normalizer = new CollectionFiltersNormalizer($decoratedProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $filterLocatorProphecy->reveal()); |
| 138 | + |
| 139 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 140 | + 'collection_operation_name' => 'get', |
| 141 | + 'operation_type' => OperationType::COLLECTION, |
| 142 | + 'resource_class' => Foo::class, |
| 143 | + 'api_sub_level' => true, |
| 144 | + ]); |
| 145 | + |
| 146 | + $this->assertEquals([ |
| 147 | + $normalizedFooOne, |
| 148 | + $normalizedFooThree, |
| 149 | + ], $actual); |
| 150 | + } |
| 151 | + |
| 152 | + public function testNormalizeSubLevelNonResourceCollection() |
| 153 | + { |
| 154 | + $notAResourceA = new NotAResource('A', 'buzz'); |
| 155 | + $notAResourceB = new NotAResource('B', 'bzzt'); |
| 156 | + |
| 157 | + $data = [$notAResourceA, $notAResourceB]; |
| 158 | + |
| 159 | + $normalizedNotAResourceA = [ |
| 160 | + 'foo' => 'A', |
| 161 | + 'bar' => 'buzz', |
| 162 | + ]; |
| 163 | + |
| 164 | + $normalizedNotAResourceB = [ |
| 165 | + 'foo' => 'B', |
| 166 | + 'bar' => 'bzzt', |
| 167 | + ]; |
| 168 | + |
| 169 | + $decoratedProphecy = $this->prophesize(NormalizerInterface::class); |
| 170 | + $decoratedProphecy->normalize($data, CollectionNormalizer::FORMAT, Argument::any())->willReturn([ |
| 171 | + $normalizedNotAResourceA, |
| 172 | + $normalizedNotAResourceB, |
| 173 | + ]); |
| 174 | + |
| 175 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 176 | + |
| 177 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 178 | + $resourceClassResolverProphecy->getResourceClass($data, null, true)->willThrow(InvalidArgumentException::class); |
| 179 | + |
| 180 | + $filterLocatorProphecy = $this->prophesize(ContainerInterface::class); |
| 181 | + |
| 182 | + $normalizer = new CollectionFiltersNormalizer($decoratedProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $filterLocatorProphecy->reveal()); |
| 183 | + |
| 184 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 185 | + 'api_sub_level' => true, |
| 186 | + ]); |
| 187 | + |
| 188 | + $this->assertEquals([ |
| 189 | + $normalizedNotAResourceA, |
| 190 | + $normalizedNotAResourceB, |
| 191 | + ], $actual); |
70 | 192 | }
|
71 | 193 |
|
72 | 194 | public function testDoNothingIfNoFilter()
|
|
0 commit comments