diff --git a/.github/workflows/ansi-mode.yml b/.github/workflows/ansi-mode.yml index a3d68bea..cdbccc24 100644 --- a/.github/workflows/ansi-mode.yml +++ b/.github/workflows/ansi-mode.yml @@ -85,6 +85,12 @@ jobs: - name: Run tests with phpunit with code coverage. run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations + env: + YII_MYSQL_DATABASE: yiitest + YII_MYSQL_HOST: 127.0.0.1 + YII_MYSQL_PORT: 3306 + YII_MYSQL_USER: root + YII_MYSQL_PASSWORD: '' - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/build-mariadb.yml b/.github/workflows/build-mariadb.yml index fedfc19d..4f941def 100644 --- a/.github/workflows/build-mariadb.yml +++ b/.github/workflows/build-mariadb.yml @@ -91,6 +91,12 @@ jobs: - name: Run tests with phpunit with code coverage. run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations + env: + YII_MYSQL_DATABASE: yiitest + YII_MYSQL_HOST: 127.0.0.1 + YII_MYSQL_PORT: 3306 + YII_MYSQL_USER: root + YII_MYSQL_PASSWORD: '' - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1e462f26..3bc5ef7a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -86,6 +86,12 @@ jobs: - name: Run tests with phpunit with code coverage. run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations + env: + YII_MYSQL_DATABASE: yiitest + YII_MYSQL_HOST: 127.0.0.1 + YII_MYSQL_PORT: 3306 + YII_MYSQL_USER: root + YII_MYSQL_PASSWORD: '' - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index b6683a09..2a286da7 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -79,3 +79,8 @@ jobs: vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations --only-covered env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} + YII_MYSQL_DATABASE: yiitest + YII_MYSQL_HOST: 127.0.0.1 + YII_MYSQL_PORT: 3306 + YII_MYSQL_USER: root + YII_MYSQL_PASSWORD: '' diff --git a/.gitignore b/.gitignore index 61ac953d..2ea2501e 100644 --- a/.gitignore +++ b/.gitignore @@ -40,7 +40,3 @@ phpunit.phar /apps /extensions /packages - -# NPM packages -/node_modules -.env diff --git a/composer.json b/composer.json index fde367f8..1b176077 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^5.25", + "vlucas/phpdotenv": "^5.6", "yiisoft/aliases": "^2.0", "yiisoft/log-target-file": "^2.0", "yiisoft/cache-file": "^3.1", @@ -60,7 +61,8 @@ "psr-4": { "Yiisoft\\Db\\Mysql\\Tests\\": "tests", "Yiisoft\\Db\\Tests\\": "vendor/yiisoft/db/tests" - } + }, + "files": ["tests/bootstrap.php"] }, "config": { "sort-packages": true, diff --git a/tests/.env b/tests/.env new file mode 100644 index 00000000..3dc0f6c8 --- /dev/null +++ b/tests/.env @@ -0,0 +1,6 @@ +ENVIRONMENT=local +YII_MYSQL_DATABASE=yii +YII_MYSQL_HOST=mysql +YII_MYSQL_PORT=3306 +YII_MYSQL_USER=root +YII_MYSQL_PASSWORD=root diff --git a/tests/CommandTest.php b/tests/CommandTest.php index be24f006..4dbfa2b4 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -8,12 +8,8 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Mysql\Connection; -use Yiisoft\Db\Mysql\Dsn; -use Yiisoft\Db\Mysql\Driver; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; -use Yiisoft\Db\Tests\Support\DbHelper; /** * @group mysql @@ -141,12 +137,6 @@ public function testUpsert(array $firstData, array $secondData): void public function testShowDatabases(): void { - $dsn = new Dsn('mysql', '127.0.0.1', ); - $db = new Connection(new Driver($dsn->asString(), 'root', ''), DbHelper::getSchemaCache()); - - $command = $db->createCommand(); - - $this->assertSame('mysql:host=127.0.0.1;port=3306', $db->getDriver()->getDsn()); - $this->assertSame(['yiitest'], $command->showDatabases()); + $this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases()); } } diff --git a/tests/PDODriverTest.php b/tests/PDODriverTest.php index 64773156..577a4ff1 100644 --- a/tests/PDODriverTest.php +++ b/tests/PDODriverTest.php @@ -6,10 +6,7 @@ use PDO; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Mysql\Connection; -use Yiisoft\Db\Mysql\Driver; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Tests\Support\DbHelper; /** * @group mysql @@ -29,7 +26,7 @@ public function testConnectionCharset(): void $this->assertEqualsIgnoringCase('utf8mb4', array_values($charset)[1]); - $pdoDriver = new Driver('mysql:host=127.0.0.1;dbname=yiitest;port=3306', 'root', ''); + $pdoDriver = $this->getDriver(); $newCharset = 'latin1'; $pdoDriver->charset($newCharset); $pdo = $pdoDriver->createConnection(); @@ -40,11 +37,7 @@ public function testConnectionCharset(): void public function testCharsetDefault(): void { - $db = new Connection( - new Driver('mysql:host=127.0.0.1;dbname=yiitest;port=3306', 'root', ''), - DbHelper::getSchemaCache(), - ); - + $db = self::getDb(); $db->open(); $command = $db->createCommand(); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 0799626f..240164ff 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -212,7 +212,7 @@ public function testGetSchemaNames(): void $schema = $db->getSchema(); - $this->assertSame(['yiitest'], $schema->getSchemaNames()); + $this->assertSame([self::getDatabaseName()], $schema->getSchemaNames()); } public function testGetTableChecks(): void @@ -233,7 +233,7 @@ public function testGetTableNamesWithSchema(): void $db = $this->getConnection(true); $schema = $db->getSchema(); - $tablesNames = $schema->getTableNames('yiitest'); + $tablesNames = $schema->getTableNames(self::getDatabaseName()); $expectedTableNames = [ 'alpha', diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 6bb30459..ab576d1c 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Mysql\Tests\Support; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; +use Yiisoft\Db\Driver\Pdo\PdoDriverInterface; use Yiisoft\Db\Mysql\Connection; use Yiisoft\Db\Mysql\Driver; use Yiisoft\Db\Mysql\Dsn; @@ -16,7 +17,7 @@ trait TestTrait protected function getConnection(bool $fixture = false): PdoConnectionInterface { - $db = new Connection(new Driver($this->getDsn(), 'root', ''), DbHelper::getSchemaCache()); + $db = new Connection($this->getDriver(), DbHelper::getSchemaCache()); if ($fixture) { DbHelper::loadFixture($db, __DIR__ . '/Fixture/mysql.sql'); @@ -27,15 +28,25 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface protected static function getDb(): PdoConnectionInterface { - $dsn = (new Dsn(databaseName: 'yiitest', options: ['charset' => 'utf8mb4']))->asString(); - - return new Connection(new Driver($dsn, 'root', ''), DbHelper::getSchemaCache()); + $dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + options: ['charset' => 'utf8mb4'], + ))->asString(); + + return new Connection(new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache()); } protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn(databaseName: 'yiitest', options: ['charset' => 'utf8mb4']))->asString(); + $this->dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + options: ['charset' => 'utf8mb4'], + ))->asString(); } return $this->dsn; @@ -59,4 +70,59 @@ public static function setUpBeforeClass(): void $db->close(); } + + private function getDriver(): PdoDriverInterface + { + return new Driver($this->getDsn(), self::getUsername(), self::getPassword()); + } + + private static function getDatabaseName(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_DATABASE') ?: ''; + } + + return getenv('YII_MYSQL_DATABASE') ?: ''; + } + + private static function getHost(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_HOST') ?: ''; + } + + return getenv('YII_MYSQL_HOST') ?: ''; + } + + private static function getPort(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_PORT') ?: ''; + } + + return getenv('YII_MYSQL_PORT') ?: ''; + } + + private static function getUsername(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_USER') ?: ''; + } + + return getenv('YII_MYSQL_USER') ?: ''; + } + + private static function getPassword(): string + { + if (self::isMariadb()) { + return getenv('YII_MARIADB_PASSWORD') ?: ''; + } + + return getenv('YII_MYSQL_PASSWORD') ?: ''; + } + + private static function isMariadb(): bool + { + return getenv('YII_MYSQL_TYPE') === 'mariadb'; + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..0b8567eb --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +load(); +}