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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\Profiler\Profile;
17 abstract class AbstractProfilerStorageTest extends TestCase
19 public function testStore()
21 for ($i = 0; $i < 10; ++$i) {
22 $profile = new Profile('token_'.$i);
23 $profile->setIp('127.0.0.1');
24 $profile->setUrl('http://foo.bar');
25 $profile->setMethod('GET');
26 $this->getStorage()->write($profile);
28 $this->assertCount(10, $this->getStorage()->find('127.0.0.1', 'http://foo.bar', 20, 'GET'), '->write() stores data in the storage');
31 public function testChildren()
33 $parentProfile = new Profile('token_parent');
34 $parentProfile->setIp('127.0.0.1');
35 $parentProfile->setUrl('http://foo.bar/parent');
37 $childProfile = new Profile('token_child');
38 $childProfile->setIp('127.0.0.1');
39 $childProfile->setUrl('http://foo.bar/child');
41 $parentProfile->addChild($childProfile);
43 $this->getStorage()->write($parentProfile);
44 $this->getStorage()->write($childProfile);
46 // Load them from storage
47 $parentProfile = $this->getStorage()->read('token_parent');
48 $childProfile = $this->getStorage()->read('token_child');
50 // Check child has link to parent
51 $this->assertNotNull($childProfile->getParent());
52 $this->assertEquals($parentProfile->getToken(), $childProfile->getParentToken());
54 // Check parent has child
55 $children = $parentProfile->getChildren();
56 $this->assertCount(1, $children);
57 $this->assertEquals($childProfile->getToken(), $children[0]->getToken());
60 public function testStoreSpecialCharsInUrl()
62 // The storage accepts special characters in URLs (Even though URLs are not
63 // supposed to contain them)
64 $profile = new Profile('simple_quote');
65 $profile->setUrl('http://foo.bar/\'');
66 $this->getStorage()->write($profile);
67 $this->assertTrue(false !== $this->getStorage()->read('simple_quote'), '->write() accepts single quotes in URL');
69 $profile = new Profile('double_quote');
70 $profile->setUrl('http://foo.bar/"');
71 $this->getStorage()->write($profile);
72 $this->assertTrue(false !== $this->getStorage()->read('double_quote'), '->write() accepts double quotes in URL');
74 $profile = new Profile('backslash');
75 $profile->setUrl('http://foo.bar/\\');
76 $this->getStorage()->write($profile);
77 $this->assertTrue(false !== $this->getStorage()->read('backslash'), '->write() accepts backslash in URL');
79 $profile = new Profile('comma');
80 $profile->setUrl('http://foo.bar/,');
81 $this->getStorage()->write($profile);
82 $this->assertTrue(false !== $this->getStorage()->read('comma'), '->write() accepts comma in URL');
85 public function testStoreDuplicateToken()
87 $profile = new Profile('token');
88 $profile->setUrl('http://example.com/');
90 $this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is unique');
92 $profile->setUrl('http://example.net/');
94 $this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is already present in the storage');
95 $this->assertEquals('http://example.net/', $this->getStorage()->read('token')->getUrl(), '->write() overwrites the current profile data');
97 $this->assertCount(1, $this->getStorage()->find('', '', 1000, ''), '->find() does not return the same profile twice');
100 public function testRetrieveByIp()
102 $profile = new Profile('token');
103 $profile->setIp('127.0.0.1');
104 $profile->setMethod('GET');
105 $this->getStorage()->write($profile);
107 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->find() retrieve a record by IP');
108 $this->assertCount(0, $this->getStorage()->find('127.0.%.1', '', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the IP');
109 $this->assertCount(0, $this->getStorage()->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
112 public function testRetrieveByUrl()
114 $profile = new Profile('simple_quote');
115 $profile->setIp('127.0.0.1');
116 $profile->setUrl('http://foo.bar/\'');
117 $profile->setMethod('GET');
118 $this->getStorage()->write($profile);
120 $profile = new Profile('double_quote');
121 $profile->setIp('127.0.0.1');
122 $profile->setUrl('http://foo.bar/"');
123 $profile->setMethod('GET');
124 $this->getStorage()->write($profile);
126 $profile = new Profile('backslash');
127 $profile->setIp('127.0.0.1');
128 $profile->setUrl('http://foo\\bar/');
129 $profile->setMethod('GET');
130 $this->getStorage()->write($profile);
132 $profile = new Profile('percent');
133 $profile->setIp('127.0.0.1');
134 $profile->setUrl('http://foo.bar/%');
135 $profile->setMethod('GET');
136 $this->getStorage()->write($profile);
138 $profile = new Profile('underscore');
139 $profile->setIp('127.0.0.1');
140 $profile->setUrl('http://foo.bar/_');
141 $profile->setMethod('GET');
142 $this->getStorage()->write($profile);
144 $profile = new Profile('semicolon');
145 $profile->setIp('127.0.0.1');
146 $profile->setUrl('http://foo.bar/;');
147 $profile->setMethod('GET');
148 $this->getStorage()->write($profile);
150 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/\'', 10, 'GET'), '->find() accepts single quotes in URLs');
151 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/"', 10, 'GET'), '->find() accepts double quotes in URLs');
152 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo\\bar/', 10, 'GET'), '->find() accepts backslash in URLs');
153 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/;', 10, 'GET'), '->find() accepts semicolon in URLs');
154 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/%', 10, 'GET'), '->find() does not interpret a "%" as a wildcard in the URL');
155 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', 'http://foo.bar/_', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the URL');
158 public function testStoreTime()
160 $dt = new \DateTime('now');
161 $start = $dt->getTimestamp();
163 for ($i = 0; $i < 3; ++$i) {
164 $dt->modify('+1 minute');
165 $profile = new Profile('time_'.$i);
166 $profile->setIp('127.0.0.1');
167 $profile->setUrl('http://foo.bar');
168 $profile->setTime($dt->getTimestamp());
169 $profile->setMethod('GET');
170 $this->getStorage()->write($profile);
173 $records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 3 * 60);
174 $this->assertCount(3, $records, '->find() returns all previously added records');
175 $this->assertEquals($records[0]['token'], 'time_2', '->find() returns records ordered by time in descendant order');
176 $this->assertEquals($records[1]['token'], 'time_1', '->find() returns records ordered by time in descendant order');
177 $this->assertEquals($records[2]['token'], 'time_0', '->find() returns records ordered by time in descendant order');
179 $records = $this->getStorage()->find('', '', 3, 'GET', $start, time() + 2 * 60);
180 $this->assertCount(2, $records, '->find() should return only first two of the previously added records');
183 public function testRetrieveByEmptyUrlAndIp()
185 for ($i = 0; $i < 5; ++$i) {
186 $profile = new Profile('token_'.$i);
187 $profile->setMethod('GET');
188 $this->getStorage()->write($profile);
190 $this->assertCount(5, $this->getStorage()->find('', '', 10, 'GET'), '->find() returns all previously added records');
191 $this->getStorage()->purge();
194 public function testRetrieveByMethodAndLimit()
196 foreach (array('POST', 'GET') as $method) {
197 for ($i = 0; $i < 5; ++$i) {
198 $profile = new Profile('token_'.$i.$method);
199 $profile->setMethod($method);
200 $this->getStorage()->write($profile);
204 $this->assertCount(5, $this->getStorage()->find('', '', 5, 'POST'));
206 $this->getStorage()->purge();
209 public function testPurge()
211 $profile = new Profile('token1');
212 $profile->setIp('127.0.0.1');
213 $profile->setUrl('http://example.com/');
214 $profile->setMethod('GET');
215 $this->getStorage()->write($profile);
217 $this->assertTrue(false !== $this->getStorage()->read('token1'));
218 $this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
220 $profile = new Profile('token2');
221 $profile->setIp('127.0.0.1');
222 $profile->setUrl('http://example.net/');
223 $profile->setMethod('GET');
224 $this->getStorage()->write($profile);
226 $this->assertTrue(false !== $this->getStorage()->read('token2'));
227 $this->assertCount(2, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
229 $this->getStorage()->purge();
231 $this->assertEmpty($this->getStorage()->read('token'), '->purge() removes all data stored by profiler');
232 $this->assertCount(0, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'), '->purge() removes all items from index');
235 public function testDuplicates()
237 for ($i = 1; $i <= 5; ++$i) {
238 $profile = new Profile('foo'.$i);
239 $profile->setIp('127.0.0.1');
240 $profile->setUrl('http://example.net/');
241 $profile->setMethod('GET');
244 $this->getStorage()->write($profile);
245 $this->getStorage()->write($profile);
246 $this->getStorage()->write($profile);
248 $this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
251 public function testStatusCode()
253 $profile = new Profile('token1');
254 $profile->setStatusCode(200);
255 $this->getStorage()->write($profile);
257 $profile = new Profile('token2');
258 $profile->setStatusCode(404);
259 $this->getStorage()->write($profile);
261 $tokens = $this->getStorage()->find('', '', 10, '');
262 $this->assertCount(2, $tokens);
263 $this->assertContains($tokens[0]['status_code'], array(200, 404));
264 $this->assertContains($tokens[1]['status_code'], array(200, 404));
268 * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
270 abstract protected function getStorage();