Skip to content

Commit 5f0557e

Browse files
Maciej Malarzfabpot
authored andcommitted
fixed parsing Mongo DSN and added Test for it
Applied Coding Standards patch Applied Fabien's comments [WebProfilerBundle] fixed parsing Mongo DSN and added Test for it Removing whitespaces Applied fabbot patch
1 parent 5e665b5 commit 5f0557e

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,8 @@ public function write(Profile $profile)
100100
protected function getMongo()
101101
{
102102
if ($this->mongo === null) {
103-
if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) {
104-
$server = $matches[1].(!empty($matches[2]) ? '/'.$matches[2] : '');
105-
$database = $matches[2];
106-
$collection = $matches[3];
107-
103+
if ($parsedDsn = $this->parseDsn($this->dsn)) {
104+
list($server, $database, $collection) = $parsedDsn;
108105
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
109106
$mongo = new $mongoClass($server);
110107
$this->mongo = $mongo->selectCollection($database, $collection);
@@ -233,4 +230,25 @@ private function getProfile(array $data)
233230

234231
return $profile;
235232
}
233+
234+
/**
235+
* @param string $dsn
236+
*
237+
* @return null|array Array($server, $database, $collection)
238+
*/
239+
private function parseDsn($dsn)
240+
{
241+
if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
242+
return null;
243+
}
244+
$server = $matches[1];
245+
$database = $matches[2];
246+
$collection = $matches[3];
247+
preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
248+
if ($matchesServer[5]=="" && $matches[2]!="") {
249+
$server .= '/'.$matches[2];
250+
}
251+
252+
return array($server, $database, $collection);
253+
}
236254
}

src/Symfony/Component/HttpKernel/Tests/Profiler/MongoDbProfilerStorageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ public static function tearDownAfterClass()
7171
}
7272
}
7373

74+
public function providerDsn()
75+
{
76+
return array(
77+
array('mongodb://localhost/symfony_tests/profiler_data', array(
78+
'mongodb://localhost/symfony_tests',
79+
'symfony_tests',
80+
'profiler_data'
81+
)),
82+
array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
83+
'mongodb://user:password@localhost/symfony_tests',
84+
'symfony_tests',
85+
'profiler_data'
86+
)),
87+
array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
88+
'mongodb://user:password@localhost/admin',
89+
'symfony_tests',
90+
'profiler_data'
91+
)),
92+
array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
93+
'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
94+
'symfony_tests',
95+
'profiler_data'
96+
))
97+
);
98+
}
99+
74100
public function testCleanup()
75101
{
76102
$dt = new \DateTime('-2 day');
@@ -87,6 +113,18 @@ public function testCleanup()
87113
self::$storage->purge();
88114
}
89115

116+
/**
117+
* @dataProvider providerDsn
118+
*/
119+
public function testDsnParser($dsn, $expected)
120+
{
121+
$r=new \ReflectionObject(self::$storage);
122+
$m=$r->getMethod('parseDsn');
123+
$m->setAccessible(true);
124+
125+
$this->assertEquals($expected, $m->invoke(self::$storage, $dsn));
126+
}
127+
90128
public function testUtf8()
91129
{
92130
$profile = new Profile('utf8_test_profile');

0 commit comments

Comments
 (0)