ChromePHPFormatterTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ChromePHPFormatterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Monolog\Formatter\ChromePHPFormatter::format
  16. */
  17. public function testDefaultFormat()
  18. {
  19. $formatter = new ChromePHPFormatter();
  20. $record = [
  21. 'level' => Logger::ERROR,
  22. 'level_name' => 'ERROR',
  23. 'channel' => 'meh',
  24. 'context' => ['from' => 'logger'],
  25. 'datetime' => new \DateTimeImmutable("@0"),
  26. 'extra' => ['ip' => '127.0.0.1'],
  27. 'message' => 'log',
  28. ];
  29. $message = $formatter->format($record);
  30. $this->assertEquals(
  31. [
  32. 'meh',
  33. [
  34. 'message' => 'log',
  35. 'context' => ['from' => 'logger'],
  36. 'extra' => ['ip' => '127.0.0.1'],
  37. ],
  38. 'unknown',
  39. 'error',
  40. ],
  41. $message
  42. );
  43. }
  44. /**
  45. * @covers Monolog\Formatter\ChromePHPFormatter::format
  46. */
  47. public function testFormatWithFileAndLine()
  48. {
  49. $formatter = new ChromePHPFormatter();
  50. $record = [
  51. 'level' => Logger::CRITICAL,
  52. 'level_name' => 'CRITICAL',
  53. 'channel' => 'meh',
  54. 'context' => ['from' => 'logger'],
  55. 'datetime' => new \DateTimeImmutable("@0"),
  56. 'extra' => ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
  57. 'message' => 'log',
  58. ];
  59. $message = $formatter->format($record);
  60. $this->assertEquals(
  61. [
  62. 'meh',
  63. [
  64. 'message' => 'log',
  65. 'context' => ['from' => 'logger'],
  66. 'extra' => ['ip' => '127.0.0.1'],
  67. ],
  68. 'test : 14',
  69. 'error',
  70. ],
  71. $message
  72. );
  73. }
  74. /**
  75. * @covers Monolog\Formatter\ChromePHPFormatter::format
  76. */
  77. public function testFormatWithoutContext()
  78. {
  79. $formatter = new ChromePHPFormatter();
  80. $record = [
  81. 'level' => Logger::DEBUG,
  82. 'level_name' => 'DEBUG',
  83. 'channel' => 'meh',
  84. 'context' => [],
  85. 'datetime' => new \DateTimeImmutable("@0"),
  86. 'extra' => [],
  87. 'message' => 'log',
  88. ];
  89. $message = $formatter->format($record);
  90. $this->assertEquals(
  91. [
  92. 'meh',
  93. 'log',
  94. 'unknown',
  95. 'log',
  96. ],
  97. $message
  98. );
  99. }
  100. /**
  101. * @covers Monolog\Formatter\ChromePHPFormatter::formatBatch
  102. */
  103. public function testBatchFormatThrowException()
  104. {
  105. $formatter = new ChromePHPFormatter();
  106. $records = [
  107. [
  108. 'level' => Logger::INFO,
  109. 'level_name' => 'INFO',
  110. 'channel' => 'meh',
  111. 'context' => [],
  112. 'datetime' => new \DateTimeImmutable("@0"),
  113. 'extra' => [],
  114. 'message' => 'log',
  115. ],
  116. [
  117. 'level' => Logger::WARNING,
  118. 'level_name' => 'WARNING',
  119. 'channel' => 'foo',
  120. 'context' => [],
  121. 'datetime' => new \DateTimeImmutable("@0"),
  122. 'extra' => [],
  123. 'message' => 'log2',
  124. ],
  125. ];
  126. $this->assertEquals(
  127. [
  128. [
  129. 'meh',
  130. 'log',
  131. 'unknown',
  132. 'info',
  133. ],
  134. [
  135. 'foo',
  136. 'log2',
  137. 'unknown',
  138. 'warn',
  139. ],
  140. ],
  141. $formatter->formatBatch($records)
  142. );
  143. }
  144. }