Change configuration to use environment variables instead of globals.
This commit is contained in:
+5
-5
@@ -1,13 +1,13 @@
|
|||||||
<phpunit bootstrap="bootstrap/bootstrap.php">
|
<phpunit bootstrap="bootstrap/bootstrap.php">
|
||||||
<php>
|
<php>
|
||||||
<var name="DB_DSN" value="sqlite::memory:" />
|
<env name="DB_DSN" value="sqlite::memory:" />
|
||||||
<var name="DB_USER" value="root" />
|
<env name="DB_USER" value="root" />
|
||||||
<var name="DB_PASSWORD" value="" />
|
<env name="DB_PASSWORD" value="" />
|
||||||
<var name="DB_NAME" value="daenerys" />
|
<env name="DB_NAME" value="daenerys" />
|
||||||
</php>
|
</php>
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="All">
|
<testsuite name="All">
|
||||||
<directory>tests</directory>
|
<directory>tests</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
+22
-1
@@ -3,6 +3,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace LotGD\Core;
|
namespace LotGD\Core;
|
||||||
|
|
||||||
|
use LotGD\Core\Exceptions\InvalidConfigurationException;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
|
use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
|
||||||
@@ -11,9 +13,28 @@ use Doctrine\ORM\Tools\SchemaTool;
|
|||||||
|
|
||||||
class Bootstrap
|
class Bootstrap
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Create a new Game object, with all the necessary configuration.
|
||||||
|
* @throws InvalidConfigurationException
|
||||||
|
* @return Game The newly created Game object.
|
||||||
|
*/
|
||||||
public static function createGame(): Game
|
public static function createGame(): Game
|
||||||
{
|
{
|
||||||
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS["DB_USER"], $GLOBALS["DB_PASSWORD"]);
|
$dsn = getenv('DB_DSN');
|
||||||
|
$user = getenv('DB_USER');
|
||||||
|
$passwd = getenv('DB_PASSWORD');
|
||||||
|
|
||||||
|
if ($dsn === false || strlen($dsn) == 0) {
|
||||||
|
throw new InvalidConfigurationException("Invalid or missing data source name: '{$dsn}'");
|
||||||
|
}
|
||||||
|
if ($user === false || strlen($user) == 0) {
|
||||||
|
throw new InvalidConfigurationException("Invalid or missing database user: '{$user}'");
|
||||||
|
}
|
||||||
|
if ($passwd === false) {
|
||||||
|
throw new InvalidConfigurationException("Invalid or missing database password: '{$passwd}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo = new \PDO($dsn, $user, $passwd);
|
||||||
|
|
||||||
// Read db annotations from model files
|
// Read db annotations from model files
|
||||||
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace LotGD\Core\Exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception if a configuration value is missing or invalid.
|
||||||
|
*/
|
||||||
|
class InvalidConfigurationException extends CoreException {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -29,7 +29,7 @@ abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
|
|||||||
{
|
{
|
||||||
if ($this->connection === null) {
|
if ($this->connection === null) {
|
||||||
if (self::$pdo === null) {
|
if (self::$pdo === null) {
|
||||||
self::$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS["DB_USER"], $GLOBALS["DB_PASSWORD"]);
|
self::$pdo = new \PDO(getenv('DB_DSN'), getenv('DB_USER'), getenv('DB_PASSWORD'));
|
||||||
|
|
||||||
// Read db annotations from model files
|
// Read db annotations from model files
|
||||||
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
||||||
@@ -47,7 +47,7 @@ abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
|
|||||||
$schemaTool->updateSchema($metaData);
|
$schemaTool->updateSchema($metaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection = $this->createDefaultDBConnection(self::$pdo, $GLOBALS["DB_NAME"]);
|
$this->connection = $this->createDefaultDBConnection(self::$pdo, getenv('DB_NAME'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// It is important to clear the cache of the entity manager every time a new test runs!
|
// It is important to clear the cache of the entity manager every time a new test runs!
|
||||||
|
|||||||
Reference in New Issue
Block a user