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 1/4] 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<>&\"'")); From 5e0f6604e828c64b70c9a0774e2418c204340054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20W=C3=B6ster?= Date: Thu, 9 May 2013 21:56:36 +0200 Subject: [PATCH 2/4] mod: don't create app in bootstrap script, unit tests do it themselves add: option to destroy app after each test to find unit tests that fail to require an app --- tests/unit/TestCase.php | 20 ++++++++++++++++++++ tests/unit/bootstrap.php | 3 +-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index d098f30..9a65138 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -6,6 +6,21 @@ class TestCase extends \yii\test\TestCase { public static $params; + protected function setUp() { + parent::setUp(); + } + + 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(); + } + } + public function getParam($name) { if (self::$params === null) { @@ -32,4 +47,9 @@ class TestCase extends \yii\test\TestCase $usedConfig = $newConfig; } } + + protected function destroyApp() + { + \yii::$app = null; + } } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 285b55b..55f8158 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -2,6 +2,7 @@ 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__; @@ -9,6 +10,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'); From c30ee60e9c6739d6862addd13ef5a5d22a971ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20W=C3=B6ster?= Date: Thu, 9 May 2013 22:06:58 +0200 Subject: [PATCH 3/4] add: new key for unit tests config named "className". Allows to run the tests using different Application instances (consoleApp/ webApp) mod: TestCase::getParam accepts second param $default=null --- tests/unit/TestCase.php | 11 ++++++----- tests/unit/data/config.php | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index 9a65138..314e2b0 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -21,12 +21,12 @@ class TestCase extends \yii\test\TestCase } } - public function getParam($name) + 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 requireApp($requiredConfig=array()) @@ -38,12 +38,13 @@ class TestCase extends \yii\test\TestCase ); $newConfig = array_merge( $defaultConfig, $requiredConfig ); + $appClass = $this->getParam( 'appClass', '\yii\web\Application' ); - if (!(\yii::$app instanceof \yii\web\Application)) { - new \yii\web\Application( $newConfig ); + if (!(\yii::$app instanceof $appClass)) { + new $appClass( $newConfig ); $usedConfig = $newConfig; } elseif ($newConfig !== $usedConfig) { - new \yii\web\Application( $newConfig ); + new $appClass( $newConfig ); $usedConfig = $newConfig; } } diff --git a/tests/unit/data/config.php b/tests/unit/data/config.php index 238a7cc..fe5f229 100644 --- a/tests/unit/data/config.php +++ b/tests/unit/data/config.php @@ -1,6 +1,8 @@ '\yii\web\Application', + 'appClass' => '\yii\console\Application', 'mysql' => array( 'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest', 'username' => 'travis', 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 4/4] 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',