4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug\Tests\Exception;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\FlattenException;
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
18 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
19 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
21 use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
22 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
23 use Symfony\Component\HttpKernel\Exception\GoneHttpException;
24 use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
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\UnsupportedMediaTypeHttpException;
31 class FlattenExceptionTest extends TestCase
33 public function testStatusCode()
35 $flattened = FlattenException::create(new \RuntimeException(), 403);
36 $this->assertEquals('403', $flattened->getStatusCode());
38 $flattened = FlattenException::create(new \RuntimeException());
39 $this->assertEquals('500', $flattened->getStatusCode());
41 $flattened = FlattenException::create(new NotFoundHttpException());
42 $this->assertEquals('404', $flattened->getStatusCode());
44 $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
45 $this->assertEquals('401', $flattened->getStatusCode());
47 $flattened = FlattenException::create(new BadRequestHttpException());
48 $this->assertEquals('400', $flattened->getStatusCode());
50 $flattened = FlattenException::create(new NotAcceptableHttpException());
51 $this->assertEquals('406', $flattened->getStatusCode());
53 $flattened = FlattenException::create(new ConflictHttpException());
54 $this->assertEquals('409', $flattened->getStatusCode());
56 $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
57 $this->assertEquals('405', $flattened->getStatusCode());
59 $flattened = FlattenException::create(new AccessDeniedHttpException());
60 $this->assertEquals('403', $flattened->getStatusCode());
62 $flattened = FlattenException::create(new GoneHttpException());
63 $this->assertEquals('410', $flattened->getStatusCode());
65 $flattened = FlattenException::create(new LengthRequiredHttpException());
66 $this->assertEquals('411', $flattened->getStatusCode());
68 $flattened = FlattenException::create(new PreconditionFailedHttpException());
69 $this->assertEquals('412', $flattened->getStatusCode());
71 $flattened = FlattenException::create(new PreconditionRequiredHttpException());
72 $this->assertEquals('428', $flattened->getStatusCode());
74 $flattened = FlattenException::create(new ServiceUnavailableHttpException());
75 $this->assertEquals('503', $flattened->getStatusCode());
77 $flattened = FlattenException::create(new TooManyRequestsHttpException());
78 $this->assertEquals('429', $flattened->getStatusCode());
80 $flattened = FlattenException::create(new UnsupportedMediaTypeHttpException());
81 $this->assertEquals('415', $flattened->getStatusCode());
84 public function testHeadersForHttpException()
86 $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
87 $this->assertEquals(array('Allow' => 'POST'), $flattened->getHeaders());
89 $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
90 $this->assertEquals(array('WWW-Authenticate' => 'Basic realm="My Realm"'), $flattened->getHeaders());
92 $flattened = FlattenException::create(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
93 $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
95 $flattened = FlattenException::create(new ServiceUnavailableHttpException(120));
96 $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
98 $flattened = FlattenException::create(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
99 $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
101 $flattened = FlattenException::create(new TooManyRequestsHttpException(120));
102 $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
106 * @dataProvider flattenDataProvider
108 public function testFlattenHttpException(\Exception $exception, $statusCode)
110 $flattened = FlattenException::create($exception);
111 $flattened2 = FlattenException::create($exception);
113 $flattened->setPrevious($flattened2);
115 $this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
116 $this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
117 $this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
121 * @dataProvider flattenDataProvider
123 public function testPrevious(\Exception $exception, $statusCode)
125 $flattened = FlattenException::create($exception);
126 $flattened2 = FlattenException::create($exception);
128 $flattened->setPrevious($flattened2);
130 $this->assertSame($flattened2, $flattened->getPrevious());
132 $this->assertSame(array($flattened2), $flattened->getAllPrevious());
138 public function testPreviousError()
140 $exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
142 $flattened = FlattenException::create($exception)->getPrevious();
144 $this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
145 $this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
146 $this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
150 * @dataProvider flattenDataProvider
152 public function testLine(\Exception $exception)
154 $flattened = FlattenException::create($exception);
155 $this->assertSame($exception->getLine(), $flattened->getLine());
159 * @dataProvider flattenDataProvider
161 public function testFile(\Exception $exception)
163 $flattened = FlattenException::create($exception);
164 $this->assertSame($exception->getFile(), $flattened->getFile());
168 * @dataProvider flattenDataProvider
170 public function testToArray(\Exception $exception, $statusCode)
172 $flattened = FlattenException::create($exception);
173 $flattened->setTrace(array(), 'foo.php', 123);
175 $this->assertEquals(array(
178 'class' => 'Exception',
179 'trace' => array(array(
180 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
184 ), $flattened->toArray());
187 public function flattenDataProvider()
190 array(new \Exception('test', 123), 500),
194 public function testRecursionInArguments()
196 $a = array('foo', array(2, &$a));
197 $exception = $this->createException($a);
199 $flattened = FlattenException::create($exception);
200 $trace = $flattened->getTrace();
201 $this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
204 public function testTooBigArray()
207 for ($i = 0; $i < 20; ++$i) {
208 for ($j = 0; $j < 50; ++$j) {
209 for ($k = 0; $k < 10; ++$k) {
210 $a[$i][$j][$k] = 'value';
216 $exception = $this->createException($a);
218 $flattened = FlattenException::create($exception);
219 $trace = $flattened->getTrace();
220 $serializeTrace = serialize($trace);
222 $this->assertContains('*SKIPPED over 10000 entries*', $serializeTrace);
223 $this->assertNotContains('*value1*', $serializeTrace);
226 private function createException($foo)
228 return new \Exception();
231 public function testSetTraceIncompleteClass()
233 $flattened = FlattenException::create(new \Exception('test', 123));
234 $flattened->setTrace(
239 'function' => 'test',
241 unserialize('O:14:"BogusTestClass":0:{}'),
248 $this->assertEquals(array(
251 'class' => 'Exception',
254 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '',
255 'file' => 'foo.php', 'line' => 123,
259 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => 'test',
260 'file' => __FILE__, 'line' => 123,
263 'incomplete-object', 'BogusTestClass',
269 ), $flattened->toArray());