Completed
Push — master ( 9f9b7a...8f3938 )
by Jacob
02:26
created

DisplayTest   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 464
Duplicated Lines 20.69 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 8
Bugs 1 Features 4
Metric Value
wmc 24
c 8
b 1
f 4
lcom 2
cbo 3
dl 96
loc 464
rs 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 20 1
A testSetStartTime() 0 8 1
A testSetConsole() 8 8 1
A testSetMemoryData() 0 11 1
A testSetQueryData() 0 14 1
A testSetSpeedData() 0 11 1
A testGetConsoleMeta() 11 11 1
A testGetConsoleMessages() 11 11 1
A testGetSpeedMeta() 0 21 1
A testGetQueryMeta() 9 9 1
A testGetQueryList() 9 9 1
A testGetMemoryMeta() 0 20 1
A testGetFileMeta() 9 9 1
A testGetFileList() 9 9 1
A testFilterMessages() 0 14 1
A testGetReadableTime() 15 15 2
A testGetReadableMemory() 15 15 2
B dataConsoleStore() 0 118 1
B dataQueryData() 0 39 1
B dataFileData() 0 35 1
A setInternalProperty() 0 7 1
A getAccessibleMethod() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
23
        $options = array(
24
            'script_path' => 'testing/testing.js',
25
            'fake_key' => 'foo bar'
26
        );
27
        $expectedOptions = array_intersect_key($options, $defaults);
28
        $expectedOptions = array_replace($defaults, $expectedOptions);
29
        $display = new Display($options);
30
        $this->assertAttributeEquals($expectedOptions, 'options', $display);
31
    }
32
33
    public function testSetStartTime()
34
    {
35
        $startTime = microtime(true);
36
        $display = new Display();
37
        $display->setStartTime($startTime);
38
39
        $this->assertAttributeEquals($startTime, 'startTime', $display);
40
    }
41
42 View Code Duplication
    public function testSetConsole()
43
    {
44
        $console = new Console();
45
        $display = new Display();
46
        $display->setConsole($console);
47
48
        $this->assertAttributeSame($console, 'console', $display);
49
    }
50
51
    public function testSetMemoryData()
52
    {
53
        $memoryData = array(
54
            'used'    => memory_get_peak_usage(),
55
            'allowed' => ini_get('memory_limit')
56
        );
57
        $display = new Display();
58
        $display->setMemoryData($memoryData);
59
60
        $this->assertAttributeEquals($memoryData, 'memoryData', $display);
61
    }
62
63
    public function testSetQueryData()
64
    {
65
        $queryData = array(
66
            'sql'     => 'SELECT * FROM testing',
67
            'explain' => array(
68
                'key' => 'value'
69
            ),
70
            'time'    => 300
71
        );
72
        $display = new Display();
73
        $display->setQueryData($queryData);
74
75
        $this->assertAttributeEquals($queryData, 'queryData', $display);
76
    }
77
78
    public function testSetSpeedData()
79
    {
80
        $speedData = array(
81
            'elapsed' => 1.234,
82
            'allowed' => 30
83
        );
84
        $display = new Display();
85
        $display->setSpeedData($speedData);
86
87
        $this->assertAttributeEquals($speedData, 'speedData', $display);
88
    }
89
90
    /**
91
     * @dataProvider dataConsoleStore
92
     */
93 View Code Duplication
    public function testGetConsoleMeta($consoleStore, $expectedMeta, $expectedMessages)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedMessages is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95
        $console = new Console();
96
        $this->setInternalProperty($console, 'store', $consoleStore);
97
        $display = new Display();
98
        $display->setConsole($console);
99
        $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta');
100
101
        $consoleMeta = $reflectedMethod->invoke($display);
102
        $this->assertEquals($expectedMeta, $consoleMeta);
103
    }
104
105
    /**
106
     * @dataProvider dataConsoleStore
107
     */
