Skip to content

Commit

Permalink
Ease local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Oct 31, 2024
1 parent b2ae795 commit 65d6d61
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ phpunit.phar
/phpunit.xml
/.phpunit.result.cache

# NPM packages
/node_modules
.env

#codeception
/tests/_output
c3.php
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -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
11 changes: 1 addition & 10 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
50 changes: 44 additions & 6 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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'),
Expand All @@ -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;
Expand All @@ -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');
}
}
8 changes: 8 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

if (getenv('ENVIRONMENT', local_only: true) === 'local') {
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();
}

0 comments on commit 65d6d61

Please sign in to comment.