2
0

ChromePHPFormatterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php declare(strict_types=1);
  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\Level;
  12. use Monolog\Test\MonologTestCase;
  13. class ChromePHPFormatterTest extends MonologTestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\ChromePHPFormatter::format
  17. */
  18. public function testDefaultFormat()
  19. {
  20. $formatter = new ChromePHPFormatter();
  21. $record = $this->getRecord(
  22. Level::Error,
  23. 'log',
  24. channel: 'meh',
  25. context: ['from' => 'logger'],
  26. datetime: new \DateTimeImmutable("@0"),
  27. extra: ['ip' => '127.0.0.1'],
  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 = $this->getRecord(
  51. Level::Critical,
  52. 'log',
  53. channel: 'meh',
  54. context: ['from' => 'logger'],
  55. datetime: new \DateTimeImmutable("@0"),
  56. extra: ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
  57. );
  58. $message = $formatter->format($record);
  59. $this->assertEquals(
  60. [
  61. 'meh',
  62. [
  63. 'message' => 'log',
  64. 'context' => ['from' => 'logger'],
  65. 'extra' => ['ip' => '127.0.0.1'],
  66. ],
  67. 'test : 14',
  68. 'error',
  69. ],
  70. $message
  71. );
  72. }
  73. /**
  74. * @covers Monolog\Formatter\ChromePHPFormatter::format
  75. */
  76. public function testFormatWithoutContext()
  77. {
  78. $formatter = new ChromePHPFormatter();
  79. $record = $this->getRecord(
  80. Level::Debug,
  81. 'log',
  82. channel: 'meh',
  83. datetime: new \DateTimeImmutable("@0"),
  84. );
  85. $message = $formatter->format($record);
  86. $this->assertEquals(
  87. [
  88. 'meh',
  89. 'log',
  90. 'unknown',
  91. 'log',
  92. ],
  93. $message
  94. );
  95. }
  96. /**
  97. * @covers Monolog\Formatter\ChromePHPFormatter::formatBatch
  98. */
  99. public function testBatchFormatThrowException()
  100. {
  101. $formatter = new ChromePHPFormatter();
  102. $records = [
  103. $this->getRecord(
  104. Level::Info,
  105. 'log',
  106. channel: 'meh',
  107. datetime: new \DateTimeImmutable("@0"),
  108. ),
  109. $this->getRecord(
  110. Level::Warning,
  111. 'log2',
  112. channel: 'foo',
  113. datetime: new \DateTimeImmutable("@0"),
  114. ),
  115. ];
  116. $this->assertEquals(
  117. [
  118. [
  119. 'meh',
  120. 'log',
  121. 'unknown',
  122. 'info',
  123. ],
  124. [
  125. 'foo',
  126. 'log2',
  127. 'unknown',
  128. 'warn',
  129. ],
  130. ],
  131. $formatter->formatBatch($records)
  132. );
  133. }
  134. }