Skip to content

Fix the registration of the "test.api_platform.client" service #3938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@
* Symfony: Allow using `ItemNormalizer` without Symfony SecurityBundle (#3801)
* Symfony: Lazy load all commands (#3798)
* Tests: adds a method to retrieve the CookieJar in the test Client `getCookieJar`
* Tests: Fix the registration of the `test.api_platform.client` service when the `FrameworkBundle` bundle is registered after the `ApiPlatformBundle` bundle (#3928)
* Validator: Add the violation code to the violation properties (#3857)
* Validator: Allow customizing the validation error status code (#3808)
* Validator: Autoconfiguration of validation groups generator via `ApiPlatform\Core\Validator\ValidationGroupsGeneratorInterface`
* Validator: Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
* Validator: Property validation through OpenAPI (#33329)
* Validator:Query filters and parameters are validated (#1723)
* Validator: Query filters and parameters are validated (#1723)
* `ExceptionInterface` now extends `\Throwable` (#3217)

## 2.5.9
Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/ApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlQueryResolverPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -51,5 +52,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new GraphQlMutationResolverPass());
$container->addCompilerPass(new DeprecateMercurePublisherPass());
$container->addCompilerPass(new MetadataAwareNameConverterPass());
$container->addCompilerPass(new TestClientPass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
use Doctrine\Common\Annotations\Annotation;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use Ramsey\Uuid\Uuid;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\DirectoryResource;
Expand All @@ -53,7 +52,6 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Uid\AbstractUid;
Expand Down Expand Up @@ -138,14 +136,6 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('api_platform.subresource_data_provider');
$container->registerForAutoconfiguration(FilterInterface::class)
->addTag('api_platform.filter');

if ($container->hasParameter('test.client.parameters')) {
$loader->load('test.xml');

if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
$container->removeDefinition('test.api_platform.client');
}
}
}

private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpClient\HttpClientTrait;

final class TestClientPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (
!class_exists(AbstractBrowser::class) ||
!trait_exists(HttpClientTrait::class) ||
!$container->hasParameter('test.client.parameters')
) {
return;
}

$container->setDefinition(
'test.api_platform.client',
(new Definition(Client::class, [new Reference('test.client')]))
->setShared(false)
->setPublic(true)
);
}
}
13 changes: 0 additions & 13 deletions src/Bridge/Symfony/Bundle/Resources/config/test.xml

This file was deleted.

2 changes: 2 additions & 0 deletions tests/Bridge/Symfony/Bundle/ApiPlatformBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlQueryResolverPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand All @@ -48,6 +49,7 @@ public function testBuild()
$containerProphecy->addCompilerPass(Argument::type(GraphQlMutationResolverPass::class))->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(DeprecateMercurePublisherPass::class))->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(MetadataAwareNameConverterPass::class))->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(TestClientPass::class))->shouldBeCalled();

$bundle = new ApiPlatformBundle();
$bundle->build($containerProphecy->reveal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
foreach ($parameters as $key => $value) {
$containerBuilderProphecy->setParameter($key, $value)->shouldBeCalled();
}
$containerBuilderProphecy->hasParameter('test.client.parameters')->wilLReturn(true);

foreach (['yaml', 'xml'] as $format) {
$definitionProphecy = $this->prophesize(Definition::class);
Expand Down Expand Up @@ -1270,7 +1269,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.swagger.listener.ui',
'api_platform.validator',
'api_platform.validator.query_parameter_validator',
'test.api_platform.client',
];

if (\in_array('odm', $doctrineIntegrationsToLoad, true)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler;

use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

final class TestClientPassTest extends TestCase
{
private $containerBuilderProphecy;
private $testClientPass;

protected function setUp(): void
{
$this->containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
$this->testClientPass = new TestClientPass();
}

public function testConstruct(): void
{
self::assertInstanceOf(CompilerPassInterface::class, $this->testClientPass);
}

public function testProcessWithoutTestClientParameters(): void
{
$this->containerBuilderProphecy->hasParameter('test.client.parameters')->willReturn(false)->shouldBeCalledOnce();
$this->containerBuilderProphecy->setDefinition('test.api_platform.client', Argument::type(Definition::class))->shouldNotBeCalled();

$this->testClientPass->process($this->containerBuilderProphecy->reveal());
}

public function testProcess(): void
{
$this->containerBuilderProphecy->hasParameter('test.client.parameters')->willReturn(true)->shouldBeCalledOnce();
$this->containerBuilderProphecy
->setDefinition(
'test.api_platform.client',
Argument::allOf(
Argument::type(Definition::class),
Argument::that(function (Definition $testClientDefinition) {
return
Client::class === $testClientDefinition->getClass() &&
!$testClientDefinition->isShared() &&
$testClientDefinition->isPublic();
})
)
)
->shouldBeCalledOnce();

$this->testClientPass->process($this->containerBuilderProphecy->reveal());
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public function __construct(string $environment, bool $debug)
public function registerBundles(): array
{
$bundles = [
new FrameworkBundle(),
new ApiPlatformBundle(),
new TwigBundle(),
new DoctrineBundle(),
new MercureBundle(),
new ApiPlatformBundle(),
new SecurityBundle(),
new WebProfilerBundle(),
new FriendsOfBehatSymfonyExtensionBundle(),
new FrameworkBundle(),
];

if (class_exists(DoctrineMongoDBBundle::class)) {
Expand Down