Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / Profiler / AbstractProfilerStorageTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\Profiler;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\Profiler\Profile;
16
17 abstract class AbstractProfilerStorageTest extends TestCase
18 {
19     public function testStore()
20     {
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);
27         }
28         $this->assertCount(10, $this->getStorage()->find('127.0.0.1', 'http://foo.bar', 20, 'GET'), '->write() stores data in the storage');
29     }
30
31     public function testChildren()
32     {
33         $parentProfile = new Profile('token_parent');
34         $parentProfile->setIp('127.0.0.1');
35         $parentProfile->setUrl('http://foo.bar/parent');
36
37         $childProfile = new Profile('token_child');
38         $childProfile->setIp('127.0.0.1');
39         $childProfile->setUrl('http://foo.bar/child');
40
41         $parentProfile->addChild($childProfile);
42
43         $this->getStorage()->write($parentProfile);
44         $this->getStorage()->write($childProfile);
45
46         // Load them from storage
47         $parentProfile = $this->getStorage()->read('token_parent');
48         $childProfile = $this->getStorage()->read('token_child');
49
50         // Check child has link to parent
51         $this->assertNotNull($childProfile->getParent());
52         $this->assertEquals($parentProfile->getToken(), $childProfile->getParentToken());
53
54         // Check parent has child
55         $children = $parentProfile->getChildren();
56         $this->assertCount(1, $children);
57         $this->assertEquals($childProfile->getToken(), $children[0]->getToken());
58     }
59
60     public function testStoreSpecialCharsInUrl()
61     {
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');
68
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');
73
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');
78
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');
83     }
84
85     public function testStoreDuplicateToken()
86     {
87         $profile = new Profile('token');
88         $profile->setUrl('http://example.com/');
89
90         $this->assertTrue($this->getStorage()->write($profile), '->write() returns true when the token is unique');
91
92         $profile->setUrl('http://example.net/');
93
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');
96
97         $this->assertCount(1, $this->getStorage()->find('', '', 1000, ''), '->find() does not return the same profile twice');
98     }
99
100     public function testRetrieveByIp()
101     {
102         $profile = new Profile('token');
103         $profile->setIp('127.0.0.1');
104         $profile->setMethod('GET');
105         $this->getStorage()->write($profile);
106
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');
110     }
111
112     public function testRetrieveByUrl()
113     {
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);
119
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);
125
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);
131
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);
137
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);
143
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);
149
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');
156     }
157
158     public function testStoreTime()
159     {
160         $dt = new \DateTime('now');
161         $start = $dt->getTimestamp();
162
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);
171         }
172
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');
178
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');
181     }
182
183     public function testRetrieveByEmptyUrlAndIp()
184     {
185         for ($i = 0; $i < 5; ++$i) {
186             $profile = new Profile('token_'.$i);
187             $profile->setMethod('GET');
188             $this->getStorage()->write($profile);
189         }
190         $this->assertCount(5, $this->getStorage()->find('', '', 10, 'GET'), '->find() returns all previously added records');
191         $this->getStorage()->purge();
192     }
193
194     public function testRetrieveByMethodAndLimit()
195     {
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);
201             }
202         }
203
204         $this->assertCount(5, $this->getStorage()->find('', '', 5, 'POST'));
205
206         $this->getStorage()->purge();
207     }
208
209     public function testPurge()
210     {
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);
216
217         $this->assertTrue(false !== $this->getStorage()->read('token1'));
218         $this->assertCount(1, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
219
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);
225
226         $this->assertTrue(false !== $this->getStorage()->read('token2'));
227         $this->assertCount(2, $this->getStorage()->find('127.0.0.1', '', 10, 'GET'));
228
229         $this->getStorage()->purge();
230
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');
233     }
234
235     public function testDuplicates()
236     {
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');
242
243             ///three duplicates
244             $this->getStorage()->write($profile);
245             $this->getStorage()->write($profile);
246             $this->getStorage()->write($profile);
247         }
248         $this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
249     }
250
251     public function testStatusCode()
252     {
253         $profile = new Profile('token1');
254         $profile->setStatusCode(200);
255         $this->getStorage()->write($profile);
256
257         $profile = new Profile('token2');
258         $profile->setStatusCode(404);
259         $this->getStorage()->write($profile);
260
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));
265     }
266
267     /**
268      * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
269      */
270     abstract protected function getStorage();
271 }