FlattenExceptionTest.php 14 KB

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