Skip to content

Commit

Permalink
Fix config prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Apr 23, 2020
1 parent 7facab2 commit 4db3d59
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
28 changes: 12 additions & 16 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
class Factory {

/**
* default ini prefix for db config
*/
const CONFIG_PREFIX = 'dealnews.db';

/**
* Creates a new PDO connection or returns one that already exists
*
Expand Down Expand Up @@ -170,23 +165,24 @@ public static function getConfig(string $db, GetConfig $cfg = null): array {

// Check for an altername environment for this db
$prefix = $cfg->get('db.factory.prefix');
if (empty($prefix)) {
$prefix = self::CONFIG_PREFIX;

if (!empty($prefix)) {
$prefix .= ".";
}

$config = [
'type' => $cfg->get($prefix . ".$db.type"),
'db' => $cfg->get($prefix . ".$db.db"),
'user' => $cfg->get($prefix . ".$db.user"),
'pass' => $cfg->get($prefix . ".$db.pass"),
'type' => $cfg->get($prefix . "$db.type"),
'db' => $cfg->get($prefix . "$db.db"),
'user' => $cfg->get($prefix . "$db.user"),
'pass' => $cfg->get($prefix . "$db.pass"),
// PDO only
'dsn' => $cfg->get($prefix . ".$db.dsn"),
'options' => $cfg->get($prefix . ".$db.options"),
'dsn' => $cfg->get($prefix . "$db.dsn"),
'options' => $cfg->get($prefix . "$db.options"),
// pgsql and mysql only
'server' => $cfg->get($prefix . ".$db.server"),
'port' => $cfg->get($prefix . ".$db.port"),
'server' => $cfg->get($prefix . "$db.server"),
'port' => $cfg->get($prefix . "$db.port"),
// mysql only
'charset' => $cfg->get($prefix . ".$db.charset"),
'charset' => $cfg->get($prefix . "$db.charset"),
];

if (empty($config['db'])) {
Expand Down
36 changes: 18 additions & 18 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function testGetConfigEmptyDB() {
// Create a map of arguments to return values.
$map = [
['db.factory.prefix', null],
[Factory::CONFIG_PREFIX . '.test.type', 'type'],
[Factory::CONFIG_PREFIX . '.test.db', null],
[Factory::CONFIG_PREFIX . '.test.user', 'user'],
[Factory::CONFIG_PREFIX . '.test.pass', 'pass'],
[Factory::CONFIG_PREFIX . '.test.dsn', 'dsn'],
[Factory::CONFIG_PREFIX . '.test.options', 'options'],
[Factory::CONFIG_PREFIX . '.test.server', 'server'],
[Factory::CONFIG_PREFIX . '.test.port', 'port'],
[Factory::CONFIG_PREFIX . '.test.charset', 'charset'],
['test.type', 'type'],
['test.db', null],
['test.user', 'user'],
['test.pass', 'pass'],
['test.dsn', 'dsn'],
['test.options', 'options'],
['test.server', 'server'],
['test.port', 'port'],
['test.charset', 'charset'],
];

// Configure the stub.
Expand Down Expand Up @@ -71,15 +71,15 @@ public function testGetConfigDefaultPrefix() {
// Create a map of arguments to return values.
$map = [
['db.factory.prefix', null],
[Factory::CONFIG_PREFIX . '.test.type', 'type'],
[Factory::CONFIG_PREFIX . '.test.db', 'db'],
[Factory::CONFIG_PREFIX . '.test.user', 'user'],
[Factory::CONFIG_PREFIX . '.test.pass', 'pass'],
[Factory::CONFIG_PREFIX . '.test.dsn', 'dsn'],
[Factory::CONFIG_PREFIX . '.test.options', 'options'],
[Factory::CONFIG_PREFIX . '.test.server', 'server'],
[Factory::CONFIG_PREFIX . '.test.port', 'port'],
[Factory::CONFIG_PREFIX . '.test.charset', 'charset'],
['test.type', 'type'],
['test.db', 'db'],
['test.user', 'user'],
['test.pass', 'pass'],
['test.dsn', 'dsn'],
['test.options', 'options'],
['test.server', 'server'],
['test.port', 'port'],
['test.charset', 'charset'],
];

// Configure the stub.
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function _shutdown() {
}

// Setup config variables
putenv('db.factory.prefix=dealnews.db');
putenv('DEALNEWS_DB_CHINOOK_TYPE=pdo');
putenv('DEALNEWS_DB_CHINOOK_DSN=sqlite:tests/chinook.db');

Expand Down

0 comments on commit 4db3d59

Please sign in to comment.