Permalink
Browse files

[WebProfilerBundle] 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
  • Loading branch information...
1 parent 6a0de7f commit 5cd274b3442c7c811a60f24095a7a91b2ee391ec @malarzm malarzm committed Jan 27, 2014
@@ -100,11 +100,8 @@ public function write(Profile $profile)
protected function getMongo()
{
if ($this->mongo === null) {
- if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) {
- $server = $matches[1].(!empty($matches[2]) ? '/'.$matches[2] : '');
- $database = $matches[2];
- $collection = $matches[3];
-
+ if ($parsedDsn = $this->parseDsn($this->dsn)) {
+ list($server, $database, $collection) = $parsedDsn;
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
$mongo = new $mongoClass($server);
$this->mongo = $mongo->selectCollection($database, $collection);
@@ -233,4 +230,25 @@ private function getProfile(array $data)
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);
+ }
}
@@ -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()
{
$dt = new \DateTime('-2 day');
@@ -87,6 +113,18 @@ public function testCleanup()
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()
{
$profile = new Profile('utf8_test_profile');

0 comments on commit 5cd274b

Please sign in to comment.