108 View Code Duplication
    public function testGetConsoleMessages($consoleStore, $expectedMeta, $expectedMessages)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedMeta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
109
    {
110
        $console = new Console();
111
        $this->setInternalProperty($console, 'store', $consoleStore);
112
        $display = new Display();
113
        $display->setConsole($console);
114
        $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages');
115
116
        $consoleMessages = $reflectedMethod->invoke($display);
117
        $this->assertEquals($expectedMessages, $consoleMessages);
118
    }
119
120
    public function testGetSpeedMeta()
121
    {
122
        $elapsedTime = 1234.678;
123
        $allowedTime = 30;
124
        $display = new Display();
125
        $display->setSpeedData(array(
126
            'elapsed' => $elapsedTime,
127
            'allowed' => $allowedTime
128
        ));
129
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
130
        $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime));
131
        $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0));
132
        $expectedMeta = array(
133
            'elapsed' => $elapsedTime,
134
            'allowed' => $allowedTime
135
        );
136
137
        $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta');
138
        $speedMeta = $reflectedMethod->invoke($display);
139
        $this->assertEquals($expectedMeta, $speedMeta);
140
    }
141
142
    /**
143
     * @dataProvider dataQueryData
144
     */
145 View Code Duplication
    public function testGetQueryMeta($queryData, $expectedMeta, $expectedList)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedList is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
146
    {
147
        $display = new Display();
148
        $display->setQueryData($queryData);
149
        $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryMeta');
150
151
        $queryMeta = $reflectedMethod->invoke($display);
152
        $this->assertEquals($expectedMeta, $queryMeta);
153
    }
154
155
    /**
156
     * @dataProvider dataQueryData
157
     */
158 View Code Duplication
    public function testGetQueryList($queryData, $expectedMeta, $expectedList)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedMeta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
159
    {
160
        $display = new Display();
161
        $display->setQueryData($queryData);
162
        $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryList');
163
164
        $queryList = $reflectedMethod->invoke($display);
165
        $this->assertEquals($expectedList, $queryList);
166
    }
167
168
    public function testGetMemoryMeta()
169
    {
170
        $usedMemory = 123456;
171
        $allowedMemory = '128M';
172
        $display = new Display();
173
        $display->setMemoryData(array(
174
            'used'    => $usedMemory,
175
            'allowed' => $allowedMemory
176
        ));
177
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory');
178
        $usedMemory = $reflectedMethod->invokeArgs($display, array($usedMemory));
179
        $expectedMeta = array(
180
            'used'    => $usedMemory,
181
            'allowed' => $allowedMemory
182
        );
183
184
        $reflectedMethod = $this->getAccessibleMethod($display, 'getMemoryMeta');
185
        $memoryMeta = $reflectedMethod->invoke($display);
186
        $this->assertEquals($expectedMeta, $memoryMeta);
187
    }
188
189
    /**
190
     * @dataProvider dataFileData
191
     */
192 View Code Duplication
    public function testGetFileMeta($fileData, $expectedMeta, $expectedList)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedList is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
193
    {
194
        $display = new Display();
195
        $display->setFileData($fileData);
196
        $reflectedMethod = $this->getAccessibleMethod($display, 'getFileMeta');
197
198
        $fileMeta = $reflectedMethod->invoke($display);
199
        $this->assertEquals($expectedMeta, $fileMeta);
200
    }
201
202
    /**
203
     * @dataProvider dataFileData
204
     */
205 View Code Duplication
    public function testGetFileList($fileData, $expectedMeta, $expectedList)
2 ignored issues
show
Unused Code introduced by
The parameter $expectedMeta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
206
    {
207
        $display = new Display();
208
        $display->setFileData($fileData);
209
        $reflectedMethod = $this->getAccessibleMethod($display, 'getFileList');
210
211
        $fileList = $reflectedMethod->invoke($display);
212
        $this->assertEquals($expectedList, $fileList);
213
    }
