ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LazyInternalFactoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Logging\Logger;
22
23use PHPUnit\Framework\TestCase;
24use ILIAS\Logging\Config\Basic\ConfigInterface as BasicConfigInterface;
25use ILIAS\Logging\Logger\Monolog\FactoryInterface as MonologFactoryInterface;
26use Monolog\Logger as MonologLogger;
29use ILIAS\Logging\Logger\Monolog\Handler\ProviderInterface as MonologHandlerProviderInterface;
30use Monolog\Handler\HandlerInterface as MonologHandlerInterface;
31
32class LazyInternalFactoryTest extends TestCase
33{
34 public function testGetLazyGhostIsLazy(): void
35 {
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);
40
41 $monolog_factory
42 ->expects($this->never())
43 ->method($this->anything());
44 $basic_config
45 ->expects($this->never())
46 ->method($this->anything());
47 $level_fetcher
48 ->expects($this->never())
49 ->method($this->anything());
50 $handler_provider
51 ->expects($this->never())
52 ->method($this->anything());
53
54 $lazy_factory = new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
55 $logger = $lazy_factory->getLazyGhost('name', $level_fetcher);
56 }
57
59 {
60 $expected_name = 'name';
61 $expected_log_level = ILIASLogLevel::EMERGENCY;
62 $expected_handler = $this->createStub(MonologHandlerInterface::class);
63
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);
68
69 $basic_config
70 ->method('isLoggingEnabled')
71 ->willReturn(true);
72 $level_fetcher
73 ->method('fetchLevel')
74 ->willReturn($expected_log_level);
75
76 $monolog_factory
77 ->expects($this->atLeastOnce())
78 ->method('logger')
79 ->with($expected_name, $expected_handler)
80 ->willReturn($this->createStub(MonologLogger::class));
81 $handler_provider
82 ->expects($this->atLeastOnce())
83 ->method('appliesTo')
84 ->with($expected_name)
85 ->willReturn(true);
86 $handler_provider
87 ->expects($this->atLeastOnce())
88 ->method('getHandler')
89 ->with($expected_name, $expected_log_level)
90 ->willReturn($expected_handler);
91
92 $lazy_factory = new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
93 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
94 // actually initialize the logger
95 $logger->info('test');
96 }
97
98 public function testGetLazyGhostIsCached(): void
99 {
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);
106
107 $basic_config
108 ->method('isLoggingEnabled')
109 ->willReturn(true);
110 $monolog_factory
111 ->method('logger')
112 ->willReturn($this->createStub(MonologLogger::class));
113 $level_fetcher_1
114 ->method('fetchLevel')
115 ->willReturn(ILIASLogLevel::INFO);
116 $level_fetcher_2
117 ->method('fetchLevel')
118 ->willReturn(ILIASLogLevel::INFO);
119 $handler_provider
120 ->method('appliesTo')
121 ->willReturn(true);
122 $handler_provider
123 ->method('getHandler')
124 ->willReturn($this->createStub(MonologHandlerInterface::class));
125
126 $level_fetcher_3
127 ->expects($this->never())
128 ->method($this->anything());
129
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);
134
135 // actually initialize the loggers
136 $logger_1->info('test');
137 $logger_2->info('test');
138 $logger_3->info('test');
139
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.');
142 }
143
145 {
146 $expected_name = 'name';
147
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);
152
153 $basic_config
154 ->method('isLoggingEnabled')
155 ->willReturn(false);
156
157 $monolog_factory
158 ->expects($this->atLeastOnce())
159 ->method('nullLogger')
160 ->with($expected_name)
161 ->willReturn($this->createStub(MonologLogger::class));
162 $monolog_factory
163 ->expects($this->never())
164 ->method('logger');
165 $level_fetcher
166 ->expects($this->never())
167 ->method($this->anything());
168 $handler_provider
169 ->expects($this->never())
170 ->method($this->anything());
171
172 $lazy_factory = new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
173 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
174 // actually initialize the logger
175 $logger->info('test');
176 }
177
178 public function testGetLazyGhostWhenLogLevelIsOff(): void
179 {
180 $expected_name = 'name';
181
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);
186
187 $basic_config
188 ->method('isLoggingEnabled')
189 ->willReturn(true);
190 $level_fetcher
191 ->method('fetchLevel')
192 ->willReturn(ILIASLogLevel::OFF);
193
194 $monolog_factory
195 ->expects($this->atLeastOnce())
196 ->method('nullLogger')
197 ->with($expected_name)
198 ->willReturn($this->createStub(MonologLogger::class));
199 $monolog_factory
200 ->expects($this->never())
201 ->method('logger');
202 $handler_provider
203 ->expects($this->never())
204 ->method($this->anything());
205
206 $lazy_factory = new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
207 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
208 // actually initialize the logger
209 $logger->info('test');
210 }
211
213 {
214 $expected_name = 'name';
215
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);
220
221 $basic_config
222 ->method('isLoggingEnabled')
223 ->willReturn(true);
224 $level_fetcher
225 ->method('fetchLevel')
226 ->willReturn(ILIASLogLevel::EMERGENCY);
227 $handler_provider
228 ->method('appliesTo')
229 ->willReturn(false);
230
231 $monolog_factory
232 ->expects($this->atLeastOnce())
233 ->method('logger')
234 ->with($expected_name) // no handler!
235 ->willReturn($this->createStub(MonologLogger::class));
236 $handler_provider
237 ->expects($this->never())
238 ->method('getHandler');
239
240 $lazy_factory = new LazyInternalFactory($monolog_factory, $basic_config, [$handler_provider]);
241 $logger = $lazy_factory->getLazyGhost($expected_name, $level_fetcher);
242 // actually initialize the logger
243 $logger->info('test');
244 }
245
247 {
248 $expected_name_1 = 'name 1';
249 $expected_name_2 = 'name 2';
250
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);
258
259 $basic_config
260 ->method('isLoggingEnabled')
261 ->willReturn(true);
262 $level_fetcher
263 ->method('fetchLevel')
264 ->willReturn(ILIASLogLevel::EMERGENCY);
265 $handler_provider_1
266 ->method('appliesTo')
267 ->willReturnMap([[$expected_name_1, true], [$expected_name_2, false]]);
268 $handler_provider_1
269 ->method('getHandler')
270 ->willReturn($handler_1);
271 $handler_provider_2
272 ->method('appliesTo')
273 ->willReturnMap([[$expected_name_1, false], [$expected_name_2, true]]);
274 $handler_provider_2
275 ->method('getHandler')
276 ->willReturn($handler_2);
277
278 $monolog_factory
279 ->expects($this->atLeast(2))
280 ->method('logger')
281 ->withParameterSetsInOrder([$expected_name_1, $handler_1], [$expected_name_2, $handler_2])
282 ->willReturn($this->createStub(MonologLogger::class));
283
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);
287 // actually initialize the logger
288 $logger_1->info('test');
289 $logger_2->info('test');
290 }
291
293 {
294 $expected_name_1 = 'name 1';
295 $expected_name_2 = 'name 2';
296
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);
304
305 $basic_config
306 ->method('isLoggingEnabled')
307 ->willReturn(true);
308 $level_fetcher
309 ->method('fetchLevel')
310 ->willReturn(ILIASLogLevel::EMERGENCY);
311 $handler_provider_1
312 ->method('appliesTo')
313 ->willReturn(true);
314 $handler_provider_1
315 ->method('getHandler')
316 ->willReturn($handler_1);
317 $handler_provider_2
318 ->method('appliesTo')
319 ->willReturn(true);
320 $handler_provider_2
321 ->method('getHandler')
322 ->willReturn($handler_2);
323
324 $monolog_factory
325 ->expects($this->atLeastOnce())
326 ->method('logger')
327 ->with($expected_name_1, $handler_1, $handler_2)
328 ->willReturn($this->createStub(MonologLogger::class));
329
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);
332 // actually initialize the logger
333 $logger->info('test');
334 }
335}
This exists to encapsulate reading the level of a logger from the config, to be able to delay it as m...