Debian Codegraph @

tests/phpunit/includes/debug/logger/LegacyLoggerTest.php

raw symbols SCIP
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.
7 *
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.
12 *
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
17 *
18 * @file
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 * @covers \MediaWiki\Logger\LegacyLogger::interpolate
32 * @dataProvider provideInterpolate
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 * @covers \MediaWiki\Logger\LegacyLogger::shouldEmit
141 * @dataProvider provideShouldEmit
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 }