NormalizerFormatterTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. public function testExceptionTraceWithArgs()
  152. {
  153. if (defined('HHVM_VERSION')) {
  154. $this->markTestSkipped('Not supported in HHVM since it detects errors differently');
  155. }
  156. // This happens i.e. in React promises or Guzzle streams where stream wrappers are registered
  157. // and no file or line are included in the trace because it's treated as internal function
  158. set_error_handler(function ($errno, $errstr, $errfile, $errline) {
  159. throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
  160. });
  161. try {
  162. // This will contain $resource and $wrappedResource as arguments in the trace item
  163. $resource = fopen('php://memory', 'rw+');
  164. fwrite($resource, 'test_resource');
  165. $wrappedResource = new TestStreamFoo($resource);
  166. // Just do something stupid with a resource/wrapped resource as argument
  167. array_keys($wrappedResource);
  168. } catch (\Exception $e) {
  169. restore_error_handler();
  170. }
  171. $formatter = new NormalizerFormatter();
  172. $record = array('context' => array('exception' => $e));
  173. $result = $formatter->format($record);
  174. $this->assertRegExp(
  175. '%"resource":"\[resource\]"%',
  176. $result['context']['exception']['trace'][0]
  177. );
  178. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  179. $pattern = '%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestStreamFoo: \)"%';
  180. } else {
  181. $pattern = '%\\\\"resource\\\\":null%';
  182. }
  183. // Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
  184. $this->assertRegExp(
  185. $pattern,
  186. $result['context']['exception']['trace'][0]
  187. );
  188. }
  189. }
  190. class TestFooNorm
  191. {
  192. public $foo = 'foo';
  193. }
  194. class TestBarNorm
  195. {
  196. public function __toString()
  197. {
  198. return 'bar';
  199. }
  200. }
  201. class TestStreamFoo
  202. {
  203. public $foo;
  204. public $resource;
  205. public function __construct($resource)
  206. {
  207. $this->resource = $resource;
  208. $this->foo = 'BAR';
  209. }
  210. public function __toString()
  211. {
  212. fseek($this->resource, 0);
  213. return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
  214. }
  215. }