JsonResponseHandlerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. */
  6. namespace Whoops\Handler;
  7. use Whoops\TestCase;
  8. use Whoops\Handler\JsonResponseHandler;
  9. use RuntimeException;
  10. class JsonResponseHandlerTest extends TestCase
  11. {
  12. /**
  13. * @return Whoops\Handler\JsonResponseHandler
  14. */
  15. private function getHandler()
  16. {
  17. return new JsonResponseHandler;
  18. }
  19. /**
  20. * @return RuntimeException
  21. */
  22. public function getException($message = 'test message')
  23. {
  24. return new RuntimeException($message);
  25. }
  26. /**
  27. * @param bool $withTrace
  28. * @return array
  29. */
  30. private function getJsonResponseFromHandler($withTrace = false)
  31. {
  32. $handler = $this->getHandler();
  33. $handler->addTraceToOutput($withTrace);
  34. $run = $this->getRunInstance();
  35. $run->pushHandler($handler);
  36. $run->register();
  37. $exception = $this->getException();
  38. ob_start();
  39. $run->handleException($exception);
  40. $json = json_decode(ob_get_clean(), true);
  41. // Check that the json response is parse-able:
  42. $this->assertEquals(json_last_error(), JSON_ERROR_NONE);
  43. return $json;
  44. }
  45. /**
  46. * @covers Whoops\Handler\JsonResponseHandler::addTraceToOutput
  47. * @covers Whoops\Handler\JsonResponseHandler::handle
  48. */
  49. public function testReturnsWithoutFrames()
  50. {
  51. $json = $this->getJsonResponseFromHandler($withTrace = false);
  52. // Check that the response has the expected keys:
  53. $this->assertArrayHasKey('error', $json);
  54. $this->assertArrayHasKey('type', $json['error']);
  55. $this->assertArrayHasKey('file', $json['error']);
  56. $this->assertArrayHasKey('line', $json['error']);
  57. // Check the field values:
  58. $this->assertEquals($json['error']['file'], __FILE__);
  59. $this->assertEquals($json['error']['message'], 'test message');
  60. $this->assertEquals($json['error']['type'], get_class($this->getException()));
  61. // Check that the trace is NOT returned:
  62. $this->assertArrayNotHasKey('trace', $json['error']);
  63. }
  64. /**
  65. * @covers Whoops\Handler\JsonResponseHandler::addTraceToOutput
  66. * @covers Whoops\Handler\JsonResponseHandler::handle
  67. */
  68. public function testReturnsWithFrames()
  69. {
  70. $json = $this->getJsonResponseFromHandler($withTrace = true);
  71. // Check that the trace is returned:
  72. $this->assertArrayHasKey('trace', $json['error']);
  73. // Check that a random frame has the expected fields
  74. $traceFrame = reset($json['error']['trace']);
  75. $this->assertArrayHasKey('file', $traceFrame);
  76. $this->assertArrayHasKey('line', $traceFrame);
  77. $this->assertArrayHasKey('function', $traceFrame);
  78. $this->assertArrayHasKey('class', $traceFrame);
  79. $this->assertArrayHasKey('args', $traceFrame);
  80. }
  81. }