-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Extract KernelTestCase from WebTestCase #9739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Test; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
/** | ||
* KernelTestCase is the base class for tests needing a Kernel. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
abstract class KernelTestCase extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected static $class; | ||
|
||
/** | ||
* @var KernelInterface | ||
*/ | ||
protected static $kernel; | ||
|
||
/** | ||
* Finds the directory where the phpunit.xml(.dist) is stored. | ||
* | ||
* If you run tests with the PHPUnit CLI tool, everything will work as | ||
* expected. If not, override this method in your test classes. | ||
* | ||
* @return string The directory where phpunit.xml(.dist) is stored | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
protected static function getPhpUnitXmlDir() | ||
{ | ||
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) { | ||
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); | ||
} | ||
|
||
$dir = static::getPhpUnitCliConfigArgument(); | ||
if ($dir === null && | ||
(is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') || | ||
is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) { | ||
$dir = getcwd(); | ||
} | ||
|
||
// Can't continue | ||
if ($dir === null) { | ||
throw new \RuntimeException('Unable to guess the Kernel directory.'); | ||
} | ||
|
||
if (!is_dir($dir)) { | ||
$dir = dirname($dir); | ||
} | ||
|
||
return $dir; | ||
} | ||
|
||
/** | ||
* Finds the value of the CLI configuration option. | ||
* | ||
* PHPUnit will use the last configuration argument on the command line, so | ||
* this only returns the last configuration argument. | ||
* | ||
* @return string The value of the PHPUnit cli configuration option | ||
*/ | ||
private static function getPhpUnitCliConfigArgument() | ||
{ | ||
$dir = null; | ||
$reversedArgs = array_reverse($_SERVER['argv']); | ||
foreach ($reversedArgs as $argIndex => $testArg) { | ||
if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') { | ||
$dir = realpath($reversedArgs[$argIndex - 1]); | ||
break; | ||
} elseif (strpos($testArg, '--configuration=') === 0) { | ||
$argPath = substr($testArg, strlen('--configuration=')); | ||
$dir = realpath($argPath); | ||
break; | ||
} | ||
} | ||
|
||
return $dir; | ||
} | ||
|
||
/** | ||
* Attempts to guess the Kernel location. | ||
* | ||
* When the Kernel is located, the file is required. | ||
* | ||
* @return string The Kernel class name | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
protected static function getKernelClass() | ||
{ | ||
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir(); | ||
|
||
$finder = new Finder(); | ||
$finder->name('*Kernel.php')->depth(0)->in($dir); | ||
$results = iterator_to_array($finder); | ||
if (!count($results)) { | ||
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.'); | ||
} | ||
|
||
$file = current($results); | ||
$class = $file->getBasename('.php'); | ||
|
||
require_once $file; | ||
|
||
return $class; | ||
} | ||
|
||
/** | ||
* Boots the Kernel for this test. | ||
* | ||
* @param array $options | ||
*/ | ||
protected static function bootKernel(array $options = array()) | ||
{ | ||
static::ensureKernelShutdown(); | ||
|
||
static::$kernel = static::createKernel($options); | ||
static::$kernel->boot(); | ||
} | ||
|
||
/** | ||
* Creates a Kernel. | ||
* | ||
* Available options: | ||
* | ||
* * environment | ||
* * debug | ||
* | ||
* @param array $options An array of options | ||
* | ||
* @return KernelInterface A KernelInterface instance | ||
*/ | ||
protected static function createKernel(array $options = array()) | ||
{ | ||
if (null === static::$class) { | ||
static::$class = static::getKernelClass(); | ||
} | ||
|
||
return new static::$class( | ||
isset($options['environment']) ? $options['environment'] : 'test', | ||
isset($options['debug']) ? $options['debug'] : true | ||
); | ||
} | ||
|
||
/** | ||
* Shutdown the Kernel. | ||
*/ | ||
protected static function ensureKernelShutdown() | ||
{ | ||
if (null !== static::$kernel) { | ||
static::$kernel->shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Clean up Kernel usage in this test. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
static::ensureKernelShutdown(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will never be reached when calling this class directly, as the kernel was set in the property in
createClient
. This needs to be refactored so that KernelTestCase is responsible for it, to allow shutting it down properly.