PsrLogMessageProcessorTest.php 2.2 KB

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