Completed
Push — master ( 358045...40fc10 )
by Jacob
02:24
created

DisplayTest::testGetConsoleMessages()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 55
Code Lines 49

Duplication

Lines 20
Ratio 36.36 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 20
loc 55
rs 8.7753
cc 6
eloc 49
nc 6
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()
1 ignored issue
show
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...
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
    public function testGetConsoleMeta()
91
    {
92
        $expectedMeta = array(
93
            'log'    => 1,
94
            'memory' => 0,
95
            'error'  => 0,
96
            'speed'  => 2
97
        );
98
        $console = new Console();
99
        $console->log('testing words');
100
        $console->logSpeed('now');
101
        $console->logSpeed();
102
        $display = new Display();
103
        $display->setConsole($console);
104
        $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta');
105
106
        $consoleMeta = $reflectedMethod->invoke($display);
107
        $this->assertEquals($expectedMeta, $consoleMeta);
108
    }
109
110
    public function testGetConsoleMessages()
111
    {
112
        $console = new Console();
113
        $testLog = 'testing more words';
114
        $console->log($testLog);
115
        $console->logMemory();
116
        $testException = new Exception('test exception');
117
        $console->logError($testException);
118
        $console->logSpeed();
119
        $display = new Display();
120
        $display->setConsole($console);
121
        $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages');
122
123
        $consoleMessages = $reflectedMethod->invoke($display);
124
        foreach ($consoleMessages as $message) {
125
            $this->assertArrayHasKey('message', $message);
126
            $this->assertInternalType('string', $message['message']);
127
            $this->assertArrayHasKey('type', $message);
128
            $this->assertInternalType('string', $message['type']);
129
            switch ($message['type']) {
130
                case 'log':
131
                    $expectedMessage = print_r($testLog, true);
132
                    $this->assertEquals($expectedMessage, $message['message']);
133
                    break;
134 View Code Duplication
                case 'memory':
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
135
                    $expectedMessage = 'PHP';
136
                    $this->assertEquals($expectedMessage, $message['message']);
137
                    $this->assertArrayHasKey('data', $message);
138
                    $this->assertInternalType('string', $message['data']);
139
                    $expectedData = memory_get_usage();
140
                    $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory');
141
                    $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData));
142
                    $this->assertEquals($expectedData, $message['data']);
143
                    break;
144
                case 'error':
145
                    $expectedMessage = sprintf("Line %s: %s in %s",
146
                        $testException->getLine(),
147
                        $testException->getMessage(),
148
                        $testException->getFile()
149
                    );
150
                    $this->assertEquals($expectedMessage, $message['message']);
151
                    break;
152 View Code Duplication
                case 'speed':
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
153
                    $expectedMessage = 'Point in Time';
154
                    $this->assertEquals($expectedMessage, $message['message']);
155
                    $this->assertArrayHasKey('data', $message);
156
                    $this->assertInternalType('string', $message['data']);
157
                    $expectedData = microtime(true);
158
                    $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
159
                    $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData));
160
                    $this->assertEquals($expectedData, $message['data']);
161
                    break;
162
            }
163
        }
164
    }
165
166
    public function testGetSpeedMeta()
167
    {
168
        $elapsedTime = 1234.678;
169
        $allowedTime = 30;
170
        $display = new Display();
171
        $display->setSpeedData(array(
172
            'elapsed' => $elapsedTime,
173
            'allowed' => $allowedTime
174
        ));
175
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
176
        $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime));
177
        $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0));
178
        $expectedMeta = array(
179
            'elapsed' => $elapsedTime,
180
            'allowed' => $allowedTime
181
        );
182
183
        $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta');
184
        $speedMeta = $reflectedMethod->invoke($display);
185
        $this->assertEquals($expectedMeta, $speedMeta);
186
    }
187
188 View Code Duplication
    public function testGetReadableTime()
189
    {
190
        $timeTest = array(
191
            '.032432' => '32.432 ms',
192
            '24.3781' => '24.378 s',
193
            '145.123' => '2.419 m'
194
        );
195
        $display = new Display();
196
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime');
197
198
        foreach ($timeTest as $rawTime => $expectedTime) {
199
            $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime));
200
            $this->assertEquals($expectedTime, $readableTime);
201
        }
202
    }
203
204 View Code Duplication
    public function testGetReadableMemory()
205
    {
206
        $memoryTest = array(
207
            '314'     => '314 b',
208
            '7403'    => '7.23 k',
209
            '2589983' => '2.47 M'
210
        );
211
        $display = new Display();
212
        $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory');
213
214
        foreach ($memoryTest as $rawMemory => $expectedMemory) {
215
            $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory));
216
            $this->assertEquals($expectedMemory, $readableMemory);
217
        }
218
    }
219
220
    protected function getAccessibleMethod(Display $display, $methodName)
221
    {
222
        $reflectedConsole = new ReflectionClass(get_class($display));
223
        $reflectedMethod = $reflectedConsole->getMethod($methodName);
224
        $reflectedMethod->setAccessible(true);
225
        return $reflectedMethod;
226
    }
227
}
228