Skip to content

Commit

Permalink
Fix the registration of the "test.api_platform.client" service (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyerbaptiste committed Jan 4, 2021
1 parent d930c18 commit a8d39a3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
Expand Up @@ -13,26 +13,31 @@

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

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

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

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
$loader->load('test.xml');

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

Expand Up @@ -14,13 +14,12 @@
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler;

use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\TestPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpClient\HttpClientTrait;

final class TestPassTest extends TestCase
{
Expand Down Expand Up @@ -49,16 +48,20 @@ public function testProcessWithoutTestClientParameters(): void
public function testProcess(): void
{
$this->containerBuilderProphecy->hasParameter('test.client.parameters')->willReturn(true)->shouldBeCalledOnce();
$this->containerBuilderProphecy->setDefinition('test.api_platform.client', Argument::type(Definition::class))->shouldBeCalledOnce();
$this->containerBuilderProphecy->fileExists(Argument::type('string'))->shouldBeCalledOnce();
$this->containerBuilderProphecy->hasExtension(Argument::type('string'))->shouldBeCalledOnce();
$this->containerBuilderProphecy->removeBindings(Argument::type('string'))->shouldBeCalledOnce();

if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
$this->containerBuilderProphecy->removeDefinition('test.api_platform.client')->shouldBeCalledOnce();
} else {
$this->containerBuilderProphecy->removeDefinition('test.api_platform.client')->shouldNotBeCalled();
}
$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->testPass->process($this->containerBuilderProphecy->reveal());
}
Expand Down

0 comments on commit a8d39a3

Please sign in to comment.