FlattenExceptionTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Tests\Component\HttpKernel\Exception;
  11. use Symfony\Component\HttpKernel\Exception\FlattenException;
  12. class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider flattenDataProvider
  16. */
  17. public function testFlattenHttpException(\Exception $exception, $statusCode)
  18. {
  19. $flattened = FlattenException::create($exception);
  20. $flattened2 = FlattenException::create($exception);
  21. $flattened->setPrevious($flattened2);
  22. $this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
  23. $this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
  24. $this->assertEquals(get_class($exception), $flattened->getClass(), 'The class is set to the class of the original exception');
  25. }
  26. /**
  27. * @dataProvider flattenDataProvider
  28. */
  29. public function testPrevious(\Exception $exception, $statusCode)
  30. {
  31. $flattened = FlattenException::create($exception);
  32. $flattened2 = FlattenException::create($exception);
  33. $flattened->setPrevious($flattened2);
  34. $this->assertSame($flattened2,$flattened->getPrevious());
  35. $this->assertSame(array($flattened2),$flattened->getAllPrevious());
  36. }
  37. /**
  38. * @dataProvider flattenDataProvider
  39. */
  40. public function testToArray(\Exception $exception, $statusCode)
  41. {
  42. $flattened = FlattenException::create($exception);
  43. $flattened->setTrace(array(),'foo.php',123);
  44. $this->assertEquals(array(
  45. array(
  46. 'message'=> 'test',
  47. 'class'=>'Exception',
  48. 'trace'=>array(array(
  49. 'namespace' => '', 'short_class' => '', 'class' => '','type' => '','function' => '', 'file' => 'foo.php','line' => 123,
  50. 'args' => array()
  51. )),
  52. )
  53. ),$flattened->toArray());
  54. }
  55. public function flattenDataProvider()
  56. {
  57. return array(
  58. array(new \Exception('test', 123), 500),
  59. );
  60. }
  61. public function testRecursionInArguments()
  62. {
  63. $a = array('foo', array(2, &$a));
  64. $exception = $this->createException($a);
  65. $flattened = FlattenException::create($exception);
  66. $trace = $flattened->getTrace();
  67. $this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
  68. }
  69. private function createException($foo)
  70. {
  71. return new \Exception();
  72. }
  73. }