Browse Source

allow unit tests to requireApp() on setUp()

tags/2.0.0-beta
Benjamin Wöster 12 years ago
parent
commit
d4b30e26c2
  1. 19
      tests/unit/TestCase.php
  2. 6
      tests/unit/framework/caching/CacheTest.php
  3. 2
      tests/unit/framework/caching/DbCacheTest.php
  4. 13
      tests/unit/framework/helpers/HtmlTest.php

19
tests/unit/TestCase.php

@ -13,4 +13,23 @@ class TestCase extends \yii\test\TestCase
}
return isset(self::$params[$name]) ? self::$params[$name] : null;
}
protected function requireApp($requiredConfig=array())
{
static $usedConfig = array();
static $defaultConfig = array(
'id' => 'testapp',
'basePath' => __DIR__,
);
$newConfig = array_merge( $defaultConfig, $requiredConfig );
if (!(\yii::$app instanceof \yii\web\Application)) {
new \yii\web\Application( $newConfig );
$usedConfig = $newConfig;
} elseif ($newConfig !== $usedConfig) {
new \yii\web\Application( $newConfig );
$usedConfig = $newConfig;
}
}
}

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->requireApp();
}
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->requireApp(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