Commit cb8f5d70 by Carsten Brandt

cleanup tests

parent e0ad7125
...@@ -10,6 +10,7 @@ class DatabaseTestCase extends TestCase ...@@ -10,6 +10,7 @@ class DatabaseTestCase extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp();
$databases = $this->getParam('databases'); $databases = $this->getParam('databases');
$this->database = $databases[$this->driverName]; $this->database = $databases[$this->driverName];
$pdo_database = 'pdo_'.$this->driverName; $pdo_database = 'pdo_'.$this->driverName;
......
...@@ -6,6 +6,7 @@ class MysqlTestCase extends TestCase ...@@ -6,6 +6,7 @@ class MysqlTestCase extends TestCase
{ {
protected function setUp() protected function setUp()
{ {
parent::setUp();
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extensions are required.'); $this->markTestSkipped('pdo and pdo_mysql extensions are required.');
} }
......
...@@ -2,21 +2,30 @@ ...@@ -2,21 +2,30 @@
namespace yiiunit; namespace yiiunit;
/**
* This is the base class for all yii framework unit tests.
*/
class TestCase extends \yii\test\TestCase class TestCase extends \yii\test\TestCase
{ {
public static $params; public static $params;
protected function setUp() { /**
parent::setUp(); * Clean up after test.
} * By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown() protected function tearDown()
{ {
parent::tearDown(); parent::tearDown();
$this->destroyApp(); $this->destroyApplication();
} }
public function getParam($name,$default=null) /**
* Returns a test configuration param from /data/config.php
* @param string $name params name
* @param mixed $default default value to use when param is not set.
* @return mixed the value of the configuration param
*/
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');
...@@ -24,7 +33,12 @@ class TestCase extends \yii\test\TestCase ...@@ -24,7 +33,12 @@ 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 mockApplication($requiredConfig=array()) /**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
*/
protected function mockApplication($config=array())
{ {
static $defaultConfig = array( static $defaultConfig = array(
'id' => 'testapp', 'id' => 'testapp',
...@@ -32,10 +46,13 @@ class TestCase extends \yii\test\TestCase ...@@ -32,10 +46,13 @@ class TestCase extends \yii\test\TestCase
); );
$appClass = $this->getParam( 'appClass', '\yii\web\Application' ); $appClass = $this->getParam( 'appClass', '\yii\web\Application' );
new $appClass(array_merge($defaultConfig,$requiredConfig)); new $appClass(array_merge($defaultConfig,$config));
} }
protected function destroyApp() /**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{ {
\Yii::$app = null; \Yii::$app = null;
} }
......
...@@ -11,13 +11,15 @@ class YiiBaseTest extends TestCase ...@@ -11,13 +11,15 @@ class YiiBaseTest extends TestCase
{ {
public $aliases; public $aliases;
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->aliases = Yii::$aliases; $this->aliases = Yii::$aliases;
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
Yii::$aliases = $this->aliases; Yii::$aliases = $this->aliases;
} }
......
...@@ -24,13 +24,15 @@ class ComponentTest extends TestCase ...@@ -24,13 +24,15 @@ class ComponentTest extends TestCase
*/ */
protected $component; protected $component;
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->component = new NewComponent(); $this->component = new NewComponent();
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
$this->component = null; $this->component = null;
} }
......
...@@ -19,8 +19,9 @@ class DictionaryTest extends \yiiunit\TestCase ...@@ -19,8 +19,9 @@ class DictionaryTest extends \yiiunit\TestCase
protected $item2; protected $item2;
protected $item3; protected $item3;
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->dictionary = new Dictionary; $this->dictionary = new Dictionary;
$this->item1 = new MapItem; $this->item1 = new MapItem;
$this->item2 = new MapItem; $this->item2 = new MapItem;
...@@ -29,8 +30,9 @@ class DictionaryTest extends \yiiunit\TestCase ...@@ -29,8 +30,9 @@ class DictionaryTest extends \yiiunit\TestCase
$this->dictionary->add('key2', $this->item2); $this->dictionary->add('key2', $this->item2);
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
$this->dictionary = null; $this->dictionary = null;
$this->item1 = null; $this->item1 = null;
$this->item2 = null; $this->item2 = null;
......
...@@ -14,13 +14,15 @@ class ObjectTest extends TestCase ...@@ -14,13 +14,15 @@ class ObjectTest extends TestCase
*/ */
protected $object; protected $object;
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->object = new NewObject; $this->object = new NewObject;
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
$this->object = null; $this->object = null;
} }
......
...@@ -19,8 +19,9 @@ class VectorTest extends \yiiunit\TestCase ...@@ -19,8 +19,9 @@ class VectorTest extends \yiiunit\TestCase
protected $item2; protected $item2;
protected $item3; protected $item3;
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->vector = new Vector; $this->vector = new Vector;
$this->item1 = new ListItem; $this->item1 = new ListItem;
$this->item2 = new ListItem; $this->item2 = new ListItem;
...@@ -29,8 +30,9 @@ class VectorTest extends \yiiunit\TestCase ...@@ -29,8 +30,9 @@ class VectorTest extends \yiiunit\TestCase
$this->vector->add($this->item2); $this->vector->add($this->item2);
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
$this->vector = null; $this->vector = null;
$this->item1 = null; $this->item1 = null;
$this->item2 = null; $this->item2 = null;
......
...@@ -12,7 +12,7 @@ use yiiunit\data\ar\Item; ...@@ -12,7 +12,7 @@ use yiiunit\data\ar\Item;
class ActiveRecordTest extends \yiiunit\DatabaseTestCase class ActiveRecordTest extends \yiiunit\DatabaseTestCase
{ {
public function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
ActiveRecord::$db = $this->getConnection(); ActiveRecord::$db = $this->getConnection();
......
...@@ -4,7 +4,7 @@ namespace yiiunit\framework\db\sqlite; ...@@ -4,7 +4,7 @@ namespace yiiunit\framework\db\sqlite;
class SqliteActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest class SqliteActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest
{ {
public function setUp() protected function setUp()
{ {
$this->driverName = 'sqlite'; $this->driverName = 'sqlite';
parent::setUp(); parent::setUp();
......
...@@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite; ...@@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite;
class SqliteCommandTest extends \yiiunit\framework\db\CommandTest class SqliteCommandTest extends \yiiunit\framework\db\CommandTest
{ {
public function setUp() protected function setUp()
{ {
$this->driverName = 'sqlite'; $this->driverName = 'sqlite';
parent::setUp(); parent::setUp();
} }
function testAutoQuoting() public function testAutoQuoting()
{ {
$db = $this->getConnection(false); $db = $this->getConnection(false);
......
...@@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite; ...@@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite;
class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest
{ {
public function setUp() protected function setUp()
{ {
$this->driverName = 'sqlite'; $this->driverName = 'sqlite';
parent::setUp(); parent::setUp();
} }
function testConstruct() public function testConstruct()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$params = $this->database; $params = $this->database;
...@@ -18,7 +18,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest ...@@ -18,7 +18,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest
$this->assertEquals($params['dsn'], $connection->dsn); $this->assertEquals($params['dsn'], $connection->dsn);
} }
function testQuoteValue() public function testQuoteValue()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$this->assertEquals(123, $connection->quoteValue(123)); $this->assertEquals(123, $connection->quoteValue(123));
...@@ -26,7 +26,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest ...@@ -26,7 +26,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest
$this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting"));
} }
function testQuoteTableName() public function testQuoteTableName()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$this->assertEquals("'table'", $connection->quoteTableName('table')); $this->assertEquals("'table'", $connection->quoteTableName('table'));
...@@ -35,7 +35,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest ...@@ -35,7 +35,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest
$this->assertEquals('(table)', $connection->quoteTableName('(table)')); $this->assertEquals('(table)', $connection->quoteTableName('(table)'));
} }
function testQuoteColumnName() public function testQuoteColumnName()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$this->assertEquals('"column"', $connection->quoteColumnName('column')); $this->assertEquals('"column"', $connection->quoteColumnName('column'));
......
...@@ -12,7 +12,7 @@ namespace yiiunit\framework\db\sqlite; ...@@ -12,7 +12,7 @@ namespace yiiunit\framework\db\sqlite;
class SqliteQueryTest extends \yiiunit\framework\db\QueryTest class SqliteQueryTest extends \yiiunit\framework\db\QueryTest
{ {
public function setUp() protected function setUp()
{ {
$this->driverName = 'sqlite'; $this->driverName = 'sqlite';
parent::setUp(); parent::setUp();
......
...@@ -8,8 +8,9 @@ use yiiunit\TestCase; ...@@ -8,8 +8,9 @@ use yiiunit\TestCase;
class HtmlTest extends TestCase class HtmlTest extends TestCase
{ {
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->mockApplication(array( $this->mockApplication(array(
'components' => array( 'components' => array(
'request' => array( 'request' => array(
......
...@@ -7,6 +7,9 @@ class VarDumperTest extends \yii\test\TestCase ...@@ -7,6 +7,9 @@ class VarDumperTest extends \yii\test\TestCase
public function testDumpObject() public function testDumpObject()
{ {
$obj = new \StdClass(); $obj = new \StdClass();
ob_start();
VarDumper::dump($obj); VarDumper::dump($obj);
$this->assertEquals("stdClass#1\n(\n)", ob_get_contents());
ob_end_clean();
} }
} }
...@@ -5,12 +5,14 @@ namespace yiiunit\framework\rbac; ...@@ -5,12 +5,14 @@ namespace yiiunit\framework\rbac;
use Yii; use Yii;
use yii\rbac\PhpManager; use yii\rbac\PhpManager;
require_once(__DIR__ . '/ManagerTestBase.php'); //require_once(__DIR__ . '/ManagerTestBase.php');
class PhpManagerTest extends ManagerTestBase class PhpManagerTest extends ManagerTestBase
{ {
public function setUp() protected function setUp()
{ {
parent::setUp();
$this->mockApplication();
$authFile = Yii::$app->getRuntimePath() . '/rbac.php'; $authFile = Yii::$app->getRuntimePath() . '/rbac.php';
@unlink($authFile); @unlink($authFile);
$this->auth = new PhpManager; $this->auth = new PhpManager;
...@@ -19,8 +21,9 @@ class PhpManagerTest extends ManagerTestBase ...@@ -19,8 +21,9 @@ class PhpManagerTest extends ManagerTestBase
$this->prepareData(); $this->prepareData();
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown();
@unlink($this->auth->authFile); @unlink($this->auth->authFile);
} }
......
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