From cb8f5d702f0921c4689b9d44d2fb71c734fd7a72 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sat, 11 May 2013 12:51:24 +0200 Subject: [PATCH] cleanup tests --- tests/unit/DatabaseTestCase.php | 1 + tests/unit/MysqlTestCase.php | 1 + tests/unit/TestCase.php | 39 ++++++++++++++++------ tests/unit/framework/YiiBaseTest.php | 6 ++-- tests/unit/framework/base/ComponentTest.php | 6 ++-- tests/unit/framework/base/DictionaryTest.php | 6 ++-- tests/unit/framework/base/ObjectTest.php | 6 ++-- tests/unit/framework/base/VectorTest.php | 6 ++-- tests/unit/framework/db/ActiveRecordTest.php | 2 +- .../framework/db/sqlite/SqliteActiveRecordTest.php | 2 +- .../unit/framework/db/sqlite/SqliteCommandTest.php | 4 +-- .../framework/db/sqlite/SqliteConnectionTest.php | 10 +++--- tests/unit/framework/db/sqlite/SqliteQueryTest.php | 2 +- tests/unit/framework/helpers/HtmlTest.php | 3 +- tests/unit/framework/helpers/VarDumperTest.php | 3 ++ tests/unit/framework/rbac/PhpManagerTest.php | 9 +++-- 16 files changed, 71 insertions(+), 35 deletions(-) diff --git a/tests/unit/DatabaseTestCase.php b/tests/unit/DatabaseTestCase.php index b39c2e5..0c025e3 100644 --- a/tests/unit/DatabaseTestCase.php +++ b/tests/unit/DatabaseTestCase.php @@ -10,6 +10,7 @@ class DatabaseTestCase extends TestCase protected function setUp() { + parent::setUp(); $databases = $this->getParam('databases'); $this->database = $databases[$this->driverName]; $pdo_database = 'pdo_'.$this->driverName; diff --git a/tests/unit/MysqlTestCase.php b/tests/unit/MysqlTestCase.php index c7ef970..3c16e03 100644 --- a/tests/unit/MysqlTestCase.php +++ b/tests/unit/MysqlTestCase.php @@ -6,6 +6,7 @@ class MysqlTestCase extends TestCase { protected function setUp() { + parent::setUp(); if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { $this->markTestSkipped('pdo and pdo_mysql extensions are required.'); } diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index fbc0da7..e83e1a8 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -2,29 +2,43 @@ namespace yiiunit; +/** + * This is the base class for all yii framework unit tests. + */ class TestCase extends \yii\test\TestCase { 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() { 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) { self::$params = require(__DIR__ . '/data/config.php'); } 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( 'id' => 'testapp', @@ -32,10 +46,13 @@ class TestCase extends \yii\test\TestCase ); $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; } diff --git a/tests/unit/framework/YiiBaseTest.php b/tests/unit/framework/YiiBaseTest.php index 4ebf45c..bc31c0a 100644 --- a/tests/unit/framework/YiiBaseTest.php +++ b/tests/unit/framework/YiiBaseTest.php @@ -11,13 +11,15 @@ class YiiBaseTest extends TestCase { public $aliases; - public function setUp() + protected function setUp() { + parent::setUp(); $this->aliases = Yii::$aliases; } - public function tearDown() + protected function tearDown() { + parent::tearDown(); Yii::$aliases = $this->aliases; } diff --git a/tests/unit/framework/base/ComponentTest.php b/tests/unit/framework/base/ComponentTest.php index 7c860e3..f1c0ba9 100644 --- a/tests/unit/framework/base/ComponentTest.php +++ b/tests/unit/framework/base/ComponentTest.php @@ -24,13 +24,15 @@ class ComponentTest extends TestCase */ protected $component; - public function setUp() + protected function setUp() { + parent::setUp(); $this->component = new NewComponent(); } - public function tearDown() + protected function tearDown() { + parent::tearDown(); $this->component = null; } diff --git a/tests/unit/framework/base/DictionaryTest.php b/tests/unit/framework/base/DictionaryTest.php index 882ffc2..9c47c29 100644 --- a/tests/unit/framework/base/DictionaryTest.php +++ b/tests/unit/framework/base/DictionaryTest.php @@ -19,8 +19,9 @@ class DictionaryTest extends \yiiunit\TestCase protected $item2; protected $item3; - public function setUp() + protected function setUp() { + parent::setUp(); $this->dictionary = new Dictionary; $this->item1 = new MapItem; $this->item2 = new MapItem; @@ -29,8 +30,9 @@ class DictionaryTest extends \yiiunit\TestCase $this->dictionary->add('key2', $this->item2); } - public function tearDown() + protected function tearDown() { + parent::tearDown(); $this->dictionary = null; $this->item1 = null; $this->item2 = null; diff --git a/tests/unit/framework/base/ObjectTest.php b/tests/unit/framework/base/ObjectTest.php index 14856e2..df002cc 100644 --- a/tests/unit/framework/base/ObjectTest.php +++ b/tests/unit/framework/base/ObjectTest.php @@ -14,13 +14,15 @@ class ObjectTest extends TestCase */ protected $object; - public function setUp() + protected function setUp() { + parent::setUp(); $this->object = new NewObject; } - public function tearDown() + protected function tearDown() { + parent::tearDown(); $this->object = null; } diff --git a/tests/unit/framework/base/VectorTest.php b/tests/unit/framework/base/VectorTest.php index b14d5dc..6d6bd23 100644 --- a/tests/unit/framework/base/VectorTest.php +++ b/tests/unit/framework/base/VectorTest.php @@ -19,8 +19,9 @@ class VectorTest extends \yiiunit\TestCase protected $item2; protected $item3; - public function setUp() + protected function setUp() { + parent::setUp(); $this->vector = new Vector; $this->item1 = new ListItem; $this->item2 = new ListItem; @@ -29,8 +30,9 @@ class VectorTest extends \yiiunit\TestCase $this->vector->add($this->item2); } - public function tearDown() + protected function tearDown() { + parent::tearDown(); $this->vector = null; $this->item1 = null; $this->item2 = null; diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php index e337a5d..f86ca8e 100644 --- a/tests/unit/framework/db/ActiveRecordTest.php +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -12,7 +12,7 @@ use yiiunit\data\ar\Item; class ActiveRecordTest extends \yiiunit\DatabaseTestCase { - public function setUp() + protected function setUp() { parent::setUp(); ActiveRecord::$db = $this->getConnection(); diff --git a/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php b/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php index 539e879..4779f85 100644 --- a/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php +++ b/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php @@ -4,7 +4,7 @@ namespace yiiunit\framework\db\sqlite; class SqliteActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest { - public function setUp() + protected function setUp() { $this->driverName = 'sqlite'; parent::setUp(); diff --git a/tests/unit/framework/db/sqlite/SqliteCommandTest.php b/tests/unit/framework/db/sqlite/SqliteCommandTest.php index 4110cd3..3898aa2 100644 --- a/tests/unit/framework/db/sqlite/SqliteCommandTest.php +++ b/tests/unit/framework/db/sqlite/SqliteCommandTest.php @@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite; class SqliteCommandTest extends \yiiunit\framework\db\CommandTest { - public function setUp() + protected function setUp() { $this->driverName = 'sqlite'; parent::setUp(); } - function testAutoQuoting() + public function testAutoQuoting() { $db = $this->getConnection(false); diff --git a/tests/unit/framework/db/sqlite/SqliteConnectionTest.php b/tests/unit/framework/db/sqlite/SqliteConnectionTest.php index eeb5aae..5685137 100644 --- a/tests/unit/framework/db/sqlite/SqliteConnectionTest.php +++ b/tests/unit/framework/db/sqlite/SqliteConnectionTest.php @@ -4,13 +4,13 @@ namespace yiiunit\framework\db\sqlite; class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest { - public function setUp() + protected function setUp() { $this->driverName = 'sqlite'; parent::setUp(); } - function testConstruct() + public function testConstruct() { $connection = $this->getConnection(false); $params = $this->database; @@ -18,7 +18,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest $this->assertEquals($params['dsn'], $connection->dsn); } - function testQuoteValue() + public function testQuoteValue() { $connection = $this->getConnection(false); $this->assertEquals(123, $connection->quoteValue(123)); @@ -26,7 +26,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); } - function testQuoteTableName() + public function testQuoteTableName() { $connection = $this->getConnection(false); $this->assertEquals("'table'", $connection->quoteTableName('table')); @@ -35,7 +35,7 @@ class SqliteConnectionTest extends \yiiunit\framework\db\ConnectionTest $this->assertEquals('(table)', $connection->quoteTableName('(table)')); } - function testQuoteColumnName() + public function testQuoteColumnName() { $connection = $this->getConnection(false); $this->assertEquals('"column"', $connection->quoteColumnName('column')); diff --git a/tests/unit/framework/db/sqlite/SqliteQueryTest.php b/tests/unit/framework/db/sqlite/SqliteQueryTest.php index f00e59e..2a3a831 100644 --- a/tests/unit/framework/db/sqlite/SqliteQueryTest.php +++ b/tests/unit/framework/db/sqlite/SqliteQueryTest.php @@ -12,7 +12,7 @@ namespace yiiunit\framework\db\sqlite; class SqliteQueryTest extends \yiiunit\framework\db\QueryTest { - public function setUp() + protected function setUp() { $this->driverName = 'sqlite'; parent::setUp(); diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 294cae0..2311321 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -8,8 +8,9 @@ use yiiunit\TestCase; class HtmlTest extends TestCase { - public function setUp() + protected function setUp() { + parent::setUp(); $this->mockApplication(array( 'components' => array( 'request' => array( diff --git a/tests/unit/framework/helpers/VarDumperTest.php b/tests/unit/framework/helpers/VarDumperTest.php index a797121..e734863 100644 --- a/tests/unit/framework/helpers/VarDumperTest.php +++ b/tests/unit/framework/helpers/VarDumperTest.php @@ -7,6 +7,9 @@ class VarDumperTest extends \yii\test\TestCase public function testDumpObject() { $obj = new \StdClass(); + ob_start(); VarDumper::dump($obj); + $this->assertEquals("stdClass#1\n(\n)", ob_get_contents()); + ob_end_clean(); } } diff --git a/tests/unit/framework/rbac/PhpManagerTest.php b/tests/unit/framework/rbac/PhpManagerTest.php index 69fdd41..d5ab41b 100644 --- a/tests/unit/framework/rbac/PhpManagerTest.php +++ b/tests/unit/framework/rbac/PhpManagerTest.php @@ -5,12 +5,14 @@ namespace yiiunit\framework\rbac; use Yii; use yii\rbac\PhpManager; -require_once(__DIR__ . '/ManagerTestBase.php'); +//require_once(__DIR__ . '/ManagerTestBase.php'); class PhpManagerTest extends ManagerTestBase { - public function setUp() + protected function setUp() { + parent::setUp(); + $this->mockApplication(); $authFile = Yii::$app->getRuntimePath() . '/rbac.php'; @unlink($authFile); $this->auth = new PhpManager; @@ -19,8 +21,9 @@ class PhpManagerTest extends ManagerTestBase $this->prepareData(); } - public function tearDown() + protected function tearDown() { + parent::tearDown(); @unlink($this->auth->authFile); }