LineFormatterTest.php 3.2 KB

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