Skip to content

Commit

Permalink
fixed parsing Mongo DSN and added Test for it
Browse files Browse the repository at this point in the history
Applied Coding Standards patch

Applied Fabien's comments

[WebProfilerBundle] fixed parsing Mongo DSN and added Test for it

Removing whitespaces

Applied fabbot patch
  • Loading branch information
Maciej Malarz authored and fabpot committed Feb 21, 2014
1 parent 5e665b5 commit 5f0557e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
Expand Up @@ -100,11 +100,8 @@ public function write(Profile $profile)
protected function getMongo() protected function getMongo()
{ {
if ($this->mongo === null) { if ($this->mongo === null) {
if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) { if ($parsedDsn = $this->parseDsn($this->dsn)) {
$server = $matches[1].(!empty($matches[2]) ? '/'.$matches[2] : ''); list($server, $database, $collection) = $parsedDsn;
$database = $matches[2];
$collection = $matches[3];

$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient'; $mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
$mongo = new $mongoClass($server); $mongo = new $mongoClass($server);
$this->mongo = $mongo->selectCollection($database, $collection); $this->mongo = $mongo->selectCollection($database, $collection);
Expand Down Expand Up @@ -233,4 +230,25 @@ private function getProfile(array $data)


return $profile; return $profile;
} }

/**
* @param string $dsn
*
* @return null|array Array($server, $database, $collection)
*/
private function parseDsn($dsn)
{
if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
return null;
}
$server = $matches[1];
$database = $matches[2];
$collection = $matches[3];
preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
if ($matchesServer[5]=="" && $matches[2]!="") {
$server .= '/'.$matches[2];
}

return array($server, $database, $collection);
}
} }
Expand Up @@ -71,6 +71,32 @@ public static function tearDownAfterClass()
} }
} }


public function providerDsn()
{
return array(
array('mongodb://localhost/symfony_tests/profiler_data', array(
'mongodb://localhost/symfony_tests',
'symfony_tests',
'profiler_data'
)),
array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/symfony_tests',
'symfony_tests',
'profiler_data'
)),
array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/admin',
'symfony_tests',
'profiler_data'
)),
array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
'symfony_tests',
'profiler_data'
))
);
}

public function testCleanup() public function testCleanup()
{ {
$dt = new \DateTime('-2 day'); $dt = new \DateTime('-2 day');
Expand All @@ -87,6 +113,18 @@ public function testCleanup()
self::$storage->purge(); self::$storage->purge();
} }


/**
* @dataProvider providerDsn
*/
public function testDsnParser($dsn, $expected)
{
$r=new \ReflectionObject(self::$storage);
$m=$r->getMethod('parseDsn');
$m->setAccessible(true);

$this->assertEquals($expected, $m->invoke(self::$storage, $dsn));
}

public function testUtf8() public function testUtf8()
{ {
$profile = new Profile('utf8_test_profile'); $profile = new Profile('utf8_test_profile');
Expand Down

0 comments on commit 5f0557e

Please sign in to comment.