Refactor Config\Env to accept DB constants as parameters

[MAILPOET-1886]
This commit is contained in:
Rostislav Wolny
2019-03-11 17:39:33 +01:00
committed by M. Shull
parent b42f184b1d
commit a35d7dc7c6
3 changed files with 25 additions and 33 deletions

View File

@@ -37,7 +37,7 @@ class Env {
static $db_charset_collate; static $db_charset_collate;
static $db_timezone_offset; static $db_timezone_offset;
static function init($file, $version) { static function init($file, $version, $db_host, $db_user, $db_password, $db_name) {
global $wpdb; global $wpdb;
self::$version = $version; self::$version = $version;
self::$file = $file; self::$file = $file;
@@ -56,27 +56,27 @@ class Env {
self::$lib_path = self::$path . '/lib'; self::$lib_path = self::$path . '/lib';
self::$plugin_prefix = 'mailpoet_'; self::$plugin_prefix = 'mailpoet_';
self::$db_prefix = $wpdb->prefix . self::$plugin_prefix; self::$db_prefix = $wpdb->prefix . self::$plugin_prefix;
self::$db_host = DB_HOST; self::$db_host = $db_host;
self::$db_port = 3306; self::$db_port = 3306;
self::$db_socket = false; self::$db_socket = false;
if (preg_match('/(?=:\d+$)/', DB_HOST)) { if (preg_match('/(?=:\d+$)/', $db_host)) {
list(self::$db_host, self::$db_port) = explode(':', DB_HOST); list(self::$db_host, self::$db_port) = explode(':', $db_host);
} else { } else {
if (preg_match('/:/', DB_HOST)) { if (preg_match('/:/', $db_host)) {
self::$db_socket = true; self::$db_socket = true;
} }
} }
self::$db_name = DB_NAME; self::$db_name = $db_name;
self::$db_username = DB_USER; self::$db_username = $db_user;
self::$db_password = DB_PASSWORD; self::$db_password = $db_password;
self::$db_charset = $wpdb->charset; self::$db_charset = $wpdb->charset;
self::$db_collation = $wpdb->collate; self::$db_collation = $wpdb->collate;
self::$db_charset_collate = $wpdb->get_charset_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(); 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( $source_name = array(
(!$socket) ? 'mysql:host=' : 'mysql:unix_socket=', (!$socket) ? 'mysql:host=' : 'mysql:unix_socket=',
$host, $host,
@@ -85,7 +85,7 @@ class Env {
$port, $port,
';', ';',
'dbname=', 'dbname=',
DB_NAME $db_name
); );
if (!empty($charset)) { if (!empty($charset)) {
$source_name[] = ';charset=' . $charset; $source_name[] = ';charset=' . $charset;

View File

@@ -8,7 +8,14 @@ require_once($mailpoet_plugin['autoloader']);
define('MAILPOET_VERSION', $mailpoet_plugin['version']); 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 = MailPoet\DI\ContainerWrapper::getInstance()->get(MailPoet\Config\Initializer::class);
$initializer->init(); $initializer->init();

View File

@@ -9,8 +9,7 @@ class EnvTest extends \MailPoetTest {
// Back up original environment values // Back up original environment values
$this->file = Env::$file; $this->file = Env::$file;
$this->version = Env::$version; $this->version = Env::$version;
Env::init('file', '1.0.0', 'localhost:3306', DB_USER, DB_PASSWORD, DB_NAME);
Env::init('file', '1.0.0');
} }
function testItCanReturnPluginPrefix() { function testItCanReturnPluginPrefix() {
@@ -23,24 +22,10 @@ class EnvTest extends \MailPoetTest {
expect(Env::$db_prefix)->equals($db_prefix); expect(Env::$db_prefix)->equals($db_prefix);
} }
function testItCanReturnDbHost() { function testItProcessDBHost() {
if (preg_match('/(?=:\d+$)/', DB_HOST)) { Env::init('file', '1.0.0', 'localhost:3306', DB_USER, DB_PASSWORD, DB_NAME);
expect(Env::$db_host)->equals(explode(':', DB_HOST)[0]); expect(Env::$db_host)->equals('localhost');
} else expect(Env::$db_host)->equals(DB_HOST); expect(Env::$db_port)->equals('3306');
}
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 testItCanReturnDbName() { function testItCanReturnDbName() {
@@ -88,6 +73,6 @@ class EnvTest extends \MailPoetTest {
function _after() { function _after() {
// Restore the original environment // Restore the original environment
Env::init($this->file, $this->version); Env::init($this->file, $this->version, DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
} }
} }