Skip to content

Commit c9fb1f1

Browse files
committed
Add unit tests
1 parent 0a6dad7 commit c9fb1f1

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

tests/Api/FormatsProviderTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Api;
15+
16+
use ApiPlatform\Core\Api\FormatsProvider;
17+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
19+
use PHPUnit\Framework\TestCase;
20+
use Symfony\Component\HttpFoundation\Request;
21+
22+
/**
23+
* @author Anthony GRASSIOT <antograssiot@free.fr>
24+
*/
25+
class FormatsProviderTest extends TestCase
26+
{
27+
public function testNoResourceClass()
28+
{
29+
$request = new Request();
30+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
31+
$resourceMetadataFactoryProphecy->create()->shouldNotBeCalled();
32+
33+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => 'application/ld+json']);
34+
35+
$this->assertSame(['jsonld' => 'application/ld+json'], $formatProvider->fromRequest($request));
36+
}
37+
38+
public function testResourceClassWithoutFilters()
39+
{
40+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
41+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
42+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata())->shouldBeCalled();
43+
44+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json']]);
45+
46+
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->fromRequest($request));
47+
}
48+
49+
public function testResourceClassWithFilters()
50+
{
51+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
52+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
53+
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => 'jsonld']);
54+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
55+
56+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'json' => ['application/json']]);
57+
58+
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->fromRequest($request));
59+
}
60+
61+
public function testResourceClassWithFiltersOverRiddingMimeTypes()
62+
{
63+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
64+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
65+
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['jsonld' => ['application/foo'], 'bar' => ['mime_types' => ['application/bar', 'application/baz']]]]);
66+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
67+
68+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'json' => ['application/json']]);
69+
70+
$this->assertSame(['jsonld' => ['application/foo'], 'bar' => ['application/bar', 'application/baz']], $formatProvider->fromRequest($request));
71+
}
72+
73+
/**
74+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
75+
* @expectedExceptionMessage You either need to add the format 'foo' to your project configuration or declare a mime type for it in your annotation.
76+
*/
77+
public function testBadFormatShortDeclaration()
78+
{
79+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
80+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
81+
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['foo']]);
82+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
83+
84+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json']]);
85+
86+
$formatProvider->fromRequest($request);
87+
}
88+
89+
/**
90+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
91+
* @expectedExceptionMessage Mime type for format 'foo' must be declared as an array.
92+
*/
93+
public function testBadFormatMimeTypeDeclaration()
94+
{
95+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
96+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
97+
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['foo' => 'application/foo']]);
98+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
99+
100+
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json']]);
101+
102+
$formatProvider->fromRequest($request);
103+
}
104+
}

tests/EventListener/AddFormatListenerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Tests\EventListener;
1515

16+
use ApiPlatform\Core\Api\FormatsProviderInterface;
1617
use ApiPlatform\Core\EventListener\AddFormatListener;
1718
use Negotiation\Negotiator;
1819
use PHPUnit\Framework\TestCase;
@@ -185,4 +186,23 @@ public function testInvalidRouteFormat()
185186
$listener = new AddFormatListener(new Negotiator(), ['json' => ['application/json']]);
186187
$listener->onKernelRequest($event);
187188
}
189+
190+
public function testResourceClassSupportedRequestFormat()
191+
{
192+
$request = new Request([], [], ['_api_resource_class' => 'Foo']);
193+
$request->setRequestFormat('csv');
194+
195+
$eventProphecy = $this->prophesize(GetResponseEvent::class);
196+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
197+
$event = $eventProphecy->reveal();
198+
199+
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
200+
$formatsProviderProphecy->fromRequest($request)->willReturn(['csv' => ['text/csv']])->shouldbeCalled();
201+
202+
$listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']], $formatsProviderProphecy->reveal());
203+
$listener->onKernelRequest($event);
204+
205+
$this->assertSame('csv', $request->getRequestFormat());
206+
$this->assertSame('text/csv', $request->getMimeType($request->getRequestFormat()));
207+
}
188208
}

tests/EventListener/DeserializeListenerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Tests\EventListener;
1515

16+
use ApiPlatform\Core\Api\FormatsProviderInterface;
1617
use ApiPlatform\Core\EventListener\DeserializeListener;
1718
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
1819
use PHPUnit\Framework\TestCase;
@@ -126,6 +127,34 @@ public function testDeserialize(string $method, bool $populateObject)
126127
$listener->onKernelRequest($eventProphecy->reveal());
127128
}
128129

130+
/**
131+
* @dataProvider methodProvider
132+
*/
133+
public function testDeserializeResourceClassSupportedFormat(string $method, bool $populateObject)
134+
{
135+
$result = $populateObject ? new \stdClass() : null;
136+
$eventProphecy = $this->prophesize(GetResponseEvent::class);
137+
138+
$request = new Request([], [], ['data' => $result, '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}');
139+
$request->setMethod($method);
140+
$request->headers->set('Content-Type', 'application/json');
141+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
142+
143+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
144+
$context = $populateObject ? [AbstractNormalizer::OBJECT_TO_POPULATE => $populateObject] : [];
145+
$serializerProphecy->deserialize('{}', 'Foo', 'json', $context)->willReturn($result)->shouldBeCalled();
146+
147+
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
148+
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn([])->shouldBeCalled();
149+
150+
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
151+
$formatsProviderProphecy->fromRequest($request)->willReturn(self::FORMATS)->shouldbeCalled();
152+
153+
$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), ['xml' => ['text/xml']], $formatsProviderProphecy->reveal());
154+
155+
$listener->onKernelRequest($eventProphecy->reveal());
156+
}
157+
129158
public function methodProvider()
130159
{
131160
return [['POST', false], ['PUT', true]];

0 commit comments

Comments
 (0)