214
215
    public function testFilterMessages()
216
    {
217
        $display = new Display();
218
        $reflectedMethod = $this->getAccessibleMethod($display, 'filterMessages');
219
220
        $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array(
221
            'type' => 'remove'
222
        )), 'keep'));
223
        $this->assertEmpty($filteredMessages);
224
        $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array(
225
            'type' => 'keep'
226
        )), 'keep'));
227
        $this->assertCount(1, $filteredMessages);
228
    }
229
230 View Code Duplication
    public function testGetReadableTime()
231
    {
232
        $timeTest = array(
233
            '.032432' => '32.432 ms',
234
            '24.3781' => '24.378 s',
235
            '145.123' => '2.419 m'
236
        );
237
        $display = new Display();
238
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
239
240
        foreach ($timeTest as $rawTime => $expectedTime) {
241
            $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime));
242
            $this->assertEquals($expectedTime, $readableTime);
243
        }
244
    }
245
246 View Code Duplication
    public function testGetReadableMemory()
247
    {
248
        $memoryTest = array(
249
            '314'     => '314 b',
250
            '7403'    => '7.23 k',
251
            '2589983' => '2.47 M'
252
        );
253
        $display = new Display();
254
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory');
255
256
        foreach ($memoryTest as $rawMemory => $expectedMemory) {
257
            $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory));
258
            $this->assertEquals($expectedMemory, $readableMemory);
259
        }
260
    }
261
262
    public function dataConsoleStore()
263
    {
264
        $testException = new Exception('testing');
265
        $display = new Display();
266
        $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime');
267
        $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory');
268
269
        return array(
270
            array(
271
                'store' => array(
272
                    array(
273
                        'data' => 'testing message',
274
                        'type' => 'log'
275
                    ),
276
                    array(
277
                        'name' => 'now',
278
                        'data' => microtime(true),
279
                        'type' => 'speed'
280
                    ),
281
                    array(
282
                        'name' => 'little later',
283
                        'data' => microtime(true) + 1,
284
                        'type' => 'speed'
285
                    ),
286
                    array(
287
                        'name' => 'invalid key',
288
                        'type' => 'foo'
289
                    )
290
                ),
291
                'meta' => array(
292
                    'log'    => 1,
293
                    'memory' => 0,
294
                    'error'  => 1,
295
                    'speed'  => 2
296
                ),
297
                'messages' => array(
298
                    array(
299
                        'message' => 'testing message',
300
                        'type'    => 'log'
301
                    ),
302
                    array(
303
                        'message' => 'now',
304
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true))),
305
                        'type'    => 'speed'
306
                    ),
307
                    array(
308
                        'message' => 'little later',
309
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)),
310
                        'type'    => 'speed'
311
                    ),
312
                    array(
313
                        'message' => 'Unrecognized console log type: foo',
314
                        'type'    => 'error'
315
                    )
316
                )
317
            ),
318
            array(
319
                'store' => array(
320
                    array(
321
                        'data' => 'another testing message',
322
                        'type' => 'log'
323
                    ),
324
                    array(
325
                        'name'      => 'test array',
326
                        'data'      => strlen(serialize(array('key' => 'value'))),
327
                        'data_type' => 'array',
328
                        'type'      => 'memory'
329
                    ),
330
                    array(
331
                        'name'      => 'memory usage test',
332
                        'data'      => memory_get_usage(),
333
                        'data_type' => '',
334
                        'type'      => 'memory'
335
                    ),
336
                    array(
337
                        'data' => $testException->getMessage(),
338
                        'file' => $testException->getFile(),
339
                        'line' => $testException->getLine(),
340
                        'type' => 'error'
341
                    )
342
                ),
343
                'meta' => array(
344
                    'log'    => 1,
345
                    'memory' => 2,
346
                    'error'  => 1,
347
                    'speed'  => 0
348
                ),
349
                'messages' => array(
350
                    array(
351
                        'message' => 'another testing message',
352
                        'type'    => 'log'
353
                    ),
354
                    array(
355
                        'message' => 'array: test array',
356
                        'data'    => $reflectedMemory->invokeArgs(
357
                            $display,
358
                            array(strlen(serialize(array('key' => 'value'))))
359
                        ),
360
                        'type'    => 'memory'
361
                    ),
362
                    array(
363
                        'message' => 'memory usage test',
364
                        'data'    => $reflectedMemory->invokeArgs($display, array(memory_get_usage())),
365
                        'type'    => 'memory'
366
                    ),
367
                    array(
368
                        'message' => sprintf(
369
                            'Line %s: %s in %s',
370
                            $testException->getLine(),
371
                            $testException->getMessage(),
372
                            $testException->getFile()
373
                        ),
374
                        'type'    => 'error'
375
                    )
376
                )
377
            )
