Refactor Config\Env to accept DB constants as parameters
[MAILPOET-1886]
This commit is contained in:
committed by
M. Shull
parent
b42f184b1d
commit
a35d7dc7c6
@@ -37,7 +37,7 @@ class Env {
|
||||
static $db_charset_collate;
|
||||
static $db_timezone_offset;
|
||||
|
||||
static function init($file, $version) {
|
||||
static function init($file, $version, $db_host, $db_user, $db_password, $db_name) {
|
||||
global $wpdb;
|
||||
self::$version = $version;
|
||||
self::$file = $file;
|
||||
@@ -56,27 +56,27 @@ class Env {
|
||||
self::$lib_path = self::$path . '/lib';
|
||||
self::$plugin_prefix = 'mailpoet_';
|
||||
self::$db_prefix = $wpdb->prefix . self::$plugin_prefix;
|
||||
self::$db_host = DB_HOST;
|
||||
self::$db_host = $db_host;
|
||||
self::$db_port = 3306;
|
||||
self::$db_socket = false;
|
||||
if (preg_match('/(?=:\d+$)/', DB_HOST)) {
|
||||
list(self::$db_host, self::$db_port) = explode(':', DB_HOST);
|
||||
if (preg_match('/(?=:\d+$)/', $db_host)) {
|
||||
list(self::$db_host, self::$db_port) = explode(':', $db_host);
|
||||
} else {
|
||||
if (preg_match('/:/', DB_HOST)) {
|
||||
if (preg_match('/:/', $db_host)) {
|
||||
self::$db_socket = true;
|
||||
}
|
||||
}
|
||||
self::$db_name = DB_NAME;
|
||||
self::$db_username = DB_USER;
|
||||
self::$db_password = DB_PASSWORD;
|
||||
self::$db_name = $db_name;
|
||||
self::$db_username = $db_user;
|
||||
self::$db_password = $db_password;
|
||||
self::$db_charset = $wpdb->charset;
|
||||
self::$db_collation = $wpdb->collate;
|
||||
self::$db_charset_collate = $wpdb->get_charset_collate();
|
||||
self::$db_source_name = self::dbSourceName(self::$db_host, self::$db_socket, self::$db_port, self::$db_charset);
|
||||
self::$db_source_name = self::dbSourceName(self::$db_host, self::$db_socket, self::$db_port, self::$db_charset, self::$db_name);
|
||||
self::$db_timezone_offset = self::getDbTimezoneOffset();
|
||||
}
|
||||
|
||||
private static function dbSourceName($host, $socket, $port, $charset) {
|
||||
private static function dbSourceName($host, $socket, $port, $charset, $db_name) {
|
||||
$source_name = array(
|
||||
(!$socket) ? 'mysql:host=' : 'mysql:unix_socket=',
|
||||
$host,
|
||||
@@ -85,7 +85,7 @@ class Env {
|
||||
$port,
|
||||
';',
|
||||
'dbname=',
|
||||
DB_NAME
|
||||
$db_name
|
||||
);
|
||||
if (!empty($charset)) {
|
||||
$source_name[] = ';charset=' . $charset;
|
||||
|
@@ -8,7 +8,14 @@ require_once($mailpoet_plugin['autoloader']);
|
||||
|
||||
define('MAILPOET_VERSION', $mailpoet_plugin['version']);
|
||||
|
||||
Env::init($mailpoet_plugin['filename'], $mailpoet_plugin['version']);
|
||||
Env::init(
|
||||
$mailpoet_plugin['filename'],
|
||||
$mailpoet_plugin['version'],
|
||||
DB_HOST,
|
||||
DB_USER,
|
||||
DB_PASSWORD,
|
||||
DB_NAME
|
||||
);
|
||||
|
||||
$initializer = MailPoet\DI\ContainerWrapper::getInstance()->get(MailPoet\Config\Initializer::class);
|
||||
$initializer->init();
|
||||
|
@@ -9,8 +9,7 @@ class EnvTest extends \MailPoetTest {
|
||||
// Back up original environment values
|
||||
$this->file = Env::$file;
|
||||
$this->version = Env::$version;
|
||||
|
||||
Env::init('file', '1.0.0');
|
||||
Env::init('file', '1.0.0', 'localhost:3306', DB_USER, DB_PASSWORD, DB_NAME);
|
||||
}
|
||||
|
||||
function testItCanReturnPluginPrefix() {
|
||||
@@ -23,24 +22,10 @@ class EnvTest extends \MailPoetTest {
|
||||
expect(Env::$db_prefix)->equals($db_prefix);
|
||||
}
|
||||
|
||||
function testItCanReturnDbHost() {
|
||||
if (preg_match('/(?=:\d+$)/', DB_HOST)) {
|
||||
expect(Env::$db_host)->equals(explode(':', DB_HOST)[0]);
|
||||
} else expect(Env::$db_host)->equals(DB_HOST);
|
||||
}
|
||||
|
||||
function testItCanReturnDbPort() {
|
||||
if (preg_match('/(?=:\d+$)/', DB_HOST)) {
|
||||
expect(Env::$db_port)->equals(explode(':', DB_HOST)[1]);
|
||||
} else expect(Env::$db_port)->equals(3306);
|
||||
}
|
||||
|
||||
function testItCanReturnSocket() {
|
||||
if (!preg_match('/(?=:\d+$)/', DB_HOST)
|
||||
&& preg_match('/:/', DB_HOST)
|
||||
) {
|
||||
expect(Env::$db_socket)->true();
|
||||
} else expect(Env::$db_socket)->false();
|
||||
function testItProcessDBHost() {
|
||||
Env::init('file', '1.0.0', 'localhost:3306', DB_USER, DB_PASSWORD, DB_NAME);
|
||||
expect(Env::$db_host)->equals('localhost');
|
||||
expect(Env::$db_port)->equals('3306');
|
||||
}
|
||||
|
||||
function testItCanReturnDbName() {
|
||||
@@ -88,6 +73,6 @@ class EnvTest extends \MailPoetTest {
|
||||
|
||||
function _after() {
|
||||
// Restore the original environment
|
||||
Env::init($this->file, $this->version);
|
||||
Env::init($this->file, $this->version, DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user