Passed
Push — master ( 67aa31...d47bdf )
by Kirill
03:20
created

DumperTest::testClosure()

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Debug;
13
14
use PHPUnit\Framework\TestCase;
15
use Psr\Log\LoggerInterface;
16
use Spiral\Debug\Dumper;
17
use Spiral\Debug\Exception\DumperException;
18
use Spiral\Debug\Renderer\ConsoleRenderer;
19
use Spiral\Debug\Renderer\HtmlRenderer;
20
use Spiral\Debug\Renderer\PlainRenderer;
21
22
class DumperTest extends TestCase
23
{
24
    public function testOutput(): void
25
    {
26
        $d = $this->makeDumper();
27
28
        ob_start();
29
        $d->dump(1);
30
        $result = ob_get_clean();
31
32
        $this->assertSame($d->dump(1, Dumper::RETURN), $result);
33
    }
34
35
    private function makeDumper(LoggerInterface $logger = null)
36
    {
37
        $d = new Dumper($logger);
38
        $d->setRenderer(Dumper::OUTPUT, new PlainRenderer());
39
        $d->setRenderer(Dumper::RETURN, new PlainRenderer());
40
        $d->setRenderer(Dumper::OUTPUT_CLI_COLORS, new PlainRenderer());
41
42
        return $d;
43
    }
44
45
    public function testScalar(): void
46
    {
47
        $d = $this->makeDumper();
48
        $result = $d->dump(123, Dumper::RETURN);
49
50
        $this->assertStringContainsString('123', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $this->assertStringContainsString('123', /** @scrutinizer ignore-type */ $result);
Loading history...
51
    }
52
53
    public function testString(): void
54
    {
55
        $d = $this->makeDumper();
56
        $result = $d->dump('hello world', Dumper::RETURN);
57
58
        $this->assertStringContainsString('hello world', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->assertStringContainsString('hello world', /** @scrutinizer ignore-type */ $result);
Loading history...
59
    }
60
61
    public function testResource(): void
62
    {
63
        $d = $this->makeDumper();
64
        $result = $d->dump(STDOUT, Dumper::RETURN);
65
66
        $this->assertStringContainsString('resource', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $this->assertStringContainsString('resource', /** @scrutinizer ignore-type */ $result);
Loading history...
67
    }
68
69
    public function testHTML(): void
70
    {
71
        $d = $this->makeDumper();
72
        $result = $d->dump('hello <br/>world', Dumper::RETURN);
73
74
        $this->assertStringContainsString('hello &lt;br/&gt;world', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $this->assertStringContainsString('hello &lt;br/&gt;world', /** @scrutinizer ignore-type */ $result);
Loading history...
75
    }
76
77
    public function testArray(): void
78
    {
79
        $d = $this->makeDumper();
80
        $result = $d->dump(['G', 'B', 'N'], Dumper::RETURN);
81
82
        $this->assertStringContainsString('array', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $this->assertStringContainsString('array', /** @scrutinizer ignore-type */ $result);
Loading history...
83
        $this->assertStringContainsString('G', $result);
84
        $this->assertStringContainsString('B', $result);
85
        $this->assertStringContainsString('N', $result);
86
    }
87
88
    public function testAnonClass(): void
89
    {
90
        $d = $this->makeDumper();
91
92
        $result = $d->dump(new class() {
93
            private $name = 'Test';
0 ignored issues
show
introduced by
The private property $name is not used, and could be removed.
Loading history...
94
        }, Dumper::RETURN);
95
96
        $this->assertStringContainsString('object', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $this->assertStringContainsString('object', /** @scrutinizer ignore-type */ $result);
Loading history...
97
        $this->assertStringContainsString('private', $result);
98
        $this->assertStringContainsString('name', $result);
99
        $this->assertStringContainsString('string(4)', $result);
100
        $this->assertStringContainsString('test', $result);
101
    }
102
103
    public function testClosure(): void
104
    {
105
        $d = $this->makeDumper();
106
107
        $result = $d->dump(static function (): void {
108
            echo 'hello world';
109
        }, Dumper::RETURN);
110
111
        $this->assertStringContainsString('Closure', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        $this->assertStringContainsString('Closure', /** @scrutinizer ignore-type */ $result);
Loading history...
112
        $this->assertStringContainsString('DumperTest.php', $result);
113
    }
114
115
    public function testToLog(): void
116
    {
117
        $mock = $this->createMock(LoggerInterface::class);
118
        $d = $this->makeDumper($mock);
119
120
        $mock->method('debug')->with($d->dump('abc', Dumper::RETURN));
121
        $this->assertNull($d->dump('abc', Dumper::LOGGER));
122
    }
123
124
    public function testErrorLog(): void
125
    {
126
        $d = $this->makeDumper();
127
128
        ini_set('error_log', 'errors.log');
129
        $this->assertNull($d->dump('abc', Dumper::ERROR_LOG));
130
131
        $this->assertStringContainsString('abc', file_get_contents('errors.log'));
132
        unlink('errors.log');
133
    }
134
135
    public function testToLogException(): void
136
    {
137
        $this->expectException(DumperException::class);
138
139
        $d = $this->makeDumper();
140
        $this->assertNull($d->dump('abc', Dumper::LOGGER));
141
    }
142
143
    public function testTargetException(): void
144
    {
145
        $this->expectException(DumperException::class);
146
147
        $d = $this->makeDumper();
148
        $this->assertNull($d->dump('abc', 9));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $d->dump('abc', 9) targeting Spiral\Debug\Dumper::dump() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
149
    }
150
151
    public function testDebugInfo(): void
152
    {
153
        $d = $this->makeDumper();
154
        $result = $d->dump(new class() {
155
            protected $inner = '_kk_';
156
157
            public function __debugInfo()
158
            {
159
                return [
160
                    '_magic_' => '_value_',
161
                ];
162
            }
163
        }, Dumper::RETURN);
164
165
        $this->assertStringContainsString('_magic_', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        $this->assertStringContainsString('_magic_', /** @scrutinizer ignore-type */ $result);
Loading history...
166
        $this->assertStringContainsString('_value_', $result);
167
        $this->assertStringNotContainsString('inner', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...ringNotContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
        $this->assertStringNotContainsString('inner', /** @scrutinizer ignore-type */ $result);
Loading history...
168
        $this->assertStringNotContainsString('_kk_', $result);
169
    }
170
171
    public function testinternal(): void
172
    {
173
        $d = $this->makeDumper();
174
        $result = $d->dump(new class() {
175
            protected $visible = '_kk_';
176
177
            /** @internal */
178
            protected $internal = '_ok_';
179
        }, Dumper::RETURN);
180
181
        $this->assertStringContainsString('visible', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
        $this->assertStringContainsString('visible', /** @scrutinizer ignore-type */ $result);
Loading history...
182
        $this->assertStringContainsString('_kk_', $result);
183
184
        $this->assertStringNotContainsString('internal', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...ringNotContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
        $this->assertStringNotContainsString('internal', /** @scrutinizer ignore-type */ $result);
Loading history...
185
        $this->assertStringNotContainsString('_ok_', $result);
186
    }
187
188
    public function testSetRenderer(): void
189
    {
190
        $this->expectException(DumperException::class);
191
192
        $d = $this->makeDumper();
193
        $d->setRenderer(8, new HtmlRenderer());
194
    }
195
196
    public function testHtmlRenderer(): void
197
    {
198
        $d = $this->makeDumper();
199
        $d->setRenderer(Dumper::RETURN, new HtmlRenderer());
200
201
        $result = $d->dump(new class() {
202
            protected static $static = 'yes';
203
204
            private $value = 123;
0 ignored issues
show
introduced by
The private property $value is not used, and could be removed.
Loading history...
205
206
            protected $visible = '_kk_';
207
208
            /** @internal */
209
            protected $internal = '_ok_';
210
        }, Dumper::RETURN);
211
212
        $this->assertStringContainsString('visible', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
        $this->assertStringContainsString('visible', /** @scrutinizer ignore-type */ $result);
Loading history...
213
        $this->assertStringContainsString('_kk_', $result);
214
215
        $this->assertStringNotContainsString('internal', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...ringNotContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

215
        $this->assertStringNotContainsString('internal', /** @scrutinizer ignore-type */ $result);
Loading history...
216
        $this->assertStringNotContainsString('_ok_', $result);
217
    }
218
219
    public function testInvertedRenderer(): void
220
    {
221
        $d = $this->makeDumper();
222
        $d->setRenderer(Dumper::RETURN, new HtmlRenderer(HtmlRenderer::INVERTED));
223
        $d->setMaxLevel(5);
224
225
        $result = $d->dump(new class() {
226
            private $value = 123;
0 ignored issues
show
introduced by
The private property $value is not used, and could be removed.
Loading history...
227
228
            protected $visible = '_kk_';
229
230
            public $data = ['name' => 'value'];
231
232
            /** @internal */
233
            protected $internal = '_ok_';
234
        }, Dumper::RETURN);
235
236
        $this->assertStringContainsString('visible', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
        $this->assertStringContainsString('visible', /** @scrutinizer ignore-type */ $result);
Loading history...
237
        $this->assertStringContainsString('_kk_', $result);
238
239
        $this->assertStringNotContainsString('internal', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...ringNotContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

239
        $this->assertStringNotContainsString('internal', /** @scrutinizer ignore-type */ $result);
Loading history...
240
        $this->assertStringNotContainsString('_ok_', $result);
241
    }
242
243
    public function testConsoleRenderer(): void
244
    {
245
        $d = $this->makeDumper();
246
        $d->setRenderer(Dumper::RETURN, new ConsoleRenderer());
247
        $d->setMaxLevel(5);
248
249
        $result = $d->dump(new class() {
250
            private $value = 123;
0 ignored issues
show
introduced by
The private property $value is not used, and could be removed.
Loading history...
251
252
            protected $visible = '_kk_';
253
254
            public $data = ['name' => 'value'];
255
256
            /** @internal */
257
            protected $internal = '_ok_';
258
        }, Dumper::RETURN);
259
260
        $this->assertStringContainsString('visible', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

260
        $this->assertStringContainsString('visible', /** @scrutinizer ignore-type */ $result);
Loading history...
261
        $this->assertStringContainsString('_kk_', $result);
262
263
        $this->assertStringNotContainsString('internal', $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...ringNotContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

263
        $this->assertStringNotContainsString('internal', /** @scrutinizer ignore-type */ $result);
Loading history...
264
        $this->assertStringNotContainsString('_ok_', $result);
265
    }
266
}
267