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
This commit is contained in:
parent
5e665b5c7a
commit
5f0557ef27
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user