bug #29704 [FrameworkBundle] improve errors in tests missing the BrowserKit component (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] improve errors in tests missing the BrowserKit component

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29696
| License       | MIT
| Doc PR        |

Commits
-------

7961a29a57 improve error message when using test client without the BrowserKit component
This commit is contained in:
Fabien Potencier 2019-01-01 18:32:41 +01:00
commit 9fc71e0f64
2 changed files with 12 additions and 1 deletions

View File

@ -18,6 +18,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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 @@ class FrameworkExtension extends Extension
$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'])) {

View File

@ -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 @@ abstract class WebTestCase extends KernelTestCase
{
$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;