HttpExceptionTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\HttpKernel\Tests\Exception;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\Exception\HttpException;
  13. class HttpExceptionTest extends TestCase
  14. {
  15. public static function headerDataProvider()
  16. {
  17. return [
  18. [['X-Test' => 'Test']],
  19. [['X-Test' => 1]],
  20. [
  21. [
  22. ['X-Test' => 'Test'],
  23. ['X-Test-2' => 'Test-2'],
  24. ],
  25. ],
  26. ];
  27. }
  28. public function testHeadersDefault()
  29. {
  30. $exception = $this->createException();
  31. $this->assertSame([], $exception->getHeaders());
  32. }
  33. /**
  34. * @dataProvider headerDataProvider
  35. */
  36. public function testHeadersConstructor($headers)
  37. {
  38. $exception = new HttpException(200, '', null, $headers);
  39. $this->assertSame($headers, $exception->getHeaders());
  40. }
  41. /**
  42. * @dataProvider headerDataProvider
  43. */
  44. public function testHeadersSetter($headers)
  45. {
  46. $exception = $this->createException();
  47. $exception->setHeaders($headers);
  48. $this->assertSame($headers, $exception->getHeaders());
  49. }
  50. public function testThrowableIsAllowedForPrevious()
  51. {
  52. $previous = new class('Error of PHP 7+') extends \Error {
  53. };
  54. $exception = $this->createException('', $previous);
  55. $this->assertSame($previous, $exception->getPrevious());
  56. }
  57. protected function createException(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = []): HttpException
  58. {
  59. return new HttpException(200, $message, $previous, $headers, $code);
  60. }
  61. }