Commit f23a677b by Benjamin Wöster

mod: incorporate suggestions

- rename requireApp() to mockApplication() - always destroy app on tearDown() - eliminates need for constant YII_DESTROY_APP_ON_TEARDOWN - mockApplication() becomes a lot easier. Destroying app on each tearDown means creating it on every call is fine. No more checking if it already exists and if it has been created from the same config. - \yii::$app should have been \Yii::$app
parent c30ee60e
...@@ -13,12 +13,7 @@ class TestCase extends \yii\test\TestCase ...@@ -13,12 +13,7 @@ class TestCase extends \yii\test\TestCase
protected function tearDown() protected function tearDown()
{ {
parent::tearDown(); parent::tearDown();
// If defined and set to true, destroy the app after each test. $this->destroyApp();
// This will cause tests to fail, that rely on an existing app, but don't
// call requireApp() in their setUp().
if (defined('YII_DESTROY_APP_ON_TEARDOWN') && YII_DESTROY_APP_ON_TEARDOWN === true) {
$this->destroyApp();
}
} }
public function getParam($name,$default=null) public function getParam($name,$default=null)
...@@ -29,28 +24,19 @@ class TestCase extends \yii\test\TestCase ...@@ -29,28 +24,19 @@ class TestCase extends \yii\test\TestCase
return isset(self::$params[$name]) ? self::$params[$name] : $default; return isset(self::$params[$name]) ? self::$params[$name] : $default;
} }
protected function requireApp($requiredConfig=array()) protected function mockApplication($requiredConfig=array())
{ {
static $usedConfig = array();
static $defaultConfig = array( static $defaultConfig = array(
'id' => 'testapp', 'id' => 'testapp',
'basePath' => __DIR__, 'basePath' => __DIR__,
); );
$newConfig = array_merge( $defaultConfig, $requiredConfig );
$appClass = $this->getParam( 'appClass', '\yii\web\Application' ); $appClass = $this->getParam( 'appClass', '\yii\web\Application' );
new $appClass(array_merge($defaultConfig,$requiredConfig));
if (!(\yii::$app instanceof $appClass)) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
} elseif ($newConfig !== $usedConfig) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
}
} }
protected function destroyApp() protected function destroyApp()
{ {
\yii::$app = null; \Yii::$app = null;
} }
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
define('YII_ENABLE_ERROR_HANDLER', false); define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true); define('YII_DEBUG', true);
define('YII_DESTROY_APP_ON_TEARDOWN', false);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__; $_SERVER['SCRIPT_FILENAME'] = __FILE__;
......
...@@ -16,7 +16,7 @@ abstract class CacheTest extends TestCase ...@@ -16,7 +16,7 @@ abstract class CacheTest extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
$this->requireApp(); $this->mockApplication();
} }
public function testSet() public function testSet()
......
...@@ -10,7 +10,7 @@ class HtmlTest extends TestCase ...@@ -10,7 +10,7 @@ class HtmlTest extends TestCase
{ {
public function setUp() public function setUp()
{ {
$this->requireApp(array( $this->mockApplication(array(
'components' => array( 'components' => array(
'request' => array( 'request' => array(
'class' => 'yii\web\Request', 'class' => 'yii\web\Request',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment