diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index dccd3af..fbc0da7 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -6,11 +6,37 @@ class TestCase extends \yii\test\TestCase { public static $params; - public function getParam($name) + protected function setUp() { + parent::setUp(); + } + + protected function tearDown() + { + parent::tearDown(); + $this->destroyApp(); + } + + public function getParam($name,$default=null) { if (self::$params === null) { self::$params = require(__DIR__ . '/data/config.php'); } - return isset(self::$params[$name]) ? self::$params[$name] : null; + return isset(self::$params[$name]) ? self::$params[$name] : $default; + } + + protected function mockApplication($requiredConfig=array()) + { + static $defaultConfig = array( + 'id' => 'testapp', + 'basePath' => __DIR__, + ); + + $appClass = $this->getParam( 'appClass', '\yii\web\Application' ); + new $appClass(array_merge($defaultConfig,$requiredConfig)); + } + + protected function destroyApp() + { + \Yii::$app = null; } } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 285b55b..a0fdab4 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -9,6 +9,4 @@ require_once(__DIR__ . '/../../yii/Yii.php'); Yii::setAlias('@yiiunit', __DIR__); -new \yii\web\Application(array('id' => 'testapp', 'basePath' => __DIR__)); - require_once(__DIR__ . '/TestCase.php'); diff --git a/tests/unit/data/config.php b/tests/unit/data/config.php index c980c57..790521b 100644 --- a/tests/unit/data/config.php +++ b/tests/unit/data/config.php @@ -1,6 +1,8 @@ '\yii\web\Application', + 'appClass' => '\yii\console\Application', 'databases' => array( 'mysql' => array( 'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest', @@ -12,5 +14,5 @@ return array( 'dsn' => 'sqlite::memory:', 'fixture' => __DIR__ . '/sqlite.sql', ), - ) + ), ); diff --git a/tests/unit/framework/caching/CacheTest.php b/tests/unit/framework/caching/CacheTest.php index f9db4f4..a17f126 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->mockApplication(); + } + public function testSet() { $cache = $this->getCacheInstance(); diff --git a/tests/unit/framework/caching/DbCacheTest.php b/tests/unit/framework/caching/DbCacheTest.php index 523281d..36174b9 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..294cae0 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->mockApplication(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<>&\"'"));