Added support for sorting by time like other profiler storage implementations.

This commit is contained in:
H. Westphal 2011-09-07 22:38:22 +02:00
parent 73692c62cf
commit 85c380647c
2 changed files with 21 additions and 1 deletions

View File

@ -37,7 +37,7 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
*/
public function find($ip, $url, $limit)
{
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url))->limit($limit);
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url))->sort(array('time' => -1))->limit($limit);
$return = array();
foreach ($cursor as $profile) {
$return[] = $profile['_id'];
@ -83,6 +83,7 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
'_id' => $profile->getToken(),
'ip' => $profile->getIp(),
'url' => $profile->getUrl() === null ? '' : $profile->getUrl(),
'time' => null === $profile->getTime() ? null : new \MongoDate(strtotime($profile->getTime())),
'profile' => serialize($profile)
));
}

View File

@ -72,6 +72,25 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
self::$storage->purge();
}
public function testStoreTime()
{
$dt = new \DateTime('2011-09-07 00:00:00');
for ($i = 0; $i < 3; $i ++) {
$dt->modify('+1 minute');
$profile = new Profile('time_'.$i);
$profile->setIp('127.0.0.1');
$profile->setUrl('http://foo.bar');
$profile->setTime($dt->format('Y-m-d H:i:s'));
self::$storage->write($profile);
}
$records = self::$storage->find('', '', 3);
$this->assertEquals(count($records), 3, '->find() returns all previously added records');
$this->assertEquals($records[0], 'time_2', '->find() returns records ordered by time in descendant order');
$this->assertEquals($records[1], 'time_1', '->find() returns records ordered by time in descendant order');
$this->assertEquals($records[2], 'time_0', '->find() returns records ordered by time in descendant order');
self::$storage->purge();
}
public function testRetrieveByIp()
{
$profile = new Profile('token');