|
2 | 2 |
|
3 | 3 | declare(strict_types=1);
|
4 | 4 |
|
5 |
| -namespace KaririCode\Logging\Tests\Processor; |
| 5 | +namespace Tests\KaririCode\Logging\Processor; |
6 | 6 |
|
| 7 | +use KaririCode\Contract\ImmutableValue; |
7 | 8 | use KaririCode\Logging\LogLevel;
|
8 | 9 | use KaririCode\Logging\LogRecord;
|
9 | 10 | use KaririCode\Logging\Processor\IntrospectionProcessor;
|
10 | 11 | use PHPUnit\Framework\TestCase;
|
11 | 12 |
|
12 |
| -class IntrospectionProcessorTest extends TestCase |
| 13 | +final class IntrospectionProcessorTest extends TestCase |
13 | 14 | {
|
14 |
| - private IntrospectionProcessor $introspectionProcessor; |
| 15 | + private IntrospectionProcessor $processor; |
15 | 16 |
|
16 | 17 | protected function setUp(): void
|
17 | 18 | {
|
18 |
| - $this->introspectionProcessor = new IntrospectionProcessor(); |
| 19 | + $this->processor = new IntrospectionProcessor(); |
19 | 20 | }
|
20 | 21 |
|
21 |
| - public function testProcessHappyPath(): void |
| 22 | + /** |
| 23 | + * @dataProvider provideNonTrackableLevels |
| 24 | + */ |
| 25 | + public function testProcessDoesNotModifyRecordForNonTrackableLevels(LogLevel $level): void |
22 | 26 | {
|
23 |
| - $record = new LogRecord(LogLevel::INFO, 'Test message'); |
24 |
| - $processedRecord = $this->introspectionProcessor->process($record); |
| 27 | + $record = $this->createMockRecord($level); |
| 28 | + $processedRecord = $this->processor->process($record); |
| 29 | + $this->assertSame($record, $processedRecord); |
| 30 | + } |
25 | 31 |
|
| 32 | + /** |
| 33 | + * @dataProvider provideTrackableLevels |
| 34 | + */ |
| 35 | + public function testProcessAddsIntrospectionDataForTrackableLevels(LogLevel $level): void |
| 36 | + { |
| 37 | + $record = $this->createMockRecord($level); |
| 38 | + $processedRecord = $this->processor->process($record); |
| 39 | + $this->assertInstanceOf(LogRecord::class, $processedRecord); |
26 | 40 | $this->assertArrayHasKey('file', $processedRecord->context);
|
27 | 41 | $this->assertArrayHasKey('line', $processedRecord->context);
|
28 | 42 | $this->assertArrayHasKey('class', $processedRecord->context);
|
29 | 43 | $this->assertArrayHasKey('function', $processedRecord->context);
|
30 | 44 | }
|
| 45 | + |
| 46 | + public function testProcessRespectsStackDepth(): void |
| 47 | + { |
| 48 | + $customDepthProcessor = new IntrospectionProcessor(2); |
| 49 | + $record = $this->createMockRecord(LogLevel::ERROR); |
| 50 | + $processedRecord = $customDepthProcessor->process($record); |
| 51 | + $this->assertInstanceOf(LogRecord::class, $processedRecord); |
| 52 | + $this->assertArrayHasKey('file', $processedRecord->context); |
| 53 | + } |
| 54 | + |
| 55 | + public function testProcessPreservesOriginalContext(): void |
| 56 | + { |
| 57 | + $originalContext = ['key' => 'value']; |
| 58 | + $record = $this->createMockRecord(LogLevel::ERROR, $originalContext); |
| 59 | + $processedRecord = $this->processor->process($record); |
| 60 | + $this->assertInstanceOf(LogRecord::class, $processedRecord); |
| 61 | + $this->assertArrayHasKey('key', $processedRecord->context); |
| 62 | + $this->assertEquals('value', $processedRecord->context['key']); |
| 63 | + } |
| 64 | + |
| 65 | + public function testGetMaxDepthHandlesInvalidTraceDepth(): void |
| 66 | + { |
| 67 | + $reflection = new \ReflectionClass(IntrospectionProcessor::class); |
| 68 | + $getMaxDepthMethod = $reflection->getMethod('getMaxDepth'); |
| 69 | + $getMaxDepthMethod->setAccessible(true); |
| 70 | + $deepProcessor = new IntrospectionProcessor(1000); |
| 71 | + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 72 | + $maxDepth = $getMaxDepthMethod->invoke($deepProcessor, $trace); |
| 73 | + $this->assertLessThanOrEqual(count($trace) - 1, $maxDepth); |
| 74 | + $this->assertGreaterThan(0, $maxDepth); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return array<array{0: LogLevel}> |
| 79 | + */ |
| 80 | + public static function provideNonTrackableLevels(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + [LogLevel::DEBUG], |
| 84 | + [LogLevel::INFO], |
| 85 | + [LogLevel::NOTICE], |
| 86 | + [LogLevel::WARNING], |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return array<array{0: LogLevel}> |
| 92 | + */ |
| 93 | + public static function provideTrackableLevels(): array |
| 94 | + { |
| 95 | + return [ |
| 96 | + [LogLevel::ERROR], |
| 97 | + [LogLevel::CRITICAL], |
| 98 | + [LogLevel::ALERT], |
| 99 | + [LogLevel::EMERGENCY], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + private function createMockRecord(LogLevel $level, array $context = []): ImmutableValue |
| 104 | + { |
| 105 | + return new LogRecord( |
| 106 | + $level, |
| 107 | + 'Test message', |
| 108 | + $context, |
| 109 | + new \DateTimeImmutable() |
| 110 | + ); |
| 111 | + } |
31 | 112 | }
|
0 commit comments