diff --git a/composer.json b/composer.json index a9b1224..9fd143d 100644 --- a/composer.json +++ b/composer.json @@ -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" @@ -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, diff --git a/tests/.env b/tests/.env new file mode 100644 index 0000000..a355b62 --- /dev/null +++ b/tests/.env @@ -0,0 +1,6 @@ +ENVIRONMENT=local +YII_ORACLE_DATABASE= +YII_ORACLE_HOST=oracle +YII_ORACLE_PORT= +YII_ORACLE_USER=system +YII_ORACLE_PASSWORD=root diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 8d5c73d..702ab79 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -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; @@ -14,7 +15,7 @@ trait TestTrait { - private string $dsn = 'oci:dbname=localhost/XE;'; + private string $dsn = ''; /** * @throws InvalidConfigException @@ -22,7 +23,7 @@ trait TestTrait */ 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'); @@ -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; @@ -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'); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..0b8567e --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,8 @@ +load(); +}