2011-07-23 19:05:48 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\HttpKernel\Profiler;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\Profiler\MongoDbProfilerStorage;
|
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profile;
|
|
|
|
|
2011-09-04 15:56:09 +01:00
|
|
|
class DummyMongoDbProfilerStorage extends MongoDbProfilerStorage {
|
|
|
|
public function getMongo() {
|
|
|
|
return parent::getMongo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-23 19:05:48 +01:00
|
|
|
class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected static $storage;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
if (extension_loaded('mongo')) {
|
2011-09-19 21:37:12 +01:00
|
|
|
self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
|
2011-09-04 15:56:09 +01:00
|
|
|
try {
|
|
|
|
self::$storage->getMongo();
|
|
|
|
} catch(\MongoConnectionException $e) {
|
|
|
|
$this->markTestSkipped('MongoDbProfilerStorageTest requires that there is a MongoDB server present on localhost');
|
|
|
|
}
|
2011-07-23 19:05:48 +01:00
|
|
|
self::$storage->purge();
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped('MongoDbProfilerStorageTest requires that the extension mongo is loaded');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStore()
|
|
|
|
{
|
2011-09-19 21:37:12 +01:00
|
|
|
for ($i = 0; $i < 10; $i++) {
|
2011-07-23 19:05:48 +01:00
|
|
|
$profile = new Profile('token_'.$i);
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
}
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar', 20)), 10, '->write() stores data in the database');
|
|
|
|
self::$storage->purge();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStoreSpecialCharsInUrl()
|
|
|
|
{
|
|
|
|
// The storage accepts special characters in URLs (Even though URLs are not
|
|
|
|
// supposed to contain them)
|
|
|
|
$profile = new Profile('simple_quote');
|
|
|
|
$profile->setUrl('127.0.0.1', 'http://foo.bar/\'');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
$this->assertTrue(false !== self::$storage->read('simple_quote'), '->write() accepts single quotes in URL');
|
|
|
|
|
|
|
|
$profile = new Profile('double_quote');
|
|
|
|
$profile->setUrl('127.0.0.1', 'http://foo.bar/"');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
$this->assertTrue(false !== self::$storage->read('double_quote'), '->write() accepts double quotes in URL');
|
|
|
|
|
|
|
|
$profile = new Profile('backslash');
|
|
|
|
$profile->setUrl('127.0.0.1', 'http://foo.bar/\\');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
$this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
|
|
|
|
|
|
|
|
self::$storage->purge();
|
|
|
|
}
|
2011-09-07 21:32:54 +01:00
|
|
|
|
2011-09-07 21:38:22 +01:00
|
|
|
public function testStoreTime()
|
|
|
|
{
|
2011-09-19 21:37:12 +01:00
|
|
|
$dt = new \DateTime('now');
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2011-09-07 21:38:22 +01:00
|
|
|
$dt->modify('+1 minute');
|
|
|
|
$profile = new Profile('time_'.$i);
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar');
|
2011-09-11 11:26:12 +01:00
|
|
|
$profile->setTime($dt->getTimestamp());
|
2011-09-07 21:38:22 +01:00
|
|
|
self::$storage->write($profile);
|
|
|
|
}
|
|
|
|
$records = self::$storage->find('', '', 3);
|
|
|
|
$this->assertEquals(count($records), 3, '->find() returns all previously added records');
|
2011-09-14 21:15:57 +01:00
|
|
|
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
|
|
|
|
$this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
|
|
|
|
$this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
|
2011-09-07 21:38:22 +01:00
|
|
|
self::$storage->purge();
|
|
|
|
}
|
|
|
|
|
2011-07-23 19:05:48 +01:00
|
|
|
public function testRetrieveByIp()
|
|
|
|
{
|
|
|
|
$profile = new Profile('token');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', '', 10)), 1, '->find() retrieve a record by IP');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.%.1', '', 10)), 0, '->find() does not interpret a "%" as a wildcard in the IP');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0._.1', '', 10)), 0, '->find() does not interpret a "_" as a wildcard in the IP');
|
|
|
|
|
|
|
|
self::$storage->purge();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrieveByUrl()
|
|
|
|
{
|
|
|
|
$profile = new Profile('simple_quote');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar/\'');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$profile = new Profile('double_quote');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar/"');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$profile = new Profile('backslash');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo\\bar/');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$profile = new Profile('percent');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar/%');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$profile = new Profile('underscore');
|
|
|
|
$profile->setIp('127.0.0.1');
|
|
|
|
$profile->setUrl('http://foo.bar/_');
|
|
|
|
self::$storage->write($profile);
|
|
|
|
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/\'', 10)), 1, '->find() accepts single quotes in URLs');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/"', 10)), 1, '->find() accepts double quotes in URLs');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo\\bar/', 10)), 1, '->find() accepts backslash in URLs');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/%', 10)), 1, '->find() does not interpret a "%" as a wildcard in the URL');
|
|
|
|
$this->assertEquals(count(self::$storage->find('127.0.0.1', 'http://foo.bar/_', 10)), 1, '->find() does not interpret a "_" as a wildcard in the URL');
|
|
|
|
|
|
|
|
self::$storage->purge();
|
|
|
|
}
|
2011-09-07 21:32:54 +01:00
|
|
|
|
|
|
|
public function testRetrieveByEmptyUrlAndIp()
|
|
|
|
{
|
2011-09-19 21:37:12 +01:00
|
|
|
for ($i = 0; $i < 5; $i++) {
|
2011-09-07 21:32:54 +01:00
|
|
|
$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();
|
|
|
|
}
|
2011-09-19 21:37:12 +01:00
|
|
|
|
|
|
|
public function testCleanup()
|
|
|
|
{
|
|
|
|
$dt = new \DateTime('-2 day');
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
|
|
|
$dt->modify('-1 day');
|
|
|
|
$profile = new Profile('time_'.$i);
|
|
|
|
$profile->setTime($dt->getTimestamp());
|
|
|
|
self::$storage->write($profile);
|
|
|
|
}
|
|
|
|
$records = self::$storage->find('', '', 3);
|
|
|
|
$this->assertEquals(count($records), 1, '->find() returns only one record');
|
|
|
|
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
|
|
|
|
self::$storage->purge();
|
|
|
|
}
|
2011-07-23 19:05:48 +01:00
|
|
|
}
|