Test Setup Failed
Push — test ( 03126c...e218c3 )
by Jonathan
07:01
created

KintTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 365
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
B testSettings() 0 27 4
B pathProvider() 0 33 1
A testShortenPath() 0 12 1
A testGetIdeLink() 0 9 1
A setUp() 0 9 2
A tearDown() 0 17 4
B testComposerGetExtras() 0 32 2
B getCalleeInfoProvider() 0 179 3
A testGetCalleeInfo() 0 11 1
1
<?php
2
3
namespace Kint\Test;
4
5
use Kint\Kint;
6
use Kint\Test\Fixtures\Php56TestClass;
7
use Kint\Test\Fixtures\TestClass;
8
use ReflectionClass;
9
use ReflectionMethod;
10
use ReflectionProperty;
11
12
class KintTest extends KintTestCase
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...
13
{
14
    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...
15
    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...
16
17
    /**
18
     * @covers \Kint\Kint::settings
19
     */
20
    public function testSettings()
21
    {
22
        $r = new ReflectionClass('Kint');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. 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...
23
24
        $props = $r->getProperties(ReflectionProperty::IS_STATIC);
25
        $props_array = array();
0 ignored issues
show
Coding Style introduced by
$props_array 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...
26
        foreach ($props as $prop) {
27
            if ($prop->isPublic() && $prop->isStatic()) {
28
                $props_array[$prop->getName()] = $prop->getValue();
0 ignored issues
show
Coding Style introduced by
$props_array 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...
29
            }
30
        }
31
32
        $props = $r->getStaticProperties();
33
34
        $this->assertEquals($props_array, $stash = Kint::settings());
0 ignored issues
show
Coding Style introduced by
$props_array 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...
35
36
        $props_array['enabled_mode'] = Kint::$enabled_mode = false;
0 ignored issues
show
Coding Style introduced by
$props_array 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...
37
        $props_array['file_link_format'] = Kint::$file_link_format = 'linky';
0 ignored issues
show
Coding Style introduced by
$props_array 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...
38
        $props_array['app_root_dirs'] = Kint::$app_root_dirs = array('/' => '<fsroot>');
0 ignored issues
show
Coding Style introduced by
$props_array 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...
39
        $props_array['max_depth'] = Kint::$max_depth = 0;
0 ignored issues
show
Coding Style introduced by
$props_array 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...
40
        $props_array['expanded'] = Kint::$expanded = true;
0 ignored issues
show
Coding Style introduced by
$props_array 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...
41
        $props_array['cli_detection'] = Kint::$cli_detection = false;
0 ignored issues
show
Coding Style introduced by
$props_array 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...
42
43
        $this->assertNotEquals($props, $r->getStaticProperties());
44
        $this->assertEquals($props_array, Kint::settings($stash));
0 ignored issues
show
Coding Style introduced by
$props_array 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...
45
        $this->assertEquals($props, $r->getStaticProperties());
46
    }
47
48
    public function pathProvider()
49
    {
50
        return array(
51
            'standard file' => array(
52
                'path' => __FILE__,
53
                'expect' => '<tests>/KintTest.php',
54
            ),
55
            'standard dir' => array(
56
                'path' => __DIR__,
57
                'expect' => '<tests>',
58
            ),
59
            'parent dir' => array(
60
                'path' => KINT_DIR,
61
                'expect' => '<kint>',
62
            ),
63
            'sub file' => array(
64
                'path' => KINT_DIR.'/src//test',
65
                'expect' => '<kint>/src/test',
66
            ),
67
            'common path' => array(
68
                'path' => dirname(KINT_DIR).'/test/',
69
                'expect' => '.../test',
70
            ),
71
            'root path' => array(
72
                'path' => '/',
73
                'expect' => '/',
74
            ),
75
            'no common path' => array(
76
                'path' => '/asdfasdf/test/',
77
                'expect' => '/asdfasdf/test',
78
            ),
79
        );
80
    }
81
82
    /**
83
     * @dataProvider pathProvider
84
     * @covers \Kint\Kint::shortenPath
85
     */
86
    public function testShortenPath($path, $expect)
87
    {
88
        Kint::$app_root_dirs = array(
89
            KINT_DIR => '<kint>',
90
            KINT_DIR.'/test' => '<test>',
91
            '' => '<Nothing!>',
92
            __DIR__ => '<tests>',
93
            KINT_DIR.'/tes' => '<tes>',
94
        );
95
96
        $this->assertEquals($expect, Kint::shortenPath($path));
97
    }
98
99
    /**
100
     * @covers \Kint\Kint::getIdeLink
101
     */
102
    public function testGetIdeLink()
103
    {
104
        Kint::$file_link_format = '<a href="%f:%l">%f:%l</a>';
105
106
        $file = uniqid('', true);
107
        $line = uniqid('', true);
108
109
        $this->assertEquals('<a href="'.$file.':'.$line.'">'.$file.':'.$line.'</a>', Kint::getIdeLink($file, $line));
110
    }
111
112
    public function setUp()
113
    {
114
        parent::setUp();
115
116
        if (!getenv('KINT_FILE')) {
117
            $this->composer_stash = file_get_contents(KINT_DIR.'/composer.json');
118
            $this->installed_stash = file_get_contents(KINT_DIR.'/vendor/composer/installed.json');
119
        }
120
    }
121
122
    public function tearDown()
123
    {
124
        parent::tearDown();
125
126
        if ($this->composer_stash) {
127
            file_put_contents(KINT_DIR.'/composer.json', $this->composer_stash);
128
            file_put_contents(KINT_DIR.'/vendor/composer/installed.json', $this->installed_stash);
129
            $this->composer_stash = null;
130
            $this->installed_stash = null;
131
            if (file_exists(KINT_DIR.'/composer/installed.json')) {
132
                unlink(KINT_DIR.'/composer/installed.json');
133
            }
134
            if (file_exists(KINT_DIR.'/composer')) {
135
                rmdir(KINT_DIR.'/composer');
136
            }
137
        }
138
    }
139
140
    /**
141
     * Test Kint::composerGetExtras.
142
     *
143
     * This is a flimsy test but it's as good as it gets without altering
144
     * composer.json mid-test without a proper setup/teardown in place
145
     *
146
     * @covers \Kint\Kint::composerGetExtras
147
     */
148
    public function testComposerGetExtras()
149
    {
150
        if (getenv('KINT_FILE')) {
151
            $this->markTestSkipped('Not testing composerGetExtras in single file build');
152
        }
153
154
        file_put_contents(KINT_DIR.'/composer.json', json_encode(array(
155
            'extra' => array(
156
                'kint' => array('test' => 'data'),
157
            ),
158
        )));
159
160
        $this->assertEquals(array('test' => 'data'), Kint::composerGetExtras('kint'));
161
162
        mkdir(KINT_DIR.'/composer');
163
        unlink(KINT_DIR.'/vendor/composer/installed.json');
164
165
        file_put_contents(KINT_DIR.'/composer/installed.json', json_encode(array(
166
            array(
167
                'extra' => array(
168
                    'kint' => array('more' => 'test', 'data'),
169
                ),
170
            ),
171
            array(
172
                'extra' => array(
173
                    'kint' => array('test' => 'ing'),
174
                ),
175
            ),
176
        )));
177
178
        $this->assertEquals(array('more' => 'test', 'data', 'test' => 'ing'), Kint::composerGetExtras('kint'));
179
    }
180
181
    public function getCalleeInfoProvider()
182
    {
183
        $basetrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
184
        $dumpframe = array(
185
            'class' => 'Kint',
186
            'function' => 'dump',
187
        );
188
189
        $data['empty trace'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
190
            'trace' => array(
191
            ),
192
            'param_count' => 1234,
193
            'expect' => array(
194
                null,
195
                array(),
196
                null,
197
                null,
198
                array(),
199
            ),
200
        );
201
202
        $data['full trace'] = array(
203
            'trace' => array_merge(array($dumpframe), $basetrace),
204
            'param_count' => 1234,
205
            'expect' => array(
206
                null,
207
                array(),
208
                $dumpframe,
209
                array(
210
                    'function' => __FUNCTION__,
211
                    'class' => __CLASS__,
212
                    'type' => '->',
213
                ),
214
            ),
215
        );
216
217
        $data['trace with params'] = array(
218
            'trace' => array_merge(
219
                    array(
220
                        $dumpframe + array(
221
                            'file' => TestClass::DUMP_FILE,
222
                            'line' => TestClass::DUMP_LINE,
223
                        ),
224
                    ),
225
                    $basetrace
226
                ),
227
            'param_count' => 3,
228
            'expect' => array(
229
                array(
230
                    array('name' => '$x', 'path' => '$x', 'expression' => false),
231
                    array('name' => '$y', 'path' => '$y', 'expression' => false),
232
                    array('name' => '$z', 'path' => '$z', 'expression' => false),
233
                ),
234
                array(),
235
                $dumpframe + array(
236
                    'file' => TestClass::DUMP_FILE,
237
                    'line' => TestClass::DUMP_LINE,
238
                ),
239
            ),
240
        );
241
242
        $data['trace with modifiers'] = array(
243
            'trace' => array_merge(
244
                    array(
245
                        $dumpframe + array(
246
                            'file' => TestClass::DUMP_FILE,
247
                            'line' => TestClass::DUMP_LINE + 1,
248
                        ),
249
                    ),
250
                    $basetrace
251
                ),
252
            'param_count' => 0,
253
            'expect' => array(
254
                array(),
255
                array('!', '+'),
256
                $dumpframe + array(
257
                    'file' => TestClass::DUMP_FILE,
258
                    'line' => TestClass::DUMP_LINE + 1,
259
                ),
260
            ),
261
        );
262
263
        $data['trace function with modifier'] = array(
264
            'trace' => array_merge(
265
                    array(
266
                        array(
267
                            'function' => 'd',
268
                            'file' => TestClass::DUMP_FILE,
269
                            'line' => TestClass::DUMP_LINE + 2,
270
                        ),
271
                    ),
272
                    $basetrace
273
                ),
274
            'param_count' => 1,
275
            'expect' => array(
276
                array(
277
                    array(
278
                        'name' => '$x',
279
                        'path' => '$x',
280
                        'expression' => false,
281
                    ),
282
                ),
283
                array('~'),
284
                array(
285
                    'function' => 'd',
286
                    'file' => TestClass::DUMP_FILE,
287
                    'line' => TestClass::DUMP_LINE + 2,
288
                ),
289
            ),
290
        );
291
292
        // HHVM doesn't support multiple unpack parameters
293
        if (KINT_PHP56 && !defined('HHVM_VERSION')) {
294
            $data['trace with unpack'] = array(
295
                'trace' => array_merge(
296
                        array(
297
                            $dumpframe + array(
298
                                'file' => Php56TestClass::DUMP_FILE,
299
                                'line' => Php56TestClass::DUMP_LINE,
300
                            ),
301
                        ),
302
                        $basetrace
303
                    ),
304
                'param_count' => 4,
305
                'expect' => array(
306
                    array(
307
                        array(
308
                            'name' => '$x',
309
                            'path' => '$x',
310
                            'expression' => false,
311
                        ),
312
                        array(
313
                            'name' => '$y',
314
                            'path' => '$y',
315
                            'expression' => false,
316
                        ),
317
                        array(
318
                            'name' => 'reset($z)',
319
                            'path' => 'reset($z)',
320
                            'expression' => false,
321
                        ),
322
                        array(
323
                            'name' => 'array_values($z)[1]',
324
                            'path' => 'array_values($z)[1]',
325
                            'expression' => false,
326
                        ),
327
                    ),
328
                    array(),
329
                    $dumpframe + array(
330
                        'file' => Php56TestClass::DUMP_FILE,
331
                        'line' => Php56TestClass::DUMP_LINE,
332
                    ),
333
                ),
334
            );
335
336
            $data['trace with double unpack'] = array(
337
                'trace' => array_merge(
338
                        array(
339
                            $dumpframe + array(
340
                                'file' => Php56TestClass::DUMP_FILE,
341
                                'line' => Php56TestClass::DUMP_LINE + 1,
342
                            ),
343
                        ),
344
                        $basetrace
345
                    ),
346
                'param_count' => 10,
347
                'expect' => array(
348
                    array(),
349
                    array(),
350
                    $dumpframe + array(
351
                        'file' => Php56TestClass::DUMP_FILE,
352
                        'line' => Php56TestClass::DUMP_LINE + 1,
353
                    ),
354
                ),
355
            );
356
        }
357
358
        return $data;
359
    }
360
361
    /**
362
     * @dataProvider getCalleeInfoProvider
363
     * @covers \Kint\Kint::getCalleeInfo
364
     */
365
    public function testGetCalleeInfo($trace, $param_count, $expect)
0 ignored issues
show
Coding Style introduced by
$param_count 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...
Coding Style Naming introduced by
The parameter $param_count is not named in camelCase.

This check marks parameter 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...
366
    {
367
        $func = new ReflectionMethod('Kint', 'getCalleeInfo');
368
        $func->setAccessible(true);
369
370
        $output = $func->invoke(null, $trace, $param_count);
0 ignored issues
show
Coding Style introduced by
$param_count 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...
371
372
        $output = array_slice($output, 0, count($expect));
373
374
        $this->assertSame($expect, $output);
375
    }
376
}
377