PsrLogMessageProcessorTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Processor;
  11. class PsrLogMessageProcessorTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @dataProvider getPairs
  15. */
  16. public function testReplacement($val, $expected)
  17. {
  18. $proc = new PsrLogMessageProcessor;
  19. $message = $proc(array(
  20. 'message' => '{foo}',
  21. 'context' => array('foo' => $val),
  22. ));
  23. $this->assertEquals($expected, $message['message']);
  24. }
  25. public function testReplacementWithContextRemoval()
  26. {
  27. $proc = new PsrLogMessageProcessor($dateFormat = null, $removeUsedContextFields = true);
  28. $message = $proc(array(
  29. 'message' => '{foo}',
  30. 'context' => array('foo' => 'bar', 'lorem' => 'ipsum'),
  31. ));
  32. $this->assertSame('bar', $message['message']);
  33. $this->assertSame(array('lorem' => 'ipsum'), $message['context']);
  34. }
  35. public function testCustomDateFormat()
  36. {
  37. $format = "Y-m-d";
  38. $date = new \DateTime();
  39. $proc = new PsrLogMessageProcessor($format);
  40. $message = $proc(array(
  41. 'message' => '{foo}',
  42. 'context' => array('foo' => $date),
  43. ));
  44. $this->assertEquals($date->format($format), $message['message']);
  45. $this->assertSame(array('foo' => $date), $message['context']);
  46. }
  47. public function getPairs()
  48. {
  49. $date = new \DateTime();
  50. return array(
  51. array('foo', 'foo'),
  52. array('3', '3'),
  53. array(3, '3'),
  54. array(null, ''),
  55. array(true, '1'),
  56. array(false, ''),
  57. array($date, $date->format(PsrLogMessageProcessor::SIMPLE_DATE)),
  58. array(new \stdClass, '[object stdClass]'),
  59. array(array(), 'array[]'),
  60. array(array(1, 2, 3), 'array[1,2,3]'),
  61. array(array('foo' => 'bar'), 'array{"foo":"bar"}'),
  62. array(stream_context_create(), '[resource]'),
  63. );
  64. }
  65. }