Restore kernel stack to fix multi-level nesting

This commit is contained in:
Damian Mooyman 2017-06-13 21:46:04 +12:00
parent 2f6336a15b
commit 603704165c

View File

@ -17,6 +17,8 @@ use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\HTTPApplication; use SilverStripe\Core\HTTPApplication;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\TestKernel; use SilverStripe\Core\TestKernel;
use SilverStripe\i18n\i18n; use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataExtension;
@ -128,13 +130,21 @@ class SapphireTest extends PHPUnit_Framework_TestCase
protected $backupGlobals = false; protected $backupGlobals = false;
/** /**
* Test application kernel. * Test application kernel stace.
* Note: This is always the root kernel. Use Injector to get the current kernel
* if nested.
* *
* @var TestKernel * @var TestKernel[]
*/ */
protected static $kernel = null; protected static $kernels = [];
/**
* Get active Kernel instance
*
* @return TestKernel
*/
protected static function kernel()
{
return end(static::$kernels);
}
/** /**
* State management container for SapphireTest * State management container for SapphireTest
@ -204,10 +214,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
// Reset state // Reset state
self::$kernel->reset(); static::kernel()->reset();
// Nest // Nest
self::$kernel->nest(); static::$kernels[] = static::kernel()->nest();
// Call state helpers // Call state helpers
static::$state->setUp($this); static::$state->setUp($this);
@ -297,10 +307,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase
static::start(); static::start();
// Reset kernel // Reset kernel
static::$kernel->reset(); static::kernel()->reset();
// Nest kernel // Nest kernel
static::$kernel->nest(); static::$kernels[] = static::kernel()->nest();
// Call state helpers // Call state helpers
static::$state->setUpOnce(static::class); static::$state->setUpOnce(static::class);
@ -331,10 +341,11 @@ class SapphireTest extends PHPUnit_Framework_TestCase
static::$state->tearDownOnce(static::class); static::$state->tearDownOnce(static::class);
// Unnest // Unnest
static::$kernel->activate(); array_pop(static::$kernels);
static::kernel()->activate();
// Reset PHP state // Reset PHP state
static::$kernel->reset(); static::kernel()->reset();
// Reset DB schema // Reset DB schema
static::resetDBSchema(); static::resetDBSchema();
@ -449,7 +460,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
*/ */
protected function getCurrentAbsolutePath() protected function getCurrentAbsolutePath()
{ {
$filename = static::$kernel->getClassLoader()->getItemPath(static::class); $filename = static::kernel()->getClassLoader()->getItemPath(static::class);
if (!$filename) { if (!$filename) {
throw new LogicException("getItemPath returned null for " . static::class); throw new LogicException("getItemPath returned null for " . static::class);
} }
@ -496,10 +507,11 @@ class SapphireTest extends PHPUnit_Framework_TestCase
static::$state->tearDown($this); static::$state->tearDown($this);
// Unnest // Unnest
self::$kernel->activate(); array_pop(static::$kernels);
static::kernel()->activate();
// Reset state // Reset state
self::$kernel->reset(); static::kernel()->reset();
} }
public static function assertContains( public static function assertContains(
@ -911,7 +923,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
return; return;
} }
// Health check // Health check
if (Injector::inst() || static::$kernel) { if (InjectorLoader::inst()->countManifests() || static::kernel()) {
throw new LogicException("SapphireTest::start() cannot be called within another application"); throw new LogicException("SapphireTest::start() cannot be called within another application");
} }
static::set_is_running_test(true); static::set_is_running_test(true);
@ -922,8 +934,8 @@ class SapphireTest extends PHPUnit_Framework_TestCase
$request->setSession($session); $request->setSession($session);
// Test application // Test application
static::$kernel = new TestKernel(); static::$kernels[] = new TestKernel();
$app = new HTTPApplication(static::$kernel); $app = new HTTPApplication(static::kernel());
// Custom application // Custom application
$app->execute(function () use ($request) { $app->execute(function () use ($request) {