19declare(strict_types=1);
23use PHPUnit\Framework\TestCase;
26use Monolog\Logger as MonologLogger;
30use Monolog\Handler\HandlerInterface as MonologHandlerInterface;
36 $basic_config = $this->createMock(BasicConfigInterface::class);
37 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
38 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
39 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
42 ->expects($this->never())
43 ->method($this->anything());
45 ->expects($this->never())
46 ->method($this->anything());
48 ->expects($this->never())
49 ->method($this->anything());
51 ->expects($this->never())
52 ->method($this->anything());
54 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
55 $logger = $lazy_factory->getLazyGhost(
'name', $level_fetcher);
60 $expected_name =
'name';
61 $expected_log_level = ILIASLogLevel::EMERGENCY;
62 $expected_handler = $this->createStub(MonologHandlerInterface::class);
64 $basic_config = $this->createMock(BasicConfigInterface::class);
65 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
66 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
67 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
70 ->method(
'isLoggingEnabled')
73 ->method(
'fetchLevel')
74 ->willReturn($expected_log_level);
77 ->expects($this->atLeastOnce())
79 ->with($expected_name, $expected_handler)
80 ->willReturn($this->createStub(MonologLogger::class));
82 ->expects($this->atLeastOnce())
84 ->with($expected_name)
87 ->expects($this->atLeastOnce())
88 ->method(
'getHandler')
89 ->with($expected_name, $expected_log_level)
90 ->willReturn($expected_handler);
92 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
93 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
100 $basic_config = $this->createMock(BasicConfigInterface::class);
101 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
102 $level_fetcher_1 = $this->createMock(LevelFetcherInterface::class);
103 $level_fetcher_2 = $this->createMock(LevelFetcherInterface::class);
104 $level_fetcher_3 = $this->createMock(LevelFetcherInterface::class);
105 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
108 ->method(
'isLoggingEnabled')
112 ->willReturn($this->createStub(MonologLogger::class));
114 ->method(
'fetchLevel')
115 ->willReturn(ILIASLogLevel::INFO);
117 ->method(
'fetchLevel')
118 ->willReturn(ILIASLogLevel::INFO);
120 ->method(
'appliesTo')
123 ->method(
'getHandler')
124 ->willReturn($this->createStub(MonologHandlerInterface::class));
127 ->expects($this->never())
128 ->method($this->anything());
130 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
131 $logger_1 = $lazy_factory->getLazyGhost(
'name', $level_fetcher_1);
132 $logger_2 = $lazy_factory->getLazyGhost(
'another name', $level_fetcher_2);
133 $logger_3 = $lazy_factory->getLazyGhost(
'name', $level_fetcher_3);
136 $logger_1->info(
'test');
137 $logger_2->info(
'test');
138 $logger_3->info(
'test');
140 $this->assertSame($logger_1, $logger_3,
'There should only be one logger per component ID.');
141 $this->assertNotSame($logger_1, $logger_2,
'Loggers with different component ID should be different.');
146 $expected_name =
'name';
148 $basic_config = $this->createStub(BasicConfigInterface::class);
149 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
150 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
151 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
154 ->method(
'isLoggingEnabled')
158 ->expects($this->atLeastOnce())
159 ->method(
'nullLogger')
160 ->with($expected_name)
161 ->willReturn($this->createStub(MonologLogger::class));
163 ->expects($this->never())
166 ->expects($this->never())
167 ->method($this->anything());
169 ->expects($this->never())
170 ->method($this->anything());
172 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
173 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
180 $expected_name =
'name';
182 $basic_config = $this->createStub(BasicConfigInterface::class);
183 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
184 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
185 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
188 ->method(
'isLoggingEnabled')
191 ->method(
'fetchLevel')
192 ->willReturn(ILIASLogLevel::OFF);
195 ->expects($this->atLeastOnce())
196 ->method(
'nullLogger')
197 ->with($expected_name)
198 ->willReturn($this->createStub(MonologLogger::class));
200 ->expects($this->never())
203 ->expects($this->never())
204 ->method($this->anything());
206 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
207 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
214 $expected_name =
'name';
216 $basic_config = $this->createMock(BasicConfigInterface::class);
217 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
218 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
219 $handler_provider = $this->createMock(MonologHandlerProviderInterface::class);
222 ->method(
'isLoggingEnabled')
225 ->method(
'fetchLevel')
226 ->willReturn(ILIASLogLevel::EMERGENCY);
228 ->method(
'appliesTo')
232 ->expects($this->atLeastOnce())
234 ->with($expected_name)
235 ->willReturn($this->createStub(MonologLogger::class));
237 ->expects($this->never())
238 ->method(
'getHandler');
240 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
241 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
248 $expected_name_1 =
'name 1';
249 $expected_name_2 =
'name 2';
251 $basic_config = $this->createMock(BasicConfigInterface::class);
252 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
253 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
254 $handler_provider_1 = $this->createMock(MonologHandlerProviderInterface::class);
255 $handler_1 = $this->createStub(MonologHandlerInterface::class);
256 $handler_provider_2 = $this->createMock(MonologHandlerProviderInterface::class);
257 $handler_2 = $this->createStub(MonologHandlerInterface::class);
260 ->method(
'isLoggingEnabled')
263 ->method(
'fetchLevel')
264 ->willReturn(ILIASLogLevel::EMERGENCY);
266 ->method(
'appliesTo')
267 ->willReturnMap([[$expected_name_1,
true], [$expected_name_2,
false]]);
269 ->method(
'getHandler')
270 ->willReturn($handler_1);
272 ->method(
'appliesTo')
273 ->willReturnMap([[$expected_name_1,
false], [$expected_name_2,
true]]);
275 ->method(
'getHandler')
276 ->willReturn($handler_2);
279 ->expects($this->atLeast(2))
281 ->withParameterSetsInOrder([$expected_name_1, $handler_1], [$expected_name_2, $handler_2])
282 ->willReturn($this->createStub(MonologLogger::class));
284 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider_1, $handler_provider_2]);
285 $logger_1 = $lazy_factory->getLazyGhost($expected_name_1, $level_fetcher);
286 $logger_2 = $lazy_factory->getLazyGhost($expected_name_2, $level_fetcher);
288 $logger_1->info(
'test');
289 $logger_2->info(
'test');
294 $expected_name_1 =
'name 1';
295 $expected_name_2 =
'name 2';
297 $basic_config = $this->createMock(BasicConfigInterface::class);
298 $monolog_factory = $this->createMock(MonologFactoryInterface::class);
299 $level_fetcher = $this->createMock(LevelFetcherInterface::class);
300 $handler_provider_1 = $this->createMock(MonologHandlerProviderInterface::class);
301 $handler_1 = $this->createStub(MonologHandlerInterface::class);
302 $handler_provider_2 = $this->createMock(MonologHandlerProviderInterface::class);
303 $handler_2 = $this->createStub(MonologHandlerInterface::class);
306 ->method(
'isLoggingEnabled')
309 ->method(
'fetchLevel')
310 ->willReturn(ILIASLogLevel::EMERGENCY);
312 ->method(
'appliesTo')
315 ->method(
'getHandler')
316 ->willReturn($handler_1);
318 ->method(
'appliesTo')
321 ->method(
'getHandler')
322 ->willReturn($handler_2);
325 ->expects($this->atLeastOnce())
327 ->with($expected_name_1, $handler_1, $handler_2)
328 ->willReturn($this->createStub(MonologLogger::class));
330 $lazy_factory =
new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider_1, $handler_provider_2]);
331 $logger = $lazy_factory->getLazyGhost($expected_name_1, $level_fetcher);
testGetLazyGhostWhenLoggingIsDisabled()
testGetLazyGhostIsInitializedAfterCallWithCorrectParameters()
testGetLazyGhostWhenLogLevelIsOff()
testGetLazyGhostWhenHandlersOnlyApplyToSomeLoggers()
testGetLazyGhostWhenHandlerIsNotApplicable()
testGetLazyGhostWithMultipleHandlers()
testGetLazyGhostIsCached()
This exists to encapsulate reading the level of a logger from the config, to be able to delay it as m...