DbManagerTestCase.php 3.16 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 8 9 10 11 12 13 14
use yii\db\Connection;
use yii\rbac\DbManager;

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

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

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

        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();
        }
44 45
    }

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

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

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

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

    protected function setUp()
    {
        parent::setUp();
73
        $this->auth = $this->createManager();
74 75 76 77 78
    }

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

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

    /**
     * @return \yii\rbac\ManagerInterface
     */
    protected function createManager()
    {
        return new DbManager(['db' => $this->getConnection()]);
    }
115
}