NormalizerFormatterTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /**
  12. * @covers Monolog\Formatter\NormalizerFormatter
  13. */
  14. class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testFormat()
  17. {
  18. $formatter = new NormalizerFormatter('Y-m-d');
  19. $formatted = $formatter->format(array(
  20. 'level_name' => 'ERROR',
  21. 'channel' => 'meh',
  22. 'message' => 'foo',
  23. 'datetime' => new \DateTime,
  24. 'extra' => array('foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
  25. 'context' => array(
  26. 'foo' => 'bar',
  27. 'baz' => 'qux',
  28. ),
  29. ));
  30. $this->assertEquals(array(
  31. 'level_name' => 'ERROR',
  32. 'channel' => 'meh',
  33. 'message' => 'foo',
  34. 'datetime' => date('Y-m-d'),
  35. 'extra' => array(
  36. 'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
  37. 'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: {})',
  38. 'baz' => array(),
  39. 'res' => '[resource]',
  40. ),
  41. 'context' => array(
  42. 'foo' => 'bar',
  43. 'baz' => 'qux',
  44. )
  45. ), $formatted);
  46. }
  47. public function testFormatExceptions()
  48. {
  49. $formatter = new NormalizerFormatter('Y-m-d');
  50. $e = new \LogicException('bar');
  51. $e2 = new \RuntimeException('foo', 0, $e);
  52. $formatted = $formatter->format(array(
  53. 'exception' => $e2,
  54. ));
  55. $this->assertGreaterThan(5, count($formatted['exception']['trace']));
  56. $this->assertTrue(isset($formatted['exception']['previous']));
  57. unset($formatted['exception']['trace'], $formatted['exception']['previous']);
  58. $this->assertEquals(array(
  59. 'exception' => array(
  60. 'class' => get_class($e2),
  61. 'message' => $e2->getMessage(),
  62. 'code' => $e2->getCode(),
  63. 'file' => $e2->getFile().':'.$e2->getLine(),
  64. )
  65. ), $formatted);
  66. }
  67. public function testBatchFormat()
  68. {
  69. $formatter = new NormalizerFormatter('Y-m-d');
  70. $formatted = $formatter->formatBatch(array(
  71. array(
  72. 'level_name' => 'CRITICAL',
  73. 'channel' => 'test',
  74. 'message' => 'bar',
  75. 'context' => array(),
  76. 'datetime' => new \DateTime,
  77. 'extra' => array(),
  78. ),
  79. array(
  80. 'level_name' => 'WARNING',
  81. 'channel' => 'log',
  82. 'message' => 'foo',
  83. 'context' => array(),
  84. 'datetime' => new \DateTime,
  85. 'extra' => array(),
  86. ),
  87. ));
  88. $this->assertEquals(array(
  89. array(
  90. 'level_name' => 'CRITICAL',
  91. 'channel' => 'test',
  92. 'message' => 'bar',
  93. 'context' => array(),
  94. 'datetime' => date('Y-m-d'),
  95. 'extra' => array(),
  96. ),
  97. array(
  98. 'level_name' => 'WARNING',
  99. 'channel' => 'log',
  100. 'message' => 'foo',
  101. 'context' => array(),
  102. 'datetime' => date('Y-m-d'),
  103. 'extra' => array(),
  104. ),
  105. ), $formatted);
  106. }
  107. /**
  108. * Test issue #137
  109. */
  110. public function testIgnoresRecursiveObjectReferences()
  111. {
  112. // set up the recursion
  113. $foo = new \stdClass();
  114. $bar = new \stdClass();
  115. $foo->bar = $bar;
  116. $bar->foo = $foo;
  117. // set an error handler to assert that the error is not raised anymore
  118. $that = $this;
  119. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  120. if (error_reporting() & $level) {
  121. restore_error_handler();
  122. $that->fail("$message should not be raised");
  123. }
  124. });
  125. $formatter = new NormalizerFormatter();
  126. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  127. $reflMethod->setAccessible(true);
  128. $res = $reflMethod->invoke($formatter, array($foo, $bar), true);
  129. restore_error_handler();
  130. $this->assertEquals(@json_encode(array($foo, $bar)), $res);
  131. }
  132. public function testIgnoresInvalidTypes()
  133. {
  134. // set up the recursion
  135. $resource = fopen(__FILE__, 'r');
  136. // set an error handler to assert that the error is not raised anymore
  137. $that = $this;
  138. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  139. if (error_reporting() & $level) {
  140. restore_error_handler();
  141. $that->fail("$message should not be raised");
  142. }
  143. });
  144. $formatter = new NormalizerFormatter();
  145. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  146. $reflMethod->setAccessible(true);
  147. $res = $reflMethod->invoke($formatter, array($resource), true);
  148. restore_error_handler();
  149. $this->assertEquals(@json_encode(array($resource)), $res);
  150. }
  151. }
  152. class TestFooNorm
  153. {
  154. public $foo = 'foo';
  155. }
  156. class TestBarNorm
  157. {
  158. public function __toString()
  159. {
  160. return 'bar';
  161. }
  162. }