Browse Source

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
tags/2.0.0-beta
Benjamin Wöster 12 years ago
parent
commit
f23a677bdf
  1. 20
      tests/unit/TestCase.php
  2. 1
      tests/unit/bootstrap.php
  3. 2
      tests/unit/framework/caching/CacheTest.php
  4. 2
      tests/unit/framework/helpers/HtmlTest.php

20
tests/unit/TestCase.php

@ -13,13 +13,8 @@ class TestCase extends \yii\test\TestCase
protected function tearDown() protected function tearDown()
{ {
parent::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) 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; 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( static $defaultConfig = array(
'id' => 'testapp', 'id' => 'testapp',
'basePath' => __DIR__, 'basePath' => __DIR__,
); );
$newConfig = array_merge( $defaultConfig, $requiredConfig );
$appClass = $this->getParam( 'appClass', '\yii\web\Application' ); $appClass = $this->getParam( 'appClass', '\yii\web\Application' );
new $appClass(array_merge($defaultConfig,$requiredConfig));
if (!(\yii::$app instanceof $appClass)) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
} elseif ($newConfig !== $usedConfig) {
new $appClass( $newConfig );
$usedConfig = $newConfig;
}
} }
protected function destroyApp() protected function destroyApp()
{ {
\yii::$app = null; \Yii::$app = null;
} }
} }

1
tests/unit/bootstrap.php

@ -2,7 +2,6 @@
define('YII_ENABLE_ERROR_HANDLER', false); define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true); define('YII_DEBUG', true);
define('YII_DESTROY_APP_ON_TEARDOWN', false);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__; $_SERVER['SCRIPT_FILENAME'] = __FILE__;

2
tests/unit/framework/caching/CacheTest.php

@ -16,7 +16,7 @@ abstract class CacheTest extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
$this->requireApp(); $this->mockApplication();
} }
public function testSet() public function testSet()

2
tests/unit/framework/helpers/HtmlTest.php

@ -10,7 +10,7 @@ class HtmlTest extends TestCase
{ {
public function setUp() public function setUp()
{ {
$this->requireApp(array( $this->mockApplication(array(
'components' => array( 'components' => array(
'request' => array( 'request' => array(
'class' => 'yii\web\Request', 'class' => 'yii\web\Request',

Loading…
Cancel
Save