KernelTestCase.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Contracts\Service\ResetInterface;
  15. /**
  16. * KernelTestCase is the base class for tests needing a Kernel.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. abstract class KernelTestCase extends TestCase
  21. {
  22. use MailerAssertionsTrait;
  23. protected static $class;
  24. /**
  25. * @var KernelInterface
  26. */
  27. protected static $kernel;
  28. /**
  29. * @var ContainerInterface
  30. */
  31. protected static $container;
  32. protected static $booted = false;
  33. private static $kernelContainer;
  34. protected function tearDown(): void
  35. {
  36. static::ensureKernelShutdown();
  37. static::$kernel = null;
  38. static::$booted = false;
  39. }
  40. /**
  41. * @return string The Kernel class name
  42. *
  43. * @throws \RuntimeException
  44. * @throws \LogicException
  45. */
  46. protected static function getKernelClass()
  47. {
  48. if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) {
  49. throw new \LogicException(sprintf('You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the "%1$s::createKernel()" or "%1$s::getKernelClass()" method.', static::class));
  50. }
  51. if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'])) {
  52. throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the "%s::createKernel()" method.', $class, static::class));
  53. }
  54. return $class;
  55. }
  56. /**
  57. * Boots the Kernel for this test.
  58. *
  59. * @return KernelInterface A KernelInterface instance
  60. */
  61. protected static function bootKernel(array $options = [])
  62. {
  63. static::ensureKernelShutdown();
  64. static::$kernel = static::createKernel($options);
  65. static::$kernel->boot();
  66. static::$booted = true;
  67. self::$kernelContainer = $container = static::$kernel->getContainer();
  68. static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
  69. return static::$kernel;
  70. }
  71. /**
  72. * Creates a Kernel.
  73. *
  74. * Available options:
  75. *
  76. * * environment
  77. * * debug
  78. *
  79. * @return KernelInterface A KernelInterface instance
  80. */
  81. protected static function createKernel(array $options = [])
  82. {
  83. if (null === static::$class) {
  84. static::$class = static::getKernelClass();
  85. }
  86. if (isset($options['environment'])) {
  87. $env = $options['environment'];
  88. } elseif (isset($_ENV['APP_ENV'])) {
  89. $env = $_ENV['APP_ENV'];
  90. } elseif (isset($_SERVER['APP_ENV'])) {
  91. $env = $_SERVER['APP_ENV'];
  92. } else {
  93. $env = 'test';
  94. }
  95. if (isset($options['debug'])) {
  96. $debug = $options['debug'];
  97. } elseif (isset($_ENV['APP_DEBUG'])) {
  98. $debug = $_ENV['APP_DEBUG'];
  99. } elseif (isset($_SERVER['APP_DEBUG'])) {
  100. $debug = $_SERVER['APP_DEBUG'];
  101. } else {
  102. $debug = true;
  103. }
  104. return new static::$class($env, $debug);
  105. }
  106. /**
  107. * Shuts the kernel down if it was used in the test - called by the tearDown method by default.
  108. */
  109. protected static function ensureKernelShutdown()
  110. {
  111. if (null !== static::$kernel) {
  112. static::$kernel->shutdown();
  113. static::$booted = false;
  114. }
  115. if (self::$kernelContainer instanceof ResetInterface) {
  116. self::$kernelContainer->reset();
  117. }
  118. static::$container = self::$kernelContainer = null;
  119. }
  120. }