WebTestCase.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Test;
  11. use Symfony\Bundle\FrameworkBundle\KernelBrowser;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  13. /**
  14. * WebTestCase is the base class for functional tests.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. abstract class WebTestCase extends KernelTestCase
  19. {
  20. use WebTestAssertionsTrait;
  21. protected function tearDown(): void
  22. {
  23. parent::tearDown();
  24. self::getClient(null);
  25. }
  26. /**
  27. * Creates a KernelBrowser.
  28. *
  29. * @param array $options An array of options to pass to the createKernel method
  30. * @param array $server An array of server parameters
  31. *
  32. * @return KernelBrowser A KernelBrowser instance
  33. */
  34. protected static function createClient(array $options = [], array $server = [])
  35. {
  36. if (static::$booted) {
  37. throw new \LogicException(sprintf('Booting the kernel before calling "%s()" is not supported, the kernel should only be booted once.', __METHOD__));
  38. }
  39. $kernel = static::bootKernel($options);
  40. try {
  41. $client = $kernel->getContainer()->get('test.client');
  42. } catch (ServiceNotFoundException $e) {
  43. if (class_exists(KernelBrowser::class)) {
  44. throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
  45. }
  46. 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".');
  47. }
  48. $client->setServerParameters($server);
  49. return self::getClient($client);
  50. }
  51. }