/vendor/symfony/framework-bundle/Test/KernelTestCase.php

https://bitbucket.org/strudlik/gitagent · PHP · 126 lines · 66 code · 17 blank · 43 comment · 10 complexity · 211c01a086276c6547d30f9e0d4f192a MD5 · raw file

  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\ResettableContainerInterface;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. /**
  15. * KernelTestCase is the base class for tests needing a Kernel.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class KernelTestCase extends TestCase
  20. {
  21. protected static $class;
  22. /**
  23. * @var KernelInterface
  24. */
  25. protected static $kernel;
  26. /**
  27. * @return string The Kernel class name
  28. *
  29. * @throws \RuntimeException
  30. * @throws \LogicException
  31. */
  32. protected static function getKernelClass()
  33. {
  34. if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) {
  35. 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));
  36. }
  37. if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'])) {
  38. 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));
  39. }
  40. return $class;
  41. }
  42. /**
  43. * Boots the Kernel for this test.
  44. *
  45. * @return KernelInterface A KernelInterface instance
  46. */
  47. protected static function bootKernel(array $options = array())
  48. {
  49. static::ensureKernelShutdown();
  50. static::$kernel = static::createKernel($options);
  51. static::$kernel->boot();
  52. return static::$kernel;
  53. }
  54. /**
  55. * Creates a Kernel.
  56. *
  57. * Available options:
  58. *
  59. * * environment
  60. * * debug
  61. *
  62. * @return KernelInterface A KernelInterface instance
  63. */
  64. protected static function createKernel(array $options = array())
  65. {
  66. if (null === static::$class) {
  67. static::$class = static::getKernelClass();
  68. }
  69. if (isset($options['environment'])) {
  70. $env = $options['environment'];
  71. } elseif (isset($_ENV['APP_ENV'])) {
  72. $env = $_ENV['APP_ENV'];
  73. } elseif (isset($_SERVER['APP_ENV'])) {
  74. $env = $_SERVER['APP_ENV'];
  75. } else {
  76. $env = 'test';
  77. }
  78. if (isset($options['debug'])) {
  79. $debug = $options['debug'];
  80. } elseif (isset($_ENV['APP_DEBUG'])) {
  81. $debug = $_ENV['APP_DEBUG'];
  82. } elseif (isset($_SERVER['APP_DEBUG'])) {
  83. $debug = $_SERVER['APP_DEBUG'];
  84. } else {
  85. $debug = true;
  86. }
  87. return new static::$class($env, $debug);
  88. }
  89. /**
  90. * Shuts the kernel down if it was used in the test.
  91. */
  92. protected static function ensureKernelShutdown()
  93. {
  94. if (null !== static::$kernel) {
  95. $container = static::$kernel->getContainer();
  96. static::$kernel->shutdown();
  97. if ($container instanceof ResettableContainerInterface) {
  98. $container->reset();
  99. }
  100. }
  101. }
  102. /**
  103. * Clean up Kernel usage in this test.
  104. */
  105. protected function tearDown()
  106. {
  107. static::ensureKernelShutdown();
  108. }
  109. }