From d4b30e26c269276a5dcd2f37054051725405e3e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20W=C3=B6ster?= Date: Thu, 9 May 2013 20:41:57 +0200 Subject: [PATCH] allow unit tests to requireApp() on setUp() --- tests/unit/TestCase.php | 19 +++++++++++++++++++ tests/unit/framework/caching/CacheTest.php | 6 ++++++ tests/unit/framework/caching/DbCacheTest.php | 2 ++ tests/unit/framework/helpers/HtmlTest.php | 13 +++---------- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index dccd3af..d098f30 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -13,4 +13,23 @@ class TestCase extends \yii\test\TestCase } return isset(self::$params[$name]) ? self::$params[$name] : null; } + + protected function requireApp($requiredConfig=array()) + { + static $usedConfig = array(); + static $defaultConfig = array( + 'id' => 'testapp', + 'basePath' => __DIR__, + ); + + $newConfig = array_merge( $defaultConfig, $requiredConfig ); + + if (!(\yii::$app instanceof \yii\web\Application)) { + new \yii\web\Application( $newConfig ); + $usedConfig = $newConfig; + } elseif ($newConfig !== $usedConfig) { + new \yii\web\Application( $newConfig ); + $usedConfig = $newConfig; + } + } } diff --git a/tests/unit/framework/caching/CacheTest.php b/tests/unit/framework/caching/CacheTest.php index f9db4f4..f3eda7a 100644 --- a/tests/unit/framework/caching/CacheTest.php +++ b/tests/unit/framework/caching/CacheTest.php @@ -13,6 +13,12 @@ abstract class CacheTest extends TestCase */ abstract protected function getCacheInstance(); + protected function setUp() + { + parent::setUp(); + $this->requireApp(); + } + public function testSet() { $cache = $this->getCacheInstance(); diff --git a/tests/unit/framework/caching/DbCacheTest.php b/tests/unit/framework/caching/DbCacheTest.php index a41667c..a68a278 100644 --- a/tests/unit/framework/caching/DbCacheTest.php +++ b/tests/unit/framework/caching/DbCacheTest.php @@ -17,6 +17,8 @@ class DbCacheTest extends CacheTest $this->markTestSkipped('pdo and pdo_mysql extensions are required.'); } + parent::setUp(); + $this->getConnection()->createCommand(" CREATE TABLE IF NOT EXISTS tbl_cache ( id char(128) NOT NULL, diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 6011594..9ba6b43 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -4,15 +4,13 @@ namespace yiiunit\framework\helpers; use Yii; use yii\helpers\Html; -use yii\web\Application; +use yiiunit\TestCase; -class HtmlTest extends \yii\test\TestCase +class HtmlTest extends TestCase { public function setUp() { - new Application(array( - 'id' => 'test', - 'basePath' => '@yiiunit/runtime', + $this->requireApp(array( 'components' => array( 'request' => array( 'class' => 'yii\web\Request', @@ -30,11 +28,6 @@ class HtmlTest extends \yii\test\TestCase $this->assertEquals($expected, $actual); } - public function tearDown() - { - Yii::$app = null; - } - public function testEncode() { $this->assertEquals("a<>&"'", Html::encode("a<>&\"'"));