PsrLogMessageProcessorTest.php 2.2 KB

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