Completed
Push — master ( a0701d...6d2756 )
by Jacob
02:22
created

DisplayTest::setInternalProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 3
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
        $display = new Display();
177
        $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime');
178
179
        return array(
180
            array(
181
                'store' => array(
182
                    array(
183
                        'data' => 'testing message',
184
                        'type' => 'log'
185
                    ),
186
                    array(
187
                        'name' => 'now',
188
                        'data' => microtime(true),
189
                        'type' => 'speed'
190
                    ),
191
                    array(
192
                        'name' => 'little later',
193
                        'data' => microtime(true) + 1,
194
                        'type' => 'speed'
195
                    ),
196
                    array(
197
                        'name' => 'invalid key',
198
                        'type' => 'foo'
199
                    )
200
                ),
201
                'meta' => array(
202
                    'log'    => 1,
203
                    'memory' => 0,
204
                    'error'  => 1,
205
                    'speed'  => 2
206
                ),
207
                'messages' => array(
208
                    array(
209
                        'message' => 'testing message',
210
                        'type'    => 'log'
211
                    ),
212
                    array(
213
                        'message' => 'now',
214
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true))),
215
                        'type'    => 'speed'
216
                    ),
217
                    array(
218
                        'message' => 'little later',
219
                        'data'    => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)),
220
                        'type'    => 'speed'
221
                    ),
222
                    array(
223
                        'message' => 'Unrecognized console log type: foo',
224
                        'type'    => 'error'
225
                    )
226
                )
227
            )
228
        );
229
    }
230
231
    protected function setInternalProperty($class, $property, $value)
232
    {
233
        $reflectedClass = new ReflectionClass(get_class($class));
234
        $reflectedProperty = $reflectedClass->getProperty($property);
235
        $reflectedProperty->setAccessible(true);
236
        $reflectedProperty->setValue($class, $value);
237
    }
238
239
    protected function getAccessibleMethod($class, $method)
240
    {
241
        $reflectedClass = new ReflectionClass(get_class($class));
242
        $reflectedMethod = $reflectedClass->getMethod($method);
243
        $reflectedMethod->setAccessible(true);
244
        return $reflectedMethod;
245
    }
246
}
247