This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Particletree\Pqp; |
||
4 | |||
5 | use Exception; |
||
6 | use PHPUnit_Framework_TestCase; |
||
7 | use ReflectionClass; |
||
8 | |||
9 | class DisplayTest extends PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | |||
12 | public function testConstruct() |
||
13 | { |
||
14 | $display = new Display(); |
||
15 | $reflectedDisplay = new ReflectionClass(get_class($display)); |
||
16 | $reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
||
17 | $reflectedProperty->setAccessible(true); |
||
18 | $defaults = $reflectedProperty->getValue($display); |
||
19 | |||
20 | $display = new Display(); |
||
21 | $this->assertAttributeEquals($defaults, 'options', $display); |
||
22 | $this->assertAttributeGreaterThan(0, 'pathTrimStart', $display); |
||
23 | |||
24 | $options = array( |
||
25 | 'script_path' => 'testing/testing.js', |
||
26 | 'fake_key' => 'foo bar', |
||
27 | 'relative_path' => false, |
||
28 | ); |
||
29 | $expectedOptions = array_intersect_key($options, $defaults); |
||
30 | $expectedOptions = array_replace($defaults, $expectedOptions); |
||
31 | $display = new Display($options); |
||
32 | $this->assertAttributeEquals($expectedOptions, 'options', $display); |
||
33 | $this->assertAttributeEquals(0, 'pathTrimStart', $display); |
||
34 | } |
||
35 | |||
36 | public function testSetStartTime() |
||
37 | { |
||
38 | $startTime = microtime(true); |
||
39 | $display = new Display(); |
||
40 | $display->setStartTime($startTime); |
||
41 | |||
42 | $this->assertAttributeEquals($startTime, 'startTime', $display); |
||
43 | } |
||
44 | |||
45 | View Code Duplication | public function testSetConsole() |
|
46 | { |
||
47 | $console = new Console(); |
||
48 | $display = new Display(); |
||
49 | $display->setConsole($console); |
||
50 | |||
51 | $this->assertAttributeSame($console, 'console', $display); |
||
52 | } |
||
53 | |||
54 | public function testSetMemoryData() |
||
55 | { |
||
56 | $memoryData = array( |
||
57 | 'used' => memory_get_peak_usage(), |
||
58 | 'allowed' => ini_get('memory_limit') |
||
59 | ); |
||
60 | $display = new Display(); |
||
61 | $display->setMemoryData($memoryData); |
||
62 | |||
63 | $this->assertAttributeEquals($memoryData, 'memoryData', $display); |
||
64 | } |
||
65 | |||
66 | public function testSetQueryData() |
||
67 | { |
||
68 | $queryData = array( |
||
69 | 'sql' => 'SELECT * FROM testing', |
||
70 | 'explain' => array( |
||
71 | 'key' => 'value' |
||
72 | ), |
||
73 | 'time' => 300 |
||
74 | ); |
||
75 | $display = new Display(); |
||
76 | $display->setQueryData($queryData); |
||
77 | |||
78 | $this->assertAttributeEquals($queryData, 'queryData', $display); |
||
79 | } |
||
80 | |||
81 | public function testSetSpeedData() |
||
82 | { |
||
83 | $speedData = array( |
||
84 | 'elapsed' => 1.234, |
||
85 | 'allowed' => 30 |
||
86 | ); |
||
87 | $display = new Display(); |
||
88 | $display->setSpeedData($speedData); |
||
89 | |||
90 | $this->assertAttributeEquals($speedData, 'speedData', $display); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @dataProvider dataPathTrimStart |
||
95 | */ |
||
96 | View Code Duplication | public function testGetPathTrimStart($cwd, $dir, $expectedPosition) |
|
0 ignored issues
–
show
|
|||
97 | { |
||
98 | $display = new Display(); |
||
99 | $reflectedMethod = $this->getAccessibleMethod($display, 'getPathTrimStart'); |
||
100 | $position = $reflectedMethod->invokeArgs($display, array($cwd, $dir)); |
||
101 | |||
102 | $this->assertEquals($expectedPosition, $position); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @dataProvider dataConsoleStore |
||
107 | */ |
||
108 | View Code Duplication | public function testGetConsoleMeta($consoleStore, $expectedMeta, $expectedMessages) |
|
109 | { |
||
110 | $console = new Console(); |
||
111 | $this->setInternalProperty($console, 'store', $consoleStore); |
||
112 | $display = new Display(); |
||
113 | $display->setConsole($console); |
||
114 | $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta'); |
||
115 | |||
116 | $consoleMeta = $reflectedMethod->invoke($display); |
||
117 | $this->assertEquals($expectedMeta, $consoleMeta); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @dataProvider dataConsoleStore |
||
122 | */ |
||
123 | View Code Duplication | public function testGetConsoleMessages($consoleStore, $expectedMeta, $expectedMessages) |
|
124 | { |
||
125 | $console = new Console(); |
||
126 | $this->setInternalProperty($console, 'store', $consoleStore); |
||
127 | $display = new Display(); |
||
128 | $display->setConsole($console); |
||
129 | $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages'); |
||
130 | |||
131 | $consoleMessages = $reflectedMethod->invoke($display); |
||
132 | $this->assertEquals($expectedMessages, $consoleMessages); |
||
133 | } |
||
134 | |||
135 | public function testGetSpeedMeta() |
||
136 | { |
||
137 | $elapsedTime = 1234.678; |
||
138 | $allowedTime = 30; |
||
139 | $display = new Display(); |
||
140 | $display->setSpeedData(array( |
||
141 | 'elapsed' => $elapsedTime, |
||
142 | 'allowed' => $allowedTime |
||
143 | )); |
||
144 | $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
||
145 | $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime)); |
||
146 | $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0)); |
||
147 | $expectedMeta = array( |
||
148 | 'elapsed' => $elapsedTime, |
||
149 | 'allowed' => $allowedTime |
||
150 | ); |
||
151 | |||
152 | $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta'); |
||
153 | $speedMeta = $reflectedMethod->invoke($display); |
||
154 | $this->assertEquals($expectedMeta, $speedMeta); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @dataProvider dataQueryData |
||
159 | */ |
||
160 | View Code Duplication | public function testGetQueryMeta($queryData, $expectedMeta, $expectedList) |
|
161 | { |
||
162 | $display = new Display(); |
||
163 | $display->setQueryData($queryData); |
||
164 | $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryMeta'); |
||
165 | |||
166 | $queryMeta = $reflectedMethod->invoke($display); |
||
167 | $this->assertEquals($expectedMeta, $queryMeta); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @dataProvider dataQueryData |
||
172 | */ |
||
173 | View Code Duplication | public function testGetQueryList($queryData, $expectedMeta, $expectedList) |
|
174 | { |
||
175 | $display = new Display(); |
||
176 | $display->setQueryData($queryData); |
||
177 | $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryList'); |
||
178 | |||
179 | $queryList = $reflectedMethod->invoke($display); |
||
180 | $this->assertEquals($expectedList, $queryList); |
||
181 | } |
||
182 | |||
183 | public function testGetMemoryMeta() |
||
184 | { |
||
185 | $usedMemory = 123456; |
||
186 | $allowedMemory = '128M'; |
||
187 | $display = new Display(); |
||
188 | $display->setMemoryData(array( |
||
189 | 'used' => $usedMemory, |
||
190 | 'allowed' => $allowedMemory |
||
191 | )); |
||
192 | $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
||
193 | $usedMemory = $reflectedMethod->invokeArgs($display, array($usedMemory)); |
||
194 | $expectedMeta = array( |
||
195 | 'used' => $usedMemory, |
||
196 | 'allowed' => $allowedMemory |
||
197 | ); |
||
198 | |||
199 | $reflectedMethod = $this->getAccessibleMethod($display, 'getMemoryMeta'); |
||
200 | $memoryMeta = $reflectedMethod->invoke($display); |
||
201 | $this->assertEquals($expectedMeta, $memoryMeta); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @dataProvider dataFileData |
||
206 | */ |
||
207 | View Code Duplication | public function testGetFileMeta($fileData, $expectedMeta, $expectedList) |
|
208 | { |
||
209 | $display = new Display(); |
||
210 | $display->setFileData($fileData); |
||
211 | $reflectedMethod = $this->getAccessibleMethod($display, 'getFileMeta'); |
||
212 | |||
213 | $fileMeta = $reflectedMethod->invoke($display); |
||
214 | $this->assertEquals($expectedMeta, $fileMeta); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @dataProvider dataFileData |
||
219 | */ |
||
220 | View Code Duplication | public function testGetFileList($fileData, $expectedMeta, $expectedList) |
|
221 | { |
||
222 | $display = new Display(); |
||
223 | $display->setFileData($fileData); |
||
224 | $reflectedMethod = $this->getAccessibleMethod($display, 'getFileList'); |
||
225 | |||
226 | $fileList = $reflectedMethod->invoke($display); |
||
227 | $this->assertEquals($expectedList, $fileList); |
||
228 | } |
||
229 | |||
230 | public function testFilterMessages() |
||
231 | { |
||
232 | $display = new Display(); |
||
233 | $reflectedMethod = $this->getAccessibleMethod($display, 'filterMessages'); |
||
234 | |||
235 | $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
||
236 | 'type' => 'remove' |
||
237 | )), 'keep')); |
||
238 | $this->assertEmpty($filteredMessages); |
||
239 | $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
||
240 | 'type' => 'keep' |
||
241 | )), 'keep')); |
||
242 | $this->assertCount(1, $filteredMessages); |
||
243 | } |
||
244 | |||
245 | View Code Duplication | public function testGetReadableTime() |
|
246 | { |
||
247 | $timeTest = array( |
||
248 | '.032432' => '32 ms', |
||
249 | '24.3781' => '24.378 s', |
||
250 | '145.123' => '2.419 m' |
||
251 | ); |
||
252 | $display = new Display(); |
||
253 | $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
||
254 | |||
255 | foreach ($timeTest as $rawTime => $expectedTime) { |
||
256 | $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
||
257 | $this->assertEquals($expectedTime, $readableTime); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | View Code Duplication | public function testGetReadableMemory() |
|
262 | { |
||
263 | $memoryTest = array( |
||
264 | '314' => '314 b', |
||
265 | '7403' => '7.23 k', |
||
266 | '2589983' => '2.47 M' |
||
267 | ); |
||
268 | $display = new Display(); |
||
269 | $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
||
270 | |||
271 | foreach ($memoryTest as $rawMemory => $expectedMemory) { |
||
272 | $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
||
273 | $this->assertEquals($expectedMemory, $readableMemory); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | public function testGetFilePath() |
||
278 | { |
||
279 | $display = new Display(array('relative_path' => false)); |
||
280 | $path = getcwd() . '/puppies'; |
||
281 | $reflectedFilePath = $this->getAccessibleMethod($display, 'getFilePath'); |
||
282 | $path = $reflectedFilePath->invokeArgs($display, array($path)); |
||
283 | $expectedPath = getcwd() . '/puppies'; |
||
284 | |||
285 | $this->assertEquals($expectedPath, $path); |
||
286 | |||
287 | $display = new Display(); |
||
288 | $path = getcwd() . '/puppies'; |
||
289 | $reflectedFilePath = $this->getAccessibleMethod($display, 'getFilePath'); |
||
290 | $path = $reflectedFilePath->invokeArgs($display, array($path)); |
||
291 | $expectedPath = '/puppies'; |
||
292 | |||
293 | $this->assertEquals($expectedPath, $path); |
||
294 | } |
||
295 | |||
296 | public function dataConsoleStore() |
||
297 | { |
||
298 | $testException = new Exception('testing'); |
||
299 | $display = new Display(); |
||
300 | $reflectedPathTrim = $this->getAccessibleMethod($display, 'getPathTrimStart'); |
||
301 | $trimStart = $reflectedPathTrim->invokeArgs($display, array(getcwd(), __DIR__)); |
||
302 | $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
||
303 | $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
||
304 | |||
305 | return array( |
||
306 | array( |
||
307 | 'store' => array( |
||
308 | array( |
||
309 | 'data' => 'testing message', |
||
310 | 'type' => 'log' |
||
311 | ), |
||
312 | array( |
||
313 | 'name' => 'now', |
||
314 | 'data' => microtime(true), |
||
315 | 'type' => 'speed' |
||
316 | ), |
||
317 | array( |
||
318 | 'name' => 'little later', |
||
319 | 'data' => microtime(true) + 1, |
||
320 | 'type' => 'speed' |
||
321 | ), |
||
322 | array( |
||
323 | 'name' => 'invalid key', |
||
324 | 'type' => 'foo' |
||
325 | ) |
||
326 | ), |
||
327 | 'meta' => array( |
||
328 | 'log' => 1, |
||
329 | 'memory' => 0, |
||
330 | 'error' => 1, |
||
331 | 'speed' => 2 |
||
332 | ), |
||
333 | 'messages' => array( |
||
334 | array( |
||
335 | 'message' => 'testing message', |
||
336 | 'type' => 'log' |
||
337 | ), |
||
338 | array( |
||
339 | 'message' => 'now', |
||
340 | 'data' => $reflectedTime->invokeArgs($display, array(microtime(true))), |
||
341 | 'type' => 'speed' |
||
342 | ), |
||
343 | array( |
||
344 | 'message' => 'little later', |
||
345 | 'data' => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)), |
||
346 | 'type' => 'speed' |
||
347 | ), |
||
348 | array( |
||
349 | 'message' => 'Unrecognized console log type: foo', |
||
350 | 'type' => 'error' |
||
351 | ) |
||
352 | ) |
||
353 | ), |
||
354 | array( |
||
355 | 'store' => array( |
||
356 | array( |
||
357 | 'data' => 'another testing message', |
||
358 | 'type' => 'log' |
||
359 | ), |
||
360 | array( |
||
361 | 'name' => 'test array', |
||
362 | 'data' => strlen(serialize(array('key' => 'value'))), |
||
363 | 'data_type' => 'array', |
||
364 | 'type' => 'memory' |
||
365 | ), |
||
366 | array( |
||
367 | 'name' => 'memory usage test', |
||
368 | 'data' => memory_get_usage(), |
||
369 | 'data_type' => '', |
||
370 | 'type' => 'memory' |
||
371 | ), |
||
372 | array( |
||
373 | 'data' => $testException->getMessage(), |
||
374 | 'file' => $testException->getFile(), |
||
375 | 'line' => $testException->getLine(), |
||
376 | 'type' => 'error' |
||
377 | ) |
||
378 | ), |
||
379 | 'meta' => array( |
||
380 | 'log' => 1, |
||
381 | 'memory' => 2, |
||
382 | 'error' => 1, |
||
383 | 'speed' => 0 |
||
384 | ), |
||
385 | 'messages' => array( |
||
386 | array( |
||
387 | 'message' => 'another testing message', |
||
388 | 'type' => 'log' |
||
389 | ), |
||
390 | array( |
||
391 | 'message' => 'array: test array', |
||
392 | 'data' => $reflectedMemory->invokeArgs( |
||
393 | $display, |
||
394 | array(strlen(serialize(array('key' => 'value')))) |
||
395 | ), |
||
396 | 'type' => 'memory' |
||
397 | ), |
||
398 | array( |
||
399 | 'message' => 'memory usage test', |
||
400 | 'data' => $reflectedMemory->invokeArgs($display, array(memory_get_usage())), |
||
401 | 'type' => 'memory' |
||
402 | ), |
||
403 | array( |
||
404 | 'message' => sprintf( |
||
405 | 'Line %s: %s in %s', |
||
406 | $testException->getLine(), |
||
407 | $testException->getMessage(), |
||
408 | substr($testException->getFile(), $trimStart) |
||
409 | ), |
||
410 | 'type' => 'error' |
||
411 | ) |
||
412 | ) |
||
413 | ) |
||
414 | ); |
||
415 | } |
||
416 | |||
417 | public function dataPathTrimStart() |
||
418 | { |
||
419 | return array( |
||
420 | array( |
||
421 | 'cwd' => '/Users/fakeUser/project', |
||
422 | 'dir' => '/Users/fakeUser/project/vendor/particletree/pqp/tests/unit', |
||
423 | 'expectedPosition' => 23, |
||
424 | ), |
||
425 | array( |
||
426 | 'cwd' => '/Users/fakeUser/project/public/path', |
||
427 | 'dir' => '/Users/fakeUser/project/vendor/particletree/pqp/tests/unit', |
||
428 | 'expectedPosition' => 24, |
||
429 | ), |
||
430 | ); |
||
431 | } |
||
432 | |||
433 | public function dataQueryData() |
||
434 | { |
||
435 | $display = new Display(); |
||
436 | $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
||
437 | |||
438 | return array( |
||
439 | array( |
||
440 | 'data' => array( |
||
441 | array( |
||
442 | 'sql' => "SELECT * FROM testing", |
||
443 | 'explain' => array('empty_key' => ''), |
||
444 | 'time' => 25 |
||
445 | ), |
||
446 | array( |
||
447 | 'sql' => "SELECT id FROM testing WHERE title = :title", |
||
448 | 'explain' => array('key' => 'value'), |
||
449 | 'time' => 5 |
||
450 | ) |
||
451 | ), |
||
452 | 'meta' => array( |
||
453 | 'count' => 2, |
||
454 | 'time' => $reflectedTime->invokeArgs($display, array(30)), |
||
455 | 'slowest' => $reflectedTime->invokeArgs($display, array(25)), |
||
456 | ), |
||
457 | 'list' => array( |
||
458 | array( |
||
459 | 'message' => 'SELECT * FROM testing', |
||
460 | 'sub_data' => array(), |
||
461 | 'data' => $reflectedTime->invokeArgs($display, array(25)) |
||
462 | ), |
||
463 | array( |
||
464 | 'message' => 'SELECT id FROM testing WHERE title = :title', |
||
465 | 'sub_data' => array('key' => 'value'), |
||
466 | 'data' => $reflectedTime->invokeArgs($display, array(5)) |
||
467 | ) |
||
468 | ) |
||
469 | ) |
||
470 | ); |
||
471 | } |
||
472 | |||
473 | public function dataFileData() |
||
474 | { |
||
475 | $display = new Display(); |
||
476 | $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
||
477 | |||
478 | return array( |
||
479 | array( |
||
480 | 'data' => array( |
||
481 | array( |
||
482 | 'name' => getcwd() . '/test-file', |
||
483 | 'size' => 1234 |
||
484 | ), |
||
485 | array( |
||
486 | 'name' => getcwd() . '/test-file-2', |
||
487 | 'size' => 66 |
||
488 | ) |
||
489 | ), |
||
490 | 'meta' => array( |
||
491 | 'count' => 2, |
||
492 | 'size' => $reflectedMemory->invokeArgs($display, array(1300)), |
||
493 | 'largest' => $reflectedMemory->invokeArgs($display, array(1234)), |
||
494 | ), |
||
495 | 'list' => array( |
||
496 | array( |
||
497 | 'message' => '/test-file', |
||
498 | 'data' => $reflectedMemory->invokeArgs($display, array(1234)) |
||
499 | ), |
||
500 | array( |
||
501 | 'message' => '/test-file-2', |
||
502 | 'data' => $reflectedMemory->invokeArgs($display, array(66)) |
||
503 | ) |
||
504 | ) |
||
505 | ) |
||
506 | ); |
||
507 | } |
||
508 | |||
509 | protected function setInternalProperty($class, $property, $value) |
||
510 | { |
||
511 | $reflectedClass = new ReflectionClass(get_class($class)); |
||
512 | $reflectedProperty = $reflectedClass->getProperty($property); |
||
513 | $reflectedProperty->setAccessible(true); |
||
514 | $reflectedProperty->setValue($class, $value); |
||
515 | } |
||
516 | |||
517 | protected function getAccessibleMethod($class, $method) |
||
518 | { |
||
519 | $reflectedClass = new ReflectionClass(get_class($class)); |
||
520 | $reflectedMethod = $reflectedClass->getMethod($method); |
||
521 | $reflectedMethod->setAccessible(true); |
||
522 | return $reflectedMethod; |
||
523 | } |
||
524 | } |
||
525 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.