2
0

NormalizerFormatterTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 testBatchFormat()
  48. {
  49. $formatter = new NormalizerFormatter('Y-m-d');
  50. $formatted = $formatter->formatBatch(array(
  51. array(
  52. 'level_name' => 'CRITICAL',
  53. 'channel' => 'test',
  54. 'message' => 'bar',
  55. 'context' => array(),
  56. 'datetime' => new \DateTime,
  57. 'extra' => array(),
  58. ),
  59. array(
  60. 'level_name' => 'WARNING',
  61. 'channel' => 'log',
  62. 'message' => 'foo',
  63. 'context' => array(),
  64. 'datetime' => new \DateTime,
  65. 'extra' => array(),
  66. ),
  67. ));
  68. $this->assertEquals(array(
  69. array(
  70. 'level_name' => 'CRITICAL',
  71. 'channel' => 'test',
  72. 'message' => 'bar',
  73. 'context' => array(),
  74. 'datetime' => date('Y-m-d'),
  75. 'extra' => array(),
  76. ),
  77. array(
  78. 'level_name' => 'WARNING',
  79. 'channel' => 'log',
  80. 'message' => 'foo',
  81. 'context' => array(),
  82. 'datetime' => date('Y-m-d'),
  83. 'extra' => array(),
  84. ),
  85. ), $formatted);
  86. }
  87. /**
  88. * Test issue #137
  89. */
  90. public function testIgnoresRecursiveObjectReferences()
  91. {
  92. // set up the recursion
  93. $foo = new \stdClass();
  94. $bar = new \stdClass();
  95. $foo->bar = $bar;
  96. $bar->foo = $foo;
  97. // set an error handler to assert that the error is not raised anymore
  98. $that = $this;
  99. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  100. if (error_reporting() & $level) {
  101. restore_error_handler();
  102. $that->fail("$message should not be raised");
  103. }
  104. });
  105. $formatter = new NormalizerFormatter();
  106. $reflMethod = new \ReflectionMethod($formatter, 'toJson');
  107. $reflMethod->setAccessible(true);
  108. $res = $reflMethod->invoke($formatter, array($foo, $bar), true);
  109. restore_error_handler();
  110. $this->assertEquals(@json_encode(array($foo, $bar)), $res);
  111. }
  112. public function testIgnoresInvalidTypes()
  113. {
  114. // set up the recursion
  115. $resource = fopen(__FILE__, 'r');
  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($resource), true);
  128. restore_error_handler();
  129. $this->assertEquals(@json_encode(array($resource)), $res);
  130. }
  131. }
  132. class TestFooNorm
  133. {
  134. public $foo = 'foo';
  135. }
  136. class TestBarNorm
  137. {
  138. public function __toString()
  139. {
  140. return 'bar';
  141. }
  142. }