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
protected function tearDown()
{
parent::tearDown();
// If defined and set to true, destroy the app after each test.
// 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();
}
$this->destroyApp();
}
public function getParam($name,$default=null)
......@@ -29,28 +24,19 @@ class TestCase extends \yii\test\TestCase
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(
'id' => 'testapp',
'basePath' => __DIR__,
);
$newConfig = array_merge( $defaultConfig, $requiredConfig );
$appClass = $this->getParam( 'appClass', '\yii\web\Application' );
if (!(\yii::$app instanceof $appClass)) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
} elseif ($newConfig !== $usedConfig) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
}
new $appClass(array_merge($defaultConfig,$requiredConfig));
}
protected function destroyApp()
{
\yii::$app = null;
\Yii::$app = null;
}
}
......@@ -2,7 +2,6 @@
define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);
define('YII_DESTROY_APP_ON_TEARDOWN', false);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
......
......@@ -16,7 +16,7 @@ abstract class CacheTest extends TestCase
protected function setUp()
{
parent::setUp();
$this->requireApp();
$this->mockApplication();
}
public function testSet()
......
......@@ -10,7 +10,7 @@ class HtmlTest extends TestCase
{
public function setUp()
{
$this->requireApp(array(
$this->mockApplication(array(
'components' => array(
'request' => array(
'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