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 #360

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions .github/workflows/ansi-mode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ jobs:

- name: Run tests with phpunit with code coverage.
run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations
env:
YII_MYSQL_DATABASE: yiitest
YII_MYSQL_HOST: 127.0.0.1
YII_MYSQL_PORT: 3306
YII_MYSQL_USER: root
YII_MYSQL_PASSWORD: ''

- name: Upload coverage to Codecov.
uses: codecov/codecov-action@v3
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build-mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ jobs:

- name: Run tests with phpunit with code coverage.
run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations
env:
YII_MYSQL_DATABASE: yiitest
YII_MYSQL_HOST: 127.0.0.1
YII_MYSQL_PORT: 3306
YII_MYSQL_USER: root
YII_MYSQL_PASSWORD: ''

- name: Upload coverage to Codecov.
uses: codecov/codecov-action@v3
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ jobs:

- name: Run tests with phpunit with code coverage.
run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations
env:
YII_MYSQL_DATABASE: yiitest
YII_MYSQL_HOST: 127.0.0.1
YII_MYSQL_PORT: 3306
YII_MYSQL_USER: root
YII_MYSQL_PASSWORD: ''

- 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 @@ -79,3 +79,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_MYSQL_DATABASE: yiitest
YII_MYSQL_HOST: 127.0.0.1
YII_MYSQL_PORT: 3306
YII_MYSQL_USER: root
YII_MYSQL_PASSWORD: ''
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,3 @@ phpunit.phar
/apps
/extensions
/packages

# NPM packages
/node_modules
.env
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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/log-target-file": "^2.0",
"yiisoft/cache-file": "^3.1",
Expand All @@ -60,7 +61,8 @@
"psr-4": {
"Yiisoft\\Db\\Mysql\\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_MYSQL_DATABASE=yii
YII_MYSQL_HOST=mysql
YII_MYSQL_PORT=3306
YII_MYSQL_USER=root
YII_MYSQL_PASSWORD=root
12 changes: 1 addition & 11 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Mysql\Connection;
use Yiisoft\Db\Mysql\Dsn;
use Yiisoft\Db\Mysql\Driver;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\DbHelper;

/**
* @group mysql
Expand Down Expand Up @@ -141,12 +137,6 @@ public function testUpsert(array $firstData, array $secondData): void

public function testShowDatabases(): void
{
$dsn = new Dsn('mysql', '127.0.0.1', );
$db = new Connection(new Driver($dsn->asString(), 'root', ''), DbHelper::getSchemaCache());

$command = $db->createCommand();

$this->assertSame('mysql:host=127.0.0.1;port=3306', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
$this->assertSame([self::getDatabaseName()], self::getDb()->createCommand()->showDatabases());
}
}
11 changes: 2 additions & 9 deletions tests/PDODriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use PDO;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Mysql\Connection;
use Yiisoft\Db\Mysql\Driver;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Support\DbHelper;

/**
* @group mysql
Expand All @@ -29,7 +26,7 @@ public function testConnectionCharset(): void

$this->assertEqualsIgnoringCase('utf8mb4', array_values($charset)[1]);

$pdoDriver = new Driver('mysql:host=127.0.0.1;dbname=yiitest;port=3306', 'root', '');
$pdoDriver = $this->getDriver();
$newCharset = 'latin1';
$pdoDriver->charset($newCharset);
$pdo = $pdoDriver->createConnection();
Expand All @@ -40,11 +37,7 @@ public function testConnectionCharset(): void

public function testCharsetDefault(): void
{
$db = new Connection(
new Driver('mysql:host=127.0.0.1;dbname=yiitest;port=3306', 'root', ''),
DbHelper::getSchemaCache(),
);

$db = self::getDb();
$db->open();
$command = $db->createCommand();

Expand Down
4 changes: 2 additions & 2 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function testGetSchemaNames(): void

$schema = $db->getSchema();

$this->assertSame(['yiitest'], $schema->getSchemaNames());
$this->assertSame([self::getDatabaseName()], $schema->getSchemaNames());
}

public function testGetTableChecks(): void
Expand All @@ -233,7 +233,7 @@ public function testGetTableNamesWithSchema(): void
$db = $this->getConnection(true);

$schema = $db->getSchema();
$tablesNames = $schema->getTableNames('yiitest');
$tablesNames = $schema->getTableNames(self::getDatabaseName());

$expectedTableNames = [
'alpha',
Expand Down
76 changes: 71 additions & 5 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Mysql\Tests\Support;

use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
use Yiisoft\Db\Mysql\Connection;
use Yiisoft\Db\Mysql\Driver;
use Yiisoft\Db\Mysql\Dsn;
Expand All @@ -16,7 +17,7 @@ trait TestTrait

protected function getConnection(bool $fixture = false): PdoConnectionInterface
{
$db = new Connection(new Driver($this->getDsn(), 'root', ''), DbHelper::getSchemaCache());
$db = new Connection($this->getDriver(), DbHelper::getSchemaCache());

if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . '/Fixture/mysql.sql');
Expand All @@ -27,15 +28,25 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn(databaseName: 'yiitest', options: ['charset' => 'utf8mb4']))->asString();

return new Connection(new Driver($dsn, 'root', ''), DbHelper::getSchemaCache());
$dsn = (new Dsn(
host: self::getHost(),
databaseName: self::getDatabaseName(),
port: self::getPort(),
options: ['charset' => 'utf8mb4'],
))->asString();

return new Connection(new Driver($dsn, self::getUsername(), self::getPassword()), DbHelper::getSchemaCache());
}

protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn(databaseName: 'yiitest', options: ['charset' => 'utf8mb4']))->asString();
$this->dsn = (new Dsn(
host: self::getHost(),
databaseName: self::getDatabaseName(),
port: self::getPort(),
options: ['charset' => 'utf8mb4'],
))->asString();
}

return $this->dsn;
Expand All @@ -59,4 +70,59 @@ public static function setUpBeforeClass(): void

$db->close();
}

private function getDriver(): PdoDriverInterface
{
return new Driver($this->getDsn(), self::getUsername(), self::getPassword());
}

private static function getDatabaseName(): string
{
if (self::isMariadb()) {
return getenv('YII_MARIADB_DATABASE') ?: '';
}

return getenv('YII_MYSQL_DATABASE') ?: '';
}

private static function getHost(): string
{
if (self::isMariadb()) {
return getenv('YII_MARIADB_HOST') ?: '';
}

return getenv('YII_MYSQL_HOST') ?: '';
}

private static function getPort(): string
{
if (self::isMariadb()) {
return getenv('YII_MARIADB_PORT') ?: '';
}

return getenv('YII_MYSQL_PORT') ?: '';
}

private static function getUsername(): string
{
if (self::isMariadb()) {
return getenv('YII_MARIADB_USER') ?: '';
}

return getenv('YII_MYSQL_USER') ?: '';
}

private static function getPassword(): string
{
if (self::isMariadb()) {
return getenv('YII_MARIADB_PASSWORD') ?: '';
}

return getenv('YII_MYSQL_PASSWORD') ?: '';
}

private static function isMariadb(): bool
{
return getenv('YII_MYSQL_TYPE') === 'mariadb';
}
}
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