|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Test; |
| 13 | + |
| 14 | +use Symfony\Component\Finder\Finder; |
| 15 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * KernelTestCase is the base class for tests needing a Kernel. |
| 19 | + * |
| 20 | + * @author Fabien Potencier <fabien@symfony.com> |
| 21 | + */ |
| 22 | +abstract class KernelTestCase extends \PHPUnit_Framework_TestCase |
<
10000
td data-grid-cell-id="diff-39d252b050ba54aafb486057978a8f85439c2bbb21168c7e4f3d70d796fc1fbe-empty-23-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
23 | +{ |
| 24 | + protected static $class; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var KernelInterface |
| 28 | + */ |
| 29 | + protected static $kernel; |
| 30 | + |
| 31 | + /** |
| 32 | + * Finds the directory where the phpunit.xml(.dist) is stored. |
| 33 | + * |
| 34 | + * If you run tests with the PHPUnit CLI tool, everything will work as |
| 35 | + * expected. If not, override this method in your test classes. |
| 36 | + * |
| 37 | + * @return string The directory where phpunit.xml(.dist) is stored |
| 38 | + * |
| 39 | + * @throws \RuntimeException |
| 40 | + */ |
| 41 | + protected static function getPhpUnitXmlDir() |
| 42 | + { |
| 43 | + if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) { |
| 44 | + throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); |
| 45 | + } |
| 46 | + |
| 47 | + $dir = static::getPhpUnitCliConfigArgument(); |
| 48 | + if ($dir === null && |
| 49 | + (is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') || |
| 50 | + is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) { |
| 51 | + $dir = getcwd(); |
| 52 | + } |
| 53 | + |
| 54 | + // Can't continue |
| 55 | + if ($dir === null) { |
| 56 | + throw new \RuntimeException('Unable to guess the Kernel directory.'); |
| 57 | + } |
| 58 | + |
| 59 | + if (!is_dir($dir)) { |
| 60 | + $dir = dirname($dir); |
| 61 | + } |
| 62 | + |
| 63 | + return $dir; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Finds the value of the CLI configuration option. |
| 68 | + * |
| 69 | + * PHPUnit will use the last configuration argument on the command line, so |
| 70 | + * this only returns the last configuration argument. |
| 71 | + * |
| 72 | + * @return string The value of the PHPUnit cli configuration option |
| 73 | + */ |
| 74 | + private static function getPhpUnitCliConfigArgument() |
| 75 | + { |
| 76 | + $dir = null; |
| 77 | + $reversedArgs = array_reverse($_SERVER['argv']); |
| 78 | + foreach ($reversedArgs as $argIndex => $testArg) { |
| 79 | + if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') { |
| 80 | + $dir = realpath($reversedArgs[$argIndex - 1]); |
| 81 | + break; |
| 82 | + } elseif (strpos($testArg, '--configuration=') === 0) { |
| 83 | + $argPath = substr($testArg, strlen('--configuration=')); |
| 84 | + $dir = realpath($argPath); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $dir; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Attempts to guess the Kernel location. |
| 94 | + * |
| 95 | + * When the Kernel is located, the file is required. |
| 96 | + * |
| 97 | + * @return string The Kernel class name |
| 98 | + * |
| 99 | + * @throws \RuntimeException |
| 100 | + */ |
| 101 | + protected static function getKernelClass() |
| 102 | + { |
| 103 | + $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir(); |
| 104 | + |
| 105 | + $finder = new Finder(); |
| 106 | + $finder->name('*Kernel.php')->depth(0)->in($dir); |
| 107 | + $results = iterator_to_array($finder); |
| 108 | + if (!count($results)) { |
| 109 | + 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 WebTestCase::createKernel() method.'); |
| 110 | + } |
| 111 | + |
| 112 | + $file = current($results); |
| 113 | + $class = $file->getBasename('.php'); |
| 114 | + |
| 115 | + require_once $file; |
| 116 | + |
| 117 | + return $class; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Boots the Kernel for this test. |
| 122 | + * |
| 123 | + * @param array $options |
| 124 | + */ |
| 125 | + protected static function bootKernel(array $options = array()) |
| 126 | + { |
| 127 | + static::ensureKernelShutdown(); |
| 128 | + |
| 129 | + static::$kernel = static::createKernel($options); |
| 130 | + static::$kernel->boot(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Creates a Kernel. |
| 135 | + * |
| 136 | + * Available options: |
| 137 | + * |
| 138 | + * * environment |
| 139 | + * * debug |
| 140 | + * |
| 141 | + * @param array $options An array of options |
| 142 | + * |
| 143 | + * @return KernelInterface A KernelInterface instance |
| 144 | + */ |
| 145 | + protected static function createKernel(array $options = array()) |
| 146 | + { |
| 147 | + if (null === static::$class) { |
| 148 | + static::$class = static::getKernelClass(); |
| 149 | + } |
| 150 | + |
| 151 | + return new static::$class( |
| 152 | + isset($options['environment']) ? $options['environment'] : 'test', |
| 153 | + isset($options['debug']) ? $options['debug'] : true |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Shutdown the Kernel. |
| 159 | + */ |
| 160 | + protected static function ensureKernelShutdown() |
| 161 | + { |
| 162 | + if (null !== static::$kernel) { |
| 163 | + static::$kernel->shutdown(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Clean up Kernel usage in this test. |
| 169 | + */ |
| 170 | + protected function tearDown() |
| 171 | + { |
| 172 | + static::ensureKernelShutdown(); |
| 173 | + } |
| 174 | +} |
0 commit comments