PsrLogMessageProcessorTest.php 2.2 KB

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