Skip to content

Commit fa62914

Browse files
committed
Refactor LoggerHandlerFactory and add new test cases
- Improve exception handling and error reporting in LoggerHandlerFactory - Add unit tests for LoggerHandlerFactory and LoggerFormatterFactory - Add tests for AssetPublisher and ComposerScripts utilities - Include test traits under tests/Trait/ - Update .gitignore to exclude test/build artifacts
1 parent 39235ab commit fa62914

9 files changed

+690
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ tmp/
6060
.vscode/launch.json
6161
.vscode/extensions.json
6262
tests/lista_de_arquivos.php
63-
lista_de_arquivos.txt
63+
tests/lista_de_arquivos_test.php
64+
lista_de_arquivos.txt
65+
lista_de_arquivos_tests.txt

src/Handler/LoggerHandlerFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use KaririCode\Contract\Logging\LogHandler;
88
use KaririCode\Logging\Contract\LoggerConfigurableFactory;
9+
use KaririCode\Logging\Exception\LoggingException;
910
use KaririCode\Logging\LoggerConfiguration;
1011
use KaririCode\Logging\LogLevel;
1112
use KaririCode\Logging\Util\ReflectionFactoryTrait;
@@ -32,6 +33,11 @@ public function initializeFromConfiguration(LoggerConfiguration $config): void
3233
public function createHandlers(string $handlerName): array
3334
{
3435
$handlersConfig = $this->getHandlersConfig($handlerName);
36+
37+
if (empty($handlersConfig)) {
38+
throw new LoggingException("No handlers configured for channel: $handlerName");
39+
}
40+
3541
$handlers = [];
3642
foreach ($handlersConfig as $key => $value) {
3743
[$handlerName, $handlerOptions] = $this->extractMergedConfig($key, $value);
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Logging\Tests\Formatter;
6+
7+
use KaririCode\Logging\Formatter\JsonFormatter;
8+
use KaririCode\Logging\Formatter\LineFormatter;
9+
use KaririCode\Logging\Formatter\LoggerFormatterFactory;
10+
use KaririCode\Logging\LoggerConfiguration;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class LoggerFormatterFactoryTest extends TestCase
14+
{
15+
private LoggerFormatterFactory $factory;
16+
private LoggerConfiguration|\PHPUnit\Framework\MockObject\MockObject $configMock;
17+
18+
protected function setUp(): void
19+
{
20+
$this->configMock = $this->createMock(LoggerConfiguration::class);
21+
$this->factory = new LoggerFormatterFactory();
22+
}
23+
24+
public function testInitializeFromConfiguration(): void
25+
{
26+
$config = [
27+
'formatters' => [
28+
'line' => LineFormatter::class,
29+
'json' => JsonFormatter::class,
30+
'custom' => 'CustomFormatter',
31+
],
32+
'channels' => [
33+
'default' => ['formatter' => 'line'],
34+
'api' => ['formatter' => ['json' => ['with' => ['prettyPrint' => true]]]],
35+
],
36+
];
37+
38+
$this->configMock->expects($this->exactly(2))
39+
->method('get')
40+
->willReturnMap([
41+
['formatters', ['line' => LineFormatter::class, 'json' => JsonFormatter::class], $config['formatters']],
42+
['channels', [], $config['channels']],
43+
]);
44+
45+
$this->factory->initializeFromConfiguration($this->configMock);
46+
47+
$this->assertInstanceOf(LoggerFormatterFactory::class, $this->factory);
48+
}
49+
50+
public function testCreateFormatterWithDefaultFormatter(): void
51+
{
52+
$config = [
53+
'formatters' => [
54+
'line' => LineFormatter::class,
55+
],
56+
'channels' => [
57+
'default' => ['formatter' => 'line'],
58+
],
59+
];
60+
61+
$this->configMock->expects($this->exactly(2))
62+
->method('get')
63+
->willReturnMap([
64+
['formatters', ['line' => LineFormatter::class, 'json' => JsonFormatter::class], $config['formatters']],
65+
['channels', [], $config['channels']],
66+
]);
67+
68+
$this->factory->initializeFromConfiguration($this->configMock);
69+
70+
$formatter = $this->factory->createFormatter('default');
71+
$this->assertInstanceOf(LineFormatter::class, $formatter);
72+
}
73+
74+
public function testCreateFormatterWithCustomFormatter(): void
75+
{
76+
$config = [
77+
'formatters' => [
78+
'json' => JsonFormatter::class,
79+
],
80+
'channels' => [
81+
'api' => ['formatter' => ['json' => ['with' => ['prettyPrint' => true]]]],
82+
],
83+
];
84+
85+
$this->configMock->expects($this->exactly(2))
86+
->method('get')
87+
->willReturnMap([
88+
['formatters', ['line' => LineFormatter::class, 'json' => JsonFormatter::class], $config['formatters']],
89+
['channels', [], $config['channels']],
90+
]);
91+
92+
$this->factory->initializeFromConfiguration($this->configMock);
93+
94+
$formatter = $this->factory->createFormatter('api');
95+
$this->assertInstanceOf(JsonFormatter::class, $formatter);
96+
}
97+
98+
public function testCreateFormatterWithNonExistentFormatter(): void
99+
{
100+
$config = [
101+
'formatters' => [
102+
'line' => LineFormatter::class,
103+
'json' => JsonFormatter::class,
104+
],
105+
'channels' => [
106+
'unknown' => ['formatter' => 'non_existent'],
107+
],
108+
];
109+
110+
$this->configMock->expects($this->exactly(2))
111+
->method('get')
112+
->willReturnMap([
113+
['formatters', ['line' => LineFormatter::class, 'json' => JsonFormatter::class], $config['formatters']],
114+
['channels', [], $config['channels']],
115+
]);
116+
117+
$this->factory->initializeFromConfiguration($this->configMock);
118+
119+
$this->expectException(\KaririCode\Logging\Exception\InvalidConfigurationException::class);
120+
$this->expectExceptionMessage('Configuration not found for key: non_existent');
121+
122+
$this->factory->createFormatter('unknown');
123+
}
124+
125+
public function testCreateFormatterWithFallbackToDefaultFormatter(): void
126+
{
127+
$config = [
128+
'formatters' => [
129+
'line' => LineFormatter::class,
130+
],
131+
'channels' => [
132+
'fallback' => [],
133+
],
134+
];
135+
136+
$this->configMock->expects($this->exactly(2))
137+
->method('get')
138+
->willReturnMap([
139+
['formatters', ['line' => LineFormatter::class, 'json' => JsonFormatter::class], $config['formatters']],
140+
['channels', [], $config['channels']],
141+
]);
142+
143+
$this->factory->initializeFromConfiguration($this->configMock);
144+
145+
$formatter = $this->factory->createFormatter('fallback');
146+
$this->assertInstanceOf(LineFormatter::class, $formatter);
147+
}
148+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\KaririCode\Logging\Handler;
6+
7+
use KaririCode\Logging\Exception\LoggingException;
8+
use KaririCode\Logging\Handler\ConsoleHandler;
9+
use KaririCode\Logging\Handler\FileHandler;
10+
use KaririCode\Logging\Handler\LoggerHandlerFactory;
11+
use KaririCode\Logging\Handler\SlackHandler;
12+
use KaririCode\Logging\LoggerConfiguration;
13+
use KaririCode\Logging\Util\SlackClient;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class LoggerHandlerFactoryTest extends TestCase
17+
{
18+
private LoggerHandlerFactory $loggerHandlerFactory;
19+
private LoggerConfiguration $config;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$logFilePath = sys_get_temp_dir() . '/file.log';
26+
27+
$this->config = new LoggerConfiguration();
28+
$this->config->set('handlers', [
29+
'file' => [
30+
'class' => FileHandler::class,
31+
'with' => [
32+
'filePath' => $logFilePath,
33+
],
34+
],
35+
'console' => ConsoleHandler::class,
36+
'slack' => SlackHandler::class,
37+
]);
38+
39+
$this->config->set('channels', [
40+
'default' => [
41+
'handlers' => ['file', 'console'],
42+
],
43+
'slack' => [
44+
'handlers' => ['slack'],
45+
],
46+
]);
47+
48+
$this->loggerHandlerFactory = new LoggerHandlerFactory();
49+
$this->loggerHandlerFactory->initializeFromConfiguration($this->config);
50+
}
51+
52+
public function testInitializeFromConfiguration(): void
53+
{
54+
// Valida se a configuração foi corretamente inicializada
55+
$reflection = new \ReflectionClass($this->loggerHandlerFactory);
56+
$handlerMap = $reflection->getProperty('handlerMap');
57+
$handlerMap->setAccessible(true);
58+
59+
$this->assertIsArray($handlerMap->getValue($this->loggerHandlerFactory));
60+
$this->assertArrayHasKey('file', $handlerMap->getValue($this->loggerHandlerFactory));
61+
$this->assertArrayHasKey('console', $handlerMap->getValue($this->loggerHandlerFactory));
62+
$this->assertArrayHasKey('slack', $handlerMap->getValue($this->loggerHandlerFactory));
63+
}
64+
65+
public function testCreateHandlersForDefaultChannel(): void
66+
{
67+
// Criando handlers para o canal 'default'
68+
$handlers = $this->loggerHandlerFactory->createHandlers('default');
69+
70+
// Verifica se os handlers são instâncias das classes corretas
71+
$this->assertIsArray($handlers);
72+
$this->assertCount(2, $handlers);
73+
$this->assertInstanceOf(FileHandler::class, $handlers[0]);
74+
$this->assertInstanceOf(ConsoleHandler::class, $handlers[1]);
75+
}
76+
77+
public function testCreateHandlersForSlackChannel(): void
78+
{
79+
// Mockando o SlackClient
80+
$mockSlackClient = $this->createMock(SlackClient::class);
81+
82+
// Configurando o LoggerConfiguration
83+
$config = new LoggerConfiguration();
84+
$config->set('handlers', [
85+
'slack' => [
86+
'class' => SlackHandler::class,
87+
'with' => [
88+
'slackClient' => $mockSlackClient, // Passando o mock de SlackClient
89+
],
90+
],
91+
]);
92+
93+
$config->set('channels', [
94+
'slack' => [
95+
'handlers' => ['slack'],
96+
],
97+
]);
98+
99+
$loggerHandlerFactory = new LoggerHandlerFactory();
100+
$loggerHandlerFactory->initializeFromConfiguration($config);
101+
102+
$handlers = $loggerHandlerFactory->createHandlers('slack');
103+
104+
$this->assertNotEmpty($handlers);
105+
$this->assertInstanceOf(SlackHandler::class, $handlers[0]);
106+
}
107+
108+
public function testCreateHandlersForNonExistingChannel(): void
109+
{
110+
$this->expectException(LoggingException::class);
111+
$this->expectExceptionMessage('No handlers configured for channel: non_existing');
112+
113+
$config = new LoggerConfiguration();
114+
$config->set('channels', [
115+
'existing_channel' => [
116+
'handlers' => ['file'],
117+
],
118+
]);
119+
120+
$loggerHandlerFactory = new LoggerHandlerFactory();
121+
$loggerHandlerFactory->initializeFromConfiguration($config);
122+
123+
$loggerHandlerFactory->createHandlers('non_existing');
124+
}
125+
126+
public function testCreateHandlersWithInvalidHandler(): void
127+
{
128+
$this->config->set('handlers', [
129+
'invalid' => 'NonExistentHandlerClass',
130+
]);
131+
$this->loggerHandlerFactory->initializeFromConfiguration($this->config);
132+
133+
$this->expectException(LoggingException::class);
134+
$this->expectExceptionMessage('No handlers configured for channel: invalid');
135+
136+
$this->loggerHandlerFactory->createHandlers('invalid');
137+
}
138+
}

tests/Processor/AbstractProcessorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public function testProcessMethod(): void
8181
}
8282
}
8383

84-
85-
8684
final class ConcreteTestProcessor extends AbstractProcessor
8785
{
8886
public function process(ImmutableValue $record): ImmutableValue

0 commit comments

Comments
 (0)