ScalarFormatterTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  26. }
  27. return $data;
  28. }
  29. public function encodeJson($data)
  30. {
  31. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  32. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  33. }
  34. return json_encode($data);
  35. }
  36. public function testFormat()
  37. {
  38. $exception = new \Exception('foo');
  39. $formatted = $this->formatter->format(array(
  40. 'foo' => 'string',
  41. 'bar' => 1,
  42. 'baz' => false,
  43. 'bam' => array(1, 2, 3),
  44. 'bat' => array('foo' => 'bar'),
  45. 'bap' => \DateTime::createFromFormat(\DateTime::ISO8601, '1970-01-01T00:00:00+0000'),
  46. 'ban' => $exception,
  47. ));
  48. $this->assertSame(array(
  49. 'foo' => 'string',
  50. 'bar' => 1,
  51. 'baz' => false,
  52. 'bam' => $this->encodeJson(array(1, 2, 3)),
  53. 'bat' => $this->encodeJson(array('foo' => 'bar')),
  54. 'bap' => '1970-01-01 00:00:00',
  55. 'ban' => $this->encodeJson(array(
  56. 'class' => get_class($exception),
  57. 'message' => $exception->getMessage(),
  58. 'code' => $exception->getCode(),
  59. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  60. 'trace' => $this->buildTrace($exception),
  61. )),
  62. ), $formatted);
  63. }
  64. public function testFormatWithErrorContext()
  65. {
  66. $context = array('file' => 'foo', 'line' => 1);
  67. $formatted = $this->formatter->format(array(
  68. 'context' => $context,
  69. ));
  70. $this->assertSame(array(
  71. 'context' => $this->encodeJson($context),
  72. ), $formatted);
  73. }
  74. public function testFormatWithExceptionContext()
  75. {
  76. $exception = new \Exception('foo');
  77. $formatted = $this->formatter->format(array(
  78. 'context' => array(
  79. 'exception' => $exception,
  80. ),
  81. ));
  82. $this->assertSame(array(
  83. 'context' => $this->encodeJson(array(
  84. 'exception' => array(
  85. 'class' => get_class($exception),
  86. 'message' => $exception->getMessage(),
  87. 'code' => $exception->getCode(),
  88. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  89. 'trace' => $this->buildTrace($exception),
  90. ),
  91. )),
  92. ), $formatted);
  93. }
  94. }