ClosureContextProcessorTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 PHPUnit\Framework\Attributes\DataProvider;
  12. class ClosureContextProcessorTest extends \Monolog\Test\MonologTestCase
  13. {
  14. public function testReplace()
  15. {
  16. $context = ['obj' => new \stdClass()];
  17. $processor = new ClosureContextProcessor();
  18. $record = $processor($this->getRecord(context: [fn () => $context]));
  19. $this->assertSame($context, $record->context);
  20. }
  21. #[DataProvider('getContexts')]
  22. public function testSkip(array $context)
  23. {
  24. $processor = new ClosureContextProcessor();
  25. $record = $processor($this->getRecord(context: $context));
  26. $this->assertSame($context, $record->context);
  27. }
  28. public function testClosureReturnsNotArray()
  29. {
  30. $object = new \stdClass();
  31. $processor = new ClosureContextProcessor();
  32. $record = $processor($this->getRecord(context: [fn () => $object]));
  33. $this->assertSame([$object], $record->context);
  34. }
  35. public function testClosureThrows()
  36. {
  37. $exception = new \Exception('For test.');
  38. $expected = [
  39. 'error_on_context_generation' => 'For test.',
  40. 'exception' => $exception,
  41. ];
  42. $processor = new ClosureContextProcessor();
  43. $record = $processor($this->getRecord(context: [fn () => throw $exception]));
  44. $this->assertSame($expected, $record->context);
  45. }
  46. public static function getContexts(): iterable
  47. {
  48. yield [['foo']];
  49. yield [['foo' => 'bar']];
  50. yield [['foo', 'bar']];
  51. yield [['foo', fn () => 'bar']];
  52. yield [[fn () => 'foo', 'bar']];
  53. yield [['foo' => fn () => 'bar']];
  54. }
  55. }