NormalizerFormatterTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 'inf' => INF,
  29. '-inf' => -INF,
  30. 'nan' => acos(4),
  31. ),
  32. ));
  33. $this->assertEquals(array(
  34. 'level_name' => 'ERROR',
  35. 'channel' => 'meh',
  36. 'message' => 'foo',
  37. 'datetime' => date('Y-m-d'),
  38. 'extra' => array(
  39. 'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
  40. 'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: bar)',
  41. 'baz' => array(),
  42. 'res' => '[resource]',
  43. ),
  44. 'context' => array(
  45. 'foo' => 'bar',
  46. 'baz' => 'qux',
  47. 'inf' => 'INF',
  48. '-inf' => '-INF',
  49. 'nan' => 'NaN',
  50. )
  51. ), $formatted);
  52. }
  53. public function testFormatExceptions()
  54. {
  55. $formatter = new NormalizerFormatter('Y-m-d');
  56. $e = new \LogicException('bar');
  57. $e2 = new \RuntimeException('foo', 0, $e);
  58. $formatted = $formatter->format(array(
  59. 'exception' => $e2,
  60. ));
  61. $this->assertGreaterThan(5, count($formatted['exception']['trace']));
  62. $this->assertTrue(isset($formatted['exception']['previous']));
  63. unset($formatted['exception']['trace'], $formatted['exception']['previous']);
  64. $this->assertEquals(array(
  65. 'exception' => array(
  66. 'class' => get_class($e2),
  67. 'message' => $e2->getMessage(),
  68. 'code' => $e2->getCode(),
  69. 'file' => $e2->getFile().':'.$e2->getLine(),
  70. )
  71. ), $formatted);
  72. }
  73. public function testBatchFormat()
  74. {
  75. $formatter = new NormalizerFormatter('Y-m-d');
  76. $formatted = $formatter->formatBatch(array(
  77. array(
  78. 'level_name' => 'CRITICAL',
  79. 'channel' => 'test',
  80. 'message' => 'bar',
  81. 'context' => array(),
  82. 'datetime' => new \DateTime,
  83. 'extra' => array(),
  84. ),
  85. array(
  86. 'level_name' => 'WARNING',
  87. 'channel' => 'log',
  88. 'message' => 'foo',
  89. 'context' => array(),
  90. 'datetime' => new \DateTime,
  91. 'extra' => array(),
  92. ),
  93. ));
  94. $this->assertEquals(array(
  95. array(
  96. 'level_name' => 'CRITICAL',
  97. 'channel' => 'test',
  98. 'message' => 'bar',
  99. 'context' => array(),
  100. 'datetime' => date('Y-m-d'),
  101. 'extra' => array(),
  102. ),
  103. array(
  104. 'level_name' => 'WARNING',
  105. 'channel' => 'log',
  106. 'message' => 'foo',
  107. 'context' => array(),
  108. 'datetime' => date('Y-m-d'),
  109. 'extra' => array(),
  110. ),
  111. ), $formatted);
  112. }
  113. /**
  114. * Test issue #137
  115. */
  116. public function testIgnoresRecursiveObjectReferences()
  117. {
  118. // set up the recursion
  119. $foo = new \stdClass();
  120. $bar = new \stdClass();
  121. $foo->bar = $bar;
  122. $bar->foo = $foo;
  123. // set an error handler to assert that the error is not raised anymore
  124. $that = $this;
  125. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  126. if (error_reporting() & $level) {
  127. restore_error_handler();
  128. $that->fail("$message should not be raised");
  129. }
  130. });
  131. $formatter = new NormalizerFormatter();
  132. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  133. $reflMethod->setAccessible(true);
  134. $res = $reflMethod->invoke($formatter, array($foo, $bar), true);
  135. restore_error_handler();
  136. $this->assertEquals(@json_encode(array($foo, $bar)), $res);
  137. }
  138. public function testIgnoresInvalidTypes()
  139. {
  140. // set up the recursion
  141. $resource = fopen(__FILE__, 'r');
  142. // set an error handler to assert that the error is not raised anymore
  143. $that = $this;
  144. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  145. if (error_reporting() & $level) {
  146. restore_error_handler();
  147. $that->fail("$message should not be raised");
  148. }
  149. });
  150. $formatter = new NormalizerFormatter();
  151. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  152. $reflMethod->setAccessible(true);
  153. $res = $reflMethod->invoke($formatter, array($resource), true);
  154. restore_error_handler();
  155. $this->assertEquals(@json_encode(array($resource)), $res);
  156. }
  157. /**
  158. * @expectedException RuntimeException
  159. */
  160. public function testThrowsOnInvalidEncoding()
  161. {
  162. $formatter = new NormalizerFormatter();
  163. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  164. $reflMethod->setAccessible(true);
  165. $res = $reflMethod->invoke($formatter, array('message' => utf8_decode('öü')));
  166. }
  167. public function testExceptionTraceWithArgs()
  168. {
  169. if (defined('HHVM_VERSION')) {
  170. $this->markTestSkipped('Not supported in HHVM since it detects errors differently');
  171. }
  172. // This happens i.e. in React promises or Guzzle streams where stream wrappers are registered
  173. // and no file or line are included in the trace because it's treated as internal function
  174. set_error_handler(function ($errno, $errstr, $errfile, $errline) {
  175. throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
  176. });
  177. try {
  178. // This will contain $resource and $wrappedResource as arguments in the trace item
  179. $resource = fopen('php://memory', 'rw+');
  180. fwrite($resource, 'test_resource');
  181. $wrappedResource = new TestFooNorm;
  182. $wrappedResource->foo = $resource;
  183. // Just do something stupid with a resource/wrapped resource as argument
  184. array_keys($wrappedResource);
  185. } catch (\Exception $e) {
  186. restore_error_handler();
  187. }
  188. $formatter = new NormalizerFormatter();
  189. $record = array('context' => array('exception' => $e));
  190. $result = $formatter->format($record);
  191. $this->assertRegExp(
  192. '%"resource":"\[resource\]"%',
  193. $result['context']['exception']['trace'][0]
  194. );
  195. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  196. $pattern = '%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \)"%';
  197. } else {
  198. $pattern = '%\\\\"foo\\\\":null%';
  199. }
  200. // Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
  201. $this->assertRegExp(
  202. $pattern,
  203. $result['context']['exception']['trace'][0]
  204. );
  205. }
  206. }
  207. class TestFooNorm
  208. {
  209. public $foo = 'foo';
  210. }
  211. class TestBarNorm
  212. {
  213. public function __toString()
  214. {
  215. return 'bar';
  216. }
  217. }
  218. class TestStreamFoo
  219. {
  220. public $foo;
  221. public $resource;
  222. public function __construct($resource)
  223. {
  224. $this->resource = $resource;
  225. $this->foo = 'BAR';
  226. }
  227. public function __toString()
  228. {
  229. fseek($this->resource, 0);
  230. return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
  231. }
  232. }