diff --git a/_config/tests.yml b/_config/tests.yml index d4b53dda2..91f3a6a60 100644 --- a/_config/tests.yml +++ b/_config/tests.yml @@ -8,3 +8,12 @@ SilverStripe\Core\Injector\Injector: extensions: %$SilverStripe\Dev\ExtensionTestState flushable: %$SilverStripe\Dev\FlushableTestState requirements: %$SilverStripe\View\Dev\RequirementsTestState +--- +Name: kerneltest +Before: '*' +--- +SilverStripe\Core\Injector\Injector: + SilverStripe\Dev\SapphireTestState: + properties: + States: + kernel: %$SilverStripe\Dev\KernelTestState diff --git a/src/Core/TestKernel.php b/src/Core/TestKernel.php index 3fa8091ed..55e69c02d 100644 --- a/src/Core/TestKernel.php +++ b/src/Core/TestKernel.php @@ -16,11 +16,14 @@ class TestKernel extends AppKernel /** * Reset kernel between tests. * Note: this avoids resetting services (See TestState for service specific reset) + * + * @return $this */ public function reset() { $this->setEnvironment(self::DEV); $this->bootPHP(); + return $this; } protected function bootPHP() diff --git a/src/Dev/KernelTestState.php b/src/Dev/KernelTestState.php new file mode 100644 index 000000000..e8e7c2e45 --- /dev/null +++ b/src/Dev/KernelTestState.php @@ -0,0 +1,93 @@ +kernels); + } + + /** + * Called on setup + * + * @param SapphireTest $test + */ + public function setUp(SapphireTest $test) + { + $this->nest(); + } + + /** + * Called on tear down + * + * @param SapphireTest $test + */ + public function tearDown(SapphireTest $test) + { + $this->unnest(); + } + + /** + * Called once on setup + * + * @param string $class Class being setup + */ + public function setUpOnce($class) + { + // If first run, get initial kernel + if (empty($this->kernels)) { + $this->kernels[] = Injector::inst()->get(Kernel::class); + } + + $this->nest(); + } + + /** + * Called once on tear down + * + * @param string $class Class being torn down + */ + public function tearDownOnce($class) + { + $this->unnest(); + } + + /** + * Nest the current kernel + */ + protected function nest() + { + // Reset state + $this->kernel()->reset(); + $this->kernels[] = $this->kernel()->nest(); + } + + protected function unnest() + { + // Unnest and reset + array_pop($this->kernels); + $this->kernel()->activate(); + $this->kernel()->reset(); + } +} diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index eb431b228..6e5d1d6e8 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -18,7 +18,7 @@ use SilverStripe\Core\Config\Config; use SilverStripe\Core\HTTPApplication; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\InjectorLoader; -use SilverStripe\Core\Kernel; +use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\Core\TestKernel; use SilverStripe\i18n\i18n; use SilverStripe\ORM\DataExtension; @@ -129,23 +129,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase */ protected $backupGlobals = false; - /** - * Test application kernel stace. - * - * @var TestKernel[] - */ - protected static $kernels = []; - - /** - * Get active Kernel instance - * - * @return TestKernel - */ - protected static function kernel() - { - return end(static::$kernels); - } - /** * State management container for SapphireTest * @@ -213,12 +196,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase */ protected function setUp() { - // Reset state - static::kernel()->reset(); - - // Nest - static::$kernels[] = static::kernel()->nest(); - // Call state helpers static::$state->setUp($this); @@ -306,12 +283,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase // Start tests static::start(); - // Reset kernel - static::kernel()->reset(); - - // Nest kernel - static::$kernels[] = static::kernel()->nest(); - // Call state helpers static::$state->setUpOnce(static::class); @@ -340,13 +311,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase // Call state helpers static::$state->tearDownOnce(static::class); - // Unnest - array_pop(static::$kernels); - static::kernel()->activate(); - - // Reset PHP state - static::kernel()->reset(); - // Reset DB schema static::resetDBSchema(); } @@ -460,7 +424,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase */ protected function getCurrentAbsolutePath() { - $filename = static::kernel()->getClassLoader()->getItemPath(static::class); + $filename = ClassLoader::inst()->getItemPath(static::class); if (!$filename) { throw new LogicException("getItemPath returned null for " . static::class); } @@ -505,13 +469,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase // Call state helpers static::$state->tearDown($this); - - // Unnest - array_pop(static::$kernels); - static::kernel()->activate(); - - // Reset state - static::kernel()->reset(); } public static function assertContains( @@ -922,8 +879,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase if (static::is_running_test()) { return; } + // Health check - if (InjectorLoader::inst()->countManifests() || static::kernel()) { + if (InjectorLoader::inst()->countManifests()) { throw new LogicException("SapphireTest::start() cannot be called within another application"); } static::set_is_running_test(true); @@ -934,8 +892,8 @@ class SapphireTest extends PHPUnit_Framework_TestCase $request->setSession($session); // Test application - static::$kernels[] = new TestKernel(); - $app = new HTTPApplication(static::kernel()); + $kernel = new TestKernel(); + $app = new HTTPApplication($kernel); // Custom application $app->execute(function () use ($request) {