Repository navigation
Expand file tree
/
Copy pathApiPlatformCompilerPassTest.php
More file actions
177 lines (137 loc) · 8.18 KB
/
Copy pathApiPlatformCompilerPassTest.php
File metadata and controls
177 lines (137 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file is part of the Silverback API Components Bundle Project
*
* (c) Daniel West <daniel@silverback.is>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silverback\ApiComponentsBundle\Tests\DependencyInjection\CompilerPass;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use Doctrine\ORM\OptimisticLockException;
use PHPUnit\Framework\TestCase;
use Silverback\ApiComponentsBundle\DependencyInjection\CompilerPass\ApiPlatformCompilerPass;
use Silverback\ApiComponentsBundle\Exception\UnroutedParentException;
use Silverback\ApiComponentsBundle\Helper\Collection\CollectionPopulator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
class ApiPlatformCompilerPassTest extends TestCase
{
private const string FLUSHER_ID = 'silverback.api_components.http_cache.flusher';
public function test_the_flusher_receives_each_invalidation_url_with_its_client(): void
{
$container = $this->createContainer();
$this->addInvalidationClient($container, 'api_platform.invalidation_http_client.0', 'http://localhost:2019/souin-api/souin');
$this->addInvalidationClient($container, 'api_platform.invalidation_http_client.1', '%env(CACHE_URL)%');
(new ApiPlatformCompilerPass())->process($container);
self::assertEquals([
['url' => 'http://localhost:2019/souin-api/souin', 'client' => new Reference('api_platform.invalidation_http_client.0')],
['url' => '%env(CACHE_URL)%', 'client' => new Reference('api_platform.invalidation_http_client.1')],
], $container->getDefinition(self::FLUSHER_ID)->getArgument('$invalidationClients'));
}
public function test_scoped_clients_are_not_given_to_the_flusher(): void
{
$container = $this->createContainer();
$container->setDefinition('app.scoped_cache_client', (new Definition(ScopingHttpClient::class))->addTag('api_platform.http_cache.http_client'));
(new ApiPlatformCompilerPass())->process($container);
self::assertSame([], $container->getDefinition(self::FLUSHER_ID)->getArgument('$invalidationClients'));
}
public function test_the_collection_listener_receives_the_items_per_page_parameter_name(): void
{
$container = $this->createContainer();
(new ApiPlatformCompilerPass())->process($container);
self::assertSame('perPage', $container->getDefinition(CollectionPopulator::class)->getArgument('$itemsPerPageParameterName'));
}
public function test_the_api_platform_purge_listener_is_replaced_when_a_purger_is_configured(): void
{
$container = $this->createContainer();
$container->setDefinition('app.purger', new Definition());
$container->setAlias('api_platform.http_cache.purger', 'app.purger');
(new ApiPlatformCompilerPass())->process($container);
self::assertFalse($container->hasDefinition('api_platform.doctrine.listener.http_cache.purge'));
self::assertTrue($container->hasDefinition('silverback.api_components.http_cache.purger'));
}
public function test_the_bundle_purger_is_removed_when_no_purger_is_configured(): void
{
$container = $this->createContainer();
(new ApiPlatformCompilerPass())->process($container);
self::assertTrue($container->hasDefinition('api_platform.doctrine.listener.http_cache.purge'));
self::assertFalse($container->hasDefinition('silverback.api_components.http_cache.purger'));
}
public function test_the_api_platform_mercure_publish_listener_is_removed(): void
{
$container = $this->createContainer();
$container->setDefinition('api_platform.doctrine.orm.listener.mercure.publish', new Definition());
(new ApiPlatformCompilerPass())->process($container);
self::assertFalse($container->hasDefinition('api_platform.doctrine.orm.listener.mercure.publish'));
}
public function test_api_platform_default_exception_statuses_are_appended_after_the_configured_ones(): void
{
$container = $this->createContainer();
$container->setParameter('api_platform.exception_to_status', [
UnroutedParentException::class => 422,
NotNormalizableValueException::class => 422,
]);
(new ApiPlatformCompilerPass())->process($container);
$exceptionToStatus = $container->getParameter('api_platform.exception_to_status');
self::assertSame([UnroutedParentException::class, NotNormalizableValueException::class], \array_slice(array_keys($exceptionToStatus), 0, 2));
self::assertSame(400, $exceptionToStatus[SerializerExceptionInterface::class]);
self::assertSame(400, $exceptionToStatus[InvalidArgumentException::class]);
self::assertSame(409, $exceptionToStatus[OptimisticLockException::class]);
}
public function test_a_configured_status_for_a_default_exception_is_kept(): void
{
$container = $this->createContainer();
$container->setParameter('api_platform.exception_to_status', [
OptimisticLockException::class => 412,
]);
(new ApiPlatformCompilerPass())->process($container);
$exceptionToStatus = $container->getParameter('api_platform.exception_to_status');
self::assertSame(412, $exceptionToStatus[OptimisticLockException::class]);
self::assertSame(OptimisticLockException::class, array_key_first($exceptionToStatus));
self::assertSame(400, $exceptionToStatus[SerializerExceptionInterface::class]);
}
public function test_the_skolem_iri_converter_is_reset_between_requests(): void
{
$container = $this->createContainer();
$container->setDefinition('api_platform.symfony.iri_converter.skolem', new Definition());
(new ApiPlatformCompilerPass())->process($container);
self::assertSame([['method' => 'reset']], $container->getDefinition('api_platform.symfony.iri_converter.skolem')->getTag('kernel.reset'));
}
public function test_a_skolem_iri_converter_already_tagged_kernel_reset_keeps_its_one_tag(): void
{
$container = $this->createContainer();
$container->setDefinition('api_platform.symfony.iri_converter.skolem', (new Definition())->addTag('kernel.reset', ['method' => 'reset']));
(new ApiPlatformCompilerPass())->process($container);
self::assertSame([['method' => 'reset']], $container->getDefinition('api_platform.symfony.iri_converter.skolem')->getTag('kernel.reset'));
}
public function test_without_a_skolem_iri_converter_nothing_is_defined(): void
{
$container = $this->createContainer();
(new ApiPlatformCompilerPass())->process($container);
self::assertFalse($container->hasDefinition('api_platform.symfony.iri_converter.skolem'));
}
private function createContainer(): ContainerBuilder
{
$container = new ContainerBuilder();
$container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', 'perPage');
$container->setParameter('api_platform.exception_to_status', []);
$container->setDefinition(CollectionPopulator::class, new Definition(CollectionPopulator::class));
$container->setDefinition(self::FLUSHER_ID, new Definition());
$container->setDefinition('api_platform.doctrine.listener.http_cache.purge', new Definition());
$container->setDefinition('silverback.api_components.http_cache.purger', new Definition());
return $container;
}
private function addInvalidationClient(ContainerBuilder $container, string $id, string $url): void
{
$definition = new Definition(ScopingHttpClient::class, [new Reference('http_client'), $url, ['base_uri' => $url]]);
$definition->setFactory([ScopingHttpClient::class, 'forBaseUri']);
$definition->addTag('api_platform.http_cache.http_client');
$container->setDefinition($id, $definition);
}
}