Test Setup Failed
Push — next ( 738e04...b06172 )
by Jonathan
25:05
created

UtilsTest::testTraceFrameIsListed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Test;
4
5
use Kint\Utils;
6
use PHPUnit_Framework_TestCase;
7
8
class UtilsTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
The property $composer_stash is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $installed_stash is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $composer_test_dir is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
9
{
10
    protected $composer_stash;
0 ignored issues
show
Coding Style introduced by
$composer_stash does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
11
    protected $installed_stash;
0 ignored issues
show
Coding Style introduced by
$installed_stash does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
12
    protected $composer_test_dir;
0 ignored issues
show
Coding Style introduced by
$composer_test_dir does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
14
    public function humanReadableBytesProvider()
15
    {
16
        return array(
17
            '1 byte' => array(
18
                1,
19
                array(
20
                    'value' => 1.0,
21
                    'unit' => 'B',
22
                ),
23
            ),
24
            '1023 bytes' => array(
25
                1023,
26
                array(
27
                    'value' => 1023.0,
28
                    'unit' => 'B',
29
                ),
30
            ),
31
            '1024 bytes' => array(
32
                1024,
33
                array(
34
                    'value' => 1.0,
35
                    'unit' => 'KB',
36
                ),
37
            ),
38
            '1 meg' => array(
39
                pow(2, 20),
40
                array(
41
                    'value' => 1.0,
42
                    'unit' => 'MB',
43
                ),
44
            ),
45
            '1 gig' => array(
46
                pow(2, 30),
47
                array(
48
                    'value' => 1.0,
49
                    'unit' => 'GB',
50
                ),
51
            ),
52
            '1 tig' => array(
53
                pow(2, 40),
54
                array(
55
                    'value' => 1.0,
56
                    'unit' => 'TB',
57
                ),
58
            ),
59
            '>1 tig' => array(
60
                pow(2, 50),
61
                array(
62
                    'value' => 1024.0,
63
                    'unit' => 'TB',
64
                ),
65
            ),
66
            'halfway' => array(
67
                15000,
68
                array(
69
                    'value' => 15000 / 1024,
70
                    'unit' => 'KB',
71
                ),
72
            ),
73
        );
74
    }
75
76
    /**
77
     * @covers \Kint\Utils::getHumanReadableBytes
78
     * @dataProvider humanReadableBytesProvider
79
     */
80
    public function testGetHumanReadableBytes($input, $expect)
81
    {
82
        $this->assertSame($expect, Utils::getHumanReadableBytes($input));
83
    }
84
85
    public function sequentialArrayProvider()
86
    {
87
        return array(
88
            'sequential array' => array(
89
                array(1, 2, 3),
90
                true,
91
            ),
92
            'explicit sequential array' => array(
93
                array(0 => 1, 1 => 2, 2 => 3),
94
                true,
95
            ),
96
            'arrays start at 1' => array(
97
                array(1 => 1, 2 => 2, 3 => 3),
98
                false,
99
            ),
100
            'wrong order' => array(
101
                array(0 => 1, 2 => 2, 1 => 3),
102
                false,
103
            ),
104
            'string keys' => array(
105
                array(0 => 1, 1 => 2, 'two' => 3),
106
                false,
107
            ),
108
            'string int keys' => array(
109
                array('0' => 1, '1' => 2, '2' => 3),
110
                true,
111
            ),
112
            'padded string int keys' => array(
113
                array('00' => 1, '01' => 2, '02' => 3),
114
                false,
115
            ),
116
        );
117
    }
118
119
    /**
120
     * @covers \Kint\Utils::isSequential
121
     * @dataProvider sequentialArrayProvider
122
     */
123
    public function testIsSequential($input, $expect)
124
    {
125
        $this->assertSame($expect, Utils::isSequential($input));
126
    }
127
128
    public function setUp()
129
    {
130
        parent::setUp();
131
132
        if (getenv('KINT_FILE')) {
133
            $this->composer_test_dir = dirname(__DIR__);
134
        } else {
135
            $this->composer_test_dir = KINT_DIR;
136
        }
137
138
        $this->composer_stash = file_get_contents($this->composer_test_dir.'/composer.json');
139
        $this->installed_stash = file_get_contents($this->composer_test_dir.'/vendor/composer/installed.json');
140
    }
141
142
    public function tearDown()
143
    {
144
        parent::tearDown();
145
146
        if ($this->composer_stash) {
147
            file_put_contents($this->composer_test_dir.'/composer.json', $this->composer_stash);
148
            file_put_contents($this->composer_test_dir.'/vendor/composer/installed.json', $this->installed_stash);
149
            $this->composer_stash = null;
150
            $this->installed_stash = null;
151
            if (file_exists($this->composer_test_dir.'/composer/installed.json')) {
152
                unlink($this->composer_test_dir.'/composer/installed.json');
153
            }
154
            if (file_exists($this->composer_test_dir.'/composer')) {
155
                rmdir($this->composer_test_dir.'/composer');
156
            }
157
        }
158
    }
159
160
    /**
161
     * This is a flimsy test but it's as good as it gets without altering
162
     * composer.json mid-test without a proper setup/teardown in place.
163
     *
164
     * @covers \Kint\Utils::composerGetExtras
165
     */
166
    public function testComposerGetExtras()
167
    {
168
        file_put_contents($this->composer_test_dir.'/composer.json', json_encode(array(
169
            'extra' => array(
170
                'kint' => array('test' => 'data'),
171
            ),
172
        )));
173
174
        if (getenv('KINT_FILE')) {
175
            $this->assertEquals(array(), Utils::composerGetExtras('kint'));
176
177
            return;
178
        } else {
179
            $this->assertEquals(array('test' => 'data'), Utils::composerGetExtras('kint'));
180
        }
181
182
        mkdir($this->composer_test_dir.'/composer');
183
        unlink($this->composer_test_dir.'/vendor/composer/installed.json');
184
185
        file_put_contents($this->composer_test_dir.'/composer/installed.json', json_encode(array(
186
            array(
187
                'extra' => array(
188
                    'kint' => array('more' => 'test', 'data'),
189
                ),
190
            ),
191
            array(
192
                'extra' => array(
193
                    'kint' => array('test' => 'ing'),
194
                ),
195
            ),
196
        )));
197
198
        $this->assertEquals(array('more' => 'test', 'data', 'test' => 'ing'), Utils::composerGetExtras('kint'));
199
    }
200
201
    public function traceProvider()
202
    {
203
        $bt = debug_backtrace(true);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bt. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
204
        $bad_bt_1 = $bt;
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
205
        $bad_bt_1[0]['test'] = 'woot';
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
206
        $bad_bt_2 = $bt;
0 ignored issues
show
Coding Style introduced by
$bad_bt_2 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
207
        $bad_bt_2[0]['function'] = 1234;
0 ignored issues
show
Coding Style introduced by
$bad_bt_2 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
208
209
        return array(
210
            'empty' => array(
211
                'trace' => array(),
212
                'expect' => false,
213
            ),
214
            'backtrace' => array(
215
                'trace' => $bt,
216
                'expect' => true,
217
            ),
218
            'bad backtrace, extra key' => array(
219
                'trace' => $bad_bt_1,
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
220
                'expect' => false,
221
            ),
222
            'bad backtrace, wrong type' => array(
223
                'trace' => $bad_bt_2,
0 ignored issues
show
Coding Style introduced by
$bad_bt_2 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
224
                'expect' => false,
225
            ),
226
            'mythical' => array(
227
                'trace' => array(
228
                    array(
229
                        'function' => 'mythical_internal_function_with_no_args_that_results_in_a_backtrace',
230
                        'file' => __FILE__,
231
                        'line' => 1,
232
                    ),
233
                ),
234
                'expect' => true,
235
            ),
236
            'normal array' => array(
237
                'trace' => array(1, 2, 3),
238
                'expect' => false,
239
            ),
240
        );
241
    }
242
243
    /**
244
     * @dataProvider traceProvider
245
     * @covers \Kint\Utils::isTrace
246
     */
247
    public function testIsTrace(array $trace, $expected)
248
    {
249
        $this->assertEquals($expected, Utils::isTrace($trace));
250
    }
251
252
    public function frameProvider()
253
    {
254
        return array(
255
            'function match' => array(
256
                'frame' => array(
257
                    'function' => 'testAWeirdFunctionName',
258
                ),
259
                'matches' => array('testaweirdfunctionname'),
260
                'expected' => true,
261
            ),
262
            'function no match denormalized' => array(
263
                'frame' => array(
264
                    'function' => 'testAWeirdFunctionName',
265
                ),
266
                'matches' => array('testAWeirdFunctionName'),
267
                'expected' => false,
268
            ),
269
            'function no match method' => array(
270
                'frame' => array(
271
                    'function' => 'testAWeirdFunctionName',
272
                ),
273
                'matches' => array(array('test', 'testaweirdfunctionname')),
274
                'expected' => false,
275
            ),
276
            'method no match function' => array(
277
                'frame' => array(
278
                    'function' => 'testAWeirdFunctionName',
279
                    'class' => 'test',
280
                ),
281
                'matches' => array('testAWeirdFunctionName'),
282
                'expected' => false,
283
            ),
284
            'method match' => array(
285
                'frame' => array(
286
                    'function' => 'testAWeirdFunctionName',
287
                    'class' => 'test',
288
                ),
289
                'matches' => array(array('test', 'testaweirdfunctionname')),
290
                'expected' => true,
291
            ),
292
        );
293
    }
294
295
    /**
296
     * @dataProvider frameProvider
297
     * @covers \Kint\Utils::traceFrameIsListed
298
     */
299
    public function testTraceFrameIsListed(array $frame, array $matches, $expected)
300
    {
301
        $this->assertEquals($expected, Utils::traceFrameIsListed($frame, $matches));
302
    }
303
304
    /**
305
     * @covers \Kint\Utils::normalizeAliases
306
     */
307
    public function testNormalizeAliases()
308
    {
309
        $input = array(
310
            'CamelCaseFunction',
311
            'snake_case_function',
312
            'One_of_the_FunctionsMyColleaguesMADE__',
313
            'stringThatCan\'tBeAfunction',
314
            'another string that can not be a function',
315
            array('clASs', 'meThod'),
316
            array($this, 'meThod'),
317
            array('a', 'b', 'c'),
318
            array('\\big\\long\\class\\name', 'method'),
319
        );
320
321
        $expected = array(
322
            'camelcasefunction',
323
            'snake_case_function',
324
            'one_of_the_functionsmycolleaguesmade__',
325
            array('class', 'method'),
326
            array('big\\long\\class\\name', 'method'),
327
        );
328
329
        Utils::normalizeAliases($input);
330
331
        $this->assertEquals($expected, $input);
332
    }
333
}
334