/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

https://github.com/kitingChris/symfony · PHP · 182 lines · 90 code · 25 blank · 67 comment · 13 complexity · 6a156670ce1e57251e4ebdc4e156a295 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 Symfony\Component\Finder\Finder;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. /**
  14. * KernelTestCase is the base class for tests needing a Kernel.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. abstract class KernelTestCase extends \PHPUnit_Framework_TestCase
  19. {
  20. protected static $class;
  21. /**
  22. * @var KernelInterface
  23. */
  24. protected static $kernel;
  25. /**
  26. * Finds the directory where the phpunit.xml(.dist) is stored.
  27. *
  28. * If you run tests with the PHPUnit CLI tool, everything will work as expected.
  29. * If not, override this method in your test classes.
  30. *
  31. * @return string The directory where phpunit.xml(.dist) is stored
  32. *
  33. * @throws \RuntimeException
  34. */
  35. protected static function getPhpUnitXmlDir()
  36. {
  37. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  38. throw new \RuntimeException('You must override the KernelTestCase::createKernel() method.');
  39. }
  40. $dir = static::getPhpUnitCliConfigArgument();
  41. if ($dir === null &&
  42. (is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
  43. is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
  44. $dir = getcwd();
  45. }
  46. // Can't continue
  47. if ($dir === null) {
  48. throw new \RuntimeException('Unable to guess the Kernel directory.');
  49. }
  50. if (!is_dir($dir)) {
  51. $dir = dirname($dir);
  52. }
  53. return $dir;
  54. }
  55. /**
  56. * Finds the value of the CLI configuration option.
  57. *
  58. * PHPUnit will use the last configuration argument on the command line, so this only returns
  59. * the last configuration argument.
  60. *
  61. * @return string The value of the PHPUnit CLI configuration option
  62. */
  63. private static function getPhpUnitCliConfigArgument()
  64. {
  65. $dir = null;
  66. $reversedArgs = array_reverse($_SERVER['argv']);
  67. foreach ($reversedArgs as $argIndex => $testArg) {
  68. if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') {
  69. $dir = realpath($reversedArgs[$argIndex - 1]);
  70. break;
  71. } elseif (strpos($testArg, '--configuration=') === 0) {
  72. $argPath = substr($testArg, strlen('--configuration='));
  73. $dir = realpath($argPath);
  74. break;
  75. }
  76. }
  77. return $dir;
  78. }
  79. /**
  80. * Attempts to guess the Kernel location.
  81. *
  82. * When the Kernel is located, the file is required.
  83. *
  84. * @return string The Kernel class name
  85. *
  86. * @throws \RuntimeException
  87. */
  88. protected static function getKernelClass()
  89. {
  90. $dir = $phpUnitDir = static::getPhpUnitXmlDir();
  91. if (isset($_SERVER['KERNEL_DIR'])) {
  92. $dir = $_SERVER['KERNEL_DIR'];
  93. if (!is_dir($dir) && is_dir("$phpUnitDir/$dir")) {
  94. $dir = "$phpUnitDir/$dir";
  95. }
  96. }
  97. $finder = new Finder();
  98. $finder->name('*Kernel.php')->depth(0)->in($dir);
  99. $results = iterator_to_array($finder);
  100. if (!count($results)) {
  101. throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the KernelTestCase::createKernel() method.');
  102. }
  103. $file = current($results);
  104. $class = $file->getBasename('.php');
  105. require_once $file;
  106. return $class;
  107. }
  108. /**
  109. * Boots the Kernel for this test.
  110. *
  111. * @param array $options
  112. */
  113. protected static function bootKernel(array $options = array())
  114. {
  115. static::ensureKernelShutdown();
  116. static::$kernel = static::createKernel($options);
  117. static::$kernel->boot();
  118. }
  119. /**
  120. * Creates a Kernel.
  121. *
  122. * Available options:
  123. *
  124. * * environment
  125. * * debug
  126. *
  127. * @param array $options An array of options
  128. *
  129. * @return KernelInterface A KernelInterface instance
  130. */
  131. protected static function createKernel(array $options = array())
  132. {
  133. if (null === static::$class) {
  134. static::$class = static::getKernelClass();
  135. }
  136. return new static::$class(
  137. isset($options['environment']) ? $options['environment'] : 'test',
  138. isset($options['debug']) ? $options['debug'] : true
  139. );
  140. }
  141. /**
  142. * Shuts the kernel down if it was used in the test.
  143. */
  144. protected static function ensureKernelShutdown()
  145. {
  146. if (null !== static::$kernel) {
  147. static::$kernel->shutdown();
  148. }
  149. }
  150. /**
  151. * Clean up Kernel usage in this test.
  152. */
  153. protected function tearDown()
  154. {
  155. static::ensureKernelShutdown();
  156. }
  157. }