Fixed MongoDbProfilerStorage::find() when passing empty parameters.

This commit is contained in:
H. Westphal 2011-09-07 22:32:54 +02:00
parent 4cd2dec01d
commit 73692c62cf
2 changed files with 32 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}