NormalizerFormatterTest.php 2.9 KB

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