3
3
namespace MongoDB \Tests ;
4
4
5
5
use MongoDB \Driver \Logging \Logger as DriverLogger ;
6
+ use MongoDB \Exception \UnexpectedValueException ;
6
7
use MongoDB \PsrLogAdapter ;
7
8
use PHPUnit \Framework \TestCase as BaseTestCase ;
8
9
use Psr \Log \AbstractLogger ;
9
10
use Psr \Log \LoggerInterface ;
10
11
use Psr \Log \LogLevel ;
11
12
12
13
use function func_get_args ;
13
- use function MongoDB \ Driver \ Logging \ log ;
14
+ use function sprintf ;
14
15
15
16
class PsrLogAdapterTest extends BaseTestCase
16
17
{
@@ -28,19 +29,32 @@ public function tearDown(): void
28
29
PsrLogAdapter::removeLogger ($ this ->logger );
29
30
}
30
31
31
- public function testDriverLogLevelsAreMappedToPsrLogLevels (): void
32
+ /**
33
+ * @testWith [-1]
34
+ * [6]
35
+ */
36
+ public function testWriteLogWithInvalidLevel (int $ level ): void
37
+ {
38
+ $ this ->expectException (UnexpectedValueException::class);
39
+ $ this ->expectExceptionMessage (sprintf ('Expected level to be >= 0 and <= 5, %d given for domain "domain" and message: message ' , $ level ));
40
+
41
+ PsrLogAdapter::writeLog ($ level , 'domain ' , 'message ' );
42
+ }
43
+
44
+ public function testWriteLogMapsDriverLogLevelsToPsrLogLevels (): void
32
45
{
33
46
PsrLogAdapter::addLogger ($ this ->logger );
34
47
35
- log (DriverLogger::LEVEL_ERROR , 'error ' );
36
- log (DriverLogger::LEVEL_CRITICAL , 'critical ' );
37
- log (DriverLogger::LEVEL_WARNING , 'warning ' );
38
- log (DriverLogger::LEVEL_MESSAGE , 'message ' );
39
- log (DriverLogger::LEVEL_INFO , 'info ' );
40
- log (DriverLogger::LEVEL_DEBUG , 'debug ' );
41
- log (DriverLogger::LEVEL_TRACE , 'trace ' );
48
+ $ domain = 'domain ' ;
49
+
50
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_ERROR , $ domain , 'error ' );
51
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_CRITICAL , $ domain , 'critical ' );
52
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_WARNING , $ domain , 'warning ' );
53
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_MESSAGE , $ domain , 'message ' );
54
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_INFO , $ domain , 'info ' );
55
+ PsrLogAdapter::writeLog (DriverLogger::LEVEL_DEBUG , $ domain , 'debug ' );
42
56
43
- $ context = ['domain ' => ' php ' ];
57
+ $ context = ['domain ' => $ domain ];
44
58
45
59
$ expectedLogs = [
46
60
[LogLevel::ERROR , 'error ' , $ context ],
@@ -49,7 +63,6 @@ public function testDriverLogLevelsAreMappedToPsrLogLevels(): void
49
63
[LogLevel::NOTICE , 'message ' , $ context ],
50
64
[LogLevel::INFO , 'info ' , $ context ],
51
65
[LogLevel::DEBUG , 'debug ' , $ context ],
52
- [LogLevel::DEBUG , 'trace ' , $ context ],
53
66
];
54
67
55
68
$ this ->assertSame ($ this ->logger ->logs , $ expectedLogs );
0 commit comments