Skip to content

Commit

Permalink
Fix parseDSN not working for SqlLocalDB
Browse files Browse the repository at this point in the history
Also fix the regex for url with fragment.
  • Loading branch information
chinpei215 committed Aug 21, 2017
1 parent fffda82 commit 8a91fca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
15 changes: 3 additions & 12 deletions src/Core/StaticConfigTrait.php
Expand Up @@ -249,9 +249,9 @@ public static function parseDsn($dsn)
throw new InvalidArgumentException('Only strings can be passed to parseDsn');
}

$pattern = '/^(?P<scheme>[\w\\\\]+):\/\/((?P<user>.*?)(:(?P<password>.*?))?@)?' .
'((?P<host>[.\w-\\\\]+)(:(?P<port>\d+))?)?' .
'(?P<path>\/[^?]*)?(\?(?P<query>.*))?$/';
$pattern = '/^(?P<scheme>[\w\\\\]+):\/\/((?P<username>.*?)(:(?P<password>.*?))?@)?' .
'((?P<host>[^?#\/:@]+)(:(?P<port>\d+))?)?' .
'(?P<path>\/[^?#]*)?(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/';
preg_match($pattern, $dsn, $parsed);

if (!$parsed) {
Expand Down Expand Up @@ -285,15 +285,6 @@ public static function parseDsn($dsn)
}
}

if (isset($parsed['user'])) {
$parsed['username'] = $parsed['user'];
}

if (isset($parsed['pass'])) {
$parsed['password'] = $parsed['pass'];
}

unset($parsed['pass'], $parsed['user']);
$parsed = $queryArgs + $parsed;

if (empty($parsed['className'])) {
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Core/StaticConfigTraitTest.php
Expand Up @@ -196,7 +196,7 @@ public function testParseDsnQuerystring()
];
$this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));

$dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
$dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null#fragment';
$expected = [
'className' => 'Cake\Mailer\Transport\MailTransport',
'client' => null,
Expand All @@ -207,6 +207,7 @@ public function testParseDsnQuerystring()
'timeout' => '30',
'tls' => null,
'username' => 'user',
'fragment' => 'fragment',
];
$this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));

Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Datasource/ConnectionManagerTest.php
Expand Up @@ -346,6 +346,18 @@ public function dsnProvider()
'username' => 'sa',
]
],
'sqllocaldb' => [
'sqlserver://username:password@(localdb)\.\DeptSharedLocalDB/database',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlserver',
'host' => '(localdb)\.\DeptSharedLocalDB',
'password' => 'password',
'database' => 'database',
'scheme' => 'sqlserver',
'username' => 'username',
]
],
'classname query arg' => [
'mysql://localhost/database?className=Custom\Driver',
[
Expand Down

0 comments on commit 8a91fca

Please sign in to comment.