|
14 | 14 | namespace ApiPlatform\State\Tests\Provider;
|
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\Get;
|
| 17 | +use ApiPlatform\Metadata\HttpOperation; |
| 18 | +use ApiPlatform\Metadata\Patch; |
17 | 19 | use ApiPlatform\Metadata\Post;
|
| 20 | +use ApiPlatform\Metadata\Put; |
18 | 21 | use ApiPlatform\State\Provider\DeserializeProvider;
|
19 | 22 | use ApiPlatform\State\ProviderInterface;
|
20 | 23 | use ApiPlatform\State\SerializerContextBuilderInterface;
|
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
21 | 26 | use PHPUnit\Framework\TestCase;
|
22 | 27 | use Symfony\Component\HttpFoundation\Request;
|
23 | 28 | use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
|
|
26 | 31 |
|
27 | 32 | class DeserializeProviderTest extends TestCase
|
28 | 33 | {
|
| 34 | + #[IgnoreDeprecations] |
29 | 35 | public function testDeserialize(): void
|
30 | 36 | {
|
| 37 | + $this->expectUserDeprecationMessage('Since api-platform/core 5.0: To assign an object to populate you should set "api_assign_object_to_populate" in your denormalizationContext, not defining it is deprecated.'); |
31 | 38 | $objectToPopulate = new \stdClass();
|
32 | 39 | $serializerContext = [];
|
33 | 40 | $operation = new Post(deserialize: true, class: 'Test');
|
@@ -128,4 +135,101 @@ public function testRequestWithEmptyContentType(): void
|
128 | 135 | $this->expectException(UnsupportedMediaTypeHttpException::class);
|
129 | 136 | $provider->provide($operation, [], $context);
|
130 | 137 | }
|
| 138 | + |
| 139 | + #[DataProvider('provideMethodsTriggeringDeprecation')] |
| 140 | + #[IgnoreDeprecations] |
| 141 | + public function testDeserializeTriggersDeprecationWhenContextNotSet(HttpOperation $operation): void |
| 142 | + { |
| 143 | + $this->expectUserDeprecationMessage('Since api-platform/core 5.0: To assign an object to populate you should set "api_assign_object_to_populate" in your denormalizationContext, not defining it is deprecated.'); |
| 144 | + |
| 145 | + $objectToPopulate = new \stdClass(); |
| 146 | + $serializerContext = []; |
| 147 | + $decorated = $this->createStub(ProviderInterface::class); |
| 148 | + $decorated->method('provide')->willReturn($objectToPopulate); |
| 149 | + |
| 150 | + $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class); |
| 151 | + $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext); |
| 152 | + |
| 153 | + $serializer = $this->createMock(SerializerInterface::class); |
| 154 | + $serializer->expects($this->once())->method('deserialize')->with( |
| 155 | + 'test', |
| 156 | + 'Test', |
| 157 | + 'format', |
| 158 | + ['uri_variables' => ['id' => 1], 'object_to_populate' => $objectToPopulate] + $serializerContext |
| 159 | + )->willReturn(new \stdClass()); |
| 160 | + |
| 161 | + $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder); |
| 162 | + $request = new Request(content: 'test'); |
| 163 | + $request->headers->set('CONTENT_TYPE', 'ok'); |
| 164 | + $request->attributes->set('input_format', 'format'); |
| 165 | + $provider->provide($operation, ['id' => 1], ['request' => $request]); |
| 166 | + } |
| 167 | + |
| 168 | + public static function provideMethodsTriggeringDeprecation(): iterable |
| 169 | + { |
| 170 | + yield 'POST method' => [new Post(deserialize: true, class: 'Test')]; |
| 171 | + yield 'PATCH method' => [new Patch(deserialize: true, class: 'Test')]; |
| 172 | + yield 'PUT method (non-standard)' => [new Put(deserialize: true, class: 'Test', extraProperties: ['standard_put' => false])]; |
| 173 | + } |
| 174 | + |
| 175 | + public function testDeserializeSetsObjectToPopulateWhenContextIsTrue(): void |
| 176 | + { |
| 177 | + $objectToPopulate = new \stdClass(); |
| 178 | + $serializerContext = [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => true]; |
| 179 | + $operation = new Post(deserialize: true, class: 'Test'); |
| 180 | + $decorated = $this->createStub(ProviderInterface::class); |
| 181 | + $decorated->method('provide')->willReturn($objectToPopulate); |
| 182 | + |
| 183 | + $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class); |
| 184 | + $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext); |
| 185 | + |
| 186 | + $serializer = $this->createMock(SerializerInterface::class); |
| 187 | + $serializer->expects($this->once())->method('deserialize')->with( |
| 188 | + 'test', |
| 189 | + 'Test', |
| 190 | + 'format', |
| 191 | + $this->callback(function (array $context) use ($objectToPopulate) { |
| 192 | + $this->assertArrayHasKey(AbstractNormalizer::OBJECT_TO_POPULATE, $context); |
| 193 | + $this->assertSame($objectToPopulate, $context[AbstractNormalizer::OBJECT_TO_POPULATE]); |
| 194 | + |
| 195 | + return true; |
| 196 | + }) |
| 197 | + )->willReturn(new \stdClass()); |
| 198 | + |
| 199 | + $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder); |
| 200 | + $request = new Request(content: 'test'); |
| 201 | + $request->headers->set('CONTENT_TYPE', 'ok'); |
| 202 | + $request->attributes->set('input_format', 'format'); |
| 203 | + $provider->provide($operation, ['id' => 1], ['request' => $request]); |
| 204 | + } |
| 205 | + |
| 206 | + public function testDeserializeDoesNotSetObjectToPopulateWhenContextIsFalse(): void |
| 207 | + { |
| 208 | + $objectToPopulate = new \stdClass(); |
| 209 | + $serializerContext = [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => false]; |
| 210 | + $operation = new Post(deserialize: true, class: 'Test'); |
| 211 | + $decorated = $this->createStub(ProviderInterface::class); |
| 212 | + $decorated->method('provide')->willReturn($objectToPopulate); |
| 213 | + |
| 214 | + $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class); |
| 215 | + $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext); |
| 216 | + |
| 217 | + $serializer = $this->createMock(SerializerInterface::class); |
| 218 | + $serializer->expects($this->once())->method('deserialize')->with( |
| 219 | + 'test', |
| 220 | + 'Test', |
| 221 | + 'format', |
| 222 | + $this->callback(function (array $context) { |
| 223 | + $this->assertArrayNotHasKey(AbstractNormalizer::OBJECT_TO_POPULATE, $context); |
| 224 | + |
| 225 | + return true; |
| 226 | + }) |
| 227 | + )->willReturn(new \stdClass()); |
| 228 | + |
| 229 | + $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder); |
| 230 | + $request = new Request(content: 'test'); |
| 231 | + $request->headers->set('CONTENT_TYPE', 'ok'); |
| 232 | + $request->attributes->set('input_format', 'format'); |
| 233 | + $provider->provide($operation, ['id' => 1], ['request' => $request]); |
| 234 | + } |
131 | 235 | }
|
0 commit comments