1111use Psr \Log \LogLevel ;
1212
1313use function func_get_args ;
14+ use function MongoDB \Driver \Monitoring \mongoc_log ;
1415use function sprintf ;
1516
1617class PsrLogAdapterTest extends BaseTestCase
@@ -20,47 +21,76 @@ class PsrLogAdapterTest extends BaseTestCase
2021 public function setUp (): void
2122 {
2223 $ this ->logger = $ this ->createTestPsrLogger ();
24+ 25+ PsrLogAdapter::addLogger ($ this ->logger );
2326 }
2427
2528 public function tearDown (): void
2629 {
2730 PsrLogAdapter::removeLogger ($ this ->logger );
2831 }
2932
33+ public function testLog (): void
34+ {
35+ /* This uses PHPC's internal mongoc_log() function to write messages
36+ * directly to libmongoc. Those messages are then relayed to
37+ * PsrLogAdapter and forwarded to each registered PSR logger.
38+ *
39+ * Note: it's not possible to test PsrLogAdapter::log() with an invalid
40+ * level since mongoc_log() already validates its level parameter. */
41+ mongoc_log (LogSubscriber::LEVEL_ERROR , 'domain1 ' , 'error ' );
42+ mongoc_log (LogSubscriber::LEVEL_CRITICAL , 'domain2 ' , 'critical ' );
43+ mongoc_log (LogSubscriber::LEVEL_WARNING , 'domain3 ' , 'warning ' );
44+ mongoc_log (LogSubscriber::LEVEL_MESSAGE , 'domain4 ' , 'message ' );
45+ mongoc_log (LogSubscriber::LEVEL_INFO , 'domain5 ' , 'info ' );
46+ mongoc_log (LogSubscriber::LEVEL_DEBUG , 'domain6 ' , 'debug ' );
47+ 48+ $ expectedLogs = [
49+ [LogLevel::ERROR , 'error ' , ['domain ' => 'domain1 ' ]],
50+ [LogLevel::ERROR , 'critical ' , ['domain ' => 'domain2 ' ]],
51+ [LogLevel::WARNING , 'warning ' , ['domain ' => 'domain3 ' ]],
52+ [LogLevel::NOTICE , 'message ' , ['domain ' => 'domain4 ' ]],
53+ [LogLevel::INFO , 'info ' , ['domain ' => 'domain5 ' ]],
54+ [LogLevel::DEBUG , 'debug ' , ['domain ' => 'domain6 ' ]],
55+ ];
56+ 57+ $ this ->assertSame ($ this ->logger ->logs , $ expectedLogs );
58+ }
59+ 3060 /**
3161 * @testWith [-1]
32- * [6 ]
62+ * [9 ]
3363 */
3464 public function testWriteLogWithInvalidLevel (int $ level ): void
3565 {
3666 $ this ->expectException (UnexpectedValueException::class);
37- $ this ->expectExceptionMessage (sprintf ('Expected level to be >= 0 and <= 5 , %d given for domain "domain" and message: message ' , $ level ));
67+ $ this ->expectExceptionMessage (sprintf ('Expected level to be >= 0 and <= 8 , %d given for domain "domain" and message: message ' , $ level ));
3868
3969 PsrLogAdapter::writeLog ($ level , 'domain ' , 'message ' );
4070 }
4171
42- public function testWriteLogMapsDriverLogLevelsToPsrLogLevels (): void
72+ public function testWriteLog (): void
4373 {
44- PsrLogAdapter::addLogger ($ this ->logger );
45- 46- $ domain = 'domain ' ;
47- 48- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_ERROR , $ domain , 'error ' );
49- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_CRITICAL , $ domain , 'critical ' );
50- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_WARNING , $ domain , 'warning ' );
51- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_MESSAGE , $ domain , 'message ' );
52- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_INFO , $ domain , 'info ' );
53- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_DEBUG , $ domain , 'debug ' );
54- 55- $ context = ['domain ' => $ domain ];
74+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_EMERGENCY , 'domain1 ' , 'emergency ' );
75+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_ALERT , 'domain2 ' , 'alert ' );
76+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_CRITICAL , 'domain3 ' , 'critical ' );
77+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_ERROR , 'domain4 ' , 'error ' );
78+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_WARN , 'domain5 ' , 'warn ' );
79+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_NOTICE , 'domain6 ' , 'notice ' );
80+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_INFO , 'domain7 ' , 'info ' );
81+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_DEBUG , 'domain8 ' , 'debug ' );
82+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_TRACE , 'domain9 ' , 'trace ' );
5683
5784 $ expectedLogs = [
58- [LogLevel::ERROR , 'error ' , $ context ],
59- [LogLevel::ERROR , 'critical ' , $ context ],
60- [LogLevel::WARNING , 'warning ' , $ context ],
61- [LogLevel::NOTICE , 'message ' , $ context ],
62- [LogLevel::INFO , 'info ' , $ context ],
63- [LogLevel::DEBUG , 'debug ' , $ context ],
85+ [LogLevel::EMERGENCY , 'emergency ' , ['domain ' => 'domain1 ' ]],
86+ [LogLevel::ALERT , 'alert ' , ['domain ' => 'domain2 ' ]],
87+ [LogLevel::CRITICAL , 'critical ' , ['domain ' => 'domain3 ' ]],
88+ [LogLevel::ERROR , 'error ' , ['domain ' => 'domain4 ' ]],
89+ [LogLevel::WARNING , 'warn ' , ['domain ' => 'domain5 ' ]],
90+ [LogLevel::NOTICE , 'notice ' , ['domain ' => 'domain6 ' ]],
91+ [LogLevel::INFO , 'info ' , ['domain ' => 'domain7 ' ]],
92+ [LogLevel::DEBUG , 'debug ' , ['domain ' => 'domain8 ' ]],
93+ [LogLevel::DEBUG , 'trace ' , ['domain ' => 'domain9 ' ]],
6494 ];
6595
6696 $ this ->assertSame ($ this ->logger ->logs , $ expectedLogs );
0 commit comments