You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.3 KiB
120 lines
3.3 KiB
11 years ago
|
<?php
|
||
|
namespace yiiunit\framework\rbac;
|
||
|
|
||
11 years ago
|
use Yii;
|
||
11 years ago
|
use yii\console\Application;
|
||
11 years ago
|
use yii\console\Controller;
|
||
11 years ago
|
use yii\db\Connection;
|
||
|
use yii\rbac\DbManager;
|
||
10 years ago
|
use yiiunit\framework\console\controllers\EchoMigrateController;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* DbManagerTestCase
|
||
|
*/
|
||
|
abstract class DbManagerTestCase extends ManagerTestCase
|
||
|
{
|
||
11 years ago
|
protected static $database;
|
||
|
protected static $driverName = 'mysql';
|
||
11 years ago
|
|
||
|
/**
|
||
|
* @var Connection
|
||
|
*/
|
||
11 years ago
|
protected static $db;
|
||
11 years ago
|
|
||
11 years ago
|
protected static function runConsoleAction($route, $params = [])
|
||
11 years ago
|
{
|
||
|
if (Yii::$app === null) {
|
||
|
new Application([
|
||
|
'id' => 'Migrator',
|
||
|
'basePath' => '@yiiunit',
|
||
10 years ago
|
'controllerMap' => [
|
||
|
'migrate' => EchoMigrateController::className(),
|
||
|
],
|
||
11 years ago
|
'components' => [
|
||
|
'db' => static::getConnection(),
|
||
|
'authManager' => '\yii\rbac\DbManager',
|
||
|
],
|
||
|
]);
|
||
|
}
|
||
11 years ago
|
|
||
|
ob_start();
|
||
10 years ago
|
$result = Yii::$app->runAction($route, $params);
|
||
10 years ago
|
echo "Result is " . $result;
|
||
11 years ago
|
if ($result !== Controller::EXIT_CODE_NORMAL) {
|
||
|
ob_end_flush();
|
||
|
} else {
|
||
|
ob_end_clean();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public static function setUpBeforeClass()
|
||
11 years ago
|
{
|
||
11 years ago
|
parent::setUpBeforeClass();
|
||
|
$databases = static::getParam('databases');
|
||
|
static::$database = $databases[static::$driverName];
|
||
|
$pdo_database = 'pdo_' . static::$driverName;
|
||
11 years ago
|
|
||
|
if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) {
|
||
11 years ago
|
static::markTestSkipped('pdo and ' . $pdo_database . ' extension are required.');
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
static::runConsoleAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
|
||
11 years ago
|
}
|
||
|
|
||
|
public static function tearDownAfterClass()
|
||
|
{
|
||
11 years ago
|
static::runConsoleAction('migrate/down', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
|
||
11 years ago
|
if (static::$db) {
|
||
|
static::$db->close();
|
||
|
}
|
||
11 years ago
|
Yii::$app = null;
|
||
11 years ago
|
parent::tearDownAfterClass();
|
||
|
}
|
||
|
|
||
|
protected function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
10 years ago
|
$this->auth = $this->createManager();
|
||
11 years ago
|
}
|
||
|
|
||
|
protected function tearDown()
|
||
|
{
|
||
|
parent::tearDown();
|
||
11 years ago
|
$this->auth->removeAll();
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws \yii\base\InvalidParamException
|
||
|
* @throws \yii\db\Exception
|
||
|
* @throws \yii\base\InvalidConfigException
|
||
|
* @return \yii\db\Connection
|
||
|
*/
|
||
11 years ago
|
public static function getConnection()
|
||
11 years ago
|
{
|
||
11 years ago
|
if (static::$db == null) {
|
||
|
$db = new Connection;
|
||
|
$db->dsn = static::$database['dsn'];
|
||
|
if (isset(static::$database['username'])) {
|
||
|
$db->username = static::$database['username'];
|
||
|
$db->password = static::$database['password'];
|
||
|
}
|
||
|
if (isset(static::$database['attributes'])) {
|
||
|
$db->attributes = static::$database['attributes'];
|
||
|
}
|
||
|
if (!$db->isActive) {
|
||
|
$db->open();
|
||
|
}
|
||
|
static::$db = $db;
|
||
11 years ago
|
}
|
||
11 years ago
|
return static::$db;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
/**
|
||
|
* @return \yii\rbac\ManagerInterface
|
||
|
*/
|
||
|
protected function createManager()
|
||
|
{
|
||
|
return new DbManager(['db' => $this->getConnection()]);
|
||
|
}
|
||
11 years ago
|
}
|