From 65d6d6171a90aaf3320aa2776388cd4d6616b51d Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 31 Oct 2024 21:15:48 +0500 Subject: [PATCH 1/4] Ease local testing --- .github/workflows/build.yml | 6 +++++ .gitignore | 4 --- composer.json | 4 ++- tests/.env | 6 +++++ tests/CommandTest.php | 11 +------- tests/Support/TestTrait.php | 50 ++++++++++++++++++++++++++++++++----- tests/bootstrap.php | 8 ++++++ 7 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 tests/.env create mode 100644 tests/bootstrap.php 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/.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 e3e50bfe..fe050948 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -313,16 +313,7 @@ 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(), - ); - - $command = $db->createCommand(); - - $this->assertSame('sqlsrv:Server=127.0.0.1,1433;Encrypt=no', $db->getDriver()->getDsn()); - $this->assertSame(['yiitest'], $command->showDatabases()); + $this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases()); } /** @link https://github.com/yiisoft/db-migration/issues/11 */ diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 517caedc..2857fc3d 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,7 +35,12 @@ 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'), @@ -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(); +} From 1e0c000b0b71fa95941aa3b6badb4c9f1dc7affc Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 31 Oct 2024 16:16:04 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- tests/CommandTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index fe050948..630eee8d 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; From 731a5128a58e523a19849da0ad5271e62a65ec0b Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Fri, 1 Nov 2024 21:08:31 +0500 Subject: [PATCH 3/4] WIP --- .github/workflows/mutation.yml | 5 +++++ 1 file changed, 5 insertions(+) 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 From 6216a661769c3f792375c9d07fda4fe9f061d0c6 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Fri, 1 Nov 2024 21:27:14 +0500 Subject: [PATCH 4/4] WIP [skip ci] --- tests/Support/TestTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 2857fc3d..dd7ae3c3 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -43,7 +43,7 @@ protected static function getDb(): PdoConnectionInterface ))->asString(); return new Connection( - new Driver($dsn, 'SA', 'YourStrong!Passw0rd'), + new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache(), ); }