4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Profiler;
14 use Symfony\Component\HttpKernel\Profiler\MongoDbProfilerStorage;
15 use Symfony\Component\HttpKernel\Profiler\Profile;
16 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
20 class MongoDbProfilerStorageTestDataCollector extends DataCollector
22 public function setData($data)
27 public function getData()
32 public function collect(Request $request, Response $response, \Exception $exception = null)
36 public function getName()
38 return 'test_data_collector';
44 * @requires extension mongo
46 class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
50 public function getDsns()
53 array('mongodb://localhost/symfony_tests/profiler_data', array(
54 'mongodb://localhost/symfony_tests',
58 array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
59 'mongodb://user:password@localhost/symfony_tests',
63 array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
64 'mongodb://user:password@localhost/admin',
68 array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
69 'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
76 public function testCleanup()
78 $dt = new \DateTime('-2 day');
79 for ($i = 0; $i < 3; ++$i) {
80 $dt->modify('-1 day');
81 $profile = new Profile('time_'.$i);
82 $profile->setTime($dt->getTimestamp());
83 $profile->setMethod('GET');
84 $this->storage->write($profile);
86 $records = $this->storage->find('', '', 3, 'GET');
87 $this->assertCount(1, $records, '->find() returns only one record');
88 $this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
89 $this->storage->purge();
93 * @dataProvider getDsns
95 public function testDsnParser($dsn, $expected)
97 $m = new \ReflectionMethod($this->storage, 'parseDsn');
98 $m->setAccessible(true);
100 $this->assertEquals($expected, $m->invoke($this->storage, $dsn));
103 public function testUtf8()
105 $profile = new Profile('utf8_test_profile');
107 $data = 'HЁʃʃϿ, ϢorЃd!';
108 $nonUtf8Data = iconv('UTF-8', 'UCS-2', $data);
110 $collector = new MongoDbProfilerStorageTestDataCollector();
111 $collector->setData($nonUtf8Data);
113 $profile->setCollectors(array($collector));
115 $this->storage->write($profile);
117 $readProfile = $this->storage->read('utf8_test_profile');
118 $collectors = $readProfile->getCollectors();
120 $this->assertCount(1, $collectors);
121 $this->assertArrayHasKey('test_data_collector', $collectors);
122 $this->assertEquals($nonUtf8Data, $collectors['test_data_collector']->getData(), 'Non-UTF8 data is properly encoded/decoded');
126 * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
128 protected function getStorage()
130 return $this->storage;
133 protected function setUp()
135 $this->storage = new MongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
136 $m = new \ReflectionMethod($this->storage, 'getMongo');
137 $m->setAccessible(true);
139 $m->invoke($this->storage);
140 } catch (\MongoConnectionException $e) {
141 $this->markTestSkipped('A MongoDB server on localhost is required.');
144 $this->storage->purge();
147 protected function tearDown()
149 $this->storage->purge();