NormalizerFormatterTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 'file' => $e2->getFile().':'.$e2->getLine(),
  63. )
  64. ), $formatted);
  65. }
  66. public function testBatchFormat()
  67. {
  68. $formatter = new NormalizerFormatter('Y-m-d');
  69. $formatted = $formatter->formatBatch(array(
  70. array(
  71. 'level_name' => 'CRITICAL',
  72. 'channel' => 'test',
  73. 'message' => 'bar',
  74. 'context' => array(),
  75. 'datetime' => new \DateTime,
  76. 'extra' => array(),
  77. ),
  78. array(
  79. 'level_name' => 'WARNING',
  80. 'channel' => 'log',
  81. 'message' => 'foo',
  82. 'context' => array(),
  83. 'datetime' => new \DateTime,
  84. 'extra' => array(),
  85. ),
  86. ));
  87. $this->assertEquals(array(
  88. array(
  89. 'level_name' => 'CRITICAL',
  90. 'channel' => 'test',
  91. 'message' => 'bar',
  92. 'context' => array(),
  93. 'datetime' => date('Y-m-d'),
  94. 'extra' => array(),
  95. ),
  96. array(
  97. 'level_name' => 'WARNING',
  98. 'channel' => 'log',
  99. 'message' => 'foo',
  100. 'context' => array(),
  101. 'datetime' => date('Y-m-d'),
  102. 'extra' => array(),
  103. ),
  104. ), $formatted);
  105. }
  106. /**
  107. * Test issue #137
  108. */
  109. public function testIgnoresRecursiveObjectReferences()
  110. {
  111. // set up the recursion
  112. $foo = new \stdClass();
  113. $bar = new \stdClass();
  114. $foo->bar = $bar;
  115. $bar->foo = $foo;
  116. // set an error handler to assert that the error is not raised anymore
  117. $that = $this;
  118. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  119. if (error_reporting() & $level) {
  120. restore_error_handler();
  121. $that->fail("$message should not be raised");
  122. }
  123. });
  124. $formatter = new NormalizerFormatter();
  125. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  126. $reflMethod->setAccessible(true);
  127. $res = $reflMethod->invoke($formatter, array($foo, $bar), true);
  128. restore_error_handler();
  129. $this->assertEquals(@json_encode(array($foo, $bar)), $res);
  130. }
  131. public function testIgnoresInvalidTypes()
  132. {
  133. // set up the recursion
  134. $resource = fopen(__FILE__, 'r');
  135. // set an error handler to assert that the error is not raised anymore
  136. $that = $this;
  137. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  138. if (error_reporting() & $level) {
  139. restore_error_handler();
  140. $that->fail("$message should not be raised");
  141. }
  142. });
  143. $formatter = new NormalizerFormatter();
  144. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  145. $reflMethod->setAccessible(true);
  146. $res = $reflMethod->invoke($formatter, array($resource), true);
  147. restore_error_handler();
  148. $this->assertEquals(@json_encode(array($resource)), $res);
  149. }
  150. public function testExceptionTraceWithArgs()
  151. {
  152. // This happens i.e. in React promises or Guzzle streams where stream wrappers are registered
  153. // and no file or line are included in the trace because it's treated as internal function
  154. set_error_handler(function ($errno, $errstr, $errfile, $errline) {
  155. throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
  156. });
  157. try {
  158. // This will contain $resource and $wrappedResource as arguments in the trace item
  159. $resource = fopen('php://memory', 'rw+');
  160. fwrite($resource, 'test_resource');
  161. $wrappedResource = new TestStreamFoo($resource);
  162. // Just do something stupid with a resource/wrapped resource as argument
  163. array_keys($wrappedResource);
  164. } catch (\Exception $e) {
  165. restore_error_handler();
  166. }
  167. $formatter = new NormalizerFormatter();
  168. $record = ['context' => ['exception' => $e]];
  169. $result = $formatter->format($record);
  170. $this->assertRegExp(
  171. '%"resource":"\[resource\]"%',
  172. $result['context']['exception']['trace'][0]
  173. );
  174. // Tests that the wrapped resource is ignored while encoding
  175. $this->assertRegExp(
  176. '%\\\\"resource\\\\":null%',
  177. $result['context']['exception']['trace'][0]
  178. );
  179. }
  180. }
  181. class TestFooNorm
  182. {
  183. public $foo = 'foo';
  184. }
  185. class TestBarNorm
  186. {
  187. public function __toString()
  188. {
  189. return 'bar';
  190. }
  191. }
  192. class TestStreamFoo
  193. {
  194. public $foo;
  195. public $resource;
  196. public function __construct($resource)
  197. {
  198. $this->resource = $resource;
  199. $this->foo = 'BAR';
  200. }
  201. public function __toString()
  202. {
  203. fseek($this->resource, 0);
  204. return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
  205. }
  206. }