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] 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');