|
14 | 14 | namespace ApiPlatform\Core\Tests\EventListener;
|
15 | 15 |
|
16 | 16 | use ApiPlatform\Core\EventListener\AddFormatListener;
|
| 17 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 18 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
17 | 19 | use Negotiation\Negotiator;
|
18 | 20 | use PHPUnit\Framework\TestCase;
|
19 | 21 | use Symfony\Component\HttpFoundation\Request;
|
@@ -185,4 +187,90 @@ public function testInvalidRouteFormat()
|
185 | 187 | $listener = new AddFormatListener(new Negotiator(), ['json' => ['application/json']]);
|
186 | 188 | $listener->onKernelRequest($event);
|
187 | 189 | }
|
| 190 | + |
| 191 | + public function testSupportedRequestFormatAndResourceClassWithoutSpecifiedFormatsInAnnotation() |
| 192 | + { |
| 193 | + $request = new Request([], [], ['_api_resource_class' => 'Foo']); |
| 194 | + $request->setRequestFormat('xml'); |
| 195 | + |
| 196 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 197 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 198 | + $event = $eventProphecy->reveal(); |
| 199 | + |
| 200 | + $dummyMetadata = new ResourceMetadata(); |
| 201 | + |
| 202 | + $resourceMetaDataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 203 | + $resourceMetaDataFactoryProphecy->create('Foo')->willReturn($dummyMetadata)->shouldbeCalled(); |
| 204 | + |
| 205 | + $listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']], $resourceMetaDataFactoryProphecy->reveal()); |
| 206 | + $listener->onKernelRequest($event); |
| 207 | + |
| 208 | + $this->assertSame('xml', $request->getRequestFormat()); |
| 209 | + $this->assertSame('text/xml', $request->getMimeType($request->getRequestFormat())); |
| 210 | + } |
| 211 | + |
| 212 | + public function testSupportedRequestFormatAndResourceClassWithSpecifiedFormatsInAnnotation() |
| 213 | + { |
| 214 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); |
| 215 | + $request->setRequestFormat('pdf'); |
| 216 | + |
| 217 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 218 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 219 | + $event = $eventProphecy->reveal(); |
| 220 | + |
| 221 | + $dummyMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['pdf' => ['application/pdf']]]); |
| 222 | + |
| 223 | + $resourceMetaDataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 224 | + $resourceMetaDataFactoryProphecy->create('Foo')->willReturn($dummyMetadata)->shouldbeCalled(); |
| 225 | + |
| 226 | + $listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml'], 'jsonld'], $resourceMetaDataFactoryProphecy->reveal()); |
| 227 | + $listener->onKernelRequest($event); |
| 228 | + |
| 229 | + $this->assertSame('pdf', $request->getRequestFormat()); |
| 230 | + $this->assertSame('application/pdf', $request->getMimeType($request->getRequestFormat())); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException |
| 235 | + * @expectedExceptionMessage Requested format "text/xml" is not supported. Supported MIME types are "application/json". |
| 236 | + */ |
| 237 | + public function testResourceClassWithSpecifiedFormatsInAnnotationOverridesDefault() |
| 238 | + { |
| 239 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); |
| 240 | + $request->setRequestFormat('xml'); |
| 241 | + |
| 242 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 243 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 244 | + $event = $eventProphecy->reveal(); |
| 245 | + |
| 246 | + $dummyMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['json' => ['application/json']]]); |
| 247 | + |
| 248 | + $resourceMetaDataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 249 | + $resourceMetaDataFactoryProphecy->create('Foo')->willReturn($dummyMetadata)->shouldbeCalled(); |
| 250 | + |
| 251 | + $listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']], $resourceMetaDataFactoryProphecy->reveal()); |
| 252 | + $listener->onKernelRequest($event); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException |
| 257 | + * @expectedExceptionMessage You either need to add the format 'json' to your project configuration or declare a mime type for it in your annotation. |
| 258 | + */ |
| 259 | + public function testResourceClassWithShortFormatsInAnnotationThatIsNotInConfig() |
| 260 | + { |
| 261 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']); |
| 262 | + $request->setRequestFormat('xml'); |
| 263 | + |
| 264 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 265 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 266 | + $event = $eventProphecy->reveal(); |
| 267 | + |
| 268 | + $dummyMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['json']]); |
| 269 | + |
| 270 | + $resourceMetaDataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 271 | + $resourceMetaDataFactoryProphecy->create('Foo')->willReturn($dummyMetadata)->shouldbeCalled(); |
| 272 | + |
| 273 | + $listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']], $resourceMetaDataFactoryProphecy->reveal()); |
| 274 | + $listener->onKernelRequest($event); |
| 275 | + } |
188 | 276 | }
|
0 commit comments