-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
MongoDbProfilerStorageTest.php
165 lines (141 loc) · 4.92 KB
/
MongoDbProfilerStorageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?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\Component\HttpKernel\Tests\Profiler;
use Symfony\Component\HttpKernel\Profiler\MongoDbProfilerStorage;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DummyMongoDbProfilerStorage extends MongoDbProfilerStorage
{
public function getMongo()
{
return parent::getMongo();
}
}
class MongoDbProfilerStorageTestDataCollector extends DataCollector
{
public function setData($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
public function collect(Request $request, Response $response, \Exception $exception = null)
{
}
public function getName()
{
return 'test_data_collector';
}
}
class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;
public static function setUpBeforeClass()
{
if (extension_loaded('mongo')) {
self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
try {
self::$storage->getMongo();
} catch (\MongoConnectionException $e) {
self::$storage = null;
}
}
}
public static function tearDownAfterClass()
{
if (self::$storage) {
self::$storage->purge();
self::$storage = null;
}
}
public function getDsns()
{
return array(
array('mongodb://localhost/symfony_tests/profiler_data', array(
'mongodb://localhost/symfony_tests',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/symfony_tests',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost/admin',
'symfony_tests',
'profiler_data',
)),
array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
'symfony_tests',
'profiler_data',
)),
);
}
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());
$profile->setMethod('GET');
self::$storage->write($profile);
}
$records = self::$storage->find('', '', 3, 'GET');
$this->assertCount(1, $records, '->find() returns only one record');
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
self::$storage->purge();
}
/**
* @dataProvider getDsns
*/
public function testDsnParser($dsn, $expected)
{
$m = new \ReflectionMethod(self::$storage, 'parseDsn');
$m->setAccessible(true);
$this->assertEquals($expected, $m->invoke(self::$storage, $dsn));
}
public function testUtf8()
{
$profile = new Profile('utf8_test_profile');
$data = 'HЁʃʃϿ, ϢorЃd!';
$nonUtf8Data = mb_convert_encoding($data, 'UCS-2');
$collector = new MongoDbProfilerStorageTestDataCollector();
$collector->setData($nonUtf8Data);
$profile->setCollectors(array($collector));
self::$storage->write($profile);
$readProfile = self::$storage->read('utf8_test_profile');
$collectors = $readProfile->getCollectors();
$this->assertCount(1, $collectors);
$this->assertArrayHasKey('test_data_collector', $collectors);
$this->assertEquals($nonUtf8Data, $collectors['test_data_collector']->getData(), 'Non-UTF8 data is properly encoded/decoded');
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
protected function getStorage()
{
return self::$storage;
}
protected function setUp()
{
if (self::$storage) {
self::$storage->purge();
} else {
$this->markTestSkipped('MongoDbProfilerStorageTest requires the mongo PHP extension and a MongoDB server on localhost');
}
}
}