MediaWiki  master
LegacyLoggerTest.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger;
22 
25 
27 
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 
130  public function testInterpolate_Error() {
131  // @todo Merge this into provideInterpolate once we drop HHVM support
132  if ( !class_exists( \Error::class ) ) {
133  $this->markTestSkipped( 'Error class does not exist' );
134  }
135 
136  $err = new \Error( 'Test error' );
137  $message = '{exception}';
138  $context = [ 'exception' => $err ];
139  $expect = '[Error ' . get_class( $err ) . '( ' .
140  $err->getFile() . ':' . $err->getLine() . ') ' .
141  $err->getMessage() . ']';
142 
143  $this->assertEquals(
144  $expect, LegacyLogger::interpolate( $message, $context ) );
145  }
146 
151  public function testShouldEmit( $level, $config, $expected ) {
152  $this->setMwGlobals( 'wgDebugLogGroups', [ 'fakechannel' => $config ] );
153  $this->assertEquals(
154  $expected,
155  LegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, [] )
156  );
157  }
158 
159  public static function provideShouldEmit() {
160  $dest = [ 'destination' => 'foobar' ];
161  $tests = [
162  [
163  LogLevel::DEBUG,
164  $dest,
165  true
166  ],
167  [
168  LogLevel::WARNING,
169  $dest + [ 'level' => LogLevel::INFO ],
170  true,
171  ],
172  [
173  LogLevel::INFO,
174  $dest + [ 'level' => LogLevel::CRITICAL ],
175  false,
176  ],
177  ];
178 
179  if ( class_exists( '\Monolog\Logger' ) ) {
180  $tests[] = [
181  \Monolog\Logger::INFO,
182  $dest + [ 'level' => LogLevel::INFO ],
183  true,
184  ];
185  $tests[] = [
186  \Monolog\Logger::WARNING,
187  $dest + [ 'level' => LogLevel::EMERGENCY ],
188  false,
189  ];
190  }
191 
192  return $tests;
193  }
194 
195 }
Config $config
Definition: MediaWiki.php:43
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2147
testInterpolate( $message, $context, $expect)
MediaWiki\Logger\LegacyLogger::interpolate provideInterpolate.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1972
static interpolate( $message, array $context)
Interpolate placeholders in logging message.
IContextSource $context
Definition: MediaWiki.php:38
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
testShouldEmit( $level, $config, $expected)
MediaWiki\Logger\LegacyLogger::shouldEmit provideShouldEmit.
testInterpolate_Error()
MediaWiki\Logger\LegacyLogger::interpolate.
static shouldEmit( $channel, $message, $level, $context)
Determine if the given message should be emitted or not.