WildfireFormatterTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. use Monolog\Logger;
  12. class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Monolog\Formatter\WildfireFormatter::format
  16. */
  17. public function testDefaultFormat()
  18. {
  19. $wildfire = new WildfireFormatter();
  20. $record = array(
  21. 'level' => Logger::ERROR,
  22. 'level_name' => 'ERROR',
  23. 'channel' => 'meh',
  24. 'context' => array('from' => 'logger'),
  25. 'datetime' => new \DateTime("@0"),
  26. 'extra' => array('ip' => '127.0.0.1'),
  27. 'message' => 'log',
  28. );
  29. $message = $wildfire->format($record);
  30. $this->assertEquals(
  31. '125|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},'
  32. .'{"message":"log","context":{"from":"logger"},"extra":{"ip":"127.0.0.1"}}]|',
  33. $message
  34. );
  35. }
  36. /**
  37. * @covers Monolog\Formatter\WildfireFormatter::format
  38. */
  39. public function testFormatWithFileAndLine()
  40. {
  41. $wildfire = new WildfireFormatter();
  42. $record = array(
  43. 'level' => Logger::ERROR,
  44. 'level_name' => 'ERROR',
  45. 'channel' => 'meh',
  46. 'context' => array('from' => 'logger'),
  47. 'datetime' => new \DateTime("@0"),
  48. 'extra' => array('ip' => '127.0.0.1', 'file' => 'test', 'line' => 14),
  49. 'message' => 'log',
  50. );
  51. $message = $wildfire->format($record);
  52. $this->assertEquals(
  53. '129|[{"Type":"ERROR","File":"test","Line":14,"Label":"meh"},'
  54. .'{"message":"log","context":{"from":"logger"},"extra":{"ip":"127.0.0.1"}}]|',
  55. $message
  56. );
  57. }
  58. /**
  59. * @covers Monolog\Formatter\WildfireFormatter::format
  60. */
  61. public function testFormatWithoutContext()
  62. {
  63. $wildfire = new WildfireFormatter();
  64. $record = array(
  65. 'level' => Logger::ERROR,
  66. 'level_name' => 'ERROR',
  67. 'channel' => 'meh',
  68. 'context' => array(),
  69. 'datetime' => new \DateTime("@0"),
  70. 'extra' => array(),
  71. 'message' => 'log',
  72. );
  73. $message = $wildfire->format($record);
  74. $this->assertEquals(
  75. '58|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},"log"]|',
  76. $message
  77. );
  78. }
  79. /**
  80. * @covers Monolog\Formatter\WildfireFormatter::formatBatch
  81. * @expectedException BadMethodCallException
  82. */
  83. public function testBatchFormatThrowException()
  84. {
  85. $wildfire = new WildfireFormatter();
  86. $record = array(
  87. 'level' => Logger::ERROR,
  88. 'level_name' => 'ERROR',
  89. 'channel' => 'meh',
  90. 'context' => array(),
  91. 'datetime' => new \DateTime("@0"),
  92. 'extra' => array(),
  93. 'message' => 'log',
  94. );
  95. $wildfire->formatBatch(array($record));
  96. }
  97. /**
  98. * Test issue #137 (https://github.com/Seldaek/monolog/pull/137)
  99. */
  100. public function testFormatWithObjectsInContext()
  101. {
  102. // Set up the recursion
  103. $foo = new \stdClass();
  104. $bar = new \stdClass();
  105. $foo->bar = $bar;
  106. $bar->foo = $foo;
  107. $record = array(
  108. 'message' => "foo",
  109. 'level' => 300,
  110. 'channel' => 'foo',
  111. 'context' => array(
  112. 'stack' => array(
  113. array($foo),
  114. array($bar),
  115. ),
  116. ),
  117. 'extra' => array(),
  118. );
  119. // Set an error handler to assert that the error is not raised anymore
  120. $that = $this;
  121. set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
  122. $that->fail("$message should not be raised anymore");
  123. });
  124. $wildfire = new WildfireFormatter();
  125. $wildfire->format($record);
  126. restore_error_handler();
  127. }
  128. }