From 4db3d596a946ba281b4d073e536b7fef2c191635 Mon Sep 17 00:00:00 2001 From: Brian Moon Date: Thu, 23 Apr 2020 00:40:52 -0500 Subject: [PATCH] Fix config prefix --- src/Factory.php | 28 ++++++++++++---------------- tests/FactoryTest.php | 36 ++++++++++++++++++------------------ tests/bootstrap.php | 1 + 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index ce27ded..5f280c0 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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 * @@ -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'])) { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 5363f43..ba0820c 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -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. @@ -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. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6894286..8c4395c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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');