| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\Logger; |
| 22 |
|
| 23 |
use MediaWiki\Logger\LegacyLogger; |
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWikiIntegrationTestCase; |
| 26 |
use Psr\Log\LogLevel; |
| 27 |
|
| 28 |
class LegacyLoggerTest extends MediaWikiIntegrationTestCase { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testInterpolate( $message, $context, $expect ) { |
| 35 |
$this->assertEquals( |
| 36 |
$expect, LegacyLogger::interpolate( $message, $context ) ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function provideInterpolate() { |
| 40 |
$e = new \Exception( 'boom!' ); |
| 41 |
$d = new \DateTime(); |
| 42 |
$err = new \Error( 'Test error' ); |
| 43 |
return [ |
| 44 |
[ |
| 45 |
'no-op', |
| 46 |
[], |
| 47 |
'no-op', |
| 48 |
], |
| 49 |
[ |
| 50 |
'Hello {world}!', |
| 51 |
[ |
| 52 |
'world' => 'World', |
| 53 |
], |
| 54 |
'Hello World!', |
| 55 |
], |
| 56 |
[ |
| 57 |
'{greeting} {user}', |
| 58 |
[ |
| 59 |
'greeting' => 'Goodnight', |
| 60 |
'user' => 'Moon', |
| 61 |
], |
| 62 |
'Goodnight Moon', |
| 63 |
], |
| 64 |
[ |
| 65 |
'Oops {key_not_set}', |
| 66 |
[], |
| 67 |
'Oops {key_not_set}', |
| 68 |
], |
| 69 |
[ |
| 70 |
'{ not interpolated }', |
| 71 |
[ |
| 72 |
'not interpolated' => 'This should NOT show up in the message', |
| 73 |
], |
| 74 |
'{ not interpolated }', |
| 75 |
], |
| 76 |
[ |
| 77 |
'{null}', |
| 78 |
[ |
| 79 |
'null' => null, |
| 80 |
], |
| 81 |
'[Null]', |
| 82 |
], |
| 83 |
[ |
| 84 |
'{bool}', |
| 85 |
[ |
| 86 |
'bool' => true, |
| 87 |
], |
| 88 |
'true', |
| 89 |
], |
| 90 |
[ |
| 91 |
'{float}', |
| 92 |
[ |
| 93 |
'float' => 1.23, |
| 94 |
], |
| 95 |
'1.23', |
| 96 |
], |
| 97 |
[ |
| 98 |
'{array}', |
| 99 |
[ |
| 100 |
'array' => [ 1, 2, 3 ], |
| 101 |
], |
| 102 |
'[Array(3)]', |
| 103 |
], |
| 104 |
[ |
| 105 |
'{exception}', |
| 106 |
[ |
| 107 |
'exception' => $e, |
| 108 |
], |
| 109 |
'[Exception ' . get_class( $e ) . '( ' . |
| 110 |
$e->getFile() . ':' . $e->getLine() . ') ' . |
| 111 |
$e->getMessage() . ']', |
| 112 |
], |
| 113 |
[ |
| 114 |
'{datetime}', |
| 115 |
[ |
| 116 |
'datetime' => $d, |
| 117 |
], |
| 118 |
$d->format( 'c' ), |
| 119 |
], |
| 120 |
[ |
| 121 |
'{object}', |
| 122 |
[ |
| 123 |
'object' => new \stdClass, |
| 124 |
], |
| 125 |
'[Object stdClass]', |
| 126 |
], |
| 127 |
[ |
| 128 |
'{exception}', |
| 129 |
[ |
| 130 |
'exception' => $err, |
| 131 |
], |
| 132 |
'[Error ' . get_class( $err ) . '( ' . |
| 133 |
$err->getFile() . ':' . $err->getLine() . ') ' . |
| 134 |
$err->getMessage() . ']', |
| 135 |
], |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testShouldEmit( $level, $config, $expected ) { |
| 144 |
$this->overrideConfigValue( MainConfigNames::DebugLogGroups, [ 'fakechannel' => $config ] ); |
| 145 |
$this->assertEquals( |
| 146 |
$expected, |
| 147 |
LegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, [] ) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
public static function provideShouldEmit() { |
| 152 |
$dest = [ 'destination' => 'foobar' ]; |
| 153 |
$tests = [ |
| 154 |
[ |
| 155 |
LogLevel::DEBUG, |
| 156 |
$dest, |
| 157 |
true |
| 158 |
], |
| 159 |
[ |
| 160 |
LogLevel::WARNING, |
| 161 |
$dest + [ 'level' => LogLevel::INFO ], |
| 162 |
true, |
| 163 |
], |
| 164 |
[ |
| 165 |
LogLevel::INFO, |
| 166 |
$dest + [ 'level' => LogLevel::CRITICAL ], |
| 167 |
false, |
| 168 |
], |
| 169 |
[ |
| 170 |
\Monolog\Logger::INFO, |
| 171 |
$dest + [ 'level' => LogLevel::INFO ], |
| 172 |
true, |
| 173 |
], |
| 174 |
[ |
| 175 |
\Monolog\Logger::WARNING, |
| 176 |
$dest + [ 'level' => LogLevel::EMERGENCY ], |
| 177 |
false, |
| 178 |
] |
| 179 |
]; |
| 180 |
|
| 181 |
return $tests; |
| 182 |
} |
| 183 |
|
| 184 |
} |