|
23 | 23 | use ApiPlatform\Serializer\AbstractItemNormalizer; |
24 | 24 | use ApiPlatform\State\Pagination\PaginatorInterface; |
25 | 25 | use ApiPlatform\State\Pagination\PartialPaginatorInterface; |
| 26 | +use PHPUnit\Framework\Attributes\DataProvider; |
26 | 27 | use PHPUnit\Framework\TestCase; |
27 | 28 | use Prophecy\Argument; |
28 | 29 | use Prophecy\PhpUnit\ProphecyTrait; |
@@ -123,6 +124,77 @@ public function testNormalizeResourceCollection(): void |
123 | 124 | ], $actual); |
124 | 125 | } |
125 | 126 |
|
| 127 | + #[DataProvider('normalizeWithKeyDataProvider')] |
| 128 | + public function testNormalizeResourceWithKeysCollection(bool $preserveKeys): void |
| 129 | + { |
| 130 | + $fooOne = new Foo(); |
| 131 | + $fooOne->id = 1; |
| 132 | + $fooOne->bar = 'baz'; |
| 133 | + |
| 134 | + $fooThree = new Foo(); |
| 135 | + $fooThree->id = 3; |
| 136 | + $fooThree->bar = 'bzz'; |
| 137 | + |
| 138 | + $data = [$fooOne, 3 => $fooThree]; |
| 139 | + |
| 140 | + $normalizedFooOne = [ |
| 141 | + '@id' => '/foos/1', |
| 142 | + '@type' => 'Foo', |
| 143 | + 'bar' => 'baz', |
| 144 | + ]; |
| 145 | + |
| 146 | + $normalizedFooThree = [ |
| 147 | + '@id' => '/foos/3', |
| 148 | + '@type' => 'Foo', |
| 149 | + 'bar' => 'bzz', |
| 150 | + ]; |
| 151 | + |
| 152 | + $contextBuilderProphecy = $this->createMock(ContextBuilderInterface::class); |
| 153 | + $contextBuilderProphecy->method('getResourceContextUri')->with(Foo::class)->willReturn('/contexts/Foo'); |
| 154 | + |
| 155 | + $resourceClassResolverProphecy = $this->createMock(ResourceClassResolverInterface::class); |
| 156 | + $resourceClassResolverProphecy->method('getResourceClass')->with($data, Foo::class)->willReturn(Foo::class); |
| 157 | + |
| 158 | + $iriConverterProphecy = $this->createMock(IriConverterInterface::class); |
| 159 | + $iriConverterProphecy->method('getIriFromResource')->with(Foo::class, UrlGeneratorInterface::ABS_PATH, null)->willReturn('/foos'); |
| 160 | + |
| 161 | + $delegateNormalizerProphecy = $this->createMock(NormalizerInterface::class); |
| 162 | + $delegateNormalizerProphecy->method('normalize')->willReturnCallback( |
| 163 | + static fn (Foo $item) => 1 === $item->id ? $normalizedFooOne : $normalizedFooThree |
| 164 | + ); |
| 165 | + |
| 166 | + $normalizer = new CollectionNormalizer($contextBuilderProphecy, $resourceClassResolverProphecy, $iriConverterProphecy); |
| 167 | + $normalizer->setNormalizer($delegateNormalizerProphecy); |
| 168 | + |
| 169 | + $actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [ |
| 170 | + 'operation_name' => 'get', |
| 171 | + 'resource_class' => Foo::class, |
| 172 | + CollectionNormalizer::PRESERVE_COLLECTION_KEYS => $preserveKeys, |
| 173 | + ]); |
| 174 | + |
| 175 | + $this->assertEquals([ |
| 176 | + '@context' => '/contexts/Foo', |
| 177 | + '@id' => '/foos', |
| 178 | + '@type' => 'hydra:Collection', |
| 179 | + 'hydra:member' => $preserveKeys ? [ |
| 180 | + $normalizedFooOne, |
| 181 | + 3 => $normalizedFooThree, |
| 182 | + ] : [ |
| 183 | + $normalizedFooOne, |
| 184 | + $normalizedFooThree, |
| 185 | + ], |
| 186 | + 'hydra:totalItems' => 2, |
| 187 | + ], $actual); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @return array<array{bool}> |
| 192 | + */ |
| 193 | + public static function normalizeWithKeyDataProvider(): array |
| 194 | + { |
| 195 | + return [[false], [true]]; |
| 196 | + } |
| 197 | + |
126 | 198 | public function testNormalizePaginator(): void |
127 | 199 | { |
128 | 200 | $this->assertEquals( |
|
0 commit comments