bug #10146 [WebProfilerBundle] fixed parsing Mongo DSN and added Test for it (malarzm)

This PR was submitted for the 2.3-dev branch but it was merged into the 2.3 branch instead (closes #10146).

Discussion
----------

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

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10105
| License       | MIT
| Doc PR        |

Commits
-------

5cd274b [WebProfilerBundle] fixed parsing Mongo DSN and added Test for it
This commit is contained in:
Fabien Potencier 2014-02-21 07:33:54 +01:00
commit a8955bf96c
2 changed files with 61 additions and 5 deletions

View File

@ -100,11 +100,8 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
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 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
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);
}
}

View File

@ -71,6 +71,32 @@ class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
}
}
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 @@ class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
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');