Sync up core repo with Parsoid
[mediawiki.git] / tests / phpunit / includes / debug / logger / LegacyLoggerTest.php
blob863bc80efe3840d2ab8e9fb67c4b76c5af3fe02c
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Tests\Logger;
23 use MediaWiki\Logger\LegacyLogger;
24 use MediaWiki\MainConfigNames;
25 use MediaWikiIntegrationTestCase;
26 use Psr\Log\LogLevel;
28 class LegacyLoggerTest extends MediaWikiIntegrationTestCase {
30 /**
31 * @covers \MediaWiki\Logger\LegacyLogger::interpolate
32 * @dataProvider provideInterpolate
34 public function testInterpolate( $message, $context, $expect ) {
35 $this->assertEquals(
36 $expect, LegacyLogger::interpolate( $message, $context ) );
39 public static function provideInterpolate() {
40 $e = new \Exception( 'boom!' );
41 $d = new \DateTime();
42 $err = new \Error( 'Test error' );
43 return [
45 'no-op',
46 [],
47 'no-op',
50 'Hello {world}!',
52 'world' => 'World',
54 'Hello World!',
57 '{greeting} {user}',
59 'greeting' => 'Goodnight',
60 'user' => 'Moon',
62 'Goodnight Moon',
65 'Oops {key_not_set}',
66 [],
67 'Oops {key_not_set}',
70 '{ not interpolated }',
72 'not interpolated' => 'This should NOT show up in the message',
74 '{ not interpolated }',
77 '{null}',
79 'null' => null,
81 '[Null]',
84 '{bool}',
86 'bool' => true,
88 'true',
91 '{float}',
93 'float' => 1.23,
95 '1.23',
98 '{array}',
100 'array' => [ 1, 2, 3 ],
102 '[Array(3)]',
105 '{exception}',
107 'exception' => $e,
109 '[Exception ' . get_class( $e ) . '( ' .
110 $e->getFile() . ':' . $e->getLine() . ') ' .
111 $e->getMessage() . ']',
114 '{datetime}',
116 'datetime' => $d,
118 $d->format( 'c' ),
121 '{object}',
123 'object' => new \stdClass,
125 '[Object stdClass]',
128 '{exception}',
130 'exception' => $err,
132 '[Error ' . get_class( $err ) . '( ' .
133 $err->getFile() . ':' . $err->getLine() . ') ' .
134 $err->getMessage() . ']',
140 * @covers \MediaWiki\Logger\LegacyLogger::shouldEmit
141 * @dataProvider provideShouldEmit
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, [] )
151 public static function provideShouldEmit() {
152 $dest = [ 'destination' => 'foobar' ];
153 $tests = [
155 LogLevel::DEBUG,
156 $dest,
157 true
160 LogLevel::WARNING,
161 $dest + [ 'level' => LogLevel::INFO ],
162 true,
165 LogLevel::INFO,
166 $dest + [ 'level' => LogLevel::CRITICAL ],
167 false,
170 \Monolog\Logger::INFO,
171 $dest + [ 'level' => LogLevel::INFO ],
172 true,
175 \Monolog\Logger::WARNING,
176 $dest + [ 'level' => LogLevel::EMERGENCY ],
177 false,
181 return $tests;