From 73692c62cfe7adcd73f74598696089657df05170 Mon Sep 17 00:00:00 2001 From: "H. Westphal" Date: Wed, 7 Sep 2011 22:32:54 +0200 Subject: [PATCH] Fixed MongoDbProfilerStorage::find() when passing empty parameters. --- .../Profiler/MongoDbProfilerStorage.php | 22 ++++++++++++++++++- .../Profiler/MongoDbProfilerStorageTest.php | 12 +++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php index 8e6edf6555..fa552e1e6f 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php @@ -37,7 +37,7 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface */ public function find($ip, $url, $limit) { - $cursor = $this->getMongo()->find(array('ip' => $ip, 'url' => $url))->limit($limit); + $cursor = $this->getMongo()->find($this->buildQuery($ip, $url))->limit($limit); $return = array(); foreach ($cursor as $profile) { $return[] = $profile['_id']; @@ -102,4 +102,24 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface return $this->mongo; } + + /** + * @param string $ip + * @param string $url + * @return array + */ + private function buildQuery($ip, $url) + { + $query = array(); + + if (!empty($ip)) { + $query['ip'] = $ip; + } + + if (!empty($url)) { + $query['url'] = $url; + } + + return $query; + } } diff --git a/tests/Symfony/Tests/Component/HttpKernel/Profiler/MongoDbProfilerStorageTest.php b/tests/Symfony/Tests/Component/HttpKernel/Profiler/MongoDbProfilerStorageTest.php index 74007cd129..6f03a858bc 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Profiler/MongoDbProfilerStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Profiler/MongoDbProfilerStorageTest.php @@ -71,7 +71,7 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase self::$storage->purge(); } - + public function testRetrieveByIp() { $profile = new Profile('token'); @@ -121,4 +121,14 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase self::$storage->purge(); } + + public function testRetrieveByEmptyUrlAndIp() + { + for ($i = 0; $i < 5; $i ++) { + $profile = new Profile('token_'.$i); + self::$storage->write($profile); + } + $this->assertEquals(count(self::$storage->find('', '', 10)), 5, '->find() returns all previously added records'); + self::$storage->purge(); + } }