PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/ErrorHandler/Tests/Exception/FlattenExceptionTest.php

https://github.com/FabienD/symfony
PHP | 302 lines | 210 code | 66 blank | 26 comment | 1 complexity | a47230dbcdf2982694fa266066fd32de MD5 | raw file
  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\ErrorHandler\Tests\Exception;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  13. use Symfony\Component\ErrorHandler\Tests\Fixtures\StringErrorCodeException;
  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::createFromThrowable(new \RuntimeException(), 403);
  34. $this->assertEquals('403', $flattened->getStatusCode());
  35. $flattened = FlattenException::createFromThrowable(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::createFromThrowable(new NotFoundHttpException());
  42. $this->assertEquals('404', $flattened->getStatusCode());
  43. $flattened = FlattenException::createFromThrowable(new UnauthorizedHttpException('Basic realm="My Realm"'));
  44. $this->assertEquals('401', $flattened->getStatusCode());
  45. $flattened = FlattenException::createFromThrowable(new BadRequestHttpException());
  46. $this->assertEquals('400', $flattened->getStatusCode());
  47. $flattened = FlattenException::createFromThrowable(new NotAcceptableHttpException());
  48. $this->assertEquals('406', $flattened->getStatusCode());
  49. $flattened = FlattenException::createFromThrowable(new ConflictHttpException());
  50. $this->assertEquals('409', $flattened->getStatusCode());
  51. $flattened = FlattenException::createFromThrowable(new MethodNotAllowedHttpException(['POST']));
  52. $this->assertEquals('405', $flattened->getStatusCode());
  53. $flattened = FlattenException::createFromThrowable(new AccessDeniedHttpException());
  54. $this->assertEquals('403', $flattened->getStatusCode());
  55. $flattened = FlattenException::createFromThrowable(new GoneHttpException());
  56. $this->assertEquals('410', $flattened->getStatusCode());
  57. $flattened = FlattenException::createFromThrowable(new LengthRequiredHttpException());
  58. $this->assertEquals('411', $flattened->getStatusCode());
  59. $flattened = FlattenException::createFromThrowable(new PreconditionFailedHttpException());
  60. $this->assertEquals('412', $flattened->getStatusCode());
  61. $flattened = FlattenException::createFromThrowable(new PreconditionRequiredHttpException());
  62. $this->assertEquals('428', $flattened->getStatusCode());
  63. $flattened = FlattenException::createFromThrowable(new ServiceUnavailableHttpException());
  64. $this->assertEquals('503', $flattened->getStatusCode());
  65. $flattened = FlattenException::createFromThrowable(new TooManyRequestsHttpException());
  66. $this->assertEquals('429', $flattened->getStatusCode());
  67. $flattened = FlattenException::createFromThrowable(new UnsupportedMediaTypeHttpException());
  68. $this->assertEquals('415', $flattened->getStatusCode());
  69. if (class_exists(SuspiciousOperationException::class)) {
  70. $flattened = FlattenException::createFromThrowable(new SuspiciousOperationException());
  71. $this->assertEquals('400', $flattened->getStatusCode());
  72. }
  73. }
  74. public function testHeadersForHttpException()
  75. {
  76. $flattened = FlattenException::createFromThrowable(new MethodNotAllowedHttpException(['POST']));
  77. $this->assertEquals(['Allow' => 'POST'], $flattened->getHeaders());
  78. $flattened = FlattenException::createFromThrowable(new UnauthorizedHttpException('Basic realm="My Realm"'));
  79. $this->assertEquals(['WWW-Authenticate' => 'Basic realm="My Realm"'], $flattened->getHeaders());
  80. $flattened = FlattenException::createFromThrowable(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::createFromThrowable(new ServiceUnavailableHttpException(120));
  83. $this->assertEquals(['Retry-After' => 120], $flattened->getHeaders());
  84. $flattened = FlattenException::createFromThrowable(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::createFromThrowable(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 testThrowable()
  102. {
  103. $error = new \DivisionByZeroError('Ouch', 42);
  104. $flattened = FlattenException::createFromThrowable($error);
  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. /**
  110. * @dataProvider flattenDataProvider
  111. */
  112. public function testPrevious(\Throwable $exception)
  113. {
  114. $flattened = FlattenException::createFromThrowable($exception);
  115. $flattened2 = FlattenException::createFromThrowable($exception);
  116. $flattened->setPrevious($flattened2);
  117. $this->assertSame($flattened2, $flattened->getPrevious());
  118. $this->assertSame([$flattened2], $flattened->getAllPrevious());
  119. }
  120. public function testPreviousError()
  121. {
  122. $exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
  123. $flattened = FlattenException::createFromThrowable($exception)->getPrevious();
  124. $this->assertEquals('Oh noes!', $flattened->getMessage(), 'The message is copied from the original exception.');
  125. $this->assertEquals(42, $flattened->getCode(), 'The code is copied from the original exception.');
  126. $this->assertEquals('ParseError', $flattened->getClass(), 'The class is set to the class of the original exception');
  127. }
  128. /**
  129. * @dataProvider flattenDataProvider
  130. */
  131. public function testLine(\Throwable $exception)
  132. {
  133. $flattened = FlattenException::createFromThrowable($exception);
  134. $this->assertSame($exception->getLine(), $flattened->getLine());
  135. }
  136. /**
  137. * @dataProvider flattenDataProvider
  138. */
  139. public function testFile(\Throwable $exception)
  140. {
  141. $flattened = FlattenException::createFromThrowable($exception);
  142. $this->assertSame($exception->getFile(), $flattened->getFile());
  143. }
  144. /**
  145. * @dataProvider stringAndIntDataProvider
  146. */
  147. public function testCode(\Throwable $exception)
  148. {
  149. $flattened = FlattenException::createFromThrowable($exception);
  150. $this->assertSame($exception->getCode(), $flattened->getCode());
  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::createFromThrowable($exception)->toArray()
  179. );
  180. }
  181. public function flattenDataProvider(): array
  182. {
  183. return [
  184. [new \Exception('test', 123), 'Exception'],
  185. [new \Error('test', 123), 'Error'],
  186. ];
  187. }
  188. public function stringAndIntDataProvider(): array
  189. {
  190. return [
  191. [new \Exception('test1', 123)],
  192. [new StringErrorCodeException('test2', '42S02')],
  193. ];
  194. }
  195. public function testAnonymousClass()
  196. {
  197. $flattened = FlattenException::createFromThrowable(new class() extends \RuntimeException {
  198. });
  199. $this->assertSame('RuntimeException@anonymous', $flattened->getClass());
  200. $flattened->setClass(\get_class(new class('Oops') extends NotFoundHttpException {
  201. }));
  202. $this->assertSame('Symfony\Component\HttpKernel\Exception\NotFoundHttpException@anonymous', $flattened->getClass());
  203. $flattened = FlattenException::createFromThrowable(new \Exception(sprintf('Class "%s" blah.', \get_class(new class() extends \RuntimeException {
  204. }))));
  205. $this->assertSame('Class "RuntimeException@anonymous" blah.', $flattened->getMessage());
  206. }
  207. public function testToStringEmptyMessage()
  208. {
  209. $exception = new \RuntimeException();
  210. $flattened = FlattenException::createFromThrowable($exception);
  211. $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
  212. $this->assertSame($exception->__toString(), $flattened->getAsString());
  213. }
  214. public function testToString()
  215. {
  216. $test = function ($a, $b, $c, $d) {
  217. return new \RuntimeException('This is a test message');
  218. };
  219. $exception = $test('foo123', 1, null, 1.5);
  220. $flattened = FlattenException::createFromThrowable($exception);
  221. $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
  222. $this->assertSame($exception->__toString(), $flattened->getAsString());
  223. }
  224. public function testToStringParent()
  225. {
  226. $exception = new \LogicException('This is message 1');
  227. $exception = new \RuntimeException('This is messsage 2', 500, $exception);
  228. $flattened = FlattenException::createFromThrowable($exception);
  229. $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
  230. $this->assertSame($exception->__toString(), $flattened->getAsString());
  231. }
  232. private function createException($foo): \Exception
  233. {
  234. return new \Exception();
  235. }
  236. }