From 3d9340032ee03f46440168fe00438b305cee0cbe Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 18 Dec 2013 00:40:52 -0500 Subject: [PATCH] test refactoring. --- apps/basic/commands/HelloController.php | 2 +- apps/basic/config/web.php | 6 ++---- apps/basic/tests/unit/_bootstrap.php | 4 ++-- apps/basic/web/index-test-acceptance.php | 5 ++--- apps/basic/web/index.php | 3 +-- extensions/yii/codeception/TestCase.php | 32 ++++++++++++++------------------ 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/apps/basic/commands/HelloController.php b/apps/basic/commands/HelloController.php index b62e5bb..ce567dd 100644 --- a/apps/basic/commands/HelloController.php +++ b/apps/basic/commands/HelloController.php @@ -25,6 +25,6 @@ class HelloController extends Controller */ public function actionIndex($message = 'hello world') { - echo $message."\n"; + echo $message . "\n"; } } diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index 966458d..472f842 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -39,16 +39,14 @@ $config = [ 'params' => $params, ]; -if (YII_ENV_DEV) -{ +if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['preload'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['gii'] = 'yii\gii\Module'; } -if (YII_ENV_TEST) -{ +if (YII_ENV_TEST) { // configuration adjustments for 'test' environment. // configuration for codeception test environments can be found in codeception folder. diff --git a/apps/basic/tests/unit/_bootstrap.php b/apps/basic/tests/unit/_bootstrap.php index d6d11e9..f37625b 100644 --- a/apps/basic/tests/unit/_bootstrap.php +++ b/apps/basic/tests/unit/_bootstrap.php @@ -2,7 +2,7 @@ // add unit testing specific bootstrap code here -yii\codeception\TestCase::$applicationConfig = yii\helpers\ArrayHelper::merge( +yii\codeception\TestCase::$appConfig = yii\helpers\ArrayHelper::merge( require(__DIR__ . '/../../config/web.php'), require(__DIR__ . '/../../config/codeception/unit.php') -); \ No newline at end of file +); diff --git a/apps/basic/web/index-test-acceptance.php b/apps/basic/web/index-test-acceptance.php index 224152b..ef9ec75 100644 --- a/apps/basic/web/index-test-acceptance.php +++ b/apps/basic/web/index-test-acceptance.php @@ -1,6 +1,6 @@ run(); +(new yii\web\Application($config))->run(); diff --git a/apps/basic/web/index.php b/apps/basic/web/index.php index e9eeb33..006e28f 100644 --- a/apps/basic/web/index.php +++ b/apps/basic/web/index.php @@ -9,5 +9,4 @@ require(__DIR__ . '/../vendor/yiisoft/yii2/yii/Yii.php'); $config = require(__DIR__ . '/../config/web.php'); -$application = new yii\web\Application($config); -$application->run(); +(new yii\web\Application($config))->run(); diff --git a/extensions/yii/codeception/TestCase.php b/extensions/yii/codeception/TestCase.php index e6809bc..34dc11d 100644 --- a/extensions/yii/codeception/TestCase.php +++ b/extensions/yii/codeception/TestCase.php @@ -3,7 +3,6 @@ namespace yii\codeception; use Yii; -use yii\helpers\ArrayHelper; /** * TestCase is the base class for all codeception unit tests @@ -14,22 +13,18 @@ use yii\helpers\ArrayHelper; class TestCase extends \PHPUnit_Framework_TestCase { /** - * @var array|string Your application base config that will be used for creating application each time before test. - * This can be an array or alias, pointing to the config file. For example for console application it can be - * '@tests/unit/console_bootstrap.php' that can be similar to existing unit tests bootstrap file. + * @var array the application configuration that will be used for creating an application instance for each test. */ - public static $applicationConfig = '@app/config/web.php'; + public static $appConfig = []; /** - * @var array|string Your application config, will be merged with base config when creating application. Can be an alias too. + * @var string the application class that [[mockApplication()]] should use */ - protected $config = []; + public static $appClass = 'yii\web\Application'; + /** - * Created application class - * @var string + * @inheritdoc */ - protected $applicationClass = 'yii\web\Application'; - protected function tearDown() { $this->destroyApplication(); @@ -37,20 +32,21 @@ class TestCase extends \PHPUnit_Framework_TestCase } /** - * Sets up `Yii::$app`. + * Mocks up the application instance. + * @param array $config the configuration that should be used to generate the application instance. + * If null, [[appConfig]] will be used. + * @return \yii\web\Application|\yii\console\Application the application instance */ - protected function mockApplication() + protected function mockApplication($config = null) { - $baseConfig = is_array(static::$applicationConfig) ? static::$applicationConfig : require(Yii::getAlias(static::$applicationConfig)); - $config = is_array($this->config)? $this->config : require(Yii::getAlias($this->config)); - new $this->applicationClass(ArrayHelper::merge($baseConfig,$config)); + return new static::$appClass($config === null ? static::$appConfig : $config); } /** - * Destroys an application created via [[mockApplication]]. + * Destroys the application instance created by [[mockApplication]]. */ protected function destroyApplication() { - \Yii::$app = null; + Yii::$app = null; } }