Skip to content

Commit 5000965

Browse files
Fix the registration of the "test.api_platform.client" service (#3938)
1 parent 4f4efc3 commit 5000965

File tree

9 files changed

+119
-28
lines changed

9 files changed

+119
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
* Symfony: Allow using `ItemNormalizer` without Symfony SecurityBundle (#3801)
5151
* Symfony: Lazy load all commands (#3798)
5252
* Tests: adds a method to retrieve the CookieJar in the test Client `getCookieJar`
53+
* Tests: Fix the registration of the `test.api_platform.client` service when the `FrameworkBundle` bundle is registered after the `ApiPlatformBundle` bundle (#3928)
5354
* Validator: Add the violation code to the violation properties (#3857)
5455
* Validator: Allow customizing the validation error status code (#3808)
5556
* Validator: Autoconfiguration of validation groups generator via `ApiPlatform\Core\Validator\ValidationGroupsGeneratorInterface`
5657
* Validator: Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
5758
* Validator: Property validation through OpenAPI (#33329)
58-
* Validator:Query filters and parameters are validated (#1723)
59+
* Validator: Query filters and parameters are validated (#1723)
5960
* `ExceptionInterface` now extends `\Throwable` (#3217)
6061

6162
## 2.5.9

src/Bridge/Symfony/Bundle/ApiPlatformBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlQueryResolverPass;
2323
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
2424
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
25+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
2526
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2627
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
2728
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -51,5 +52,6 @@ public function build(ContainerBuilder $container)
5152
$container->addCompilerPass(new GraphQlMutationResolverPass());
5253
$container->addCompilerPass(new DeprecateMercurePublisherPass());
5354
$container->addCompilerPass(new MetadataAwareNameConverterPass());
55+
$container->addCompilerPass(new TestClientPass());
5456
}
5557
}

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use Doctrine\Common\Annotations\Annotation;
4242
use phpDocumentor\Reflection\DocBlockFactoryInterface;
4343
use Ramsey\Uuid\Uuid;
44-
use Symfony\Component\BrowserKit\AbstractBrowser;
4544
use Symfony\Component\Cache\Adapter\ArrayAdapter;
4645
use Symfony\Component\Config\FileLocator;
4746
use Symfony\Component\Config\Resource\DirectoryResource;
@@ -53,7 +52,6 @@
5352
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
5453
use Symfony\Component\DependencyInjection\Reference;
5554
use Symfony\Component\Finder\Finder;
56-
use Symfony\Component\HttpClient\HttpClientTrait;
5755
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
5856
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
5957
use Symfony\Component\Uid\AbstractUid;
@@ -138,14 +136,6 @@ public function load(array $configs, ContainerBuilder $container): void
138136
->addTag('api_platform.subresource_data_provider');
139137
$container->registerForAutoconfiguration(FilterInterface::class)
140138
->addTag('api_platform.filter');
141-
142-
if ($container->hasParameter('test.client.parameters')) {
143-
$loader->load('test.xml');
144-
145-
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
146-
$container->removeDefinition('test.api_platform.client');
147-
}
148-
}
149139
}
150140

151141
private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
17+
use Symfony\Component\BrowserKit\AbstractBrowser;
18+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Definition;
21+
use Symfony\Component\DependencyInjection\Reference;
22+
use Symfony\Component\HttpClient\HttpClientTrait;
23+
24+
final class TestClientPass implements CompilerPassInterface
25+
{
26+
public function process(ContainerBuilder $container)
27+
{
28+
if (
29+
!class_exists(AbstractBrowser::class) ||
30+
!trait_exists(HttpClientTrait::class) ||
31+
!$container->hasParameter('test.client.parameters')
32+
) {
33+
return;
34+
}
35+
36+
$container->setDefinition(
37+
'test.api_platform.client',
38+
(new Definition(Client::class, [new Reference('test.client')]))
39+
->setShared(false)
40+
->setPublic(true)
41+
);
42+
}
43+
}

src/Bridge/Symfony/Bundle/Resources/config/test.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/Bridge/Symfony/Bundle/ApiPlatformBundleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlQueryResolverPass;
2424
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
2525
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
26+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
2627
use ApiPlatform\Core\Tests\ProphecyTrait;
2728
use PHPUnit\Framework\TestCase;
2829
use Prophecy\Argument;
@@ -48,6 +49,7 @@ public function testBuild()
4849
$containerProphecy->addCompilerPass(Argument::type(GraphQlMutationResolverPass::class))->shouldBeCalled();
4950
$containerProphecy->addCompilerPass(Argument::type(DeprecateMercurePublisherPass::class))->shouldBeCalled();
5051
$containerProphecy->addCompilerPass(Argument::type(MetadataAwareNameConverterPass::class))->shouldBeCalled();
52+
$containerProphecy->addCompilerPass(Argument::type(TestClientPass::class))->shouldBeCalled();
5153

5254
$bundle = new ApiPlatformBundle();
5355
$bundle->build($containerProphecy->reveal());

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11591159
foreach ($parameters as $key => $value) {
11601160
$containerBuilderProphecy->setParameter($key, $value)->shouldBeCalled();
11611161
}
1162-
$containerBuilderProphecy->hasParameter('test.client.parameters')->wilLReturn(true);
11631162

11641163
foreach (['yaml', 'xml'] as $format) {
11651164
$definitionProphecy = $this->prophesize(Definition::class);
@@ -1270,7 +1269,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12701269
'api_platform.swagger.listener.ui',
12711270
'api_platform.validator',
12721271
'api_platform.validator.query_parameter_validator',
1273-
'test.api_platform.client',
12741272
];
12751273

12761274
if (\in_array('odm', $doctrineIntegrationsToLoad, true)) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
17+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
18+
use PHPUnit\Framework\TestCase;
19+
use Prophecy\Argument;
20+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21+
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Definition;
23+
24+
final class TestClientPassTest extends TestCase
25+
{
26+
private $containerBuilderProphecy;
27+
private $testClientPass;
28+
29+
protected function setUp(): void
30+
{
31+
$this->containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
32+
$this->testClientPass = new TestClientPass();
33+
}
34+
35+
public function testConstruct(): void
36+
{
37+
self::assertInstanceOf(CompilerPassInterface::class, $this->testClientPass);
38+
}
39+
40+
public function testProcessWithoutTestClientParameters(): void
41+
{
42+
$this->containerBuilderProphecy->hasParameter('test.client.parameters')->willReturn(false)->shouldBeCalledOnce();
43+
$this->containerBuilderProphecy->setDefinition('test.api_platform.client', Argument::type(Definition::class))->shouldNotBeCalled();
44+
45+
$this->testClientPass->process($this->containerBuilderProphecy->reveal());
46+
}
47+
48+
public function testProcess(): void
49+
{
50+
$this->containerBuilderProphecy->hasParameter('test.client.parameters')->willReturn(true)->shouldBeCalledOnce();
51+
$this->containerBuilderProphecy
52+
->setDefinition(
53+
'test.api_platform.client',
54+
Argument::allOf(
55+
Argument::type(Definition::class),
56+
Argument::that(function (Definition $testClientDefinition) {
57+
return
58+
Client::class === $testClientDefinition->getClass() &&
59+
!$testClientDefinition->isShared() &&
60+
$testClientDefinition->isPublic();
61+
})
62+
)
63+
)
64+
->shouldBeCalledOnce();
65+
66+
$this->testClientPass->process($this->containerBuilderProphecy->reveal());
67+
}
68+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function __construct(string $environment, bool $debug)
6161
public function registerBundles(): array
6262
{
6363
$bundles = [
64-
new FrameworkBundle(),
64+
new ApiPlatformBundle(),
6565
new TwigBundle(),
6666
new DoctrineBundle(),
6767
new MercureBundle(),
68-
new ApiPlatformBundle(),
6968
new SecurityBundle(),
7069
new WebProfilerBundle(),
7170
new FriendsOfBehatSymfonyExtensionBundle(),
71+
new FrameworkBundle(),
7272
];
7373

7474
if (class_exists(DoctrineMongoDBBundle::class)) {

0 commit comments

Comments
 (0)