|
2 | 2 |
|
3 | 3 | declare(strict_types=1);
|
4 | 4 |
|
5 |
| -namespace KaririCode\Logging\Tests\Logger; |
| 5 | +namespace Tests\KaririCode\Logging; |
6 | 6 |
|
7 | 7 | use KaririCode\Contract\Logging\Logger;
|
| 8 | +use KaririCode\Logging\Decorator\AsyncLogger; |
| 9 | +use KaririCode\Logging\Formatter\LoggerFormatterFactory; |
| 10 | +use KaririCode\Logging\Handler\LoggerHandlerFactory; |
| 11 | +use KaririCode\Logging\LoggerConfiguration; |
8 | 12 | use KaririCode\Logging\LoggerFactory;
|
9 |
| -use KaririCode\Logging\LogLevel; |
| 13 | +use KaririCode\Logging\LoggerManager; |
| 14 | +use KaririCode\Logging\Processor\LoggerProcessorFactory; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
10 | 16 | use PHPUnit\Framework\TestCase;
|
11 | 17 |
|
12 | 18 | class LoggerFactoryTest extends TestCase
|
13 | 19 | {
|
14 |
| - private LoggerFactory $factory; |
| 20 | + private LoggerConfiguration|MockObject $config; |
| 21 | + private LoggerHandlerFactory|MockObject $handlerFactory; |
| 22 | + private LoggerProcessorFactory|MockObject $processorFactory; |
| 23 | + private LoggerFormatterFactory|MockObject $formatterFactory; |
| 24 | + private LoggerFactory $loggerFactory; |
15 | 25 |
|
16 | 26 | protected function setUp(): void
|
17 | 27 | {
|
18 |
| - $this->factory = new LoggerFactory(); |
| 28 | + $this->config = $this->createMock(LoggerConfiguration::class); |
| 29 | + $this->handlerFactory = $this->createMock(LoggerHandlerFactory::class); |
| 30 | + $this->processorFactory = $this->createMock(LoggerProcessorFactory::class); |
| 31 | + $this->formatterFactory = $this->createMock(LoggerFormatterFactory::class); |
| 32 | + |
| 33 | + $this->loggerFactory = new LoggerFactory( |
| 34 | + $this->config, |
| 35 | + $this->handlerFactory, |
| 36 | + $this->processorFactory, |
| 37 | + $this->formatterFactory |
| 38 | + ); |
19 | 39 | }
|
20 | 40 |
|
21 | 41 | public function testCreateLogger(): void
|
22 | 42 | {
|
23 |
| - $config = [ |
24 |
| - 'path' => '/var/log/app.log', |
25 |
| - 'level' => LogLevel::DEBUG, |
26 |
| - ]; |
| 43 | + $channelName = 'test_channel'; |
| 44 | + |
| 45 | + $this->handlerFactory->expects($this->once()) |
| 46 | + ->method('createHandlers') |
| 47 | + ->with($channelName) |
| 48 | + ->willReturn([]); |
| 49 | + |
| 50 | + $this->processorFactory->expects($this->once()) |
| 51 | + ->method('createProcessors') |
| 52 | + ->with($channelName) |
| 53 | + ->willReturn([]); |
| 54 | + |
| 55 | + $this->formatterFactory->expects($this->once()) |
| 56 | + ->method('createFormatter') |
| 57 | + ->with($channelName) |
| 58 | + ->willReturn($this->createMock(\KaririCode\Contract\Logging\LogFormatter::class)); |
| 59 | + |
| 60 | + $logger = $this->loggerFactory->createLogger($channelName); |
| 61 | + |
| 62 | + $this->assertInstanceOf(Logger::class, $logger); |
| 63 | + $this->assertInstanceOf(LoggerManager::class, $logger); |
| 64 | + } |
| 65 | + |
| 66 | + public function testCreatePerformanceLogger(): void |
| 67 | + { |
| 68 | + $this->config->expects($this->once()) |
| 69 | + ->method('get') |
| 70 | + ->with('performance.threshold', 1000) |
| 71 | + ->willReturn(500); |
| 72 | + |
| 73 | + $this->handlerFactory->method('createHandlers')->willReturn([]); |
| 74 | + $this->processorFactory->method('createProcessors')->willReturn([]); |
| 75 | + $this->formatterFactory->method('createFormatter')->willReturn($this->createMock(\KaririCode\Contract\Logging\LogFormatter::class)); |
27 | 76 |
|
28 |
| - $logger = $this->factory->createLogger('test', $config); |
| 77 | + $logger = $this->loggerFactory->createPerformanceLogger(); |
29 | 78 |
|
30 | 79 | $this->assertInstanceOf(Logger::class, $logger);
|
| 80 | + $this->assertInstanceOf(LoggerManager::class, $logger); |
31 | 81 | }
|
32 | 82 |
|
33 | 83 | public function testCreateQueryLogger(): void
|
34 | 84 | {
|
35 |
| - $config = [ |
36 |
| - 'path' => '/var/log/query.log', |
37 |
| - 'level' => LogLevel::INFO, |
38 |
| - ]; |
| 85 | + $this->config->expects($this->once()) |
| 86 | + ->method('get') |
| 87 | + ->with('query.threshold', 100) |
| 88 | + ->willReturn(50); |
| 89 | + |
| 90 | + $this->handlerFactory->method('createHandlers')->willReturn([]); |
| 91 | + $this->processorFactory->method('createProcessors')->willReturn([]); |
| 92 | + $this->formatterFactory->method('createFormatter')->willReturn($this->createMock(\KaririCode\Contract\Logging\LogFormatter::class)); |
| 93 | + |
| 94 | + $logger = $this->loggerFactory->createQueryLogger(); |
| 95 | + |
| 96 | + $this->assertInstanceOf(Logger::class, $logger); |
| 97 | + $this->assertInstanceOf(LoggerManager::class, $logger); |
| 98 | + } |
| 99 | + |
| 100 | + public function testCreateErrorLogger(): void |
| 101 | + { |
| 102 | + $this->handlerFactory->method('createHandlers')->willReturn([]); |
| 103 | + $this->processorFactory->method('createProcessors')->willReturn([]); |
| 104 | + $this->formatterFactory->method('createFormatter')->willReturn($this->createMock(\KaririCode\Contract\Logging\LogFormatter::class)); |
39 | 105 |
|
40 |
| - $logger = $this->factory->createQueryLogger($config); |
| 106 | + $logger = $this->loggerFactory->createErrorLogger(); |
41 | 107 |
|
42 | 108 | $this->assertInstanceOf(Logger::class, $logger);
|
| 109 | + $this->assertInstanceOf(LoggerManager::class, $logger); |
43 | 110 | }
|
44 | 111 |
|
45 | 112 | public function testCreateAsyncLogger(): void
|
46 | 113 | {
|
47 |
| - $mockLogger = $this->createMock(Logger::class); |
48 |
| - $asyncLogger = $this->factory->createAsyncLogger($mockLogger, 10); |
| 114 | + $baseLogger = $this->createMock(Logger::class); |
| 115 | + $batchSize = 10; |
| 116 | + |
| 117 | + $asyncLogger = $this->loggerFactory->createAsyncLogger($baseLogger, $batchSize); |
| 118 | + |
| 119 | + $this->assertInstanceOf(AsyncLogger::class, $asyncLogger); |
| 120 | + } |
| 121 | + |
| 122 | + public function testCreateLoggerWithInvalidChannel(): void |
| 123 | + { |
| 124 | + $this->handlerFactory->method('createHandlers')->willThrowException(new \InvalidArgumentException('Invalid channel')); |
| 125 | + |
| 126 | + $this->expectException(\InvalidArgumentException::class); |
| 127 | + $this->expectExceptionMessage('Invalid channel'); |
49 | 128 |
|
50 |
| - $this->assertInstanceOf(Logger::class, $asyncLogger); |
| 129 | + $this->loggerFactory->createLogger('invalid_channel'); |
51 | 130 | }
|
52 | 131 | }
|
0 commit comments