Test Setup Failed
Push — test ( dd7d7c...b3915a )
by Jonathan
03:06
created

KintTest::testSetStatesFromStatics()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 0
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Test;
4
5
use Kint\Kint;
6
use Kint\Object\BasicObject;
7
use Kint\Parser\Parser;
8
use Kint\Parser\TimestampPlugin;
9
use Kint\Renderer\TextRenderer;
10
use Kint\Test\Fixtures\Php56TestClass;
11
use Kint\Test\Fixtures\TestClass;
12
use Prophecy\Argument;
13
use ReflectionClass;
14
use ReflectionProperty;
15
16
class KintTest extends KintTestCase
17
{
18
    /**
19
     * @covers \Kint\Kint::__construct
20
     * @covers \Kint\Kint::getParser
21
     * @covers \Kint\Kint::getRenderer
22
     */
23
    public function testConstruct()
24
    {
25
        $p = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
26
        $r = new TextRenderer();
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...
27
28
        $k = new Kint($p, $r);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
29
30
        $this->assertSame($p, $k->getParser());
31
        $this->assertSame($r, $k->getRenderer());
32
    }
33
34
    /**
35
     * @covers \Kint\Kint::setParser
36
     * @covers \Kint\Kint::getParser
37
     */
38 View Code Duplication
    public function testGetSetParser()
0 ignored issues
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...
39
    {
40
        $p = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
41
        $p2 = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p2. 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...
42
43
        $k = new Kint($p, new TextRenderer());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
44
45
        $this->assertSame($p, $k->getParser());
46
        $k->setParser($p2);
47
        $this->assertSame($p2, $k->getParser());
48
        $this->assertNotSame($p, $p2);
49
    }
50
51
    /**
52
     * @covers \Kint\Kint::setRenderer
53
     * @covers \Kint\Kint::getRenderer
54
     */
55 View Code Duplication
    public function testGetSetRenderer()
0 ignored issues
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...
56
    {
57
        $r = new TextRenderer();
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...
58
        $r2 = new TextRenderer();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r2. 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...
59
60
        $k = new Kint(new Parser(), $r);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
61
62
        $this->assertSame($r, $k->getRenderer());
63
        $k->setRenderer($r2);
64
        $this->assertSame($r2, $k->getRenderer());
65
        $this->assertNotSame($r, $r2);
66
    }
67
68
    /**
69
     * @covers \Kint\Kint::setStatesFromStatics
70
     */
71
    public function testSetStatesFromStatics()
72
    {
73
        $p1 = new TimestampPlugin();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p1. 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...
74
        $p2 = new TimestampPlugin();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p2. 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...
75
        $p3 = new TimestampPlugin();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p3. 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...
76
        $p4 = new TimestampPlugin();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p4. 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...
77
78
        $parser = $this->prophesize('Kint\\Parser\\Parser');
79
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
80
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
81
82
        $renderer->setExpand(true)->shouldBeCalledTimes(1);
83
        $renderer->setReturnMode(true)->shouldBeCalledTimes(1);
84
        $renderer->setShowTrace(true)->shouldBeCalledTimes(1);
85
86
        $parser->setDepthLimit(42)->shouldBeCalledTimes(1);
87
        $parser->clearPlugins()->shouldBeCalledTimes(1);
88
89
        $renderer->filterParserPlugins(array($p1, $p2, $p3, $p4))->shouldBeCalledTimes(1)->willReturn(array($p1, $p3, $p4));
90
91
        // Argument::that is a workaround for a bug in prophet's Argument::is
92
        $parser->addPlugin(
93
            Argument::that(function ($arg) use ($p1) {
94
                return $arg === $p1;
95
            })
96
        )->shouldBeCalledTimes(1);
97
        $parser->addPlugin(
98
            Argument::that(function ($arg) use ($p3) {
99
                return $arg === $p3;
100
            })
101
        )->shouldBeCalledTimes(1);
102
        $parser->addPlugin(
103
            Argument::that(function ($arg) use ($p4) {
104
                return $arg === $p4;
105
            })
106
        )->shouldBeCalledTimes(1);
107
108
        $k->setStatesFromStatics(array(
109
            'expanded' => true,
110
            'return' => true,
111
            'display_called_from' => true,
112
            'max_depth' => 42,
113
            'plugins' => array($p1, $p2, $p3, $p4),
114
        ));
115
    }
116
117
    /**
118
     * @covers \Kint\Kint::setStatesFromStatics
119
     */
120
    public function testSetStatesFromStaticsStringPlugins()
121
    {
122
        $r = new ReflectionProperty('Kint\\Kint', 'plugin_pool');
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...
123
        $r->setAccessible(true);
124
        $r->setValue(array());
125
126
        $parser = $this->prophesize('Kint\\Parser\\Parser');
127
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
128
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
129
130
        $renderer->setExpand(false)->shouldBeCalledTimes(1);
131
        $renderer->setReturnMode(false)->shouldBeCalledTimes(1);
132
        $renderer->setShowTrace(false)->shouldBeCalledTimes(1);
133
134
        $parser->setDepthLimit(false)->shouldBeCalledTimes(1);
135
        $parser->clearPlugins()->shouldBeCalledTimes(1);
136
137
        $renderer->filterParserPlugins(Argument::any())->shouldBeCalledTimes(1)->will(function ($args) {
138
            $out = array();
139
140
            foreach ($args[0] as $plugin) {
141
                if ($plugin instanceof TimestampPlugin) {
142
                    $out[] = $plugin;
143
                }
144
            }
145
146
            return $out;
147
        });
148
149
        $parser->addPlugin(Argument::type('Kint\\Parser\\TimestampPlugin'))->shouldBeCalledTimes(1);
150
151
        $k->setStatesFromStatics(array(
152
            'plugins' => array(
153
                'Kint\\Parser\\TimestampPlugin',
154
                'Kint\\Parser\\MicrotimePlugin',
155
            ),
156
        ));
157
    }
158
159
    /**
160
     * @covers \Kint\Kint::setStatesFromStatics
161
     */
162
    public function testSetStatesFromStaticsEmpty()
163
    {
164
        $parser = $this->prophesize('Kint\\Parser\\Parser');
165
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
166
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
167
168
        $renderer->setExpand(false)->shouldBeCalledTimes(1);
169
        $renderer->setReturnMode(false)->shouldBeCalledTimes(1);
170
        $renderer->setShowTrace(false)->shouldBeCalledTimes(1);
171
172
        $parser->setDepthLimit(false)->shouldBeCalledTimes(1);
173
        $parser->clearPlugins()->shouldBeCalledTimes(1);
174
175
        $k->setStatesFromStatics(array());
176
    }
177
178
    /**
179
     * @covers \Kint\Kint::setStatesFromCallInfo
180
     */
181
    public function testSetStatesFromCallInfo()
182
    {
183
        $r = new TextRenderer();
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...
184
        $p = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
185
        $k = new Kint($p, $r);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
186
187
        // Set up defaults
188
        $k->setStatesFromStatics(array(
189
            'max_depth' => 42,
190
        ));
191
192
        $k->setStatesFromCallInfo(array('foo' => 'bar'));
193
194
        $this->assertSame(array('foo' => 'bar'), $r->getCallInfo());
195
        $this->assertFalse($r->getExpand());
196
        $this->assertFalse($r->getReturnMode());
197
        $this->assertSame(42, $p->getDepthLimit());
198
        $this->assertNull($p->getCallerClass());
199
200
        $k->setStatesFromCallInfo(array(
201
            'modifiers' => array('!', '@', '+'),
202
            'caller' => array(
203
                'class' => 'test1234',
204
            ),
205
        ));
206
207
        $this->assertTrue($r->getExpand());
208
        $this->assertTrue($r->getReturnMode());
209
        $this->assertFalse($p->getDepthLimit());
210
        $this->assertSame('test1234', $p->getCallerClass());
211
    }
212
213
    /**
214
     * @covers \Kint\Kint::dumpAll
215
     */
216
    public function testDumpAll()
217
    {
218
        $parser = $this->prophesize('Kint\\Parser\\Parser');
219
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
220
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
221
222
        $dumpee = $k;
223
        $base = BasicObject::blank();
224
225
        $renderer->preRender()->shouldBeCalledTimes(1)->willReturn('pre.');
226
227
        $parser->parse(Argument::is($dumpee), Argument::is($base))->shouldBeCalledTimes(2)->willReturn($base);
228
        $renderer->render(Argument::is($base))->shouldBeCalledTimes(2)->willReturn('render.');
229
230
        $renderer->postRender()->shouldBeCalledTimes(1)->willReturn('post');
231
232
        $this->assertSame('pre.render.render.post', $k->dumpAll(array($dumpee, $dumpee), array($base, $base)));
233
    }
234
235
    /**
236
     * @covers \Kint\Kint::dumpAll
237
     */
238
    public function testDumpNothing()
239
    {
240
        $parser = $this->prophesize('Kint\\Parser\\Parser');
241
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
242
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
243
244
        $renderer->preRender()->shouldBeCalledTimes(1)->willReturn('pre.');
245
        $renderer->renderNothing()->shouldBeCalledTimes(1)->willReturn('nothing.');
246
        $renderer->postRender()->shouldBeCalledTimes(1)->willReturn('post');
247
248
        $parser->parse()->shouldNotBeCalled();
249
250
        $this->assertSame('pre.nothing.post', $k->dumpAll(array(), array()));
251
    }
252
253
    /**
254
     * @covers \Kint\Kint::dumpAll
255
     * @expectedException \InvalidArgumentException
256
     */
257 View Code Duplication
    public function testDumpAllUnmatchingArgs()
0 ignored issues
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...
258
    {
259
        $p = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
260
        $r = new TextRenderer();
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...
261
        $k = new Kint($p, $r);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
262
263
        $k->dumpAll(array($k), array(BasicObject::blank(), 'bar' => 'baz'));
0 ignored issues
show
Documentation introduced by
array(\Kint\Object\Basic...lank(), 'bar' => 'baz') is of type array<integer|string,obj...ject>","bar":"string"}>, but the function expects a array<integer,object<Kint\Object\BasicObject>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
264
    }
265
266
    /**
267
     * @covers \Kint\Kint::dumpAll
268
     * @expectedException \InvalidArgumentException
269
     */
270 View Code Duplication
    public function testDumpAllIncorrectBase()
0 ignored issues
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...
271
    {
272
        $p = new Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
273
        $r = new TextRenderer();
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...
274
        $k = new Kint($p, $r);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
275
276
        $k->dumpAll(array($k), array('foo'));
0 ignored issues
show
Documentation introduced by
array('foo') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<Kint\Object\BasicObject>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
277
    }
278
279
    /**
280
     * @covers \Kint\Kint::dumpVar
281
     */
282
    public function testDumpVar()
283
    {
284
        $parser = $this->prophesize('Kint\\Parser\\Parser');
285
        $renderer = $this->prophesize('Kint\\Renderer\\TextRenderer');
286
        $k = new Kint($parser->reveal(), $renderer->reveal());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
287
288
        $dumpee = $k;
289
        $base = BasicObject::blank();
290
291
        $parser->parse(Argument::is($dumpee), Argument::is($base))->shouldBeCalledTimes(1)->willReturn($base);
292
        $renderer->render(Argument::is($base))->shouldBeCalledTimes(1)->willReturn('render');
293
294
        $this->assertSame('render', $k->dumpVar($dumpee, $base));
295
    }
296
297
    /**
298
     * @covers \Kint\Kint::getStatics
299
     */
300
    public function testGetStatics()
301
    {
302
        $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...
303
304
        $props = $r->getProperties(ReflectionProperty::IS_STATIC);
305
        $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...
306
        foreach ($props as $prop) {
307
            if ($prop->isPublic() && $prop->isStatic()) {
308
                $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...
309
            }
310
        }
311
312
        ksort($props_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...
313
314
        $this->assertSame($props_array, $stash = Kint::getStatics());
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...
315
    }
316
317
    public function staticModeProvider()
318
    {
319
        return array(
320
            'no options' => array(
321
                array(),
322
                false,
323
            ),
324
            'auto without cli' => array(
325
                array(
326
                    'enabled_mode' => true,
327
                    'mode_default' => 42,
328
                    'cli_detection' => false,
329
                    'mode_default_cli' => 43,
330
                    'renderers' => array(
331
                        42 => 'Kint\\Renderer\\RichRenderer',
332
                        43 => 'Kint\\Renderer\\CliRenderer',
333
                        44 => 'Kint\\Renderer\\PlainRenderer',
334
                    ),
335
                ),
336
                'Kint\\Renderer\\RichRenderer',
337
            ),
338
            'auto with cli' => array(
339
                array(
340
                    'enabled_mode' => true,
341
                    'mode_default' => 42,
342
                    'cli_detection' => true,
343
                    'mode_default_cli' => 43,
344
                    'renderers' => array(
345
                        42 => 'Kint\\Renderer\\RichRenderer',
346
                        43 => 'Kint\\Renderer\\CliRenderer',
347
                        44 => 'Kint\\Renderer\\PlainRenderer',
348
                    ),
349
                ),
350
                'Kint\\Renderer\\CliRenderer',
351
            ),
352
            'specific' => array(
353
                array(
354
                    'enabled_mode' => 44,
355
                    'mode_default' => 42,
356
                    'cli_detection' => true,
357
                    'mode_default_cli' => 43,
358
                    'renderers' => array(
359
                        42 => 'Kint\\Renderer\\RichRenderer',
360
                        43 => 'Kint\\Renderer\\CliRenderer',
361
                        44 => 'Kint\\Renderer\\PlainRenderer',
362
                    ),
363
                ),
364
                'Kint\\Renderer\\PlainRenderer',
365
            ),
366
            'disabled' => array(
367
                array('enabled_mode' => false),
368
                false,
369
            ),
370
            'missing renderer' => array(
371
                array(
372
                    'enabled_mode' => 45,
373
                    'mode_default' => 42,
374
                    'cli_detection' => true,
375
                    'mode_default_cli' => 43,
376
                    'renderers' => array(
377
                        42 => 'Kint\\Renderer\\RichRenderer',
378
                        43 => 'Kint\\Renderer\\CliRenderer',
379
                        44 => 'Kint\\Renderer\\PlainRenderer',
380
                    ),
381
                ),
382
                'Kint\\Renderer\\TextRenderer',
383
            ),
384
        );
385
    }
386
387
    /**
388
     * @covers \Kint\Kint::createFromStatics
389
     * @dataProvider staticModeProvider
390
     */
391
    public function testCreateFromStatics($statics, $renderer_class)
0 ignored issues
show
Coding Style introduced by
$renderer_class 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 $renderer_class 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...
392
    {
393
        $k = Kint::createFromStatics($statics);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $k. 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...
394
395
        if ($renderer_class) {
0 ignored issues
show
Coding Style introduced by
$renderer_class 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...
396
            $this->assertSame($renderer_class, get_class($k->getRenderer()));
0 ignored issues
show
Coding Style introduced by
$renderer_class 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...
397
        } else {
398
            $this->assertNull($k);
399
        }
400
    }
401
402
    public function baseProvider()
403
    {
404
        return array(
405
            'normal params' => array(
406
                array(
407
                    array(
408
                        'name' => '$a',
409
                        'path' => '$a',
410
                        'expression' => false,
411
                    ),
412
                    array(
413
                        'name' => '$b[...]',
414
                        'path' => '$b[$a]',
415
                        'expression' => false,
416
                    ),
417
                ),
418
                2,
419
                array(
420
                    BasicObject::blank('$a'),
421
                    BasicObject::blank('$b[...]', '$b[$a]'),
422
                ),
423
            ),
424
            'blacklisted params' => array(
425
                array(
426
                    array(
427
                        'name' => 'true',
428
                        'path' => 'true',
429
                        'expression' => false,
430
                    ),
431
                    array(
432
                        'name' => '[...]',
433
                        'path' => '[$a, $b, $c]',
434
                        'expression' => false,
435
                    ),
436
                ),
437
                2,
438
                array(
439
                    BasicObject::blank(null, 'true'),
440
                    BasicObject::blank(null, '[$a, $b, $c]'),
441
                ),
442
            ),
443
            'expression params' => array(
444
                array(
445
                    array(
446
                        'name' => '$a + $b',
447
                        'path' => '$a + $b',
448
                        'expression' => true,
449
                    ),
450
                    array(
451
                        'name' => '[...] + $c[...]',
452
                        'path' => '[$a, $b] + $c[$d]',
453
                        'expression' => true,
454
                    ),
455
                ),
456
                2,
457
                array(
458
                    BasicObject::blank('$a + $b', '($a + $b)'),
459
                    BasicObject::blank('[...] + $c[...]', '([$a, $b] + $c[$d])'),
460
                ),
461
            ),
462
            'missing params' => array(
463
                array(),
464
                2,
465
                array(
466
                    BasicObject::blank(null, '$0'),
467
                    BasicObject::blank(null, '$1'),
468
                ),
469
            ),
470
            'no params' => array(
471
                array(),
472
                0,
473
                array(),
474
            ),
475
        );
476
    }
477
478
    /**
479
     * @covers \Kint\Kint::getBasesFromParamInfo
480
     * @dataProvider baseProvider
481
     */
482
    public function testGetBasesFromParamInfo(array $paraminfo, $count, array $expect)
483
    {
484
        $bases = Kint::getBasesFromParamInfo($paraminfo, $count);
485
486
        $this->assertEquals($expect, $bases);
487
    }
488
489
    public function getCallInfoProvider()
490
    {
491
        $aliases = array(
492
            array('kint', 'dump'),
493
            'd',
494
            's',
495
        );
496
497
        $basetrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
498
        $basetrace[0]['file'] = __FILE__;
499
        $basetrace[0]['line'] = __LINE__;
500
        $dumpframe = array(
501
            'class' => 'Kint',
502
            'function' => 'dump',
503
        );
504
505
        $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...
506
            'aliases' => $aliases,
507
            'trace' => array(
508
            ),
509
            'param_count' => 1234,
510
            'expect' => array(
511
                'params' => null,
512
                'modifiers' => array(),
513
                'callee' => null,
514
                'caller' => null,
515
                'trace' => array(),
516
            ),
517
        );
518
519
        $data['full trace'] = array(
520
            'aliases' => $aliases,
521
            'trace' => array_merge(
522
                $basetrace,
523
                array(
524
                    $dumpframe + array(
525
                        'file' => TestClass::DUMP_FILE,
526
                        'line' => TestClass::DUMP_LINE,
527
                    ),
528
                ),
529
                array(
530
                    array(
531
                        'function' => 'usort',
532
                    ),
533
                ),
534
                $basetrace
535
            ),
536
            'param_count' => 1234,
537
            'expect' => array(
538
                'params' => null,
539
                'modifiers' => array(),
540
                'callee' => $dumpframe + array(
541
                    'file' => TestClass::DUMP_FILE,
542
                    'line' => TestClass::DUMP_LINE,
543
                ),
544
                'caller' => array(
545
                    'function' => 'usort',
546
                ),
547
                'trace' => array_merge(
548
                    array(
549
                        $dumpframe + array(
550
                            'file' => TestClass::DUMP_FILE,
551
                            'line' => TestClass::DUMP_LINE,
552
                        ),
553
                    ),
554
                    $basetrace
555
                ),
556
            ),
557
        );
558
559
        $data['unmatching trace'] = $data['full trace'];
560
        $data['unmatching trace']['aliases'] = array();
561
        $data['unmatching trace']['expect']['callee'] = null;
562
        $data['unmatching trace']['expect']['caller'] = null;
563
        $data['unmatching trace']['expect']['trace'] = array_merge(
564
            $basetrace,
565
            array(
566
                $dumpframe + array(
567
                    'file' => TestClass::DUMP_FILE,
568
                    'line' => TestClass::DUMP_LINE,
569
                ),
570
            ),
571
            $basetrace
572
        );
573
574
        $data['trace with params'] = array(
575
            'aliases' => $aliases,
576
            'trace' => array_merge(
577
                    array(
578
                        $dumpframe + array(
579
                            'file' => TestClass::DUMP_FILE,
580
                            'line' => TestClass::DUMP_LINE,
581
                        ),
582
                    ),
583
                    $basetrace
584
                ),
585
            'param_count' => 3,
586
            'expect' => array(
587
                'params' => array(
588
                    array('name' => '$x', 'path' => '$x', 'expression' => false),
589
                    array('name' => '$y', 'path' => '$y', 'expression' => false),
590
                    array('name' => '$z', 'path' => '$z', 'expression' => false),
591
                ),
592
                'modifiers' => array(),
593
                'callee' => $dumpframe + array(
594
                    'file' => TestClass::DUMP_FILE,
595
                    'line' => TestClass::DUMP_LINE,
596
                ),
597
                'caller' => $basetrace[0],
598
                'trace' => array_merge(
599
                    array(
600
                        $dumpframe + array(
601
                            'file' => TestClass::DUMP_FILE,
602
                            'line' => TestClass::DUMP_LINE,
603
                        ),
604
                    ),
605
                    $basetrace
606
                ),
607
            ),
608
        );
609
610
        $data['trace with modifiers'] = array(
611
            'aliases' => $aliases,
612
            'trace' => array_merge(
613
                    array(
614
                        $dumpframe + array(
615
                            'file' => TestClass::DUMP_FILE,
616
                            'line' => TestClass::DUMP_LINE + 1,
617
                        ),
618
                    ),
619
                    $basetrace
620
                ),
621
            'param_count' => 0,
622
            'expect' => array(
623
                'params' => array(),
624
                'modifiers' => array('!', '+'),
625
                'callee' => $dumpframe + array(
626
                    'file' => TestClass::DUMP_FILE,
627
                    'line' => TestClass::DUMP_LINE + 1,
628
                ),
629
                'caller' => $basetrace[0],
630
                'trace' => array_merge(
631
                    array(
632
                        $dumpframe + array(
633
                            'file' => TestClass::DUMP_FILE,
634
                            'line' => TestClass::DUMP_LINE + 1,
635
                        ),
636
                    ),
637
                    $basetrace
638
                ),
639
            ),
640
        );
641
642
        $data['trace function with modifier'] = array(
643
            'aliases' => $aliases,
644
            'trace' => array_merge(
645
                    array(
646
                        array(
647
                            'function' => 'd',
648
                            'file' => TestClass::DUMP_FILE,
649
                            'line' => TestClass::DUMP_LINE + 2,
650
                        ),
651
                    ),
652
                    $basetrace
653
                ),
654
            'param_count' => 1,
655
            'expect' => array(
656
                'params' => array(
657
                    array(
658
                        'name' => '$x',
659
                        'path' => '$x',
660
                        'expression' => false,
661
                    ),
662
                ),
663
                'modifiers' => array('~'),
664
                'callee' => array(
665
                    'function' => 'd',
666
                    'file' => TestClass::DUMP_FILE,
667
                    'line' => TestClass::DUMP_LINE + 2,
668
                ),
669
                'caller' => $basetrace[0],
670
                'trace' => array_merge(
671
                    array(
672
                        array(
673
                            'function' => 'd',
674
                            'file' => TestClass::DUMP_FILE,
675
                            'line' => TestClass::DUMP_LINE + 2,
676
                        ),
677
                    ),
678
                    $basetrace
679
                ),
680
            ),
681
        );
682
683
        $data['trace function with multiple hits'] = array(
684
            'aliases' => $aliases,
685
            'trace' => array_merge(
686
                    array(
687
                        array(
688
                            'function' => 'd',
689
                            'file' => TestClass::DUMP_FILE,
690
                            'line' => TestClass::DUMP_LINE + 3,
691
                        ),
692
                    ),
693
                    $basetrace
694
                ),
695
            'param_count' => 1,
696
            'expect' => array(
697
                'params' => null,
698
                'modifiers' => array(),
699
                'callee' => array(
700
                    'function' => 'd',
701
                    'file' => TestClass::DUMP_FILE,
702
                    'line' => TestClass::DUMP_LINE + 3,
703
                ),
704
                'caller' => $basetrace[0],
705
                'trace' => array_merge(
706
                    array(
707
                        array(
708
                            'function' => 'd',
709
                            'file' => TestClass::DUMP_FILE,
710
                            'line' => TestClass::DUMP_LINE + 3,
711
                        ),
712
                    ),
713
                    $basetrace
714
                ),
715
            ),
716
        );
717
718
        // HHVM doesn't support multiple unpack parameters
719
        if (KINT_PHP56 && !defined('HHVM_VERSION')) {
720
            $data['trace with unpack'] = array(
721
                'aliases' => $aliases,
722
                'trace' => array_merge(
723
                        array(
724
                            $dumpframe + array(
725
                                'file' => Php56TestClass::DUMP_FILE,
726
                                'line' => Php56TestClass::DUMP_LINE,
727
                            ),
728
                        ),
729
                        $basetrace
730
                    ),
731
                'param_count' => 4,
732
                'expect' => array(
733
                    'params' => array(
734
                        array(
735
                            'name' => '$x',
736
                            'path' => '$x',
737
                            'expression' => false,
738
                        ),
739
                        array(
740
                            'name' => '$y',
741
                            'path' => '$y',
742
                            'expression' => false,
743
                        ),
744
                        array(
745
                            'name' => 'reset($z)',
746
                            'path' => 'reset($z)',
747
                            'expression' => false,
748
                        ),
749
                        array(
750
                            'name' => 'array_values($z)[1]',
751
                            'path' => 'array_values($z)[1]',
752
                            'expression' => false,
753
                        ),
754
                    ),
755
                    'modifiers' => array(),
756
                    'callee' => $dumpframe + array(
757
                        'file' => Php56TestClass::DUMP_FILE,
758
                        'line' => Php56TestClass::DUMP_LINE,
759
                    ),
760
                    'caller' => $basetrace[0],
761
                    'trace' => array_merge(
762
                        array(
763
                            $dumpframe + array(
764
                                'file' => Php56TestClass::DUMP_FILE,
765
                                'line' => Php56TestClass::DUMP_LINE,
766
                            ),
767
                        ),
768
                        $basetrace
769
                    ),
770
                ),
771
            );
772
773
            $data['trace with double unpack'] = array(
774
                'aliases' => $aliases,
775
                'trace' => array_merge(
776
                        array(
777
                            $dumpframe + array(
778
                                'file' => Php56TestClass::DUMP_FILE,
779
                                'line' => Php56TestClass::DUMP_LINE + 1,
780
                            ),
781
                        ),
782
                        $basetrace
783
                    ),
784
                'param_count' => 10,
785
                'expect' => array(
786
                    'params' => array(),
787
                    'modifiers' => array(),
788
                    'callee' => $dumpframe + array(
789
                        'file' => Php56TestClass::DUMP_FILE,
790
                        'line' => Php56TestClass::DUMP_LINE + 1,
791
                    ),
792
                    'caller' => $basetrace[0],
793
                    'trace' => array_merge(
794
                        array(
795
                            $dumpframe + array(
796
                                'file' => Php56TestClass::DUMP_FILE,
797
                                'line' => Php56TestClass::DUMP_LINE + 1,
798
                            ),
799
                        ),
800
                        $basetrace
801
                    ),
802
                ),
803
            );
804
        }
805
806
        return $data;
807
    }
808
809
    /**
810
     * @dataProvider getCallInfoProvider
811
     * @covers \Kint\Kint::getCallInfo
812
     */
813
    public function testGetCallInfo($aliases, $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...
814
    {
815
        $this->assertSame($expect, Kint::getCallInfo($aliases, $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...
816
    }
817
818
    public function pathProvider()
819
    {
820
        return array(
821
            'standard file' => array(
822
                'path' => __FILE__,
823
                'expect' => '<tests>/KintTest.php',
824
            ),
825
            'standard dir' => array(
826
                'path' => __DIR__,
827
                'expect' => '<tests>',
828
            ),
829
            'parent dir' => array(
830
                'path' => KINT_DIR,
831
                'expect' => '<kint>',
832
            ),
833
            'sub file' => array(
834
                'path' => KINT_DIR.'/src//test',
835
                'expect' => '<kint>/src/test',
836
            ),
837
            'common path' => array(
838
                'path' => dirname(KINT_DIR).'/test/',
839
                'expect' => '.../test',
840
            ),
841
            'root path' => array(
842
                'path' => '/',
843
                'expect' => '/',
844
            ),
845
            'no common path' => array(
846
                'path' => '/asdfasdf/test/',
847
                'expect' => '/asdfasdf/test',
848
            ),
849
        );
850
    }
851
852
    /**
853
     * @dataProvider pathProvider
854
     * @covers \Kint\Kint::shortenPath
855
     */
856
    public function testShortenPath($path, $expect)
857
    {
858
        Kint::$app_root_dirs = array(
859
            KINT_DIR => '<kint>',
860
            KINT_DIR.'/test' => '<test>',
861
            '' => '<Nothing!>',
862
            __DIR__ => '<tests>',
863
            KINT_DIR.'/tes' => '<tes>',
864
        );
865
866
        $this->assertEquals($expect, Kint::shortenPath($path));
867
    }
868
869
    /**
870
     * @covers \Kint\Kint::getIdeLink
871
     */
872
    public function testGetIdeLink()
873
    {
874
        Kint::$file_link_format = '<a href="%f:%l">%f:%l</a>';
875
876
        $file = uniqid('', true);
877
        $line = uniqid('', true);
878
879
        $this->assertEquals('<a href="'.$file.':'.$line.'">'.$file.':'.$line.'</a>', Kint::getIdeLink($file, $line));
880
    }
881
}
882