FlattenExceptionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Symfony\Component\Debug\Exception\FlattenException;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  14. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  15. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  16. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  17. use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
  18. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  19. use Symfony\Component\HttpKernel\Exception\GoneHttpException;
  20. use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
  21. use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
  22. use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
  23. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  24. use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
  25. use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
  26. class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
  27. {
  28. public function testStatusCode()
  29. {
  30. $flattened = FlattenException::create(new \RuntimeException(), 403);
  31. $this->assertEquals('403', $flattened->getStatusCode());
  32. $flattened = FlattenException::create(new \RuntimeException());
  33. $this->assertEquals('500', $flattened->getStatusCode());
  34. $flattened = FlattenException::create(new NotFoundHttpException());
  35. $this->assertEquals('404', $flattened->getStatusCode());
  36. $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
  37. $this->assertEquals('401', $flattened->getStatusCode());
  38. $flattened = FlattenException::create(new BadRequestHttpException());
  39. $this->assertEquals('400', $flattened->getStatusCode());
  40. $flattened = FlattenException::create(new NotAcceptableHttpException());
  41. $this->assertEquals('406', $flattened->getStatusCode());
  42. $flattened = FlattenException::create(new ConflictHttpException());
  43. $this->assertEquals('409', $flattened->getStatusCode());
  44. $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
  45. $this->assertEquals('405', $flattened->getStatusCode());
  46. $flattened = FlattenException::create(new AccessDeniedHttpException());
  47. $this->assertEquals('403', $flattened->getStatusCode());
  48. $flattened = FlattenException::create(new GoneHttpException());
  49. $this->assertEquals('410', $flattened->getStatusCode());
  50. $flattened = FlattenException::create(new LengthRequiredHttpException());
  51. $this->assertEquals('411', $flattened->getStatusCode());
  52. $flattened = FlattenException::create(new PreconditionFailedHttpException());
  53. $this->assertEquals('412', $flattened->getStatusCode());
  54. $flattened = FlattenException::create(new PreconditionRequiredHttpException());
  55. $this->assertEquals('428', $flattened->getStatusCode());
  56. $flattened = FlattenException::create(new ServiceUnavailableHttpException());
  57. $this->assertEquals('503', $flattened->getStatusCode());
  58. $flattened = FlattenException::create(new TooManyRequestsHttpException());
  59. $this->assertEquals('429', $flattened->getStatusCode());
  60. $flattened = FlattenException::create(new UnsupportedMediaTypeHttpException());
  61. $this->assertEquals('415', $flattened->getStatusCode());
  62. }
  63. public function testHeadersForHttpException()
  64. {
  65. $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
  66. $this->assertEquals(array('Allow' => 'POST'), $flattened->getHeaders());
  67. $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
  68. $this->assertEquals(array('WWW-Authenticate' => 'Basic realm="My Realm"'), $flattened->getHeaders());
  69. $flattened = FlattenException::create(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
  70. $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
  71. $flattened = FlattenException::create(new ServiceUnavailableHttpException(120));
  72. $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
  73. $flattened = FlattenException::create(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
  74. $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
  75. $flattened = FlattenException::create(new TooManyRequestsHttpException(120));
  76. $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
  77. }
  78. /**
  79. * @dataProvider flattenDataProvider
  80. */
  81. public function testFlattenHttpException(\Exception $exception, $statusCode)
  82. {
  83. $flattened = FlattenException::create($exception);
  84. $flattened2 = FlattenException::create($exception);
  85. $flattened->setPrevious($flattened2);
  86. $this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
  87. $this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
  88. $this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
  89. }
  90. /**
  91. * @dataProvider flattenDataProvider
  92. */
  93. public function testPrevious(\Exception $exception, $statusCode)
  94. {
  95. $flattened = FlattenException::create($exception);
  96. $flattened2 = FlattenException::create($exception);
  97. $flattened->setPrevious($flattened2);
  98. $this->assertSame($flattened2, $flattened->getPrevious());
  99. $this->assertSame(array($flattened2), $flattened->getAllPrevious());
  100. }
  101. /**
  102. * @requires PHP 7.0
  103. */
  104. public function testPreviousError()
  105. {
  106. $exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
  107. $flattened = FlattenException::create($exception)->getPrevious();
  108. $this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
  109. $this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
  110. $this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
  111. }
  112. /**
  113. * @dataProvider flattenDataProvider
  114. */
  115. public function testLine(\Exception $exception)
  116. {
  117. $flattened = FlattenException::create($exception);
  118. $this->assertSame($exception->getLine(), $flattened->getLine());
  119. }
  120. /**
  121. * @dataProvider flattenDataProvider
  122. */
  123. public function testFile(\Exception $exception)
  124. {
  125. $flattened = FlattenException::create($exception);
  126. $this->assertSame($exception->getFile(), $flattened->getFile());
  127. }
  128. /**
  129. * @dataProvider flattenDataProvider
  130. */
  131. public function testToArray(\Exception $exception, $statusCode)
  132. {
  133. $flattened = FlattenException::create($exception);
  134. $flattened->setTrace(array(), 'foo.php', 123);
  135. $this->assertEquals(array(
  136. array(
  137. 'message' => 'test',
  138. 'class' => 'Exception',
  139. 'trace' => array(array(
  140. 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
  141. 'args' => array(),
  142. )),
  143. ),
  144. ), $flattened->toArray());
  145. }
  146. public function flattenDataProvider()
  147. {
  148. return array(
  149. array(new \Exception('test', 123), 500),
  150. );
  151. }
  152. public function testRecursionInArguments()
  153. {
  154. $a = array('foo', array(2, &$a));
  155. $exception = $this->createException($a);
  156. $flattened = FlattenException::create($exception);
  157. $trace = $flattened->getTrace();
  158. $this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
  159. }
  160. public function testTooBigArray()
  161. {
  162. $a = array();
  163. for ($i = 0; $i < 20; ++$i) {
  164. for ($j = 0; $j < 50; ++$j) {
  165. for ($k = 0; $k < 10; ++$k) {
  166. $a[$i][$j][$k] = 'value';
  167. }
  168. }
  169. }
  170. $a[20] = 'value';
  171. $a[21] = 'value1';
  172. $exception = $this->createException($a);
  173. $flattened = FlattenException::create($exception);
  174. $trace = $flattened->getTrace();
  175. $serializeTrace = serialize($trace);
  176. $this->assertContains('*SKIPPED over 10000 entries*', $serializeTrace);
  177. $this->assertNotContains('*value1*', $serializeTrace);
  178. }
  179. private function createException($foo)
  180. {
  181. return new \Exception();
  182. }
  183. public function testSetTraceIncompleteClass()
  184. {
  185. $flattened = FlattenException::create(new \Exception('test', 123));
  186. $flattened->setTrace(
  187. array(
  188. array(
  189. 'file' => __FILE__,
  190. 'line' => 123,
  191. 'function' => 'test',
  192. 'args' => array(
  193. unserialize('O:14:"BogusTestClass":0:{}'),
  194. ),
  195. ),
  196. ),
  197. 'foo.php', 123
  198. );
  199. $this->assertEquals(array(
  200. array(
  201. 'message' => 'test',
  202. 'class' => 'Exception',
  203. 'trace' => array(
  204. array(
  205. 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '',
  206. 'file' => 'foo.php', 'line' => 123,
  207. 'args' => array(),
  208. ),
  209. array(
  210. 'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => 'test',
  211. 'file' => __FILE__, 'line' => 123,
  212. 'args' => array(
  213. array(
  214. 'incomplete-object', 'BogusTestClass',
  215. ),
  216. ),
  217. ),
  218. ),
  219. ),
  220. ), $flattened->toArray());
  221. }
  222. }