DbManagerTestCase.php 3.06 KB
Newer Older
1 2 3
<?php
namespace yiiunit\framework\rbac;

4
use Yii;
5
use yii\console\Application;
6
use yii\console\Controller;
7
use yii\console\controllers\MigrateController;
8 9 10 11 12 13 14 15
use yii\db\Connection;
use yii\rbac\DbManager;

/**
 * DbManagerTestCase
 */
abstract class DbManagerTestCase extends ManagerTestCase
{
16 17
    protected static $database;
    protected static $driverName = 'mysql';
18 19 20 21

    /**
     * @var Connection
     */
22
    protected static $db;
23

24
    protected static function runConsoleAction($route, $params = [])
25 26 27 28 29 30 31 32 33 34 35
    {
        if (Yii::$app === null) {
            new Application([
                'id' => 'Migrator',
                'basePath' => '@yiiunit',
                'components' => [
                    'db' => static::getConnection(),
                    'authManager' => '\yii\rbac\DbManager',
                ],
            ]);
        }
36 37 38 39 40 41 42 43 44

        ob_start();
        $result = Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
        echo "Result is ".$result;
        if ($result !== Controller::EXIT_CODE_NORMAL) {
            ob_end_flush();
        } else {
            ob_end_clean();
        }
45 46
    }

47
    public static function setUpBeforeClass()
48
    {
49 50 51 52
        parent::setUpBeforeClass();
        $databases = static::getParam('databases');
        static::$database = $databases[static::$driverName];
        $pdo_database = 'pdo_' . static::$driverName;
53 54

        if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) {
55
            static::markTestSkipped('pdo and ' . $pdo_database . ' extension are required.');
56 57
        }

58
        static::runConsoleAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
59 60 61 62
    }

    public static function tearDownAfterClass()
    {
63
        static::runConsoleAction('migrate/down', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
64 65 66
        if (static::$db) {
            static::$db->close();
        }
67
        Yii::$app = null;
68 69 70 71 72 73
        parent::tearDownAfterClass();
    }

    protected function setUp()
    {
        parent::setUp();
74
        $this->auth = new DbManager(['db' => $this->getConnection()]);
75

76 77 78 79 80
    }

    protected function tearDown()
    {
        parent::tearDown();
81
        $this->auth->removeAll();
82 83 84 85 86 87 88 89
    }

    /**
     * @throws \yii\base\InvalidParamException
     * @throws \yii\db\Exception
     * @throws \yii\base\InvalidConfigException
     * @return \yii\db\Connection
     */
90
    public static function getConnection()
91
    {
92 93 94 95 96 97 98 99 100 101 102 103 104 105
        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;
106
        }
107
        return static::$db;
108 109
    }
}