Browse Source

RBAC migration is now aware of custom table names, speeded up RBAC tests

tags/2.0.0-rc
Alexander Makarov 11 years ago
parent
commit
905e39ede9
  1. 53
      framework/rbac/migrations/m140506_102106_rbac_init.php
  2. 2
      tests/unit/TestCase.php
  3. 4
      tests/unit/extensions/elasticsearch/ElasticSearchTestCase.php
  4. 2
      tests/unit/extensions/mongodb/MongoDbTestCase.php
  5. 2
      tests/unit/extensions/redis/RedisCacheTest.php
  6. 4
      tests/unit/extensions/redis/RedisTestCase.php
  7. 2
      tests/unit/extensions/sphinx/SphinxTestCase.php
  8. 2
      tests/unit/framework/caching/DbCacheTest.php
  9. 2
      tests/unit/framework/db/DatabaseTestCase.php
  10. 70
      tests/unit/framework/rbac/DbManagerTestCase.php
  11. 2
      tests/unit/framework/rbac/PgSQLManagerTest.php
  12. 2
      tests/unit/framework/rbac/SqliteManagerTest.php

53
framework/rbac/migrations/m140506_102106_rbac_init.php

@ -1,25 +1,42 @@
<?php <?php
use yii\base\InvalidConfigException;
use yii\db\Schema; use yii\db\Schema;
use yii\rbac\DbManager;
class m140506_102106_rbac_init extends \yii\db\Migration class m140506_102106_rbac_init extends \yii\db\Migration
{ {
/**
* @throws yii\base\InvalidConfigException
* @return DbManager
*/
protected function getAuthManager()
{
$authManager = Yii::$app->getAuthManager();
if (!$authManager instanceof DbManager) {
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
}
return $authManager;
}
public function up() public function up()
{ {
$authManager = $this->getAuthManager();
$tableOptions = null; $tableOptions = null;
if ($this->db->driverName === 'mysql') { if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
} }
$this->createTable('{{%auth_rule}}', [ $this->createTable($authManager->ruleTable, [
'name' => Schema::TYPE_STRING . '(64) NOT NULL', 'name' => Schema::TYPE_STRING . '(64) NOT NULL',
'data' => Schema::TYPE_TEXT, 'data' => Schema::TYPE_TEXT,
'created_at' => Schema::TYPE_INTEGER, 'created_at' => Schema::TYPE_INTEGER,
'updated_at' => Schema::TYPE_INTEGER, 'updated_at' => Schema::TYPE_INTEGER,
], $tableOptions); ], $tableOptions);
$this->addPrimaryKey('pk-auth_rule', '{{%auth_rule}}', 'name'); $this->addPrimaryKey('pk-auth_rule', $authManager->ruleTable, 'name');
$this->createTable('{{%auth_item}}', [ $this->createTable($authManager->itemTable, [
'name' => Schema::TYPE_STRING . '(64) NOT NULL', 'name' => Schema::TYPE_STRING . '(64) NOT NULL',
'type' => Schema::TYPE_INTEGER . ' NOT NULL', 'type' => Schema::TYPE_INTEGER . ' NOT NULL',
'description' => Schema::TYPE_TEXT, 'description' => Schema::TYPE_TEXT,
@ -28,32 +45,34 @@ class m140506_102106_rbac_init extends \yii\db\Migration
'created_at' => Schema::TYPE_INTEGER, 'created_at' => Schema::TYPE_INTEGER,
'updated_at' => Schema::TYPE_INTEGER, 'updated_at' => Schema::TYPE_INTEGER,
], $tableOptions); ], $tableOptions);
$this->addPrimaryKey('pk-auth_item', '{{%auth_item}}', 'name'); $this->addPrimaryKey('pk-auth_item', $authManager->itemTable, 'name');
$this->addForeignKey('fk-auth_item-rule_name', '{{%auth_item}}', 'rule_name', '{{%auth_rule}}', 'name', 'SET NULL', 'CASCADE'); $this->addForeignKey('fk-auth_item-rule_name', $authManager->itemTable, 'rule_name', $authManager->ruleTable, 'name', 'SET NULL', 'CASCADE');
$this->createIndex('idx-auth_item-type', '{{%auth_item}}', 'type'); $this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type');
$this->createTable('{{%auth_item_child}}', [ $this->createTable($authManager->itemChildTable, [
'parent' => Schema::TYPE_STRING . '(64) NOT NULL', 'parent' => Schema::TYPE_STRING . '(64) NOT NULL',
'child' => Schema::TYPE_STRING . '(64) NOT NULL', 'child' => Schema::TYPE_STRING . '(64) NOT NULL',
], $tableOptions); ], $tableOptions);
$this->addPrimaryKey('pk-auth_item_child', '{{%auth_item_child}}', ['parent', 'child']); $this->addPrimaryKey('pk-auth_item_child', $authManager->itemChildTable, ['parent', 'child']);
$this->addForeignKey('fk-auth_item_child-parent', '{{%auth_item_child}}', 'parent', '{{%auth_item}}', 'name', 'CASCADE', 'CASCADE'); $this->addForeignKey('fk-auth_item_child-parent', $authManager->itemChildTable, 'parent', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE');
$this->addForeignKey('fk-auth_item_child-child', '{{%auth_item_child}}', 'child', '{{%auth_item}}', 'name', 'CASCADE', 'CASCADE'); $this->addForeignKey('fk-auth_item_child-child', $authManager->itemChildTable, 'child', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE');
$this->createTable('{{%auth_assignment}}', [ $this->createTable($authManager->assignmentTable, [
'item_name' => Schema::TYPE_STRING . '(64) NOT NULL', 'item_name' => Schema::TYPE_STRING . '(64) NOT NULL',
'user_id' => Schema::TYPE_STRING . '(64) NOT NULL', 'user_id' => Schema::TYPE_STRING . '(64) NOT NULL',
'created_at' => Schema::TYPE_INTEGER, 'created_at' => Schema::TYPE_INTEGER,
], $tableOptions); ], $tableOptions);
$this->addPrimaryKey('pk-auth_assignment', '{{%auth_assignment}}', ['item_name', 'user_id']); $this->addPrimaryKey('pk-auth_assignment', $authManager->assignmentTable, ['item_name', 'user_id']);
$this->addForeignKey('fk-auth_assignment-item_name', '{{%auth_assignment}}', 'item_name', '{{%auth_item}}', 'name', 'CASCADE', 'CASCADE'); $this->addForeignKey('fk-auth_assignment-item_name', $authManager->assignmentTable, 'item_name', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE');
} }
public function down() public function down()
{ {
$this->dropTable('{{%auth_assignment}}'); $authManager = $this->getAuthManager();
$this->dropTable('{{%auth_item_child}}');
$this->dropTable('{{%auth_item}}'); $this->dropTable($authManager->assignmentTable);
$this->dropTable('{{%auth_rule}}'); $this->dropTable($authManager->itemChildTable);
$this->dropTable($authManager->itemTable);
$this->dropTable($authManager->ruleTable);
} }
} }

2
tests/unit/TestCase.php

@ -27,7 +27,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
* @param mixed $default default value to use when param is not set. * @param mixed $default default value to use when param is not set.
* @return mixed the value of the configuration param * @return mixed the value of the configuration param
*/ */
public function getParam($name, $default = null) public static function getParam($name, $default = null)
{ {
if (static::$params === null) { if (static::$params === null) {
static::$params = require(__DIR__ . '/data/config.php'); static::$params = require(__DIR__ . '/data/config.php');

4
tests/unit/extensions/elasticsearch/ElasticSearchTestCase.php

@ -17,7 +17,7 @@ class ElasticSearchTestCase extends TestCase
{ {
$this->mockApplication(); $this->mockApplication();
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : null; $params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : null;
if ($params === null || !isset($params['dsn'])) { if ($params === null || !isset($params['dsn'])) {
$this->markTestSkipped('No elasticsearch server connection configured.'); $this->markTestSkipped('No elasticsearch server connection configured.');
@ -40,7 +40,7 @@ class ElasticSearchTestCase extends TestCase
*/ */
public function getConnection($reset = true) public function getConnection($reset = true)
{ {
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : []; $params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : [];
$db = new Connection(); $db = new Connection();
if ($reset) { if ($reset) {

2
tests/unit/extensions/mongodb/MongoDbTestCase.php

@ -34,7 +34,7 @@ class MongoDbTestCase extends TestCase
if (!extension_loaded('mongo')) { if (!extension_loaded('mongo')) {
$this->markTestSkipped('mongo extension required.'); $this->markTestSkipped('mongo extension required.');
} }
$config = $this->getParam('mongodb'); $config = self::getParam('mongodb');
if (!empty($config)) { if (!empty($config)) {
$this->mongoDbConfig = $config; $this->mongoDbConfig = $config;
} }

2
tests/unit/extensions/redis/RedisCacheTest.php

@ -22,7 +22,7 @@ class RedisCacheTest extends CacheTestCase
*/ */
protected function getCacheInstance() protected function getCacheInstance()
{ {
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = isset($databases['redis']) ? $databases['redis'] : null; $params = isset($databases['redis']) ? $databases['redis'] : null;
if ($params === null) { if ($params === null) {
$this->markTestSkipped('No redis server connection configured.'); $this->markTestSkipped('No redis server connection configured.');

4
tests/unit/extensions/redis/RedisTestCase.php

@ -15,7 +15,7 @@ abstract class RedisTestCase extends TestCase
{ {
protected function setUp() protected function setUp()
{ {
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = isset($databases['redis']) ? $databases['redis'] : null; $params = isset($databases['redis']) ? $databases['redis'] : null;
if ($params === null) { if ($params === null) {
$this->markTestSkipped('No redis server connection configured.'); $this->markTestSkipped('No redis server connection configured.');
@ -36,7 +36,7 @@ abstract class RedisTestCase extends TestCase
*/ */
public function getConnection($reset = true) public function getConnection($reset = true)
{ {
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = isset($databases['redis']) ? $databases['redis'] : []; $params = isset($databases['redis']) ? $databases['redis'] : [];
$db = new Connection($params); $db = new Connection($params);
if ($reset) { if ($reset) {

2
tests/unit/extensions/sphinx/SphinxTestCase.php

@ -48,7 +48,7 @@ class SphinxTestCase extends TestCase
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extension are required.'); $this->markTestSkipped('pdo and pdo_mysql extension are required.');
} }
$config = $this->getParam('sphinx'); $config = self::getParam('sphinx');
if (!empty($config)) { if (!empty($config)) {
$this->sphinxConfig = $config['sphinx']; $this->sphinxConfig = $config['sphinx'];
$this->dbConfig = $config['db']; $this->dbConfig = $config['db'];

2
tests/unit/framework/caching/DbCacheTest.php

@ -40,7 +40,7 @@ class DbCacheTest extends CacheTestCase
public function getConnection($reset = true) public function getConnection($reset = true)
{ {
if ($this->_connection === null) { if ($this->_connection === null) {
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$params = $databases['mysql']; $params = $databases['mysql'];
$db = new \yii\db\Connection; $db = new \yii\db\Connection;
$db->dsn = $params['dsn']; $db->dsn = $params['dsn'];

2
tests/unit/framework/db/DatabaseTestCase.php

@ -16,7 +16,7 @@ abstract class DatabaseTestCase extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
$databases = $this->getParam('databases'); $databases = self::getParam('databases');
$this->database = $databases[$this->driverName]; $this->database = $databases[$this->driverName];
$pdo_database = 'pdo_'.$this->driverName; $pdo_database = 'pdo_'.$this->driverName;

70
tests/unit/framework/rbac/DbManagerTestCase.php

@ -11,21 +11,25 @@ use yii\rbac\DbManager;
*/ */
abstract class DbManagerTestCase extends ManagerTestCase abstract class DbManagerTestCase extends ManagerTestCase
{ {
protected $database; protected static $database;
protected $driverName = 'mysql'; protected static $driverName = 'mysql';
/** /**
* @var Connection * @var Connection
*/ */
protected $db; protected static $db;
protected function getMigrator() /**
* @return MigrateController
*/
protected static function getMigrator()
{ {
$app = new Application([ $app = new Application([
'id' => 'Migrator', 'id' => 'Migrator',
'basePath' => '@yiiunit', 'basePath' => '@yiiunit',
'components' => [ 'components' => [
'db' => $this->getConnection(), 'db' => static::getConnection(),
'authManager' => '\yii\rbac\DbManager',
], ],
]); ]);
@ -35,29 +39,41 @@ abstract class DbManagerTestCase extends ManagerTestCase
return $migrator; return $migrator;
} }
protected function setUp() public static function setUpBeforeClass()
{ {
parent::setUp(); parent::setUpBeforeClass();
$databases = $this->getParam('databases'); $databases = static::getParam('databases');
$this->database = $databases[$this->driverName]; static::$database = $databases[static::$driverName];
$pdo_database = 'pdo_'.$this->driverName; $pdo_database = 'pdo_' . static::$driverName;
if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) { if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) {
$this->markTestSkipped('pdo and '.$pdo_database.' extension are required.'); static::markTestSkipped('pdo and ' . $pdo_database . ' extension are required.');
} }
static::getMigrator()->run('up');
}
public static function tearDownAfterClass()
{
static::getMigrator()->run('down');
if (static::$db) {
static::$db->close();
}
\Yii::$app = null;
parent::tearDownAfterClass();
}
protected function setUp()
{
parent::setUp();
$this->auth = new DbManager(['db' => $this->getConnection()]); $this->auth = new DbManager(['db' => $this->getConnection()]);
$this->getMigrator()->run('up');
} }
protected function tearDown() protected function tearDown()
{ {
parent::tearDown(); parent::tearDown();
$this->getMigrator()->run('down'); $this->auth->removeAll();
if ($this->db) {
$this->db->close();
}
$this->destroyApplication();
} }
/** /**
@ -68,24 +84,24 @@ abstract class DbManagerTestCase extends ManagerTestCase
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
* @return \yii\db\Connection * @return \yii\db\Connection
*/ */
public function getConnection($reset = true, $open = true) public static function getConnection($reset = true, $open = true)
{ {
if (!$reset && $this->db) { if (!$reset && static::$db) {
return $this->db; return static::$db;
} }
$db = new Connection; $db = new Connection;
$db->dsn = $this->database['dsn']; $db->dsn = static::$database['dsn'];
if (isset($this->database['username'])) { if (isset(static::$database['username'])) {
$db->username = $this->database['username']; $db->username = static::$database['username'];
$db->password = $this->database['password']; $db->password = static::$database['password'];
} }
if (isset($this->database['attributes'])) { if (isset(static::$database['attributes'])) {
$db->attributes = $this->database['attributes']; $db->attributes = static::$database['attributes'];
} }
if ($open) { if ($open) {
$db->open(); $db->open();
} }
$this->db = $db; static::$db = $db;
return $db; return $db;
} }

2
tests/unit/framework/rbac/PgSQLManagerTest.php

@ -6,5 +6,5 @@ namespace yiiunit\framework\rbac;
*/ */
class PgSQLManagerTest extends DbManagerTestCase class PgSQLManagerTest extends DbManagerTestCase
{ {
protected $driverName = 'pgsql'; protected static $driverName = 'pgsql';
} }

2
tests/unit/framework/rbac/SqliteManagerTest.php

@ -6,5 +6,5 @@ namespace yiiunit\framework\rbac;
*/ */
class SqliteManagerTest extends DbManagerTestCase class SqliteManagerTest extends DbManagerTestCase
{ {
protected $driverName = 'sqlite'; protected static $driverName = 'sqlite';
} }

Loading…
Cancel
Save