From 7961a29a57bf4f7fe4885ebf32f4e463268565c4 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 27 Dec 2018 23:36:09 +0100 Subject: [PATCH] improve error message when using test client without the BrowserKit component --- .../DependencyInjection/FrameworkExtension.php | 5 +++++ src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 9ec02f07228a..a7b7beaa7281 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -18,6 +18,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader; use Symfony\Bundle\FullStack; +use Symfony\Component\BrowserKit\Client; use Symfony\Component\Cache\Adapter\AbstractAdapter; use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; @@ -223,6 +224,10 @@ public function load(array $configs, ContainerBuilder $container) $container->getDefinition('test.client.history')->setPrivate(true); $container->getDefinition('test.client.cookiejar')->setPrivate(true); $container->getDefinition('test.session.listener')->setPrivate(true); + + if (!class_exists(Client::class)) { + $container->removeDefinition('test.client'); + } } if ($this->isConfigEnabled($container, $config['session'])) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php index b5aaa2604b24..7302dc78aa37 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Test; use Symfony\Bundle\FrameworkBundle\Client; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; /** * WebTestCase is the base class for functional tests. @@ -32,7 +33,12 @@ protected static function createClient(array $options = array(), array $server = { $kernel = static::bootKernel($options); - $client = $kernel->getContainer()->get('test.client'); + try { + $client = $kernel->getContainer()->get('test.client'); + } catch (ServiceNotFoundException $e) { + throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit".'); + } + $client->setServerParameters($server); return $client;