|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Context; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use RoadRunner\PsrLogger\Context\DefaultProcessor; |
| 11 | +use RoadRunner\PsrLogger\Context\ObjectProcessor\BuiltInTypeProcessor; |
| 12 | + |
| 13 | +#[CoversClass(DefaultProcessor::class)] |
| 14 | +class DefaultProcessorTest extends TestCase |
| 15 | +{ |
| 16 | + private DefaultProcessor $processor; |
| 17 | + |
| 18 | + public static function builtInTypeValuesProvider(): array |
| 19 | + { |
| 20 | + return [ |
| 21 | + 'string' => ['test string', 'test string'], |
| 22 | + 'integer' => [42, 42], |
| 23 | + 'float' => [3.14, 3.14], |
| 24 | + 'boolean true' => [true, true], |
| 25 | + 'boolean false' => [false, false], |
| 26 | + 'null' => [null, null], |
| 27 | + 'empty array' => [[], []], |
| 28 | + 'simple array' => [[1, 2, 3], [1, 2, 3]], |
| 29 | + 'associative array' => [['key' => 'value'], ['key' => 'value']], |
| 30 | + 'resource' => [\fopen('php://memory', 'r'), 'stream resource'], |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + #[DataProvider('builtInTypeValuesProvider')] |
| 35 | + public function testCanProcessBuiltInTypes(mixed $value, mixed $expected): void |
| 36 | + { |
| 37 | + $this->assertSame($expected, ($this->processor)($value)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testProcessNull(): void |
| 41 | + { |
| 42 | + $recursiveProcessor = static fn($v) => $v; |
| 43 | + $result = ($this->processor)(null, $recursiveProcessor); |
| 44 | + $this->assertNull($result); |
| 45 | + } |
| 46 | + |
| 47 | + public function testProcessScalarValues(): void |
| 48 | + { |
| 49 | + $values = ['test string', 42, 3.14, true, false]; |
| 50 | + $recursiveProcessor = static fn($v) => $v; |
| 51 | + |
| 52 | + foreach ($values as $value) { |
| 53 | + $result = ($this->processor)($value, $recursiveProcessor); |
| 54 | + $this->assertSame($value, $result); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function testProcessSimpleArray(): void |
| 59 | + { |
| 60 | + $array = [1, 2, 'three', true]; |
| 61 | + $recursiveProcessor = static fn($v) => $v; // Identity function for simple values |
| 62 | + |
| 63 | + $result = ($this->processor)($array, $recursiveProcessor); |
| 64 | + |
| 65 | + $this->assertSame([1, 2, 'three', true], $result); |
| 66 | + } |
| 67 | + |
| 68 | + public function testProcessNestedArray(): void |
| 69 | + { |
| 70 | + $array = [ |
| 71 | + 'level1' => [ |
| 72 | + 'level2' => [ |
| 73 | + 'value' => 'deep', |
| 74 | + ], |
| 75 | + ], |
| 76 | + ]; |
| 77 | + |
| 78 | + $result = ($this->processor)($array); |
| 79 | + |
| 80 | + $this->assertArrayHasKey('level1', $result); |
| 81 | + $this->assertIsArray($result['level1']); |
| 82 | + $this->assertArrayHasKey('level2', $result['level1']); |
| 83 | + $this->assertIsArray($result['level1']['level2']); |
| 84 | + $this->assertSame('deep', $result['level1']['level2']['value']); |
| 85 | + } |
| 86 | + |
| 87 | + protected function setUp(): void |
| 88 | + { |
| 89 | + $this->processor = new DefaultProcessor(); |
| 90 | + } |
| 91 | +} |
0 commit comments