PsrLogMessageProcessorTest.php 2.2 KB

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