Skip to content

Commit

Permalink
Added support for different config nodes and custom config locations;…
Browse files Browse the repository at this point in the history
… fixed connection issues due to incomplete DSN
  • Loading branch information
ludwig-wacker committed Apr 17, 2018
1 parent 188f435 commit 97b3062
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
32 changes: 21 additions & 11 deletions src/Helper/DbSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ class DbSettings
*/
private $config;

/**
* @var string
*/
private $configNode;

/**
* @param string $file path to app/etc/local.xml
* @param string $configNode
*/
public function __construct($file)
public function __construct(string $file, string $configNode = 'default_setup')
{
$this->configNode = $configNode;

$this->setFile($file);
}

Expand All @@ -50,7 +58,7 @@ public function setFile($file)
);
}

$saved = libxml_use_internal_errors(true);
$saved = libxml_use_internal_errors(true);
$config = simplexml_load_file($file);
libxml_use_internal_errors($saved);

Expand All @@ -65,8 +73,10 @@ public function setFile($file)
throw new InvalidArgumentException('DB global resources was not found in "app/etc/local.xml"-file');
}

if (!$resources->default_setup->connection) {
throw new InvalidArgumentException('DB settings (default_setup) was not found in "app/etc/local.xml"-file');
if (!$resources->{$this->configNode}->connection) {
throw new InvalidArgumentException(
sprintf('DB settings (%s) was not found in "app/etc/local.xml"-file', $this->configNode)
);
}

$this->parseResources($resources);
Expand All @@ -89,7 +99,7 @@ private function parseResources(SimpleXMLElement $resources)
'password' => null,
);

$config = array_merge($config, (array) $resources->default_setup->connection);
$config = array_merge($config, (array) $resources->{$this->configNode}->connection);
$config['prefix'] = (string) $resources->db->table_prefix;

// known parameters: host, port, unix_socket, dbname, username, password, options, charset, persistent,
Expand All @@ -111,12 +121,12 @@ private function parseResources(SimpleXMLElement $resources)
$this->config = $config;

$this->tablePrefix = $config['prefix'];
$this->host = $config['host'];
$this->port = $config['port'];
$this->unixSocket = $config['unix_socket'];
$this->dbName = $config['dbname'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->host = $config['host'];
$this->port = $config['port'];
$this->unixSocket = $config['unix_socket'];
$this->dbName = $config['dbname'];
$this->username = $config['username'];
$this->password = $config['password'];
}

/**
Expand Down
42 changes: 36 additions & 6 deletions src/Module/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,51 @@

use Codeception\Lib\ModuleContainer;
use Codeception\Module\Db;

use Mothership\Codeception\Helper\DbSettings;

/**
* Class Database
*
* The purpose of this module is to load the DB settings from Magento's local.xml and configure codeception's DB module
* accordingly.
* This is especially useful when Codeception Tests are run in different environments and the connection settings
* shouldn't be hardcoded in the codeception config files.
*
* @package Mothership\Codeception\Module
*/
class Database extends Db
{
public function __construct(ModuleContainer $moduleContainer, $config = null)
/**
* Database constructor.
* @param ModuleContainer $moduleContainer
* @param array $config
*/
public function __construct(ModuleContainer $moduleContainer, $config = [])
{
// This (obviously) NEEDS to happen before the parent construct
$dbSettings = new DbSettings('app/etc/local.xml');
// This obviously NEEDS to happen before the parent constructor is called

// 'config_location' acts as a full override
if (array_key_exists('config_location', $config)) {
$configLocation = $config['config_location'];
} else {
$configLocation = class_exists('\\Mage') ? \Mage::getBaseDir() . '/app/etc/local.xml' : 'app/etc/local.xml';
}

$config = [
'dsn' => $dbSettings->getDsn(),
$configNode = array_key_exists('config_node', $config) ? $config['config_node'] : 'default_setup';
$dbSettings = new DbSettings($configLocation, $configNode);

$_config = [
'dsn' => $dbSettings->getDsn() . ';dbname=' . $dbSettings->getDatabaseName(),
'user' => $dbSettings->getUsername(),
'password' => $dbSettings->getPassword()
];

parent::__construct($moduleContainer, $config);
$this->debugSection('MS-DB-Middleware',
sprintf("Trying to connect to '%s', user: '%s', db: '%s'. Full DSN: '%s'.",
$dbSettings->getDsn(), $dbSettings->getUsername(), $dbSettings->getDatabaseName(), $config['dsn'])
);

parent::__construct($moduleContainer, $_config);
}
}

0 comments on commit 97b3062

Please sign in to comment.