diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1b86df23..ed8ea1a3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -103,6 +103,12 @@ jobs: - name: Run tests with phpunit with code coverage. run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always + env: + YII_MSSQL_DATABASE: yiitest + YII_MSSQL_HOST: 127.0.0.1 + YII_MSSQL_PORT: 1433 + YII_MSSQL_USER: SA + YII_MSSQL_PASSWORD: YourStrong!Passw0rd - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index 825b2828..5e0141a7 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -87,3 +87,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_MSSQL_DATABASE: yiitest + YII_MSSQL_HOST: 127.0.0.1 + YII_MSSQL_PORT: 1433 + YII_MSSQL_USER: SA + YII_MSSQL_PASSWORD: YourStrong!Passw0rd diff --git a/.gitignore b/.gitignore index 3d694002..d3a42a79 100644 --- a/.gitignore +++ b/.gitignore @@ -36,10 +36,6 @@ phpunit.phar /phpunit.xml /.phpunit.result.cache -# NPM packages -/node_modules -.env - #codeception /tests/_output c3.php diff --git a/composer.json b/composer.json index fe47d418..e31df273 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,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/cache-file": "^3.1", "yiisoft/var-dumper": "^1.5" @@ -55,7 +56,8 @@ "psr-4": { "Yiisoft\\Db\\Mssql\\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..8f4f762c --- /dev/null +++ b/tests/.env @@ -0,0 +1,6 @@ +ENVIRONMENT=local +YII_MSSQL_DATABASE=tempdb +YII_MSSQL_HOST=mssql +YII_MSSQL_PORT=1433 +YII_MSSQL_USER=SA +YII_MSSQL_PASSWORD=YourStrong!Passw0rd diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 301b76c6..9c59b9f5 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -13,13 +13,9 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mssql\Column; -use Yiisoft\Db\Mssql\Connection; -use Yiisoft\Db\Mssql\Dsn; -use Yiisoft\Db\Mssql\Driver; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonCommandTest; -use Yiisoft\Db\Tests\Support\DbHelper; use function trim; @@ -313,16 +309,14 @@ public function testDropDefaultValueSql(): void public function testShowDatabases(): void { - $dsn = new Dsn(options: ['Encrypt' => 'no']); - $db = new Connection( - new Driver($dsn->asString(), 'SA', 'YourStrong!Passw0rd'), - DbHelper::getSchemaCache(), - ); + $expectedDatabases = []; + if (self::getDatabaseName() !== 'tempdb') { + $expectedDatabases[] = self::getDatabaseName(); + } - $command = $db->createCommand(); + $actualDatabases = self::getDb()->createCommand()->showDatabases(); - $this->assertSame('sqlsrv:Server=127.0.0.1,1433;Encrypt=no', $db->getDriver()->getDsn()); - $this->assertSame(['yiitest'], $command->showDatabases()); + $this->assertSame($expectedDatabases, $actualDatabases); } /** @link https://github.com/yiisoft/db-migration/issues/11 */ diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 517caedc..dc81e17f 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Mssql\Tests\Support; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; +use Yiisoft\Db\Driver\Pdo\PdoDriverInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Mssql\Connection; @@ -23,10 +24,7 @@ trait TestTrait */ protected function getConnection(bool $fixture = false): PdoConnectionInterface { - $db = new Connection( - new Driver($this->getDsn(), 'SA', 'YourStrong!Passw0rd'), - DbHelper::getSchemaCache() - ); + $db = new Connection($this->getDriver(), DbHelper::getSchemaCache()); if ($fixture) { DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture"); @@ -37,10 +35,15 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface protected static function getDb(): PdoConnectionInterface { - $dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString(); + $dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + options: ['Encrypt' => 'no'] + ))->asString(); return new Connection( - new Driver($dsn, 'SA', 'YourStrong!Passw0rd'), + new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache(), ); } @@ -48,7 +51,12 @@ protected static function getDb(): PdoConnectionInterface protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString(); + $this->dsn = (new Dsn( + host: self::getHost(), + databaseName: self::getDatabaseName(), + port: self::getPort(), + options: ['Encrypt' => 'no'] + ))->asString(); } return $this->dsn; @@ -68,4 +76,34 @@ protected function setFixture(string $fixture): void { $this->fixture = $fixture; } + + private function getDriver(): PdoDriverInterface + { + return new Driver($this->getDsn(), self::getUsername(), self::getPassword()); + } + + private static function getDatabaseName(): string + { + return getenv('YII_MSSQL_DATABASE') ?? ''; + } + + private static function getHost(): string + { + return getenv('YII_MSSQL_HOST') ?? ''; + } + + private static function getPort(): string + { + return getenv('YII_MSSQL_PORT') ?? ''; + } + + private static function getUsername(): string + { + return getenv('YII_MSSQL_USER') ?? ''; + } + + private static function getPassword(): string + { + return getenv('YII_MSSQL_PASSWORD') ?? ''; + } } 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(); +}