LineFormatterTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  13. * @covers Monolog\Formatter\LineFormatter
  14. */
  15. class LineFormatterTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testDefFormatWithString()
  18. {
  19. $formatter = new LineFormatter(null, 'Y-m-d');
  20. $message = $formatter->format(array(
  21. 'level_name' => 'WARNING',
  22. 'channel' => 'log',
  23. 'context' => array(),
  24. 'message' => 'foo',
  25. 'datetime' => new \DateTime,
  26. 'extra' => array(),
  27. ));
  28. $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  29. }
  30. public function testDefFormatWithArrayContext()
  31. {
  32. $formatter = new LineFormatter(null, 'Y-m-d');
  33. $message = $formatter->format(array(
  34. 'level_name' => 'ERROR',
  35. 'channel' => 'meh',
  36. 'message' => 'foo',
  37. 'datetime' => new \DateTime,
  38. 'extra' => array(),
  39. 'context' => array(
  40. 'foo' => 'bar',
  41. 'baz' => 'qux',
  42. )
  43. ));
  44. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message);
  45. }
  46. public function testDefFormatExtras()
  47. {
  48. $formatter = new LineFormatter(null, 'Y-m-d');
  49. $message = $formatter->format(array(
  50. 'level_name' => 'ERROR',
  51. 'channel' => 'meh',
  52. 'context' => array(),
  53. 'datetime' => new \DateTime,
  54. 'extra' => array('ip' => '127.0.0.1'),
  55. 'message' => 'log',
  56. ));
  57. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
  58. }
  59. public function testDefFormatWithObject()
  60. {
  61. $formatter = new LineFormatter(null, 'Y-m-d');
  62. $message = $formatter->format(array(
  63. 'level_name' => 'ERROR',
  64. 'channel' => 'meh',
  65. 'context' => array(),
  66. 'datetime' => new \DateTime,
  67. 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
  68. 'message' => 'foobar',
  69. ));
  70. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\Formatter\\TestFoo: {"foo":"foo"})","bar":"[object] (Monolog\\Formatter\\TestBar: {})","baz":[],"res":"[resource]"}'."\n", $message);
  71. }
  72. public function testBatchFormat()
  73. {
  74. $formatter = new LineFormatter(null, 'Y-m-d');
  75. $message = $formatter->formatBatch(array(
  76. array(
  77. 'level_name' => 'CRITICAL',
  78. 'channel' => 'test',
  79. 'message' => 'bar',
  80. 'context' => array(),
  81. 'datetime' => new \DateTime,
  82. 'extra' => array(),
  83. ),
  84. array(
  85. 'level_name' => 'WARNING',
  86. 'channel' => 'log',
  87. 'message' => 'foo',
  88. 'context' => array(),
  89. 'datetime' => new \DateTime,
  90. 'extra' => array(),
  91. ),
  92. ));
  93. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  94. }
  95. }
  96. class TestFoo
  97. {
  98. public $foo = 'foo';
  99. }
  100. class TestBar
  101. {
  102. public function __toString()
  103. {
  104. return 'bar';
  105. }
  106. }