Completed
Push — master ( b40822...9f9b7a )
by Jacob
02:27
created

DisplayTest::testGetReadableMemory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

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