ClosureContextProcessorTest.php 1.9 KB

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