Skip to content

Commit 32a2f62

Browse files
authored
Merge pull request #3027 from teohhanhui/fix/create-client-error-when-browser-kit-not-installed
Fix ApiTestCase::createClient when the BrowserKit component is not installed
2 parents d933888 + 6ac50c9 commit 32a2f62

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"ramsey/uuid-doctrine": "^1.4",
6060
"sebastian/object-enumerator": "^3.0.3",
6161
"symfony/asset": "^3.4 || ^4.0",
62+
"symfony/browser-kit": "^4.3",
6263
"symfony/cache": "^3.4 || ^4.0",
6364
"symfony/config": "^3.4 || ^4.0",
6465
"symfony/console": "^3.4 || ^4.0",

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
4747
use Symfony\Component\DependencyInjection\Reference;
4848
use Symfony\Component\Finder\Finder;
49+
use Symfony\Component\HttpClient\HttpClientTrait;
4950
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
5051
use Symfony\Component\Validator\Validator\ValidatorInterface;
5152
use Symfony\Component\Yaml\Yaml;
@@ -128,8 +129,12 @@ public function load(array $configs, ContainerBuilder $container): void
128129
$container->registerForAutoconfiguration(FilterInterface::class)
129130
->addTag('api_platform.filter');
130131

131-
if ($container->hasParameter('test.client.parameters') && class_exists(AbstractBrowser::class) && trait_exists('Symfony\Component\HttpClient\HttpClientTrait')) {
132+
if ($container->hasParameter('test.client.parameters')) {
132133
$loader->load('test.xml');
134+
135+
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
136+
$container->removeDefinition('test.api_platform.client');
137+
}
133138
}
134139
}
135140

src/Bridge/Symfony/Bundle/Test/ApiTestCase.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test;
1515

16-
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1716
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Symfony\Component\BrowserKit\AbstractBrowser;
1818
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19+
use Symfony\Component\HttpClient\HttpClientTrait;
1920

2021
/**
2122
* Base class for functional API tests.
@@ -54,10 +55,11 @@ protected static function createClient(array $kernelOptions = [], array $default
5455
*/
5556
$client = $kernel->getContainer()->get('test.api_platform.client');
5657
} catch (ServiceNotFoundException $e) {
57-
if (class_exists(KernelBrowser::class) && trait_exists('Symfony\Component\HttpClient\HttpClientTrait')) {
58-
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
58+
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
59+
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit and HttpClient components are not available. Try running "composer require --dev symfony/browser-kit symfony/http-client".');
5960
}
60-
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit and HttpClient components are not available. Try running "composer require --dev symfony/browser-kit symfony/http-client".');
61+
62+
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
6163
}
6264

6365
$client->setDefaultOptions($defaultOptions);

0 commit comments

Comments
 (0)