From 7cd9123c3b40a67acbc95d651655919e98b75686 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 24 May 2013 18:34:44 +0400 Subject: [PATCH] Tests cleanup - Better names - Removed not used MySQLTestCase - Moved base testcase for DB to db namespace - Minor style fixes --- tests/unit/DatabaseTestCase.php | 51 ----- tests/unit/MysqlTestCase.php | 37 --- tests/unit/TestCase.php | 2 +- tests/unit/framework/caching/ApcCacheTest.php | 3 +- tests/unit/framework/caching/CacheTest.php | 170 -------------- tests/unit/framework/caching/CacheTestCase.php | 170 ++++++++++++++ tests/unit/framework/caching/DbCacheTest.php | 3 +- tests/unit/framework/caching/FileCacheTest.php | 3 +- tests/unit/framework/caching/MemCacheTest.php | 3 +- tests/unit/framework/caching/MemCachedTest.php | 3 +- tests/unit/framework/caching/WinCacheTest.php | 5 +- tests/unit/framework/caching/XCacheTest.php | 3 +- tests/unit/framework/caching/ZendDataCacheTest.php | 4 +- tests/unit/framework/db/ActiveRecordTest.php | 3 +- tests/unit/framework/db/CommandTest.php | 2 +- tests/unit/framework/db/ConnectionTest.php | 2 +- tests/unit/framework/db/DatabaseTestCase.php | 52 +++++ tests/unit/framework/db/QueryTest.php | 2 +- .../framework/db/mssql/MssqlActiveRecordTest.php | 4 +- tests/unit/framework/db/mssql/MssqlCommandTest.php | 4 +- .../framework/db/mssql/MssqlConnectionTest.php | 4 +- tests/unit/framework/db/mssql/MssqlQueryTest.php | 4 +- .../framework/db/sqlite/SqliteActiveRecordTest.php | 5 +- .../unit/framework/db/sqlite/SqliteCommandTest.php | 5 +- .../framework/db/sqlite/SqliteConnectionTest.php | 5 +- tests/unit/framework/db/sqlite/SqliteQueryTest.php | 11 +- tests/unit/framework/helpers/ArrayHelperTest.php | 4 +- tests/unit/framework/helpers/JsonTest.php | 3 +- tests/unit/framework/helpers/StringHelperTest.php | 3 +- tests/unit/framework/helpers/VarDumperTest.php | 3 +- tests/unit/framework/rbac/ManagerTestBase.php | 249 --------------------- tests/unit/framework/rbac/ManagerTestCase.php | 249 +++++++++++++++++++++ tests/unit/framework/rbac/PhpManagerTest.php | 4 +- tests/unit/framework/web/UrlManagerTest.php | 3 +- tests/unit/framework/web/UrlRuleTest.php | 3 +- 35 files changed, 522 insertions(+), 559 deletions(-) delete mode 100644 tests/unit/DatabaseTestCase.php delete mode 100644 tests/unit/MysqlTestCase.php delete mode 100644 tests/unit/framework/caching/CacheTest.php create mode 100644 tests/unit/framework/caching/CacheTestCase.php create mode 100644 tests/unit/framework/db/DatabaseTestCase.php delete mode 100644 tests/unit/framework/rbac/ManagerTestBase.php create mode 100644 tests/unit/framework/rbac/ManagerTestCase.php diff --git a/tests/unit/DatabaseTestCase.php b/tests/unit/DatabaseTestCase.php deleted file mode 100644 index 9d54173..0000000 --- a/tests/unit/DatabaseTestCase.php +++ /dev/null @@ -1,51 +0,0 @@ -getParam('databases'); - $this->database = $databases[$this->driverName]; - $pdo_database = 'pdo_'.$this->driverName; - - if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) { - $this->markTestSkipped('pdo and pdo_'.$pdo_database.' extension are required.'); - } - } - - /** - * @param bool $reset whether to clean up the test database - * @param bool $open whether to open and populate test database - * @return \yii\db\Connection - */ - public function getConnection($reset = true, $open = true) - { - if (!$reset && $this->db) { - return $this->db; - } - $db = new \yii\db\Connection; - $db->dsn = $this->database['dsn']; - if (isset($this->database['username'])) { - $db->username = $this->database['username']; - $db->password = $this->database['password']; - } - if ($open) { - $db->open(); - $lines = explode(';', file_get_contents($this->database['fixture'])); - foreach ($lines as $line) { - if (trim($line) !== '') { - $db->pdo->exec($line); - } - } - } - $this->db = $db; - return $db; - } -} diff --git a/tests/unit/MysqlTestCase.php b/tests/unit/MysqlTestCase.php deleted file mode 100644 index 3c16e03..0000000 --- a/tests/unit/MysqlTestCase.php +++ /dev/null @@ -1,37 +0,0 @@ -markTestSkipped('pdo and pdo_mysql extensions are required.'); - } - } - - /** - * @param bool $reset whether to clean up the test database - * @return \yii\db\Connection - */ - public function getConnection($reset = true) - { - $params = $this->getParam('mysql'); - $db = new \yii\db\Connection; - $db->dsn = $params['dsn']; - $db->username = $params['username']; - $db->password = $params['password']; - if ($reset) { - $db->open(); - $lines = explode(';', file_get_contents($params['fixture'])); - foreach ($lines as $line) { - if (trim($line) !== '') { - $db->pdo->exec($line); - } - } - } - return $db; - } -} diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index 44cb238..479f85d 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -5,7 +5,7 @@ namespace yiiunit; /** * This is the base class for all yii framework unit tests. */ -class TestCase extends \yii\test\TestCase +abstract class TestCase extends \yii\test\TestCase { public static $params; diff --git a/tests/unit/framework/caching/ApcCacheTest.php b/tests/unit/framework/caching/ApcCacheTest.php index c059554..17bcb6e 100644 --- a/tests/unit/framework/caching/ApcCacheTest.php +++ b/tests/unit/framework/caching/ApcCacheTest.php @@ -1,12 +1,11 @@ mockApplication(); - } - - protected function tearDown() - { - static::$time = null; - } - - /** - * @return Cache - */ - public function prepare() - { - $cache = $this->getCacheInstance(); - - $cache->flush(); - $cache->set('string_test', 'string_test'); - $cache->set('number_test', 42); - $cache->set('array_test', array('array_test' => 'array_test')); - $cache['arrayaccess_test'] = new \stdClass(); - - return $cache; - } - - /** - * default value of cache prefix is application id - */ - public function testKeyPrefix() - { - $cache = $this->getCacheInstance(); - $this->assertNotNull(\Yii::$app->id); - $this->assertEquals(\Yii::$app->id, $cache->keyPrefix); - } - - public function testSet() - { - $cache = $this->getCacheInstance(); - - $this->assertTrue($cache->set('string_test', 'string_test')); - $this->assertTrue($cache->set('number_test', 42)); - $this->assertTrue($cache->set('array_test', array('array_test' => 'array_test'))); - } - - public function testGet() - { - $cache = $this->prepare(); - - $this->assertEquals('string_test', $cache->get('string_test')); - - $this->assertEquals(42, $cache->get('number_test')); - - $array = $cache->get('array_test'); - $this->assertArrayHasKey('array_test', $array); - $this->assertEquals('array_test', $array['array_test']); - } - - public function testArrayAccess() - { - $cache = $this->getCacheInstance(); - - $cache['arrayaccess_test'] = new \stdClass(); - $this->assertInstanceOf('stdClass', $cache['arrayaccess_test']); - } - - public function testGetNonExistent() - { - $cache = $this->getCacheInstance(); - - $this->assertFalse($cache->get('non_existent_key')); - } - - public function testStoreSpecialValues() - { - $cache = $this->getCacheInstance(); - - $this->assertTrue($cache->set('null_value', null)); - $this->assertNull($cache->get('null_value')); - - $this->assertTrue($cache->set('bool_value', true)); - $this->assertTrue($cache->get('bool_value')); - } - - public function testMget() - { - $cache = $this->prepare(); - - $this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test'))); - // ensure that order does not matter - $this->assertEquals(array('number_test' => 42, 'string_test' => 'string_test'), $cache->mget(array('number_test', 'string_test'))); - $this->assertEquals(array('number_test' => 42, 'non_existent_key' => null), $cache->mget(array('number_test', 'non_existent_key'))); - } - - public function testExpire() - { - $cache = $this->getCacheInstance(); - - $this->assertTrue($cache->set('expire_test', 'expire_test', 2)); - sleep(1); - $this->assertEquals('expire_test', $cache->get('expire_test')); - // wait a bit more than 2 sec to avoid random test failure - usleep(2500000); - $this->assertFalse($cache->get('expire_test')); - } - - public function testAdd() - { - $cache = $this->prepare(); - - // should not change existing keys - $this->assertFalse($cache->add('number_test', 13)); - $this->assertEquals(42, $cache->get('number_test')); - - // should store data if it's not there yet - $this->assertFalse($cache->get('add_test')); - $this->assertTrue($cache->add('add_test', 13)); - $this->assertEquals(13, $cache->get('add_test')); - } - - public function testDelete() - { - $cache = $this->prepare(); - - $this->assertNotNull($cache->get('number_test')); - $this->assertTrue($cache->delete('number_test')); - $this->assertFalse($cache->get('number_test')); - } - - public function testFlush() - { - $cache = $this->prepare(); - $this->assertTrue($cache->flush()); - $this->assertFalse($cache->get('number_test')); - } -} diff --git a/tests/unit/framework/caching/CacheTestCase.php b/tests/unit/framework/caching/CacheTestCase.php new file mode 100644 index 0000000..5808fa0 --- /dev/null +++ b/tests/unit/framework/caching/CacheTestCase.php @@ -0,0 +1,170 @@ +mockApplication(); + } + + protected function tearDown() + { + static::$time = null; + } + + /** + * @return Cache + */ + public function prepare() + { + $cache = $this->getCacheInstance(); + + $cache->flush(); + $cache->set('string_test', 'string_test'); + $cache->set('number_test', 42); + $cache->set('array_test', array('array_test' => 'array_test')); + $cache['arrayaccess_test'] = new \stdClass(); + + return $cache; + } + + /** + * default value of cache prefix is application id + */ + public function testKeyPrefix() + { + $cache = $this->getCacheInstance(); + $this->assertNotNull(\Yii::$app->id); + $this->assertEquals(\Yii::$app->id, $cache->keyPrefix); + } + + public function testSet() + { + $cache = $this->getCacheInstance(); + + $this->assertTrue($cache->set('string_test', 'string_test')); + $this->assertTrue($cache->set('number_test', 42)); + $this->assertTrue($cache->set('array_test', array('array_test' => 'array_test'))); + } + + public function testGet() + { + $cache = $this->prepare(); + + $this->assertEquals('string_test', $cache->get('string_test')); + + $this->assertEquals(42, $cache->get('number_test')); + + $array = $cache->get('array_test'); + $this->assertArrayHasKey('array_test', $array); + $this->assertEquals('array_test', $array['array_test']); + } + + public function testArrayAccess() + { + $cache = $this->getCacheInstance(); + + $cache['arrayaccess_test'] = new \stdClass(); + $this->assertInstanceOf('stdClass', $cache['arrayaccess_test']); + } + + public function testGetNonExistent() + { + $cache = $this->getCacheInstance(); + + $this->assertFalse($cache->get('non_existent_key')); + } + + public function testStoreSpecialValues() + { + $cache = $this->getCacheInstance(); + + $this->assertTrue($cache->set('null_value', null)); + $this->assertNull($cache->get('null_value')); + + $this->assertTrue($cache->set('bool_value', true)); + $this->assertTrue($cache->get('bool_value')); + } + + public function testMget() + { + $cache = $this->prepare(); + + $this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test'))); + // ensure that order does not matter + $this->assertEquals(array('number_test' => 42, 'string_test' => 'string_test'), $cache->mget(array('number_test', 'string_test'))); + $this->assertEquals(array('number_test' => 42, 'non_existent_key' => null), $cache->mget(array('number_test', 'non_existent_key'))); + } + + public function testExpire() + { + $cache = $this->getCacheInstance(); + + $this->assertTrue($cache->set('expire_test', 'expire_test', 2)); + sleep(1); + $this->assertEquals('expire_test', $cache->get('expire_test')); + // wait a bit more than 2 sec to avoid random test failure + usleep(2500000); + $this->assertFalse($cache->get('expire_test')); + } + + public function testAdd() + { + $cache = $this->prepare(); + + // should not change existing keys + $this->assertFalse($cache->add('number_test', 13)); + $this->assertEquals(42, $cache->get('number_test')); + + // should store data if it's not there yet + $this->assertFalse($cache->get('add_test')); + $this->assertTrue($cache->add('add_test', 13)); + $this->assertEquals(13, $cache->get('add_test')); + } + + public function testDelete() + { + $cache = $this->prepare(); + + $this->assertNotNull($cache->get('number_test')); + $this->assertTrue($cache->delete('number_test')); + $this->assertFalse($cache->get('number_test')); + } + + public function testFlush() + { + $cache = $this->prepare(); + $this->assertTrue($cache->flush()); + $this->assertFalse($cache->get('number_test')); + } +} diff --git a/tests/unit/framework/caching/DbCacheTest.php b/tests/unit/framework/caching/DbCacheTest.php index f5bbba5..3a26595 100644 --- a/tests/unit/framework/caching/DbCacheTest.php +++ b/tests/unit/framework/caching/DbCacheTest.php @@ -3,12 +3,11 @@ namespace yiiunit\framework\caching; use yii\caching\DbCache; -use yiiunit\TestCase; /** * Class for testing file cache backend */ -class DbCacheTest extends CacheTest +class DbCacheTest extends CacheTestCase { private $_cacheInstance; private $_connection; diff --git a/tests/unit/framework/caching/FileCacheTest.php b/tests/unit/framework/caching/FileCacheTest.php index b3ac8b7..62f8637 100644 --- a/tests/unit/framework/caching/FileCacheTest.php +++ b/tests/unit/framework/caching/FileCacheTest.php @@ -1,12 +1,11 @@ getParam('databases'); + $this->database = $databases[$this->driverName]; + $pdo_database = 'pdo_'.$this->driverName; + + if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) { + $this->markTestSkipped('pdo and pdo_'.$pdo_database.' extension are required.'); + } + } + + /** + * @param bool $reset whether to clean up the test database + * @param bool $open whether to open and populate test database + * @return \yii\db\Connection + */ + public function getConnection($reset = true, $open = true) + { + if (!$reset && $this->db) { + return $this->db; + } + $db = new \yii\db\Connection; + $db->dsn = $this->database['dsn']; + if (isset($this->database['username'])) { + $db->username = $this->database['username']; + $db->password = $this->database['password']; + } + if ($open) { + $db->open(); + $lines = explode(';', file_get_contents($this->database['fixture'])); + foreach ($lines as $line) { + if (trim($line) !== '') { + $db->pdo->exec($line); + } + } + } + $this->db = $db; + return $db; + } +} diff --git a/tests/unit/framework/db/QueryTest.php b/tests/unit/framework/db/QueryTest.php index 9486acb..8362906 100644 --- a/tests/unit/framework/db/QueryTest.php +++ b/tests/unit/framework/db/QueryTest.php @@ -7,7 +7,7 @@ use yii\db\Command; use yii\db\Query; use yii\db\DataReader; -class QueryTest extends \yiiunit\DatabaseTestCase +class QueryTest extends DatabaseTestCase { function testSelect() { diff --git a/tests/unit/framework/db/mssql/MssqlActiveRecordTest.php b/tests/unit/framework/db/mssql/MssqlActiveRecordTest.php index 2cf0b01..76c30b9 100644 --- a/tests/unit/framework/db/mssql/MssqlActiveRecordTest.php +++ b/tests/unit/framework/db/mssql/MssqlActiveRecordTest.php @@ -2,7 +2,9 @@ namespace yiiunit\framework\db\mssql; -class MssqlActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest +use yiiunit\framework\db\ActiveRecordTest; + +class MssqlActiveRecordTest extends ActiveRecordTest { protected function setUp() { diff --git a/tests/unit/framework/db/mssql/MssqlCommandTest.php b/tests/unit/framework/db/mssql/MssqlCommandTest.php index 11d7565..f3c66c1 100644 --- a/tests/unit/framework/db/mssql/MssqlCommandTest.php +++ b/tests/unit/framework/db/mssql/MssqlCommandTest.php @@ -2,7 +2,9 @@ namespace yiiunit\framework\db\mssql; -class MssqlCommandTest extends \yiiunit\framework\db\CommandTest +use yiiunit\framework\db\CommandTest; + +class MssqlCommandTest extends CommandTest { public function setUp() { diff --git a/tests/unit/framework/db/mssql/MssqlConnectionTest.php b/tests/unit/framework/db/mssql/MssqlConnectionTest.php index 870af0a..42ac380 100644 --- a/tests/unit/framework/db/mssql/MssqlConnectionTest.php +++ b/tests/unit/framework/db/mssql/MssqlConnectionTest.php @@ -2,7 +2,9 @@ namespace yiiunit\framework\db\mssql; -class MssqlConnectionTest extends \yiiunit\framework\db\ConnectionTest +use yiiunit\framework\db\ConnectionTest; + +class MssqlConnectionTest extends ConnectionTest { public function setUp() { diff --git a/tests/unit/framework/db/mssql/MssqlQueryTest.php b/tests/unit/framework/db/mssql/MssqlQueryTest.php index 17d6681..08af19d 100644 --- a/tests/unit/framework/db/mssql/MssqlQueryTest.php +++ b/tests/unit/framework/db/mssql/MssqlQueryTest.php @@ -2,7 +2,9 @@ namespace yiiunit\framework\db\mssql; -class MssqlQueryTest extends \yiiunit\framework\db\QueryTest +use yiiunit\framework\db\QueryTest; + +class MssqlQueryTest extends QueryTest { public function setUp() { diff --git a/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php b/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php index 4779f85..573108e 100644 --- a/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php +++ b/tests/unit/framework/db/sqlite/SqliteActiveRecordTest.php @@ -1,8 +1,9 @@ auth->createItem($name, $type, $description, $bizRule, $data); - $this->assertTrue($item instanceof Item); - $this->assertEquals($item->type, $type); - $this->assertEquals($item->name, $name); - $this->assertEquals($item->description, $description); - $this->assertEquals($item->bizRule, $bizRule); - $this->assertEquals($item->data, $data); - - // test shortcut - $name2 = 'createUser'; - $item2 = $this->auth->createRole($name2, $description, $bizRule, $data); - $this->assertEquals($item2->type, Item::TYPE_ROLE); - - // test adding an item with the same name - $this->setExpectedException('\yii\base\Exception'); - $this->auth->createItem($name, $type, $description, $bizRule, $data); - } - - public function testGetItem() - { - $this->assertTrue($this->auth->getItem('readPost') instanceof Item); - $this->assertTrue($this->auth->getItem('reader') instanceof Item); - $this->assertNull($this->auth->getItem('unknown')); - } - - public function testRemoveAuthItem() - { - $this->assertTrue($this->auth->getItem('updatePost') instanceof Item); - $this->assertTrue($this->auth->removeItem('updatePost')); - $this->assertNull($this->auth->getItem('updatePost')); - $this->assertFalse($this->auth->removeItem('updatePost')); - } - - public function testChangeItemName() - { - $item = $this->auth->getItem('readPost'); - $this->assertTrue($item instanceof Item); - $this->assertTrue($this->auth->hasItemChild('reader', 'readPost')); - $item->name = 'readPost2'; - $item->save(); - $this->assertNull($this->auth->getItem('readPost')); - $this->assertEquals($this->auth->getItem('readPost2'), $item); - $this->assertFalse($this->auth->hasItemChild('reader', 'readPost')); - $this->assertTrue($this->auth->hasItemChild('reader', 'readPost2')); - } - - public function testAddItemChild() - { - $this->auth->addItemChild('createPost', 'updatePost'); - - // test adding upper level item to lower one - $this->setExpectedException('\yii\base\Exception'); - $this->auth->addItemChild('readPost', 'reader'); - } - - public function testAddItemChild2() - { - // test adding inexistent items - $this->setExpectedException('\yii\base\Exception'); - $this->assertFalse($this->auth->addItemChild('createPost2', 'updatePost')); - } - - public function testRemoveItemChild() - { - $this->assertTrue($this->auth->hasItemChild('reader', 'readPost')); - $this->assertTrue($this->auth->removeItemChild('reader', 'readPost')); - $this->assertFalse($this->auth->hasItemChild('reader', 'readPost')); - $this->assertFalse($this->auth->removeItemChild('reader', 'readPost')); - } - - public function testGetItemChildren() - { - $this->assertEquals(array(), $this->auth->getItemChildren('readPost')); - $children = $this->auth->getItemChildren('author'); - $this->assertEquals(3, count($children)); - $this->assertTrue(reset($children) instanceof Item); - } - - public function testAssign() - { - $auth = $this->auth->assign('new user', 'createPost', 'rule', 'data'); - $this->assertTrue($auth instanceof Assignment); - $this->assertEquals($auth->userId, 'new user'); - $this->assertEquals($auth->itemName, 'createPost'); - $this->assertEquals($auth->bizRule, 'rule'); - $this->assertEquals($auth->data, 'data'); - - $this->setExpectedException('\yii\base\Exception'); - $this->auth->assign('new user', 'createPost2', 'rule', 'data'); - } - - public function testRevoke() - { - $this->assertTrue($this->auth->isAssigned('author B', 'author')); - $auth = $this->auth->getAssignment('author B', 'author'); - $this->assertTrue($auth instanceof Assignment); - $this->assertTrue($this->auth->revoke('author B', 'author')); - $this->assertFalse($this->auth->isAssigned('author B', 'author')); - $this->assertFalse($this->auth->revoke('author B', 'author')); - } - - public function testGetAssignments() - { - $this->auth->assign('author B', 'deletePost'); - $auths = $this->auth->getAssignments('author B'); - $this->assertEquals(2, count($auths)); - $this->assertTrue(reset($auths) instanceof Assignment); - } - - public function testGetItems() - { - $this->assertEquals(count($this->auth->getRoles()), 4); - $this->assertEquals(count($this->auth->getOperations()), 4); - $this->assertEquals(count($this->auth->getTasks()), 1); - $this->assertEquals(count($this->auth->getItems()), 9); - - $this->assertEquals(count($this->auth->getItems('author B', null)), 1); - $this->assertEquals(count($this->auth->getItems('author C', null)), 0); - $this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_ROLE)), 1); - $this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_OPERATION)), 0); - } - - public function testClearAll() - { - $this->auth->clearAll(); - $this->assertEquals(count($this->auth->getRoles()), 0); - $this->assertEquals(count($this->auth->getOperations()), 0); - $this->assertEquals(count($this->auth->getTasks()), 0); - $this->assertEquals(count($this->auth->getItems()), 0); - $this->assertEquals(count($this->auth->getAssignments('author B')), 0); - } - - public function testClearAssignments() - { - $this->auth->clearAssignments(); - $this->assertEquals(count($this->auth->getAssignments('author B')), 0); - } - - public function testDetectLoop() - { - $this->setExpectedException('\yii\base\Exception'); - $this->auth->addItemChild('readPost', 'readPost'); - } - - public function testExecuteBizRule() - { - $this->assertTrue($this->auth->executeBizRule(null, array(), null)); - $this->assertTrue($this->auth->executeBizRule('return 1 == true;', array(), null)); - $this->assertTrue($this->auth->executeBizRule('return $params[0] == $params[1];', array(1, '1'), null)); - $this->assertFalse($this->auth->executeBizRule('invalid;', array(), null)); - } - - public function testCheckAccess() - { - $results = array( - 'reader A' => array( - 'createPost' => false, - 'readPost' => true, - 'updatePost' => false, - 'updateOwnPost' => false, - 'deletePost' => false, - ), - 'author B' => array( - 'createPost' => true, - 'readPost' => true, - 'updatePost' => true, - 'updateOwnPost' => true, - 'deletePost' => false, - ), - 'editor C' => array( - 'createPost' => false, - 'readPost' => true, - 'updatePost' => true, - 'updateOwnPost' => false, - 'deletePost' => false, - ), - 'admin D' => array( - 'createPost' => true, - 'readPost' => true, - 'updatePost' => true, - 'updateOwnPost' => false, - 'deletePost' => true, - ), - ); - - $params = array('authorID' => 'author B'); - - foreach (array('reader A', 'author B', 'editor C', 'admin D') as $user) { - $params['userID'] = $user; - foreach (array('createPost', 'readPost', 'updatePost', 'updateOwnPost', 'deletePost') as $operation) { - $result = $this->auth->checkAccess($user, $operation, $params); - $this->assertEquals($results[$user][$operation], $result); - } - } - } - - protected function prepareData() - { - $this->auth->createOperation('createPost', 'create a post'); - $this->auth->createOperation('readPost', 'read a post'); - $this->auth->createOperation('updatePost', 'update a post'); - $this->auth->createOperation('deletePost', 'delete a post'); - - $task = $this->auth->createTask('updateOwnPost', 'update a post by author himself', 'return $params["authorID"] == $params["userID"];'); - $task->addChild('updatePost'); - - $role = $this->auth->createRole('reader'); - $role->addChild('readPost'); - - $role = $this->auth->createRole('author'); - $role->addChild('reader'); - $role->addChild('createPost'); - $role->addChild('updateOwnPost'); - - $role = $this->auth->createRole('editor'); - $role->addChild('reader'); - $role->addChild('updatePost'); - - $role = $this->auth->createRole('admin'); - $role->addChild('editor'); - $role->addChild('author'); - $role->addChild('deletePost'); - - $this->auth->assign('reader A', 'reader'); - $this->auth->assign('author B', 'author'); - $this->auth->assign('editor C', 'editor'); - $this->auth->assign('admin D', 'admin'); - } -} diff --git a/tests/unit/framework/rbac/ManagerTestCase.php b/tests/unit/framework/rbac/ManagerTestCase.php new file mode 100644 index 0000000..7cb4941 --- /dev/null +++ b/tests/unit/framework/rbac/ManagerTestCase.php @@ -0,0 +1,249 @@ +auth->createItem($name, $type, $description, $bizRule, $data); + $this->assertTrue($item instanceof Item); + $this->assertEquals($item->type, $type); + $this->assertEquals($item->name, $name); + $this->assertEquals($item->description, $description); + $this->assertEquals($item->bizRule, $bizRule); + $this->assertEquals($item->data, $data); + + // test shortcut + $name2 = 'createUser'; + $item2 = $this->auth->createRole($name2, $description, $bizRule, $data); + $this->assertEquals($item2->type, Item::TYPE_ROLE); + + // test adding an item with the same name + $this->setExpectedException('\yii\base\Exception'); + $this->auth->createItem($name, $type, $description, $bizRule, $data); + } + + public function testGetItem() + { + $this->assertTrue($this->auth->getItem('readPost') instanceof Item); + $this->assertTrue($this->auth->getItem('reader') instanceof Item); + $this->assertNull($this->auth->getItem('unknown')); + } + + public function testRemoveAuthItem() + { + $this->assertTrue($this->auth->getItem('updatePost') instanceof Item); + $this->assertTrue($this->auth->removeItem('updatePost')); + $this->assertNull($this->auth->getItem('updatePost')); + $this->assertFalse($this->auth->removeItem('updatePost')); + } + + public function testChangeItemName() + { + $item = $this->auth->getItem('readPost'); + $this->assertTrue($item instanceof Item); + $this->assertTrue($this->auth->hasItemChild('reader', 'readPost')); + $item->name = 'readPost2'; + $item->save(); + $this->assertNull($this->auth->getItem('readPost')); + $this->assertEquals($this->auth->getItem('readPost2'), $item); + $this->assertFalse($this->auth->hasItemChild('reader', 'readPost')); + $this->assertTrue($this->auth->hasItemChild('reader', 'readPost2')); + } + + public function testAddItemChild() + { + $this->auth->addItemChild('createPost', 'updatePost'); + + // test adding upper level item to lower one + $this->setExpectedException('\yii\base\Exception'); + $this->auth->addItemChild('readPost', 'reader'); + } + + public function testAddItemChild2() + { + // test adding inexistent items + $this->setExpectedException('\yii\base\Exception'); + $this->assertFalse($this->auth->addItemChild('createPost2', 'updatePost')); + } + + public function testRemoveItemChild() + { + $this->assertTrue($this->auth->hasItemChild('reader', 'readPost')); + $this->assertTrue($this->auth->removeItemChild('reader', 'readPost')); + $this->assertFalse($this->auth->hasItemChild('reader', 'readPost')); + $this->assertFalse($this->auth->removeItemChild('reader', 'readPost')); + } + + public function testGetItemChildren() + { + $this->assertEquals(array(), $this->auth->getItemChildren('readPost')); + $children = $this->auth->getItemChildren('author'); + $this->assertEquals(3, count($children)); + $this->assertTrue(reset($children) instanceof Item); + } + + public function testAssign() + { + $auth = $this->auth->assign('new user', 'createPost', 'rule', 'data'); + $this->assertTrue($auth instanceof Assignment); + $this->assertEquals($auth->userId, 'new user'); + $this->assertEquals($auth->itemName, 'createPost'); + $this->assertEquals($auth->bizRule, 'rule'); + $this->assertEquals($auth->data, 'data'); + + $this->setExpectedException('\yii\base\Exception'); + $this->auth->assign('new user', 'createPost2', 'rule', 'data'); + } + + public function testRevoke() + { + $this->assertTrue($this->auth->isAssigned('author B', 'author')); + $auth = $this->auth->getAssignment('author B', 'author'); + $this->assertTrue($auth instanceof Assignment); + $this->assertTrue($this->auth->revoke('author B', 'author')); + $this->assertFalse($this->auth->isAssigned('author B', 'author')); + $this->assertFalse($this->auth->revoke('author B', 'author')); + } + + public function testGetAssignments() + { + $this->auth->assign('author B', 'deletePost'); + $auths = $this->auth->getAssignments('author B'); + $this->assertEquals(2, count($auths)); + $this->assertTrue(reset($auths) instanceof Assignment); + } + + public function testGetItems() + { + $this->assertEquals(count($this->auth->getRoles()), 4); + $this->assertEquals(count($this->auth->getOperations()), 4); + $this->assertEquals(count($this->auth->getTasks()), 1); + $this->assertEquals(count($this->auth->getItems()), 9); + + $this->assertEquals(count($this->auth->getItems('author B', null)), 1); + $this->assertEquals(count($this->auth->getItems('author C', null)), 0); + $this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_ROLE)), 1); + $this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_OPERATION)), 0); + } + + public function testClearAll() + { + $this->auth->clearAll(); + $this->assertEquals(count($this->auth->getRoles()), 0); + $this->assertEquals(count($this->auth->getOperations()), 0); + $this->assertEquals(count($this->auth->getTasks()), 0); + $this->assertEquals(count($this->auth->getItems()), 0); + $this->assertEquals(count($this->auth->getAssignments('author B')), 0); + } + + public function testClearAssignments() + { + $this->auth->clearAssignments(); + $this->assertEquals(count($this->auth->getAssignments('author B')), 0); + } + + public function testDetectLoop() + { + $this->setExpectedException('\yii\base\Exception'); + $this->auth->addItemChild('readPost', 'readPost'); + } + + public function testExecuteBizRule() + { + $this->assertTrue($this->auth->executeBizRule(null, array(), null)); + $this->assertTrue($this->auth->executeBizRule('return 1 == true;', array(), null)); + $this->assertTrue($this->auth->executeBizRule('return $params[0] == $params[1];', array(1, '1'), null)); + $this->assertFalse($this->auth->executeBizRule('invalid;', array(), null)); + } + + public function testCheckAccess() + { + $results = array( + 'reader A' => array( + 'createPost' => false, + 'readPost' => true, + 'updatePost' => false, + 'updateOwnPost' => false, + 'deletePost' => false, + ), + 'author B' => array( + 'createPost' => true, + 'readPost' => true, + 'updatePost' => true, + 'updateOwnPost' => true, + 'deletePost' => false, + ), + 'editor C' => array( + 'createPost' => false, + 'readPost' => true, + 'updatePost' => true, + 'updateOwnPost' => false, + 'deletePost' => false, + ), + 'admin D' => array( + 'createPost' => true, + 'readPost' => true, + 'updatePost' => true, + 'updateOwnPost' => false, + 'deletePost' => true, + ), + ); + + $params = array('authorID' => 'author B'); + + foreach (array('reader A', 'author B', 'editor C', 'admin D') as $user) { + $params['userID'] = $user; + foreach (array('createPost', 'readPost', 'updatePost', 'updateOwnPost', 'deletePost') as $operation) { + $result = $this->auth->checkAccess($user, $operation, $params); + $this->assertEquals($results[$user][$operation], $result); + } + } + } + + protected function prepareData() + { + $this->auth->createOperation('createPost', 'create a post'); + $this->auth->createOperation('readPost', 'read a post'); + $this->auth->createOperation('updatePost', 'update a post'); + $this->auth->createOperation('deletePost', 'delete a post'); + + $task = $this->auth->createTask('updateOwnPost', 'update a post by author himself', 'return $params["authorID"] == $params["userID"];'); + $task->addChild('updatePost'); + + $role = $this->auth->createRole('reader'); + $role->addChild('readPost'); + + $role = $this->auth->createRole('author'); + $role->addChild('reader'); + $role->addChild('createPost'); + $role->addChild('updateOwnPost'); + + $role = $this->auth->createRole('editor'); + $role->addChild('reader'); + $role->addChild('updatePost'); + + $role = $this->auth->createRole('admin'); + $role->addChild('editor'); + $role->addChild('author'); + $role->addChild('deletePost'); + + $this->auth->assign('reader A', 'reader'); + $this->auth->assign('author B', 'author'); + $this->auth->assign('editor C', 'editor'); + $this->auth->assign('admin D', 'admin'); + } +} diff --git a/tests/unit/framework/rbac/PhpManagerTest.php b/tests/unit/framework/rbac/PhpManagerTest.php index d5ab41b..b3b7c4f 100644 --- a/tests/unit/framework/rbac/PhpManagerTest.php +++ b/tests/unit/framework/rbac/PhpManagerTest.php @@ -5,9 +5,7 @@ namespace yiiunit\framework\rbac; use Yii; use yii\rbac\PhpManager; -//require_once(__DIR__ . '/ManagerTestBase.php'); - -class PhpManagerTest extends ManagerTestBase +class PhpManagerTest extends ManagerTestCase { protected function setUp() { diff --git a/tests/unit/framework/web/UrlManagerTest.php b/tests/unit/framework/web/UrlManagerTest.php index 553b163..0f08790 100644 --- a/tests/unit/framework/web/UrlManagerTest.php +++ b/tests/unit/framework/web/UrlManagerTest.php @@ -3,8 +3,9 @@ namespace yiiunit\framework\web; use yii\web\Request; use yii\web\UrlManager; +use yiiunit\TestCase; -class UrlManagerTest extends \yiiunit\TestCase +class UrlManagerTest extends TestCase { public function testCreateUrl() { diff --git a/tests/unit/framework/web/UrlRuleTest.php b/tests/unit/framework/web/UrlRuleTest.php index e0761ba..f697805 100644 --- a/tests/unit/framework/web/UrlRuleTest.php +++ b/tests/unit/framework/web/UrlRuleTest.php @@ -5,8 +5,9 @@ namespace yiiunit\framework\web; use yii\web\UrlManager; use yii\web\UrlRule; use yii\web\Request; +use yiiunit\TestCase; -class UrlRuleTest extends \yiiunit\TestCase +class UrlRuleTest extends TestCase { public function testCreateUrl() {