Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ease local testing #326

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
18 changes: 6 additions & 12 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
Expand Down
52 changes: 45 additions & 7 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,18 +35,28 @@ 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(),
);
}

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