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