From f23a677bdf5831f8805e53eb48e888f03cdd30f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20W=C3=B6ster?= Date: Fri, 10 May 2013 02:01:08 +0200 Subject: [PATCH] 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 --- tests/unit/TestCase.php | 22 ++++------------------ tests/unit/bootstrap.php | 1 - tests/unit/framework/caching/CacheTest.php | 2 +- tests/unit/framework/helpers/HtmlTest.php | 2 +- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index 314e2b0..fbc0da7 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -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; } } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 55f8158..a0fdab4 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -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__; diff --git a/tests/unit/framework/caching/CacheTest.php b/tests/unit/framework/caching/CacheTest.php index f3eda7a..a17f126 100644 --- a/tests/unit/framework/caching/CacheTest.php +++ b/tests/unit/framework/caching/CacheTest.php @@ -16,7 +16,7 @@ abstract class CacheTest extends TestCase protected function setUp() { parent::setUp(); - $this->requireApp(); + $this->mockApplication(); } public function testSet() diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 9ba6b43..294cae0 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -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',