Move app nesting to a test state helper

This commit is contained in:
Damian Mooyman 2017-06-13 22:05:57 +12:00
parent 603704165c
commit b220534f06
4 changed files with 111 additions and 48 deletions

View File

@ -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

View File

@ -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()

View File

@ -0,0 +1,93 @@
<?php
namespace SilverStripe\Dev;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\TestKernel;
/**
* Handles nesting of kernel before / after tests
*/
class KernelTestState implements TestState
{
/**
* Stack of kernels
*
* @var TestKernel[]
*/
protected $kernels = [];
/**
* Get active Kernel instance
*
* @return TestKernel
*/
protected function kernel()
{
return end($this->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();
}
}

View File

@ -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) {