Fix unix_socket parsing from DB_HOST
[MAILPOET-1886]
This commit is contained in:
committed by
M. Shull
parent
a35d7dc7c6
commit
5193df6a6d
@@ -59,12 +59,13 @@ class Env {
|
||||
self::$db_host = $db_host;
|
||||
self::$db_port = 3306;
|
||||
self::$db_socket = false;
|
||||
// Peel off the port parameter
|
||||
if (preg_match('/(?=:\d+$)/', $db_host)) {
|
||||
list(self::$db_host, self::$db_port) = explode(':', $db_host);
|
||||
} else {
|
||||
if (preg_match('/:/', $db_host)) {
|
||||
self::$db_socket = true;
|
||||
}
|
||||
// Peel off the socket parameter
|
||||
if (preg_match('/:\//', self::$db_host)) {
|
||||
list(self::$db_host, self::$db_socket) = explode(':', $db_host);
|
||||
}
|
||||
self::$db_name = $db_name;
|
||||
self::$db_username = $db_user;
|
||||
@@ -78,7 +79,7 @@ class Env {
|
||||
|
||||
private static function dbSourceName($host, $socket, $port, $charset, $db_name) {
|
||||
$source_name = array(
|
||||
(!$socket) ? 'mysql:host=' : 'mysql:unix_socket=',
|
||||
'mysql:host=',
|
||||
$host,
|
||||
';',
|
||||
'port=',
|
||||
@@ -87,6 +88,9 @@ class Env {
|
||||
'dbname=',
|
||||
$db_name
|
||||
);
|
||||
if (!empty($socket)) {
|
||||
$source_name[] = ';unix_socket=' . $socket;
|
||||
}
|
||||
if (!empty($charset)) {
|
||||
$source_name[] = ';charset=' . $charset;
|
||||
}
|
||||
|
@@ -23,9 +23,22 @@ class EnvTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
function testItProcessDBHost() {
|
||||
Env::init('file', '1.0.0', 'localhost:3306', DB_USER, DB_PASSWORD, DB_NAME);
|
||||
Env::init('file', '1.0.0', 'localhost', 'db_user', 'pass123', 'db_name');
|
||||
expect(Env::$db_host)->equals('localhost');
|
||||
expect(Env::$db_port)->equals('3306');
|
||||
expect(Env::$db_source_name)->equals('mysql:host=localhost;port=3306;dbname=db_name;charset='. ENV::$db_charset);
|
||||
|
||||
Env::init('file', '1.0.0', 'localhost:3307', 'db_user', 'pass123', 'db_name');
|
||||
expect(Env::$db_host)->equals('localhost');
|
||||
expect(Env::$db_port)->equals('3307');
|
||||
expect(Env::$db_source_name)->equals('mysql:host=localhost;port=3307;dbname=db_name;charset='. ENV::$db_charset);
|
||||
}
|
||||
|
||||
function testItProcessDBHostWithSocket() {
|
||||
Env::init('file', '1.0.0', 'localhost:/var/lib/mysql/mysql55.sock', 'db_user', 'pass123', 'db_name');
|
||||
expect(Env::$db_host)->equals('localhost');
|
||||
expect(Env::$db_socket)->equals('/var/lib/mysql/mysql55.sock');
|
||||
expect(Env::$db_source_name)->equals('mysql:host=localhost;port=3306;dbname=db_name;unix_socket=/var/lib/mysql/mysql55.sock;charset='. ENV::$db_charset);
|
||||
}
|
||||
|
||||
function testItCanReturnDbName() {
|
||||
@@ -58,13 +71,6 @@ class EnvTest extends \MailPoetTest {
|
||||
expect(Env::$db_charset_collate)->equals($charset_collate);
|
||||
}
|
||||
|
||||
function testItCanGenerateDbSourceName() {
|
||||
$source_name = ((!ENV::$db_socket) ? 'mysql:host=' : 'mysql:unix_socket=') .
|
||||
ENV::$db_host . ';port=' . ENV::$db_port . ';dbname=' . DB_NAME .
|
||||
(!empty(ENV::$db_charset) ? ';charset=' . ENV::$db_charset : '');
|
||||
expect(Env::$db_source_name)->equals($source_name);
|
||||
}
|
||||
|
||||
function testItCanGetDbTimezoneOffset() {
|
||||
expect(Env::getDbTimezoneOffset('+1.5'))->equals("+01:30");
|
||||
expect(Env::getDbTimezoneOffset('+11'))->equals("+11:00");
|
||||
|
Reference in New Issue
Block a user