Completed
Push — master ( 6d2756...b40822 )
by Jacob
02:21
created

DisplayTest::dataConsoleStore()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 118
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 118
rs 8.2857
cc 1
eloc 88
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
    public function testGetReadableTime()
143
    {
144
        $timeTest = array(
145
            '.032432' => '32.432 ms',
146
            '24.3781' => '24.378 s',
147
            '145.123' => '2.419 m'
148
        );
149
        $display = new Display();
150
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
151
152
        foreach ($timeTest as $rawTime => $expectedTime) {
153
            $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime));
154
            $this->assertEquals($expectedTime, $readableTime);
155
        }
156
    }
157
158 View Code Duplication
    public function testGetReadableMemory()
159
    {
160
        $memoryTest = array(
161
            '314'     => '314 b',
162
            '7403'    => '7.23 k',
163
            '2589983' => '2.47 M'
164
        );
165
        $display = new Display();
166
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory');
167
168
        foreach ($memoryTest as $rawMemory => $expectedMemory) {
169
            $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory));
170
            $this->assertEquals($expectedMemory, $readableMemory);
171
        }
172
    }
173
174
    public function dataConsoleStore()
175
    {
176
        $testException = new Exception('testing');
177
        $display = new Display();
178
        $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime');
179
        $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory');
180
181
        return array(
182
            array(
183
                'store' => array(
184
                    array(
185
                        'data' => 'testing message',
186
                        'type' => 'log'
187
                    ),
188
                    array(
189
                        'name' => 'now',
190
                        'data' => microtime(true),
191
                        'type' => 'speed'
192
                    ),
193
                    array(
194
                        'name' => 'little later',
195
                        'data' => microtime(true) + 1,
196
                        'type' => 'speed'
197
                    ),
198
                    array(
199
                        'name' => 'invalid key',
200
                        'type' => 'foo'
201
                    )
202
                ),
203
                'meta' => array(
204
                    'log'    => 1,
205
                    'memory' => 0,
206
                    'error'  => 1,
207
                    'speed'  => 2
208
                ),
209
                'messages' => array(
210
                    array(
211
                        'message' => 'testing message',
212
                        'type'    => 'log'
213
                    ),
214
                    array(
215
                        'message' => 'now',
216
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true))),
217
                        'type'    => 'speed'
218
                    ),
219
                    array(
220
                        'message' => 'little later',
221
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)),
222
                        'type'    => 'speed'
223
                    ),
224
                    array(
225
                        'message' => 'Unrecognized console log type: foo',
226
                        'type'    => 'error'
227
                    )
228
                )
229
            ),
230
            array(
231
                'store' => array(
232
                    array(
233
                        'data' => 'another testing message',
234
                        'type' => 'log'
235
                    ),
236
                    array(
237
                        'name'      => 'test array',
238
                        'data'      => strlen(serialize(array('key' => 'value'))),
239
                        'data_type' => 'array',
240
                        'type'      => 'memory'
241
                    ),
242
                    array(
243
                        'name'      => 'memory usage test',
244
                        'data'      => memory_get_usage(),
245
                        'data_type' => '',
246
                        'type'      => 'memory'
247
                    ),
248
                    array(
249
                        'data' => $testException->getMessage(),
250
                        'file' => $testException->getFile(),
251
                        'line' => $testException->getLine(),
252
                        'type' => 'error'
253
                    )
254
                ),
255
                'meta' => array(
256
                    'log'    => 1,
257
                    'memory' => 2,
258
                    'error'  => 1,
259
                    'speed'  => 0
260
                ),
261
                'messages' => array(
262
                    array(
263
                        'message' => 'another testing message',
264
                        'type'    => 'log'
265
                    ),
266
                    array(
267
                        'message' => 'array: test array',
268
                        'data'    => $reflectedMemory->invokeArgs(
269
                            $display,
270
                            array(strlen(serialize(array('key' => 'value'))))
271
                        ),
272
                        'type'    => 'memory'
273
                    ),
274
                    array(
275
                        'message' => 'memory usage test',
276
                        'data'    => $reflectedMemory->invokeArgs($display, array(memory_get_usage())),
277
                        'type'    => 'memory'
278
                    ),
279
                    array(
280
                        'message' => sprintf(
281
                            'Line %s: %s in %s',
282
                            $testException->getLine(),
283
                            $testException->getMessage(),
284
                            $testException->getFile()
285
                        ),
286
                        'type'    => 'error'
287
                    )
288
                )
289
            )
290
        );
291
    }
292
293
    protected function setInternalProperty($class, $property, $value)
294
    {
295
        $reflectedClass = new ReflectionClass(get_class($class));
296
        $reflectedProperty = $reflectedClass->getProperty($property);
297
        $reflectedProperty->setAccessible(true);
298
        $reflectedProperty->setValue($class, $value);
299
    }
300
301
    protected function getAccessibleMethod($class, $method)
302
    {
303
        $reflectedClass = new ReflectionClass(get_class($class));
304
        $reflectedMethod = $reflectedClass->getMethod($method);
305
        $reflectedMethod->setAccessible(true);
306
        return $reflectedMethod;
307
    }
308
}
309