|
2 | 2 |
|
3 | 3 | declare(strict_types=1);
|
4 | 4 |
|
5 |
| -namespace KaririCode\Logging\Tests\Handler; |
| 5 | +namespace Tests\KaririCode\Logging\Handler; |
6 | 6 |
|
| 7 | +use KaririCode\Contract\ImmutableValue; |
| 8 | +use KaririCode\Contract\Logging\LogRotator; |
7 | 9 | use KaririCode\Logging\Exception\LoggingException;
|
8 | 10 | use KaririCode\Logging\Handler\RotatingFileHandler;
|
9 | 11 | use KaririCode\Logging\LogLevel;
|
10 |
| -use KaririCode\Logging\LogRecord; |
| 12 | +use PHPUnit\Framework\MockObject\MockObject; |
11 | 13 | use PHPUnit\Framework\TestCase;
|
12 | 14 |
|
13 | 15 | class RotatingFileHandlerTest extends TestCase
|
14 | 16 | {
|
15 |
| - private string $testLogDir; |
16 |
| - private string $testLogFile; |
17 |
| - private RotatingFileHandler $rotatingFileHandler; |
| 17 | + private string $tempDir; |
| 18 | + private string $logFile; |
| 19 | + private LogRotator|MockObject $mockRotator; |
18 | 20 |
|
19 | 21 | protected function setUp(): void
|
20 | 22 | {
|
21 |
| - $this->testLogDir = sys_get_temp_dir() . '/test_rotating_logs'; |
22 |
| - $this->testLogFile = $this->testLogDir . '/test_rotating.log'; |
23 |
| - |
24 |
| - if (!is_dir($this->testLogDir)) { |
25 |
| - mkdir($this->testLogDir, 0777, true); |
26 |
| - } |
27 |
| - |
28 |
| - $this->rotatingFileHandler = new RotatingFileHandler( |
29 |
| - $this->testLogFile |
30 |
| - ); |
| 23 | + $this->tempDir = sys_get_temp_dir() . '/rotating_file_handler_test_' . uniqid(); |
| 24 | + mkdir($this->tempDir); |
| 25 | + $this->logFile = $this->tempDir . '/test.log'; |
| 26 | + $this->mockRotator = $this->createMock(LogRotator::class); |
31 | 27 | }
|
32 | 28 |
|
33 |
| - public function testHandleHappyPath(): void |
| 29 | + protected function tearDown(): void |
34 | 30 | {
|
35 |
| - $record = new LogRecord(LogLevel::INFO, 'Test message'); |
36 |
| - $this->rotatingFileHandler->handle($record); |
37 |
| - |
38 |
| - $this->assertFileExists($this->testLogFile); |
39 |
| - $this->assertStringContainsString('Test message', file_get_contents($this->testLogFile)); |
| 31 | + $this->removeDirectory($this->tempDir); |
40 | 32 | }
|
41 | 33 |
|
42 |
| - public function testRotateLogs(): void |
| 34 | + private function removeDirectory(string $dir): void |
43 | 35 | {
|
44 |
| - $this->rotatingFileHandler = new RotatingFileHandler($this->testLogFile, 2, 10); // Smaller file size |
45 |
| - |
46 |
| - for ($i = 0; $i < 5; ++$i) { |
47 |
| - $record = new LogRecord(LogLevel::INFO, str_repeat('A', 10)); |
48 |
| - $this->rotatingFileHandler->handle($record); |
| 36 | + if (!is_dir($dir)) { |
| 37 | + return; |
49 | 38 | }
|
| 39 | + $files = array_diff(scandir($dir), ['.', '..']); |
| 40 | + foreach ($files as $file) { |
| 41 | + (is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file"); |
| 42 | + } |
| 43 | + rmdir($dir); |
| 44 | + } |
50 | 45 |
|
51 |
| - $this->assertFileExists($this->testLogFile); |
52 |
| - $this->assertFileExists($this->testLogFile . '.1'); |
53 |
| - $this->assertFileExists($this->testLogFile . '.2'); |
| 46 | + public function testHandleWritesLogWhenLevelIsHighEnough(): void |
| 47 | + { |
| 48 | + $handler = new RotatingFileHandler($this->logFile, $this->mockRotator, LogLevel::INFO); |
| 49 | + $record = $this->createMock(ImmutableValue::class); |
| 50 | + $record->method('get')->willReturn([ |
| 51 | + 'level' => LogLevel::ERROR, |
| 52 | + 'message' => 'Test error message', |
| 53 | + 'context' => [], |
| 54 | + ]); |
| 55 | + |
| 56 | + $handler->handle($record); |
| 57 | + |
| 58 | + $this->assertFileExists($this->logFile); |
| 59 | + $this->assertStringContainsString('Test error message', file_get_contents($this->logFile)); |
54 | 60 | }
|
55 | 61 |
|
56 |
| - public function testHandleThrowsExceptionOnWriteFailure(): void |
| 62 | + public function testHandleDoesNotWriteLogWhenLevelIsTooLow(): void |
57 | 63 | {
|
58 |
| - $this->expectException(LoggingException::class); |
59 |
| - $this->expectExceptionMessage('Unable to create log directory: /invalid'); |
60 |
| - |
61 |
| - // Suppress warnings for this test |
62 |
| - set_error_handler(function ($errno, $errstr, $errfile, $errline) { |
63 |
| - return true; |
64 |
| - }); |
65 |
| - |
66 |
| - try { |
67 |
| - $invalidHandler = new RotatingFileHandler('/invalid/path.log'); |
68 |
| - $record = new LogRecord(LogLevel::INFO, 'Test message'); |
69 |
| - $invalidHandler->handle($record); |
70 |
| - } finally { |
71 |
| - restore_error_handler(); |
72 |
| - } |
| 64 | + $handler = new RotatingFileHandler($this->logFile, $this->mockRotator, LogLevel::ERROR); |
| 65 | + $record = $this->createMock(ImmutableValue::class); |
| 66 | + $record->method('get')->willReturn([ |
| 67 | + 'level' => LogLevel::INFO, |
| 68 | + 'message' => 'Test info message', |
| 69 | + 'context' => [], |
| 70 | + ]); |
| 71 | + |
| 72 | + $handler->handle($record); |
| 73 | + |
| 74 | + $this->assertFileDoesNotExist($this->logFile); |
73 | 75 | }
|
74 | 76 |
|
75 |
| - public function testMaxFiles(): void |
| 77 | + public function testHandleRotatesFileWhenNecessary(): void |
76 | 78 | {
|
77 |
| - $this->rotatingFileHandler = new RotatingFileHandler($this->testLogFile, 3, 10); |
| 79 | + $this->mockRotator->method('shouldRotate')->willReturn(true); |
| 80 | + $this->mockRotator->expects($this->once())->method('rotate'); |
78 | 81 |
|
79 |
| - for ($i = 0; $i < 5; ++$i) { |
80 |
| - $record = new LogRecord(LogLevel::INFO, str_repeat('A', 10)); |
81 |
| - $this->rotatingFileHandler->handle($record); |
82 |
| - } |
| 82 | + $handler = new RotatingFileHandler($this->logFile, $this->mockRotator); |
| 83 | + $record = $this->createMock(ImmutableValue::class); |
| 84 | + $record->method('get')->willReturn([ |
| 85 | + 'level' => LogLevel::INFO, |
| 86 | + 'message' => 'Test rotation message', |
| 87 | + 'context' => [], |
| 88 | + ]); |
| 89 | + |
| 90 | + $handler->handle($record); |
83 | 91 |
|
84 |
| - $this->assertFileExists($this->testLogFile); |
85 |
| - $this->assertFileExists($this->testLogFile . '.1'); |
86 |
| - $this->assertFileExists($this->testLogFile . '.2'); |
87 |
| - $this->assertFileExists($this->testLogFile . '.3'); |
88 |
| - $this->assertFileDoesNotExist($this->testLogFile . '.4'); |
| 92 | + $this->assertFileExists($this->logFile); |
| 93 | + $this->assertStringContainsString('Test rotation message', file_get_contents($this->logFile)); |
89 | 94 | }
|
90 | 95 |
|
91 |
| - public function testRespectLogLevel(): void |
| 96 | + public function testHandleThrowsLoggingExceptionOnError(): void |
92 | 97 | {
|
93 |
| - $this->rotatingFileHandler = new RotatingFileHandler($this->testLogFile, 2, 50, LogLevel::WARNING); |
| 98 | + $this->mockRotator->method('shouldRotate')->willThrowException(new \RuntimeException('Rotation error')); |
94 | 99 |
|
95 |
| - $infoRecord = new LogRecord(LogLevel::INFO, 'Info message'); |
96 |
| - $warningRecord = new LogRecord(LogLevel::WARNING, 'Warning message'); |
| 100 | + $handler = new RotatingFileHandler($this->logFile, $this->mockRotator); |
| 101 | + $record = $this->createMock(ImmutableValue::class); |
| 102 | + $record->method('get')->willReturn([ |
| 103 | + 'level' => LogLevel::INFO, |
| 104 | + 'message' => 'Test error handling', |
| 105 | + 'context' => [], |
| 106 | + ]); |
97 | 107 |
|
98 |
| - $this->rotatingFileHandler->handle($infoRecord); |
99 |
| - $this->rotatingFileHandler->handle($warningRecord); |
| 108 | + $this->expectException(LoggingException::class); |
| 109 | + $this->expectExceptionMessage('Error handling log record: Rotation error'); |
| 110 | + |
| 111 | + $handler->handle($record); |
| 112 | + } |
100 | 113 |
|
101 |
| - $this->assertFileExists($this->testLogFile); |
102 |
| - $content = file_get_contents($this->testLogFile); |
103 |
| - $this->assertStringNotContainsString('Info message', $content); |
104 |
| - $this->assertStringContainsString('Warning message', $content); |
| 114 | + public function testHandleReopensFileAfterRotation(): void |
| 115 | + { |
| 116 | + $this->mockRotator->method('shouldRotate')->willReturnOnConsecutiveCalls(true, false); |
| 117 | + $this->mockRotator->expects($this->once())->method('rotate'); |
| 118 | + |
| 119 | + $handler = new RotatingFileHandler($this->logFile, $this->mockRotator); |
| 120 | + $record = $this->createMock(ImmutableValue::class); |
| 121 | + $record->method('get')->willReturn([ |
| 122 | + 'level' => LogLevel::INFO, |
| 123 | + 'message' => 'Test message', |
| 124 | + 'context' => [], |
| 125 | + ]); |
| 126 | + |
| 127 | + // First call - should rotate and reopen |
| 128 | + $handler->handle($record); |
| 129 | + |
| 130 | + // Second call - should write to the reopened file |
| 131 | + $handler->handle($record); |
| 132 | + |
| 133 | + $this->assertFileExists($this->logFile); |
| 134 | + $logContent = file_get_contents($this->logFile); |
| 135 | + $this->assertEquals(2, substr_count($logContent, 'Test message')); |
105 | 136 | }
|
106 | 137 | }
|
0 commit comments