|
| 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 | +} |
0 commit comments