Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e9373b
commit ddf2a25
Showing
4 changed files
with
195 additions
and
216 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/Symfony/Bundle/FrameworkBundle/Test/BaseKernelTestCase.php
This file contains 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,130 @@ | ||
<?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 PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* KernelTestCase is the base class for tests needing a Kernel. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* | ||
* @internal | ||
*/ | ||
abstract class BaseKernelTestCase extends TestCase | ||
{ | ||
protected static $class; | ||
|
||
/** | ||
* @var KernelInterface | ||
*/ | ||
protected static $kernel; | ||
|
||
/** | ||
* @var ContainerInterface | ||
*/ | ||
protected static $container; | ||
|
||
/** | ||
* @return string The Kernel class name | ||
* | ||
* @throws \RuntimeException | ||
* @throws \LogicException | ||
*/ | ||
protected static function getKernelClass() | ||
{ | ||
if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) { | ||
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)); | ||
} | ||
|
||
if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'])) { | ||
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)); | ||
} | ||
|
||
return $class; | ||
} | ||
|
||
/** | ||
* Boots the Kernel for this test. | ||
* | ||
* @return KernelInterface A KernelInterface instance | ||
*/ | ||
protected static function bootKernel(array $options = []) | ||
{ | ||
static::ensureKernelShutdown(); | ||
|
||
static::$kernel = static::createKernel($options); | ||
static::$kernel->boot(); | ||
|
||
$container = static::$kernel->getContainer(); | ||
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container; | ||
|
||
return static::$kernel; | ||
} | ||
|
||
/** | ||
* Creates a Kernel. | ||
* | ||
* Available options: | ||
* | ||
* * environment | ||
* * debug | ||
* | ||
* @return KernelInterface A KernelInterface instance | ||
*/ | ||
protected static function createKernel(array $options = []) | ||
{ | ||
if (null === static::$class) { | ||
static::$class = static::getKernelClass(); | ||
} | ||
|
||
if (isset($options['environment'])) { | ||
$env = $options['environment']; | ||
} elseif (isset($_ENV['APP_ENV'])) { | ||
$env = $_ENV['APP_ENV']; | ||
} elseif (isset($_SERVER['APP_ENV'])) { | ||
$env = $_SERVER['APP_ENV']; | ||
} else { | ||
$env = 'test'; | ||
} | ||
|
||
if (isset($options['debug'])) { | ||
$debug = $options['debug']; | ||
} elseif (isset($_ENV['APP_DEBUG'])) { | ||
$debug = $_ENV['APP_DEBUG']; | ||
} elseif (isset($_SERVER['APP_DEBUG'])) { | ||
$debug = $_SERVER['APP_DEBUG']; | ||
} else { | ||
$debug = true; | ||
} | ||
|
||
return new static::$class($env, $debug); | ||
} | ||
|
||
/** | ||
* Shuts the kernel down if it was used in the test. | ||
*/ | ||
protected static function ensureKernelShutdown() | ||
{ | ||
if (null !== static::$kernel) { | ||
$container = static::$kernel->getContainer(); | ||
static::$kernel->shutdown(); | ||
if ($container instanceof ResetInterface) { | ||
$container->reset(); | ||
} | ||
} | ||
static::$container = null; | ||
} | ||
} |
This file contains 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
30 changes: 30 additions & 0 deletions
30
src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCaseNew.php
This file contains 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,30 @@ | ||
<?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; | ||
|
||
/** | ||
* KernelTestCaseNew is the base class for tests needing a Kernel >= PHPUNIT 8. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* | ||
* @internal | ||
*/ | ||
abstract class KernelTestCaseNew extends BaseKernelTestCase | ||
{ | ||
/** | ||
* Clean up Kernel usage in this test. | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
static::ensureKernelShutdown(); | ||
} | ||
} |
Oops, something went wrong.