378
        );
379
    }
380
381
    public function dataQueryData()
382
    {
383
        $display = new Display();
384
        $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime');
385
386
        return array(
387
            array(
388
                'data' => array(
389
                    array(
390
                        'sql'     => "SELECT * FROM testing",
391
                        'explain' => array('empty_key' => ''),
392
                        'time'    => 25
393
                    ),
394
                    array(
395
                        'sql'     => "SELECT id FROM testing WHERE title = :title",
396
                        'explain' => array('key' => 'value'),
397
                        'time'    => 5
398
                    )
399
                ),
400
                'meta' => array(
401
                    'count'   => 2,
402
                    'time'    => $reflectedTime->invokeArgs($display, array(30)),
403
                    'slowest' => $reflectedTime->invokeArgs($display, array(25)),
404
                ),
405
                'list' => array(
406
                    array(
407
                        'message'  => 'SELECT * FROM testing',
408
                        'sub_data' => array(),
409
                        'data'     => $reflectedTime->invokeArgs($display, array(25))
410
                    ),
411
                    array(
412
                        'message'  => 'SELECT id FROM testing WHERE title = :title',
413
                        'sub_data' => array('key' => 'value'),
414
                        'data'     => $reflectedTime->invokeArgs($display, array(5))
415
                    )
416
                )
417
            )
418
        );
419
    }
420
421
    public function dataFileData()
422
    {
423
        $display = new Display();
424
        $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory');
425
426
        return array(
427
            array(
428
                'data' => array(
429
                    array(
430
                        'name' => 'test-file',
431
                        'size' => 1234
432
                    ),
433
                    array(
434
                        'name' => 'test-file-2',
435
                        'size' => 66
436
                    )
437
                ),
438
                'meta' => array(
439
                    'count'   => 2,
440
                    'size'    => $reflectedMemory->invokeArgs($display, array(1300)),
441
                    'largest' => $reflectedMemory->invokeArgs($display, array(1234)),
442
                ),
443
                'list' => array(
444
                    array(
445
                        'message' => 'test-file',
446
                        'data'    => $reflectedMemory->invokeArgs($display, array(1234))
447
                    ),
448
                    array(
449
                        'message' => 'test-file-2',
450
                        'data'    => $reflectedMemory->invokeArgs($display, array(66))
451
                    )
452
                )
453
            )
454
        );
455
    }
456
457
    protected function setInternalProperty($class, $property, $value)
458
    {
459
        $reflectedClass = new ReflectionClass(get_class($class));
460
        $reflectedProperty = $reflectedClass->getProperty($property);
461
        $reflectedProperty->setAccessible(true);
462
        $reflectedProperty->setValue($class, $value);
463
    }
464
465
    protected function getAccessibleMethod($class, $method)
466
    {
467
        $reflectedClass = new ReflectionClass(get_class($class));
468
        $reflectedMethod = $reflectedClass->getMethod($method);
469
        $reflectedMethod->setAccessible(true);
470
        return $reflectedMethod;
471
    }
472
}
473