Skip to content

Commit

Permalink
Fixed RBAC databases tests
Browse files Browse the repository at this point in the history
tests were reusing static DB connection instance resulting in all tests
to be run against MySQL only!

PgSQL and Sqlite tests are now failing because of the issue reported in yiisoft#13501.
  • Loading branch information
cebe committed May 31, 2017
1 parent fa4d762 commit 2feb094
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
49 changes: 28 additions & 21 deletions tests/framework/rbac/DbManagerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class DbManagerTestCase extends ManagerTestCase
/**
* @var Connection
*/
protected static $db;
protected $db;

protected static function runConsoleAction($route, $params = [])
{
Expand All @@ -34,7 +34,7 @@ protected static function runConsoleAction($route, $params = [])
'migrate' => EchoMigrateController::className(),
],
'components' => [
'db' => static::getConnection(),
'db' => static::createConnection(),
'authManager' => '\yii\rbac\DbManager',
],
]);
Expand Down Expand Up @@ -67,9 +67,6 @@ public static function setUpBeforeClass()
public static function tearDownAfterClass()
{
static::runConsoleAction('migrate/down', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
if (static::$db) {
static::$db->close();
}
Yii::$app = null;
parent::tearDownAfterClass();
}
Expand All @@ -84,6 +81,10 @@ protected function tearDown()
{
parent::tearDown();
$this->auth->removeAll();
if ($this->db && static::$driverName !== 'sqlite') {
$this->db->close();
}
$this->db = null;
}

/**
Expand All @@ -92,24 +93,30 @@ protected function tearDown()
* @throws \yii\base\InvalidConfigException
* @return \yii\db\Connection
*/
public static function getConnection()
public function getConnection()
{
if ($this->db === null) {
$this->db = static::createConnection();
}
return $this->db;
}

public static function createConnection()
{
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;
$db = new Connection;
$db->dsn = static::$database['dsn'];
echo "\n" . $db->dsn . "\n";
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();
}
return static::$db;
return $db;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/framework/rbac/PgSQLManagerCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace yiiunit\framework\rbac;

use yii\caching\FileCache;
use yii\rbac\DbManager;

/**
* PgSQLManagerTest
* @group db
* @group rbac
* @group pgsql
*/
class PgSQLManagerCacheTest extends DbManagerTestCase
{
protected static $driverName = 'pgsql';

/**
* @return \yii\rbac\ManagerInterface
*/
protected function createManager()
{
return new DbManager([
'db' => $this->getConnection(),
'cache' => new FileCache(['cachePath' => '@yiiunit/runtime/cache']),
'defaultRoles' => ['myDefaultRole']
]);
}

}
11 changes: 11 additions & 0 deletions tests/framework/rbac/SqliteManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@
class SqliteManagerTest extends DbManagerTestCase
{
protected static $driverName = 'sqlite';

protected static $sqliteDb;

public static function createConnection()
{
// sqlite db is in memory so it can not be reused
if (static::$sqliteDb === null) {
static::$sqliteDb = parent::createConnection();
}
return static::$sqliteDb;
}
}

0 comments on commit 2feb094

Please sign in to comment.