-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
ipl\Tests\Orm\Lib\DataProviders
plus integration into github actions
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace ipl\Tests\Orm\Lib; | ||
|
||
use ipl\Sql\Connection; | ||
use RuntimeException; | ||
|
||
trait DataProviders | ||
{ | ||
/** | ||
* Create the schema for the test database | ||
* | ||
* @param Connection $db | ||
* @param string $driver | ||
* | ||
* @return void | ||
*/ | ||
abstract protected function createSchema(Connection $db, string $driver): void; | ||
|
||
/** | ||
* Drop the schema of the test database | ||
* | ||
* @param Connection $db | ||
* @param string $driver | ||
* | ||
* @return void | ||
*/ | ||
abstract protected function dropSchema(Connection $db, string $driver): void; | ||
|
||
/** | ||
* Provide the MySQL database connection | ||
* | ||
* @return array{mysql: Connection[], pgsql: Connection[]} | ||
*/ | ||
public function databases(): array | ||
{ | ||
return [ | ||
'mysql' => [$this->createConnection('mysql')], | ||
'pgsql' => [$this->createConnection('pgsql')] | ||
]; | ||
} | ||
|
||
/** | ||
* Get the value of an environment variable | ||
* | ||
* @param string $name | ||
* | ||
* @return string | ||
* | ||
* @throws RuntimeException if the environment variable is not set | ||
*/ | ||
private function getEnvVariable(string $name): string | ||
{ | ||
$value = getenv($name); | ||
if ($value === false) { | ||
throw new RuntimeException("Environment variable $name is not set"); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Create a database connection | ||
* | ||
* @param string $driver | ||
* | ||
* @return Connection | ||
*/ | ||
private function createConnection(string $driver): Connection | ||
{ | ||
return new Connection([ | ||
'db' => $driver, | ||
'host' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_HOST'), | ||
'port' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_PORT'), | ||
'username' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_USER'), | ||
'password' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_PASSWORD'), | ||
'dbname' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB'), | ||
]); | ||
} | ||
|
||
public function setUp(): void | ||
{ | ||
$this->createSchema($this->createConnection('mysql'), 'mysql'); | ||
$this->createSchema($this->createConnection('pgsql'), 'pgsql'); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->dropSchema($this->createConnection('mysql'), 'mysql'); | ||
$this->dropSchema($this->createConnection('pgsql'), 'pgsql'); | ||
} | ||
} |