PluginProbe
UpStream: a Project Management Plugin for WordPress / trunk
UpStream: a Project Management Plugin for WordPress vtrunk
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / vendor / symfony / debug / Tests / Exception / FlattenExceptionTest.php

FlattenExceptionTest.php in UpStream: a Project Management Plugin for WordPress trunk, at vendor/symfony/debug/Tests/Exception/FlattenExceptionTest.php

315 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Debug\Tests\Exception;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\FlattenException;
16 use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
17 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
18 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19 use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
20 use Symfony\Component\HttpKernel\Exception\GoneHttpException;
21 use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
22 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
23 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
24 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25 use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
26 use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
27 use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
28 use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
29 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
30 use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
31
32 class FlattenExceptionTest extends TestCase
33 {
34 public function testStatusCode()
35 {
36 $flattened = FlattenException::create(new \RuntimeException(), 403);
37 $this->assertEquals('403', $flattened->getStatusCode());
38
39 $flattened = FlattenException::create(new \RuntimeException());
40 $this->assertEquals('500', $flattened->getStatusCode());
41
42 $flattened = FlattenException::create(new NotFoundHttpException());
43 $this->assertEquals('404', $flattened->getStatusCode());
44
45 $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
46 $this->assertEquals('401', $flattened->getStatusCode());
47
48 $flattened = FlattenException::create(new BadRequestHttpException());
49 $this->assertEquals('400', $flattened->getStatusCode());
50
51 $flattened = FlattenException::create(new NotAcceptableHttpException());
52 $this->assertEquals('406', $flattened->getStatusCode());
53
54 $flattened = FlattenException::create(new ConflictHttpException());
55 $this->assertEquals('409', $flattened->getStatusCode());
56
57 $flattened = FlattenException::create(new MethodNotAllowedHttpException(['POST']));
58 $this->assertEquals('405', $flattened->getStatusCode());
59
60 $flattened = FlattenException::create(new AccessDeniedHttpException());
61 $this->assertEquals('403', $flattened->getStatusCode());
62
63 $flattened = FlattenException::create(new GoneHttpException());
64 $this->assertEquals('410', $flattened->getStatusCode());
65
66 $flattened = FlattenException::create(new LengthRequiredHttpException());
67 $this->assertEquals('411', $flattened->getStatusCode());
68
69 $flattened = FlattenException::create(new PreconditionFailedHttpException());
70 $this->assertEquals('412', $flattened->getStatusCode());
71
72 $flattened = FlattenException::create(new PreconditionRequiredHttpException());
73 $this->assertEquals('428', $flattened->getStatusCode());
74
75 $flattened = FlattenException::create(new ServiceUnavailableHttpException());
76 $this->assertEquals('503', $flattened->getStatusCode());
77
78 $flattened = FlattenException::create(new TooManyRequestsHttpException());
79 $this->assertEquals('429', $flattened->getStatusCode());
80
81 $flattened = FlattenException::create(new UnsupportedMediaTypeHttpException());
82 $this->assertEquals('415', $flattened->getStatusCode());
83
84 if (class_exists(SuspiciousOperationException::class)) {
85 $flattened = FlattenException::create(new SuspiciousOperationException());
86 $this->assertEquals('400', $flattened->getStatusCode());
87 }
88 }
89
90 public function testHeadersForHttpException()
91 {
92 $flattened = FlattenException::create(new MethodNotAllowedHttpException(['POST']));
93 $this->assertEquals(['Allow' => 'POST'], $flattened->getHeaders());
94
95 $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
96 $this->assertEquals(['WWW-Authenticate' => 'Basic realm="My Realm"'], $flattened->getHeaders());
97
98 $flattened = FlattenException::create(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
99 $this->assertEquals(['Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'], $flattened->getHeaders());
100
101 $flattened = FlattenException::create(new ServiceUnavailableHttpException(120));
102 $this->assertEquals(['Retry-After' => 120], $flattened->getHeaders());
103
104 $flattened = FlattenException::create(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
105 $this->assertEquals(['Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'], $flattened->getHeaders());
106
107 $flattened = FlattenException::create(new TooManyRequestsHttpException(120));
108 $this->assertEquals(['Retry-After' => 120], $flattened->getHeaders());
109 }
110
111 /**
112 * @dataProvider flattenDataProvider
113 */
114 public function testFlattenHttpException(\Exception $exception)
115 {
116 $flattened = FlattenException::create($exception);
117 $flattened2 = FlattenException::create($exception);
118
119 $flattened->setPrevious($flattened2);
120
121 $this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
122 $this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
123 $this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
124 }
125
126 /**
127 * @dataProvider flattenDataProvider
128 */
129 public function testPrevious(\Exception $exception)
130 {
131 $flattened = FlattenException::create($exception);
132 $flattened2 = FlattenException::create($exception);
133
134 $flattened->setPrevious($flattened2);
135
136 $this->assertSame($flattened2, $flattened->getPrevious());
137
138 $this->assertSame([$flattened2], $flattened->getAllPrevious());
139 }
140
141 /**
142 * @requires PHP 7.0
143 */
144 public function testPreviousError()
145 {
146 $exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
147
148 $flattened = FlattenException::create($exception)->getPrevious();
149
150 $this->assertEquals('Parse error: Oh noes!', $flattened->getMessage(), 'The message is copied from the original exception.');
151 $this->assertEquals(42, $flattened->getCode(), 'The code is copied from the original exception.');
152 $this->assertEquals('Symfony\Component\Debug\Exception\FatalThrowableError', $flattened->getClass(), 'The class is set to the class of the original exception');
153 }
154
155 /**
156 * @dataProvider flattenDataProvider
157 */
158 public function testLine(\Exception $exception)
159 {
160 $flattened = FlattenException::create($exception);
161 $this->assertSame($exception->getLine(), $flattened->getLine());
162 }
163
164 /**
165 * @dataProvider flattenDataProvider
166 */
167 public function testFile(\Exception $exception)
168 {
169 $flattened = FlattenException::create($exception);
170 $this->assertSame($exception->getFile(), $flattened->getFile());
171 }
172
173 /**
174 * @dataProvider flattenDataProvider
175 */
176 public function testToArray(\Exception $exception)
177 {
178 $flattened = FlattenException::create($exception);
179 $flattened->setTrace([], 'foo.php', 123);
180
181 $this->assertEquals([
182 [
183 'message' => 'test',
184 'class' => 'Exception',
185 'trace' => [[
186 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
187 'args' => [],
188 ]],
189 ],
190 ], $flattened->toArray());
191 }
192
193 public function flattenDataProvider()
194 {
195 return [
196 [new \Exception('test', 123)],
197 ];
198 }
199
200 public function testArguments()
201 {
202 if (\PHP_VERSION_ID >= 70400) {
203 $this->markTestSkipped('PHP 7.4 removes arguments from exception traces.');
204 }
205
206 $dh = opendir(__DIR__);
207 $fh = tmpfile();
208
209 $incomplete = unserialize('O:14:"BogusTestClass":0:{}');
210
211 $exception = $this->createException([
212 (object) ['foo' => 1],
213 new NotFoundHttpException(),
214 $incomplete,
215 $dh,
216 $fh,
217 function () {},
218 [1, 2],
219 ['foo' => 123],
220 null,
221 true,
222 false,
223 0,
224 0.0,
225 '0',
226 '',
227 \INF,
228 \NAN,
229 ]);
230
231 $flattened = FlattenException::create($exception);
232 $trace = $flattened->getTrace();
233 $args = $trace[1]['args'];
234 $array = $args[0][1];
235
236 closedir($dh);
237 fclose($fh);
238
239 $i = 0;
240 $this->assertSame(['object', 'stdClass'], $array[$i++]);
241 $this->assertSame(['object', 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'], $array[$i++]);
242 $this->assertSame(['incomplete-object', 'BogusTestClass'], $array[$i++]);
243 $this->assertSame(['resource', \defined('HHVM_VERSION') ? 'Directory' : 'stream'], $array[$i++]);
244 $this->assertSame(['resource', 'stream'], $array[$i++]);
245
246 $args = $array[$i++];
247 $this->assertSame($args[0], 'object');
248 $this->assertTrue('Closure' === $args[1] || is_subclass_of($args[1], '\Closure'), 'Expect object class name to be Closure or a subclass of Closure.');
249
250 $this->assertSame(['array', [['integer', 1], ['integer', 2]]], $array[$i++]);
251 $this->assertSame(['array', ['foo' => ['integer', 123]]], $array[$i++]);
252 $this->assertSame(['null', null], $array[$i++]);
253 $this->assertSame(['boolean', true], $array[$i++]);
254 $this->assertSame(['boolean', false], $array[$i++]);
255 $this->assertSame(['integer', 0], $array[$i++]);
256 $this->assertSame(['float', 0.0], $array[$i++]);
257 $this->assertSame(['string', '0'], $array[$i++]);
258 $this->assertSame(['string', ''], $array[$i++]);
259 $this->assertSame(['float', \INF], $array[$i++]);
260
261 // assertEquals() does not like NAN values.
262 $this->assertEquals('float', $array[$i][0]);
263 $this->assertNan($array[$i][1]);
264 }
265
266 public function testRecursionInArguments()
267 {
268 if (\PHP_VERSION_ID >= 70400) {
269 $this->markTestSkipped('PHP 7.4 removes arguments from exception traces.');
270 }
271
272 $a = null;
273 $a = ['foo', [2, &$a]];
274 $exception = $this->createException($a);
275
276 $flattened = FlattenException::create($exception);
277 $trace = $flattened->getTrace();
278 $this->assertStringContainsString('*DEEP NESTED ARRAY*', serialize($trace));
279 }
280
281 public function testTooBigArray()
282 {
283 if (\PHP_VERSION_ID >= 70400) {
284 $this->markTestSkipped('PHP 7.4 removes arguments from exception traces.');
285 }
286
287 $a = [];
288 for ($i = 0; $i < 20; ++$i) {
289 for ($j = 0; $j < 50; ++$j) {
290 for ($k = 0; $k < 10; ++$k) {
291 $a[$i][$j][$k] = 'value';
292 }
293 }
294 }
295 $a[20] = 'value';
296 $a[21] = 'value1';
297 $exception = $this->createException($a);
298
299 $flattened = FlattenException::create($exception);
300 $trace = $flattened->getTrace();
301
302 $this->assertSame($trace[1]['args'][0], ['array', ['array', '*SKIPPED over 10000 entries*']]);
303
304 $serializeTrace = serialize($trace);
305
306 $this->assertStringContainsString('*SKIPPED over 10000 entries*', $serializeTrace);
307 $this->assertStringNotContainsString('*value1*', $serializeTrace);
308 }
309
310 private function createException($foo)
311 {
312 return new \Exception();
313 }
314 }
315