Skip to content

Commit

Permalink
Ease local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Nov 1, 2024
1 parent d992d1a commit 98f1357
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 @@ -54,7 +55,8 @@
"psr-4": {
"Yiisoft\\Db\\Oracle\\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_ORACLE_DATABASE=
YII_ORACLE_HOST=oracle
YII_ORACLE_PORT=
YII_ORACLE_USER=system
YII_ORACLE_PASSWORD=root
51 changes: 46 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\Oracle\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\Oracle\Connection;
Expand All @@ -14,15 +15,15 @@

trait TestTrait
{
private string $dsn = 'oci:dbname=localhost/XE;';
private string $dsn = '';

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

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

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn(databaseName: 'XE', options: ['charset' => 'AL32UTF8']))->asString();
$dsn = (new Dsn(
host: self::getHost(),
databaseName: self::getDatabaseName(),
port: self::getPort(),
options: ['charset' => 'AL32UTF8'],
))->asString();

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

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

return $this->dsn;
Expand All @@ -56,4 +67,34 @@ protected function setDsn(string $dsn): void
{
$this->dsn = $dsn;
}

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

private static function getDatabaseName(): string
{
return getenv('YII_ORACLE_DATABASE') ?: 'XE';
}

private static function getHost(): string
{
return getenv('YII_ORACLE_HOST');
}

private static function getPort(): string
{
return getenv('YII_ORACLE_PORT') ?: '1521';
}

private static function getUsername(): string
{
return getenv('YII_ORACLE_USER');
}

private static function getPassword(): string
{
return getenv('YII_ORACLE_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 98f1357

Please sign in to comment.