ClosureContextProcessorTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Test\TestCase;
  12. class ClosureContextProcessorTest extends TestCase
  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. /**
  22. * @dataProvider getContexts
  23. */
  24. public function testSkip(array $context)
  25. {
  26. $processor = new ClosureContextProcessor();
  27. $record = $processor($this->getRecord(context: $context));
  28. $this->assertSame($context, $record->context);
  29. }
  30. public function testClosureReturnsNotArray()
  31. {
  32. $object = new \stdClass();
  33. $processor = new ClosureContextProcessor();
  34. $record = $processor($this->getRecord(context: [fn () => $object]));
  35. $this->assertSame([$object], $record->context);
  36. }
  37. public function testClosureThrows()
  38. {
  39. $exception = new \Exception('For test.');
  40. $expected = [
  41. 'error_on_context_generation' => 'For test.',
  42. 'exception' => $exception,
  43. ];
  44. $processor = new ClosureContextProcessor();
  45. $record = $processor($this->getRecord(context: [fn () => throw $exception]));
  46. $this->assertSame($expected, $record->context);
  47. }
  48. public static function getContexts(): iterable
  49. {
  50. yield [['foo']];
  51. yield [['foo' => 'bar']];
  52. yield [['foo', 'bar']];
  53. yield [['foo', fn () => 'bar']];
  54. yield [[fn () => 'foo', 'bar']];
  55. yield [['foo' => fn () => 'bar']];
  56. }
  57. }