Browse Source

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
tags/2.0.0-beta
Carsten Brandt 12 years ago
parent
commit
e0ad712527
  1. 30
      tests/unit/TestCase.php
  2. 2
      tests/unit/bootstrap.php
  3. 4
      tests/unit/data/config.php
  4. 6
      tests/unit/framework/caching/CacheTest.php
  5. 2
      tests/unit/framework/caching/DbCacheTest.php
  6. 13
      tests/unit/framework/helpers/HtmlTest.php

30
tests/unit/TestCase.php

@ -6,11 +6,37 @@ class TestCase extends \yii\test\TestCase
{
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) {
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;
}
}

2
tests/unit/bootstrap.php

@ -9,6 +9,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');

4
tests/unit/data/config.php

@ -1,6 +1,8 @@
<?php
return array(
//'appClass' => '\yii\web\Application',
'appClass' => '\yii\console\Application',
'databases' => array(
'mysql' => array(
'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest',
@ -12,5 +14,5 @@ return array(
'dsn' => 'sqlite::memory:',
'fixture' => __DIR__ . '/sqlite.sql',
),
)
),
);

6
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->mockApplication();
}
public function testSet()
{
$cache = $this->getCacheInstance();

2
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,

13
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->mockApplication(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&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));

Loading…
Cancel
Save