Skip to content

Commit 18ba513

Browse files
committed
feat: add tests for introspection processor behavior
- Added test for processing of non-trackable log levels, ensuring no modification of the log record. - Added test for processing of trackable log levels, verifying introspection data is added to the context. - Added test to validate respect for custom stack depth in introspection. - Added test to ensure original context is preserved when introspection is applied. - Added test for handling invalid trace depth in introspection with reflection.
1 parent 982697a commit 18ba513

File tree

1 file changed

+88
-7
lines changed

1 file changed

+88
-7
lines changed

tests/Processor/IntrospectionProcessorTest.php

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,111 @@
22

33
declare(strict_types=1);
44

5-
namespace KaririCode\Logging\Tests\Processor;
5+
namespace Tests\KaririCode\Logging\Processor;
66

7+
use KaririCode\Contract\ImmutableValue;
78
use KaririCode\Logging\LogLevel;
89
use KaririCode\Logging\LogRecord;
910
use KaririCode\Logging\Processor\IntrospectionProcessor;
1011
use PHPUnit\Framework\TestCase;
1112

12-
class IntrospectionProcessorTest extends TestCase
13+
final class IntrospectionProcessorTest extends TestCase
1314
{
14-
private IntrospectionProcessor $introspectionProcessor;
15+
private IntrospectionProcessor $processor;
1516

1617
protected function setUp(): void
1718
{
18-
$this->introspectionProcessor = new IntrospectionProcessor();
19+
$this->processor = new IntrospectionProcessor();
1920
}
2021

21-
public function testProcessHappyPath(): void
22+
/**
23+
* @dataProvider provideNonTrackableLevels
24+
*/
25+
public function testProcessDoesNotModifyRecordForNonTrackableLevels(LogLevel $level): void
2226
{
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+
}
2531

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);
2640
$this->assertArrayHasKey('file', $processedRecord->context);
2741
$this->assertArrayHasKey('line', $processedRecord->context);
2842
$this->assertArrayHasKey('class', $processedRecord->context);
2943
$this->assertArrayHasKey('function', $processedRecord->context);
3044
}
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+
}
31112
}

0 commit comments

Comments
 (0)