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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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 <br/>world', $result); |
|
|
|
|
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); |
|
|
|
|
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'; |
|
|
|
|
94
|
|
|
}, Dumper::RETURN); |
95
|
|
|
|
96
|
|
|
$this->assertStringContainsString('object', $result); |
|
|
|
|
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); |
|
|
|
|
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)); |
|
|
|
|
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); |
|
|
|
|
166
|
|
|
$this->assertStringContainsString('_value_', $result); |
167
|
|
|
$this->assertStringNotContainsString('inner', $result); |
|
|
|
|
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); |
|
|
|
|
182
|
|
|
$this->assertStringContainsString('_kk_', $result); |
183
|
|
|
|
184
|
|
|
$this->assertStringNotContainsString('internal', $result); |
|
|
|
|
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; |
|
|
|
|
205
|
|
|
|
206
|
|
|
protected $visible = '_kk_'; |
207
|
|
|
|
208
|
|
|
/** @internal */ |
209
|
|
|
protected $internal = '_ok_'; |
210
|
|
|
}, Dumper::RETURN); |
211
|
|
|
|
212
|
|
|
$this->assertStringContainsString('visible', $result); |
|
|
|
|
213
|
|
|
$this->assertStringContainsString('_kk_', $result); |
214
|
|
|
|
215
|
|
|
$this->assertStringNotContainsString('internal', $result); |
|
|
|
|
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; |
|
|
|
|
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); |
|
|
|
|
237
|
|
|
$this->assertStringContainsString('_kk_', $result); |
238
|
|
|
|
239
|
|
|
$this->assertStringNotContainsString('internal', $result); |
|
|
|
|
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; |
|
|
|
|
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); |
|
|
|
|
261
|
|
|
$this->assertStringContainsString('_kk_', $result); |
262
|
|
|
|
263
|
|
|
$this->assertStringNotContainsString('internal', $result); |
|
|
|
|
264
|
|
|
$this->assertStringNotContainsString('_ok_', $result); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|