ScalarFormatterTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Formatter;
  11. class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
  12. {
  13. private $formatter;
  14. public function setUp()
  15. {
  16. $this->formatter = new ScalarFormatter();
  17. }
  18. public function buildTrace(\Exception $e)
  19. {
  20. $data = array();
  21. $trace = $e->getTrace();
  22. foreach ($trace as $frame) {
  23. if (isset($frame['file'])) {
  24. $data[] = $frame['file'].':'.$frame['line'];
  25. } else {
  26. $data[] = json_encode($frame);
  27. }
  28. }
  29. return $data;
  30. }
  31. public function encodeJson($data)
  32. {
  33. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  34. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  35. }
  36. return json_encode($data);
  37. }
  38. public function testFormat()
  39. {
  40. $exception = new \Exception('foo');
  41. $formatted = $this->formatter->format(array(
  42. 'foo' => 'string',
  43. 'bar' => 1,
  44. 'baz' => false,
  45. 'bam' => array(1, 2, 3),
  46. 'bat' => array('foo' => 'bar'),
  47. 'bap' => \DateTime::createFromFormat(\DateTime::ISO8601, '1970-01-01T00:00:00+0000'),
  48. 'ban' => $exception,
  49. ));
  50. $this->assertSame(array(
  51. 'foo' => 'string',
  52. 'bar' => 1,
  53. 'baz' => false,
  54. 'bam' => $this->encodeJson(array(1, 2, 3)),
  55. 'bat' => $this->encodeJson(array('foo' => 'bar')),
  56. 'bap' => '1970-01-01 00:00:00',
  57. 'ban' => $this->encodeJson(array(
  58. 'class' => get_class($exception),
  59. 'message' => $exception->getMessage(),
  60. 'code' => $exception->getCode(),
  61. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  62. 'trace' => $this->buildTrace($exception),
  63. )),
  64. ), $formatted);
  65. }
  66. public function testFormatWithErrorContext()
  67. {
  68. $context = array('file' => 'foo', 'line' => 1);
  69. $formatted = $this->formatter->format(array(
  70. 'context' => $context,
  71. ));
  72. $this->assertSame(array(
  73. 'context' => $this->encodeJson($context),
  74. ), $formatted);
  75. }
  76. public function testFormatWithExceptionContext()
  77. {
  78. $exception = new \Exception('foo');
  79. $formatted = $this->formatter->format(array(
  80. 'context' => array(
  81. 'exception' => $exception,
  82. ),
  83. ));
  84. $this->assertSame(array(
  85. 'context' => $this->encodeJson(array(
  86. 'exception' => array(
  87. 'class' => get_class($exception),
  88. 'message' => $exception->getMessage(),
  89. 'code' => $exception->getCode(),
  90. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  91. 'trace' => $this->buildTrace($exception),
  92. ),
  93. )),
  94. ), $formatted);
  95. }
  96. }