Skip to content

Commit 42eb24b

Browse files
authored
Ease local testing (#326)
1 parent 9576f8b commit 42eb24b

File tree

8 files changed

+79
-24
lines changed

8 files changed

+79
-24
lines changed

.github/workflows/build.yml

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ jobs:
103103

104104
- name: Run tests with phpunit with code coverage.
105105
run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always
106+
env:
107+
YII_MSSQL_DATABASE: yiitest
108+
YII_MSSQL_HOST: 127.0.0.1
109+
YII_MSSQL_PORT: 1433
110+
YII_MSSQL_USER: SA
111+
YII_MSSQL_PASSWORD: YourStrong!Passw0rd
106112

107113
- name: Upload coverage to Codecov.
108114
uses: codecov/codecov-action@v3

.github/workflows/mutation.yml

+5
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ jobs:
8787
vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations --only-covered
8888
env:
8989
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
90+
YII_MSSQL_DATABASE: yiitest
91+
YII_MSSQL_HOST: 127.0.0.1
92+
YII_MSSQL_PORT: 1433
93+
YII_MSSQL_USER: SA
94+
YII_MSSQL_PASSWORD: YourStrong!Passw0rd

.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ phpunit.phar
3636
/phpunit.xml
3737
/.phpunit.result.cache
3838

39-
# NPM packages
40-
/node_modules
41-
.env
42-
4339
#codeception
4440
/tests/_output
4541
c3.php

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"roave/infection-static-analysis-plugin": "^1.16",
4343
"spatie/phpunit-watcher": "^1.23",
4444
"vimeo/psalm": "^5.25",
45+
"vlucas/phpdotenv": "^5.6",
4546
"yiisoft/aliases": "^2.0",
4647
"yiisoft/cache-file": "^3.1",
4748
"yiisoft/var-dumper": "^1.5"
@@ -55,7 +56,8 @@
5556
"psr-4": {
5657
"Yiisoft\\Db\\Mssql\\Tests\\": "tests",
5758
"Yiisoft\\Db\\Tests\\": "vendor/yiisoft/db/tests"
58-
}
59+
},
60+
"files": ["tests/bootstrap.php"]
5961
},
6062
"config": {
6163
"sort-packages": true,

tests/.env

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ENVIRONMENT=local
2+
YII_MSSQL_DATABASE=tempdb
3+
YII_MSSQL_HOST=mssql
4+
YII_MSSQL_PORT=1433
5+
YII_MSSQL_USER=SA
6+
YII_MSSQL_PASSWORD=YourStrong!Passw0rd

tests/CommandTest.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
use Yiisoft\Db\Exception\NotSupportedException;
1515
use Yiisoft\Db\Expression\Expression;
1616
use Yiisoft\Db\Mssql\Column\ColumnBuilder;
17-
use Yiisoft\Db\Mssql\Connection;
18-
use Yiisoft\Db\Mssql\Dsn;
19-
use Yiisoft\Db\Mssql\Driver;
2017
use Yiisoft\Db\Mssql\IndexType;
2118
use Yiisoft\Db\Mssql\Tests\Provider\CommandProvider;
2219
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
2320
use Yiisoft\Db\Query\Query;
2421
use Yiisoft\Db\Tests\Common\CommonCommandTest;
25-
use Yiisoft\Db\Tests\Support\DbHelper;
2622

2723
use function array_filter;
2824
use function trim;
@@ -317,16 +313,14 @@ public function testDropDefaultValueSql(): void
317313

318314
public function testShowDatabases(): void
319315
{
320-
$dsn = new Dsn(options: ['Encrypt' => 'no']);
321-
$db = new Connection(
322-
new Driver($dsn->asString(), 'SA', 'YourStrong!Passw0rd'),
323-
DbHelper::getSchemaCache(),
324-
);
316+
$expectedDatabases = [];
317+
if (self::getDatabaseName() !== 'tempdb') {
318+
$expectedDatabases[] = self::getDatabaseName();
319+
}
325320

326-
$command = $db->createCommand();
321+
$actualDatabases = self::getDb()->createCommand()->showDatabases();
327322

328-
$this->assertSame('sqlsrv:Server=127.0.0.1,1433;Encrypt=no', $db->getDriver()->getDsn());
329-
$this->assertSame(['yiitest'], $command->showDatabases());
323+
$this->assertSame($expectedDatabases, $actualDatabases);
330324
}
331325

332326
/** @link https://github.com/yiisoft/db-migration/issues/11 */

tests/Support/TestTrait.php

+45-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Mssql\Tests\Support;
66

77
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
8+
use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
89
use Yiisoft\Db\Exception\Exception;
910
use Yiisoft\Db\Exception\InvalidConfigException;
1011
use Yiisoft\Db\Mssql\Connection;
@@ -23,10 +24,7 @@ trait TestTrait
2324
*/
2425
protected function getConnection(bool $fixture = false): PdoConnectionInterface
2526
{
26-
$db = new Connection(
27-
new Driver($this->getDsn(), 'SA', 'YourStrong!Passw0rd'),
28-
DbHelper::getSchemaCache()
29-
);
27+
$db = new Connection($this->getDriver(), DbHelper::getSchemaCache());
3028

3129
if ($fixture) {
3230
DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture");
@@ -37,18 +35,28 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface
3735

3836
protected static function getDb(): PdoConnectionInterface
3937
{
40-
$dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString();
38+
$dsn = (new Dsn(
39+
host: self::getHost(),
40+
databaseName: self::getDatabaseName(),
41+
port: self::getPort(),
42+
options: ['Encrypt' => 'no']
43+
))->asString();
4144

4245
return new Connection(
43-
new Driver($dsn, 'SA', 'YourStrong!Passw0rd'),
46+
new Driver($dsn, self::getUsername(), self::getPassword()),
4447
DbHelper::getSchemaCache(),
4548
);
4649
}
4750

4851
protected function getDsn(): string
4952
{
5053
if ($this->dsn === '') {
51-
$this->dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString();
54+
$this->dsn = (new Dsn(
55+
host: self::getHost(),
56+
databaseName: self::getDatabaseName(),
57+
port: self::getPort(),
58+
options: ['Encrypt' => 'no']
59+
))->asString();
5260
}
5361

5462
return $this->dsn;
@@ -68,4 +76,34 @@ protected function setFixture(string $fixture): void
6876
{
6977
$this->fixture = $fixture;
7078
}
79+
80+
private function getDriver(): PdoDriverInterface
81+
{
82+
return new Driver($this->getDsn(), self::getUsername(), self::getPassword());
83+
}
84+
85+
private static function getDatabaseName(): string
86+
{
87+
return getenv('YII_MSSQL_DATABASE') ?? '';
88+
}
89+
90+
private static function getHost(): string
91+
{
92+
return getenv('YII_MSSQL_HOST') ?? '';
93+
}
94+
95+
private static function getPort(): string
96+
{
97+
return getenv('YII_MSSQL_PORT') ?? '';
98+
}
99+
100+
private static function getUsername(): string
101+
{
102+
return getenv('YII_MSSQL_USER') ?? '';
103+
}
104+
105+
private static function getPassword(): string
106+
{
107+
return getenv('YII_MSSQL_PASSWORD') ?? '';
108+
}
71109
}

tests/bootstrap.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (getenv('ENVIRONMENT', local_only: true) === 'local') {
6+
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
7+
$dotenv->load();
8+
}

0 commit comments

Comments
 (0)