1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Yii\Debug\FlattenException; |
9
|
|
|
|
10
|
|
|
final class FlattenExceptionTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testMessage(): void |
13
|
|
|
{ |
14
|
|
|
$flattened = new FlattenException(new \Exception('test')); |
15
|
|
|
$this->assertEquals('test', $flattened->getMessage()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testCode(): void |
19
|
|
|
{ |
20
|
|
|
$flattened = new FlattenException(new \Exception('test', 100)); |
21
|
|
|
$this->assertEquals(100, $flattened->getCode()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testFile(): void |
25
|
|
|
{ |
26
|
|
|
$flattened = new FlattenException(new \Exception('test', 100)); |
27
|
|
|
$this->assertEquals(__FILE__, $flattened->getFile()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testLine(): void |
31
|
|
|
{ |
32
|
|
|
$flattened = new FlattenException(new \Exception('test', 100)); |
33
|
|
|
$this->assertEquals(__LINE__ - 1, $flattened->getLine()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testTrace(): void |
37
|
|
|
{ |
38
|
|
|
$i = (new \Exception('test')); |
39
|
|
|
$flattened = new FlattenException($i); |
40
|
|
|
|
41
|
|
|
$trace = $flattened->getTrace(); |
42
|
|
|
$this->assertEquals(__NAMESPACE__, $trace[0]['namespace']); |
43
|
|
|
$this->assertEquals(self::class, $trace[0]['class']); |
44
|
|
|
$this->assertEquals('FlattenExceptionTest', $trace[0]['short_class']); |
45
|
|
|
$this->assertEquals(__FUNCTION__, $trace[0]['function']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testPrevious(): void |
49
|
|
|
{ |
50
|
|
|
$exception2 = new \Exception(); |
51
|
|
|
$exception = new \Exception('test', 0, $exception2); |
52
|
|
|
|
53
|
|
|
$flattened = new FlattenException($exception); |
54
|
|
|
$flattened2 = new FlattenException($exception2); |
55
|
|
|
|
56
|
|
|
$this->assertSame($flattened2->getTrace(), $flattened->getPrevious()->getTrace()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testTraceAsString(): void |
60
|
|
|
{ |
61
|
|
|
$exception = $this->createException('test'); |
62
|
|
|
$this->assertEquals($exception->getTraceAsString(), (new FlattenException($exception))->getTraceAsString()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testToString(): void |
66
|
|
|
{ |
67
|
|
|
$exception = new \Exception(); |
68
|
|
|
$this->assertEquals($exception->__toString(), (new FlattenException($exception))->__toString(), 'empty'); |
69
|
|
|
$exception = new \Exception('test'); |
70
|
|
|
$this->assertEquals($exception->__toString(), (new FlattenException($exception))->__toString()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testClass(): void |
74
|
|
|
{ |
75
|
|
|
$this->assertEquals('Exception', (new FlattenException(new \Exception()))->getClass()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testArguments(): void |
79
|
|
|
{ |
80
|
|
|
$this->markTestSkipped('Should be fixed'); |
81
|
|
|
|
82
|
|
|
$dh = opendir(__DIR__); |
83
|
|
|
$fh = tmpfile(); |
84
|
|
|
|
85
|
|
|
$incomplete = unserialize('O:14:"BogusTestClass":0:{}'); |
86
|
|
|
|
87
|
|
|
$exception = $this->createException([ |
88
|
|
|
(object)['foo' => 1], |
89
|
|
|
new \RuntimeException('test'), |
90
|
|
|
$incomplete, |
91
|
|
|
$dh, |
92
|
|
|
$fh, |
93
|
|
|
function () { |
94
|
|
|
}, |
95
|
|
|
[1, 2], |
96
|
|
|
['foo' => 123], |
97
|
|
|
null, |
98
|
|
|
true, |
99
|
|
|
false, |
100
|
|
|
0, |
101
|
|
|
0.0, |
102
|
|
|
'0', |
103
|
|
|
'', |
104
|
|
|
INF, |
105
|
|
|
NAN, |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$flattened = new FlattenException($exception); |
109
|
|
|
$trace = $flattened->getTrace(); |
110
|
|
|
$args = $trace[0]['args']; |
111
|
|
|
$array = $args[0][1]; |
112
|
|
|
|
113
|
|
|
closedir($dh); |
114
|
|
|
fclose($fh); |
115
|
|
|
|
116
|
|
|
$i = 0; |
117
|
|
|
$this->assertSame(['object', 'stdClass'], $array[$i++]); |
118
|
|
|
$this->assertSame(['object', \RuntimeException::class], $array[$i++]); |
119
|
|
|
$this->assertSame(['incomplete-object', 'BogusTestClass'], $array[$i++]); |
120
|
|
|
$this->assertSame(['resource', 'stream'], $array[$i++]); |
121
|
|
|
$this->assertSame(['resource', 'stream'], $array[$i++]); |
122
|
|
|
|
123
|
|
|
$args = $array[$i++]; |
124
|
|
|
$this->assertSame($args[0], 'object'); |
125
|
|
|
$this->assertTrue(\Closure::class === $args[1] || is_subclass_of($args[1], '\\' . \Closure::class), 'Expect object class name to be Closure or a subclass of Closure.'); |
126
|
|
|
|
127
|
|
|
$this->assertSame(['array', [['integer', 1], ['integer', 2]]], $array[$i++]); |
128
|
|
|
$this->assertSame(['array', ['foo' => ['integer', 123]]], $array[$i++]); |
129
|
|
|
$this->assertSame(['null', null], $array[$i++]); |
130
|
|
|
$this->assertSame(['boolean', true], $array[$i++]); |
131
|
|
|
$this->assertSame(['boolean', false], $array[$i++]); |
132
|
|
|
$this->assertSame(['integer', 0], $array[$i++]); |
133
|
|
|
$this->assertSame(['float', 0.0], $array[$i++]); |
134
|
|
|
$this->assertSame(['string', '0'], $array[$i++]); |
135
|
|
|
$this->assertSame(['string', ''], $array[$i++]); |
136
|
|
|
$this->assertSame(['float', INF], $array[$i++]); |
137
|
|
|
|
138
|
|
|
// assertEquals() does not like NAN values. |
139
|
|
|
$this->assertEquals('float', $array[$i][0]); |
140
|
|
|
$this->assertNan($array[$i++][1]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testClosureSerialize(): void |
144
|
|
|
{ |
145
|
|
|
$exception = $this->createException(fn () => 1 + 1); |
146
|
|
|
|
147
|
|
|
$flattened = new FlattenException($exception); |
148
|
|
|
$this->assertStringContainsString(\Closure::class, serialize($flattened)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testRecursionInArguments(): void |
152
|
|
|
{ |
153
|
|
|
$this->markTestSkipped('Should be fixed'); |
154
|
|
|
|
155
|
|
|
$a = ['foo']; |
156
|
|
|
$a[] = [2, &$a]; |
157
|
|
|
$exception = $this->createException($a); |
158
|
|
|
|
159
|
|
|
$flattened = new FlattenException($exception); |
160
|
|
|
$trace = $flattened->getTrace(); |
161
|
|
|
$this->assertStringContainsString('*DEEP NESTED ARRAY*', serialize($trace)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testTooBigArray(): void |
165
|
|
|
{ |
166
|
|
|
$this->markTestSkipped('Should be fixed'); |
167
|
|
|
|
168
|
|
|
$a = []; |
169
|
|
|
for ($i = 0; $i < 20; ++$i) { |
170
|
|
|
for ($j = 0; $j < 50; ++$j) { |
171
|
|
|
for ($k = 0; $k < 10; ++$k) { |
172
|
|
|
$a[$i][$j][$k] = 'value'; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
$a[20] = 'value'; |
177
|
|
|
$a[21] = 'value1'; |
178
|
|
|
$exception = $this->createException($a); |
179
|
|
|
|
180
|
|
|
$flattened = new FlattenException($exception); |
181
|
|
|
$trace = $flattened->getTrace(); |
182
|
|
|
|
183
|
|
|
$this->assertSame($trace[0]['args'][0], ['array', ['array', '*SKIPPED over 10000 entries*']]); |
184
|
|
|
|
185
|
|
|
$serializeTrace = serialize($trace); |
186
|
|
|
|
187
|
|
|
$this->assertStringContainsString('*SKIPPED over 10000 entries*', $serializeTrace); |
188
|
|
|
$this->assertStringNotContainsString('*value1*', $serializeTrace); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function createException($foo): \Exception |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
return new \Exception(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.