|
14 | 14 | namespace Micro\Plugin\Logger\Test\Unit\Business\Provider; |
15 | 15 |
|
16 | 16 | use Micro\Framework\Kernel\KernelInterface; |
| 17 | +use Micro\Plugin\Logger\Business\Factory\LoggerFactoryInterface; |
17 | 18 | use Micro\Plugin\Logger\Business\Provider\LoggerProvider; |
18 | 19 | use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface; |
| 20 | +use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface; |
| 21 | +use Micro\Plugin\Logger\Exception\LoggerAdapterAlreadyExistsException; |
| 22 | +use Micro\Plugin\Logger\Exception\LoggerAdapterNameInvalidException; |
| 23 | +use Micro\Plugin\Logger\Exception\LoggerAdapterNotRegisteredException; |
| 24 | +use Micro\Plugin\Logger\Plugin\LoggerProviderPluginInterface; |
19 | 25 | use PHPUnit\Framework\TestCase; |
20 | 26 | use Psr\Log\LoggerInterface; |
21 | 27 |
|
22 | 28 | class LoggerProviderTest extends TestCase |
23 | 29 | { |
24 | | - public function testGetLogger() |
| 30 | + private LoggerProvider $loggerProvider; |
| 31 | + |
| 32 | + private KernelInterface $kernel; |
| 33 | + |
| 34 | + private LoggerPluginConfigurationInterface $loggerPluginConfiguration; |
| 35 | + |
| 36 | + private LoggerFactoryInterface $loggerFactory; |
| 37 | + |
| 38 | + private LoggerProviderPluginInterface $loggerProviderPlugin; |
| 39 | + |
| 40 | + private LoggerInterface $logger; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $this->kernel = $this->createMock(KernelInterface::class); |
| 45 | + $this->loggerPluginConfiguration = $this->createMock(LoggerPluginConfigurationInterface::class); |
| 46 | + $this->loggerFactory = $this->createMock(LoggerFactoryInterface::class); |
| 47 | + $this->loggerProviderPlugin = $this->createMock(LoggerProviderPluginInterface::class); |
| 48 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 49 | + |
| 50 | + $this->loggerProvider = new LoggerProvider($this->kernel, $this->loggerPluginConfiguration); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetLoggerWhenOnlyOneproviderRegistered(): void |
| 54 | + { |
| 55 | + $loggerProviderTypeConfig = $this->createMock(LoggerProviderTypeConfigurationInterface::class); |
| 56 | + |
| 57 | + $this->loggerPluginConfiguration |
| 58 | + ->method('getLoggerProviderTypeConfig') |
| 59 | + ->with('loggerName') |
| 60 | + ->willReturn($loggerProviderTypeConfig); |
| 61 | + |
| 62 | + $this->kernel |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('plugins') |
| 65 | + ->with(LoggerProviderPluginInterface::class) |
| 66 | + ->willReturn([$this->loggerProviderPlugin]); |
| 67 | + |
| 68 | + $this->loggerFactory |
| 69 | + ->expects($this->once()) |
| 70 | + ->method('create') |
| 71 | + ->with($loggerProviderTypeConfig) |
| 72 | + ->willReturn($this->logger); |
| 73 | + |
| 74 | + $this->loggerProviderPlugin |
| 75 | + ->expects($this->once()) |
| 76 | + ->method('getLoggerFactory') |
| 77 | + ->willReturn($this->loggerFactory); |
| 78 | + |
| 79 | + $this->loggerProviderPlugin |
| 80 | + ->expects($this->once()) |
| 81 | + ->method('getLoggerAdapterName') |
| 82 | + ->willReturn('test_adapter'); |
| 83 | + |
| 84 | + $this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName')); |
| 85 | + // Cached |
| 86 | + $this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @dataProvider dataProviderGetLoggerWhenManyOneProviderRegistered |
| 91 | + */ |
| 92 | + public function testGetLoggerWhenManyOneProviderRegistered(bool $shouldAdaptedNotFoundException): void |
| 93 | + { |
| 94 | + $loggerProviderTypeConfig = $this->createMock(LoggerProviderTypeConfigurationInterface::class); |
| 95 | + $loggerProviderTypeConfig |
| 96 | + ->expects($this->once()) |
| 97 | + ->method('getType') |
| 98 | + ->willReturn($shouldAdaptedNotFoundException ? 'not_registered_adapter' : 'test_adapter'); |
| 99 | + |
| 100 | + if ($shouldAdaptedNotFoundException) { |
| 101 | + $this->expectException(LoggerAdapterNotRegisteredException::class); |
| 102 | + } |
| 103 | + |
| 104 | + $providerNotUsed = $this->createMock(LoggerProviderPluginInterface::class); |
| 105 | + $providerNotUsed |
| 106 | + ->expects($this->once()) |
| 107 | + ->method('getLoggerAdapterName') |
| 108 | + ->willReturn('not_used_provider'); |
| 109 | + |
| 110 | + $this->loggerPluginConfiguration |
| 111 | + ->method('getLoggerProviderTypeConfig') |
| 112 | + ->with('loggerName') |
| 113 | + ->willReturn($loggerProviderTypeConfig); |
| 114 | + |
| 115 | + $this->loggerProviderPlugin |
| 116 | + ->expects($this->once()) |
| 117 | + ->method('getLoggerAdapterName') |
| 118 | + ->willReturn('test_adapter'); |
| 119 | + |
| 120 | + $this->kernel |
| 121 | + ->expects($this->once()) |
| 122 | + ->method('plugins') |
| 123 | + ->with(LoggerProviderPluginInterface::class) |
| 124 | + ->willReturn([ |
| 125 | + $providerNotUsed, |
| 126 | + $this->loggerProviderPlugin, |
| 127 | + ]); |
| 128 | + |
| 129 | + if (!$shouldAdaptedNotFoundException) { |
| 130 | + $this->loggerFactory |
| 131 | + ->expects($this->once()) |
| 132 | + ->method('create') |
| 133 | + ->with($loggerProviderTypeConfig) |
| 134 | + ->willReturn($this->logger); |
| 135 | + |
| 136 | + $this->loggerProviderPlugin |
| 137 | + ->expects($this->once()) |
| 138 | + ->method('getLoggerFactory') |
| 139 | + ->willReturn($this->loggerFactory); |
| 140 | + } |
| 141 | + |
| 142 | + $this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName')); |
| 143 | + // Cached |
| 144 | + $this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName')); |
| 145 | + } |
| 146 | + |
| 147 | + public function dataProviderGetLoggerWhenManyOneProviderRegistered() |
| 148 | + { |
| 149 | + return [ |
| 150 | + [true], |
| 151 | + [false], |
| 152 | + ]; |
| 153 | + } |
| 154 | + |
| 155 | + public function testGetLoggerNotRegisteredAdapterThrowsException(): void |
25 | 156 | { |
26 | | - $kernel = $this->createMock(KernelInterface::class); |
27 | | - $loggerPluginConfiguration = $this->createMock(LoggerPluginConfigurationInterface::class); |
| 157 | + $this->expectException(LoggerAdapterAlreadyExistsException::class); |
28 | 158 |
|
29 | | - $loggerProvider = new LoggerProvider( |
30 | | - $kernel, |
31 | | - $loggerPluginConfiguration |
32 | | - ); |
| 159 | + $this->loggerProviderPlugin |
| 160 | + ->method('getLoggerAdapterName') |
| 161 | + ->willReturn('duplicated_provider'); |
| 162 | + |
| 163 | + $this->kernel |
| 164 | + ->expects($this->once()) |
| 165 | + ->method('plugins') |
| 166 | + ->willReturn([ |
| 167 | + $this->loggerProviderPlugin, |
| 168 | + $this->loggerProviderPlugin, |
| 169 | + ]) |
| 170 | + ; |
| 171 | + |
| 172 | + $this->loggerProvider->getLogger('loggerName'); |
| 173 | + } |
| 174 | + |
| 175 | + public function testAdapterNameInvalidThrowsException(): void |
| 176 | + { |
| 177 | + $this->expectException(LoggerAdapterNameInvalidException::class); |
| 178 | + |
| 179 | + $this->loggerProviderPlugin |
| 180 | + ->method('getLoggerAdapterName') |
| 181 | + ->willReturn(' '); |
| 182 | + |
| 183 | + $this->kernel |
| 184 | + ->expects($this->once()) |
| 185 | + ->method('plugins') |
| 186 | + ->willReturn([ |
| 187 | + $this->loggerProviderPlugin, |
| 188 | + ]) |
| 189 | + ; |
| 190 | + |
| 191 | + $this->loggerProvider->getLogger('loggerName'); |
| 192 | + } |
| 193 | + |
| 194 | + public function testAdapterAlreadyRegisteredThrowsException(): void |
| 195 | + { |
| 196 | + $this->expectException(LoggerAdapterNotRegisteredException::class); |
| 197 | + $this->kernel |
| 198 | + ->expects($this->once()) |
| 199 | + ->method('plugins') |
| 200 | + ->willReturn([ |
| 201 | + ]) |
| 202 | + ; |
33 | 203 |
|
34 | | - $this->assertInstanceOf(LoggerInterface::class, $loggerProvider->getLogger('default')); |
| 204 | + $this->loggerProvider->getLogger('loggerName'); |
35 | 205 | } |
36 | 206 | } |
0 commit comments