Skip to content

Commit f06043b

Browse files
committed
Fix and enhance unit tests for logging classes
- Update 'LoggerFactoryTest.php' to correct errors and ensure compatibility with the current configuration. - Modify 'LoggerRegistryTest.php' to improve test coverage and verify proper logger registration. - Complete and fix 'LoggerServiceProviderTest.php', resolving issues related to undefined methods and type incompatibilities, ensuring all tests pass.
1 parent 4a6242f commit f06043b

File tree

3 files changed

+241
-198
lines changed

3 files changed

+241
-198
lines changed

tests/Logger/LoggerFactoryTest.php

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,130 @@
22

33
declare(strict_types=1);
44

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

77
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;
812
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;
1016
use PHPUnit\Framework\TestCase;
1117

1218
class LoggerFactoryTest extends TestCase
1319
{
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;
1525

1626
protected function setUp(): void
1727
{
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+
);
1939
}
2040

2141
public function testCreateLogger(): void
2242
{
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));
2776

28-
$logger = $this->factory->createLogger('test', $config);
77+
$logger = $this->loggerFactory->createPerformanceLogger();
2978

3079
$this->assertInstanceOf(Logger::class, $logger);
80+
$this->assertInstanceOf(LoggerManager::class, $logger);
3181
}
3282

3383
public function testCreateQueryLogger(): void
3484
{
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));
39105

40-
$logger = $this->factory->createQueryLogger($config);
106+
$logger = $this->loggerFactory->createErrorLogger();
41107

42108
$this->assertInstanceOf(Logger::class, $logger);
109+
$this->assertInstanceOf(LoggerManager::class, $logger);
43110
}
44111

45112
public function testCreateAsyncLogger(): void
46113
{
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');
49128

50-
$this->assertInstanceOf(Logger::class, $asyncLogger);
129+
$this->loggerFactory->createLogger('invalid_channel');
51130
}
52131
}

tests/Logger/LoggerRegistryTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,51 @@
99
use KaririCode\Logging\LoggerRegistry;
1010
use PHPUnit\Framework\TestCase;
1111

12-
class LoggerRegistryTest extends TestCase
12+
final class LoggerRegistryTest extends TestCase
1313
{
1414
private LoggerRegistry $registry;
15+
protected Logger $mockLogger;
1516

1617
protected function setUp(): void
1718
{
1819
$this->registry = new LoggerRegistry();
20+
$this->mockLogger = $this->createMock(Logger::class);
1921
}
2022

2123
public function testAddAndGetLogger(): void
2224
{
23-
$mockLogger = $this->createMock(Logger::class);
24-
$this->registry->addLogger('test', $mockLogger);
25+
$this->registry->addLogger('test', $this->mockLogger);
2526

26-
$this->assertSame($mockLogger, $this->registry->getLogger('test'));
27+
$this->assertSame($this->mockLogger, $this->registry->getLogger('test'));
2728
}
2829

2930
public function testGetNonexistentLogger(): void
3031
{
3132
$this->expectException(LoggerNotFoundException::class);
32-
$this->expectExceptionMessage('Logger with name "nonexistent" not found.'); // Corrigido para usar aspas duplas
33+
$this->expectExceptionMessage('Logger with name "nonexistent" not found.');
3334

3435
$this->registry->getLogger('nonexistent');
3536
}
3637

3738
public function testRemoveLogger(): void
3839
{
39-
$mockLogger = $this->createMock(Logger::class);
40-
$this->registry->addLogger('test', $mockLogger);
40+
$this->registry->addLogger('test', $this->mockLogger);
4141
$this->registry->removeLogger('test');
4242

4343
$this->expectException(LoggerNotFoundException::class);
44-
$this->expectExceptionMessage('Logger with name "test" not found.'); // Corrigido para usar aspas duplas
44+
$this->expectExceptionMessage('Logger with name "test" not found.');
4545

4646
$this->registry->getLogger('test');
4747
}
4848

4949
public function testCannotAddLoggerWithSameNameTwice(): void
5050
{
51-
$mockLogger = $this->createMock(Logger::class);
52-
$this->registry->addLogger('test', $mockLogger);
51+
$this->registry->addLogger('test', $this->mockLogger);
5352

5453
$this->expectException(\InvalidArgumentException::class);
5554
$this->expectExceptionMessage('Logger with name "test" already exists.');
5655

57-
$this->registry->addLogger('test', $mockLogger);
56+
$this->registry->addLogger('test', $this->mockLogger);
5857
}
5958

6059
public function testRemoveNonexistentLogger(): void

0 commit comments

Comments
 (0)