Commit e0ad7125 by Carsten Brandt

Merge pull request #204 branch 'unittest-app-dependency' of…

Merge pull request #204 branch 'unittest-app-dependency' of https://github.com/bwoester/yii2 into bwoester-unittest-app-dependency * 'unittest-app-dependency' of https://github.com/bwoester/yii2: 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 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 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 allow unit tests to requireApp() on setUp() Conflicts: tests/unit/data/config.php
parents abeb02e9 f23a677b
...@@ -6,11 +6,37 @@ class TestCase extends \yii\test\TestCase ...@@ -6,11 +6,37 @@ class TestCase extends \yii\test\TestCase
{ {
public static $params; 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) { if (self::$params === null) {
self::$params = require(__DIR__ . '/data/config.php'); 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;
} }
} }
...@@ -9,6 +9,4 @@ require_once(__DIR__ . '/../../yii/Yii.php'); ...@@ -9,6 +9,4 @@ require_once(__DIR__ . '/../../yii/Yii.php');
Yii::setAlias('@yiiunit', __DIR__); Yii::setAlias('@yiiunit', __DIR__);
new \yii\web\Application(array('id' => 'testapp', 'basePath' => __DIR__));
require_once(__DIR__ . '/TestCase.php'); require_once(__DIR__ . '/TestCase.php');
<?php <?php
return array( return array(
//'appClass' => '\yii\web\Application',
'appClass' => '\yii\console\Application',
'databases' => array( 'databases' => array(
'mysql' => array( 'mysql' => array(
'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest', 'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest',
...@@ -12,5 +14,5 @@ return array( ...@@ -12,5 +14,5 @@ return array(
'dsn' => 'sqlite::memory:', 'dsn' => 'sqlite::memory:',
'fixture' => __DIR__ . '/sqlite.sql', 'fixture' => __DIR__ . '/sqlite.sql',
), ),
) ),
); );
...@@ -13,6 +13,12 @@ abstract class CacheTest extends TestCase ...@@ -13,6 +13,12 @@ abstract class CacheTest extends TestCase
*/ */
abstract protected function getCacheInstance(); abstract protected function getCacheInstance();
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testSet() public function testSet()
{ {
$cache = $this->getCacheInstance(); $cache = $this->getCacheInstance();
......
...@@ -17,6 +17,8 @@ class DbCacheTest extends CacheTest ...@@ -17,6 +17,8 @@ class DbCacheTest extends CacheTest
$this->markTestSkipped('pdo and pdo_mysql extensions are required.'); $this->markTestSkipped('pdo and pdo_mysql extensions are required.');
} }
parent::setUp();
$this->getConnection()->createCommand(" $this->getConnection()->createCommand("
CREATE TABLE IF NOT EXISTS tbl_cache ( CREATE TABLE IF NOT EXISTS tbl_cache (
id char(128) NOT NULL, id char(128) NOT NULL,
......
...@@ -4,15 +4,13 @@ namespace yiiunit\framework\helpers; ...@@ -4,15 +4,13 @@ namespace yiiunit\framework\helpers;
use Yii; use Yii;
use yii\helpers\Html; use yii\helpers\Html;
use yii\web\Application; use yiiunit\TestCase;
class HtmlTest extends \yii\test\TestCase class HtmlTest extends TestCase
{ {
public function setUp() public function setUp()
{ {
new Application(array( $this->mockApplication(array(
'id' => 'test',
'basePath' => '@yiiunit/runtime',
'components' => array( 'components' => array(
'request' => array( 'request' => array(
'class' => 'yii\web\Request', 'class' => 'yii\web\Request',
...@@ -30,11 +28,6 @@ class HtmlTest extends \yii\test\TestCase ...@@ -30,11 +28,6 @@ class HtmlTest extends \yii\test\TestCase
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function tearDown()
{
Yii::$app = null;
}
public function testEncode() public function testEncode()
{ {
$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'")); $this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment