FlattenExceptionTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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->assertEquals(get_class($exception), $flattened->getClass(), '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. * @dataProvider flattenDataProvider
  103. */
  104. public function testLine(\Exception $exception)
  105. {
  106. $flattened = FlattenException::create($exception);
  107. $this->assertSame($exception->getLine(), $flattened->getLine());
  108. }
  109. /**
  110. * @dataProvider flattenDataProvider
  111. */
  112. public function testFile(\Exception $exception)
  113. {
  114. $flattened = FlattenException::create($exception);
  115. $this->assertSame($exception->getFile(), $flattened->getFile());
  116. }
  117. /**
  118. * @dataProvider flattenDataProvider
  119. */
  120. public function testToArray(\Exception $exception, $statusCode)
  121. {
  122. $flattened = FlattenException::create($exception);
  123. $flattened->setTrace(array(), 'foo.php', 123);
  124. $this->assertEquals(array(
  125. array(
  126. 'message'=> 'test',
  127. 'class'=>'Exception',
  128. 'trace'=>array(array(
  129. 'namespace' => '', 'short_class' => '', 'class' => '','type' => '','function' => '', 'file' => 'foo.php', 'line' => 123,
  130. 'args' => array()
  131. )),
  132. )
  133. ), $flattened->toArray());
  134. }
  135. public function flattenDataProvider()
  136. {
  137. return array(
  138. array(new \Exception('test', 123), 500),
  139. );
  140. }
  141. public function testRecursionInArguments()
  142. {
  143. $a = array('foo', array(2, &$a));
  144. $exception = $this->createException($a);
  145. $flattened = FlattenException::create($exception);
  146. $trace = $flattened->getTrace();
  147. $this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
  148. }
  149. private function createException($foo)
  150. {
  151. return new \Exception();
  152. }
  153. public function testSetTraceIncompleteClass()
  154. {
  155. $flattened = FlattenException::create(new \Exception('test', 123));
  156. $flattened->setTrace(
  157. array(
  158. array(
  159. 'file' => __FILE__,
  160. 'line' => 123,
  161. 'function' => 'test',
  162. 'args' => array(
  163. unserialize('O:14:"BogusTestClass":0:{}')
  164. ),
  165. ),
  166. ),
  167. 'foo.php', 123
  168. );
  169. $this->assertEquals(array(
  170. array(
  171. 'message'=> 'test',
  172. 'class'=>'Exception',
  173. 'trace'=>array(
  174. array(
  175. 'namespace' => '', 'short_class' => '', 'class' => '','type' => '','function' => '',
  176. 'file' => 'foo.php', 'line' => 123,
  177. 'args' => array(),
  178. ),
  179. array(
  180. 'namespace' => '', 'short_class' => '', 'class' => '','type' => '','function' => 'test',
  181. 'file' => __FILE__, 'line' => 123,
  182. 'args' => array(
  183. array(
  184. 'incomplete-object', 'BogusTestClass'
  185. ),
  186. ),
  187. )
  188. ),
  189. )
  190. ), $flattened->toArray());
  191. }
  192. }