MediaWiki  1.33.0
KafkaHandlerTest.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
24 use Monolog\Logger;
25 use Wikimedia\TestingAccessWrapper;
26 
31 
32  protected function setUp() {
33  if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
34  || !class_exists( 'Kafka\Produce' )
35  ) {
36  $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
37  }
38 
39  parent::setUp();
40  }
41 
42  public function topicNamingProvider() {
43  return [
44  [ [], 'monolog_foo' ],
45  [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
46  ];
47  }
48 
52  public function testTopicNaming( $options, $expect ) {
53  $produce = $this->getMockBuilder( 'Kafka\Produce' )
54  ->disableOriginalConstructor()
55  ->getMock();
56  $produce->expects( $this->any() )
57  ->method( 'getAvailablePartitions' )
58  ->will( $this->returnValue( [ 'A' ] ) );
59  $produce->expects( $this->once() )
60  ->method( 'setMessages' )
61  ->with( $expect, $this->anything(), $this->anything() );
62  $produce->expects( $this->any() )
63  ->method( 'send' )
64  ->will( $this->returnValue( true ) );
65 
66  $handler = new KafkaHandler( $produce, $options );
67  $handler->handle( [
68  'channel' => 'foo',
69  'level' => Logger::EMERGENCY,
70  'extra' => [],
71  'context' => [],
72  ] );
73  }
74 
75  public function swallowsExceptionsWhenRequested() {
76  return [
77  // defaults to false
78  [ [], true ],
79  // also try false explicitly
80  [ [ 'swallowExceptions' => false ], true ],
81  // turn it on
82  [ [ 'swallowExceptions' => true ], false ],
83  ];
84  }
85 
89  public function testGetAvailablePartitionsException( $options, $expectException ) {
90  $produce = $this->getMockBuilder( 'Kafka\Produce' )
91  ->disableOriginalConstructor()
92  ->getMock();
93  $produce->expects( $this->any() )
94  ->method( 'getAvailablePartitions' )
95  ->will( $this->throwException( new \Kafka\Exception ) );
96  $produce->expects( $this->any() )
97  ->method( 'send' )
98  ->will( $this->returnValue( true ) );
99 
100  if ( $expectException ) {
101  $this->setExpectedException( 'Kafka\Exception' );
102  }
103 
104  $handler = new KafkaHandler( $produce, $options );
105  $handler->handle( [
106  'channel' => 'foo',
107  'level' => Logger::EMERGENCY,
108  'extra' => [],
109  'context' => [],
110  ] );
111 
112  if ( !$expectException ) {
113  $this->assertTrue( true, 'no exception was thrown' );
114  }
115  }
116 
120  public function testSendException( $options, $expectException ) {
121  $produce = $this->getMockBuilder( 'Kafka\Produce' )
122  ->disableOriginalConstructor()
123  ->getMock();
124  $produce->expects( $this->any() )
125  ->method( 'getAvailablePartitions' )
126  ->will( $this->returnValue( [ 'A' ] ) );
127  $produce->expects( $this->any() )
128  ->method( 'send' )
129  ->will( $this->throwException( new \Kafka\Exception ) );
130 
131  if ( $expectException ) {
132  $this->setExpectedException( 'Kafka\Exception' );
133  }
134 
135  $handler = new KafkaHandler( $produce, $options );
136  $handler->handle( [
137  'channel' => 'foo',
138  'level' => Logger::EMERGENCY,
139  'extra' => [],
140  'context' => [],
141  ] );
142 
143  if ( !$expectException ) {
144  $this->assertTrue( true, 'no exception was thrown' );
145  }
146  }
147 
148  public function testHandlesNullFormatterResult() {
149  $produce = $this->getMockBuilder( 'Kafka\Produce' )
150  ->disableOriginalConstructor()
151  ->getMock();
152  $produce->expects( $this->any() )
153  ->method( 'getAvailablePartitions' )
154  ->will( $this->returnValue( [ 'A' ] ) );
155  $mockMethod = $produce->expects( $this->exactly( 2 ) )
156  ->method( 'setMessages' );
157  $produce->expects( $this->any() )
158  ->method( 'send' )
159  ->will( $this->returnValue( true ) );
160  // evil hax
161  $matcher = TestingAccessWrapper::newFromObject( $mockMethod )->matcher;
162  TestingAccessWrapper::newFromObject( $matcher )->parametersMatcher =
163  new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
164  [ $this->anything(), $this->anything(), [ 'words' ] ],
165  [ $this->anything(), $this->anything(), [ 'lines' ] ]
166  ] );
167 
168  $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface::class );
169  $formatter->expects( $this->any() )
170  ->method( 'format' )
171  ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
172 
173  $handler = new KafkaHandler( $produce, [] );
174  $handler->setFormatter( $formatter );
175  for ( $i = 0; $i < 3; ++$i ) {
176  $handler->handle( [
177  'channel' => 'foo',
178  'level' => Logger::EMERGENCY,
179  'extra' => [],
180  'context' => [],
181  ] );
182  }
183  }
184 
186  $produce = $this->getMockBuilder( 'Kafka\Produce' )
187  ->disableOriginalConstructor()
188  ->getMock();
189  $produce->expects( $this->any() )
190  ->method( 'getAvailablePartitions' )
191  ->will( $this->returnValue( [ 'A' ] ) );
192  $produce->expects( $this->once() )
193  ->method( 'setMessages' )
194  ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
195  $produce->expects( $this->any() )
196  ->method( 'send' )
197  ->will( $this->returnValue( true ) );
198 
199  $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface::class );
200  $formatter->expects( $this->any() )
201  ->method( 'format' )
202  ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
203 
204  $handler = new KafkaHandler( $produce, [] );
205  $handler->setFormatter( $formatter );
206  $handler->handleBatch( [
207  [
208  'channel' => 'foo',
209  'level' => Logger::EMERGENCY,
210  'extra' => [],
211  'context' => [],
212  ],
213  [
214  'channel' => 'foo',
215  'level' => Logger::EMERGENCY,
216  'extra' => [],
217  'context' => [],
218  ],
219  [
220  'channel' => 'foo',
221  'level' => Logger::EMERGENCY,
222  'extra' => [],
223  'context' => [],
224  ],
225  ] );
226  }
227 }
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWiki\Logger\Monolog\KafkaHandlerTest\testHandlesNullFormatterResult
testHandlesNullFormatterResult()
Definition: KafkaHandlerTest.php:148
MediaWiki\Logger\Monolog\KafkaHandlerTest\topicNamingProvider
topicNamingProvider()
Definition: KafkaHandlerTest.php:42
anything
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
Definition: distributors.txt:39
MediaWiki\Logger\Monolog\KafkaHandlerTest\testTopicNaming
testTopicNaming( $options, $expect)
topicNamingProvider
Definition: KafkaHandlerTest.php:52
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
php
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
MediaWiki\Logger\Monolog\KafkaHandlerTest
\MediaWiki\Logger\Monolog\KafkaHandler
Definition: KafkaHandlerTest.php:30
MediaWiki\Logger\Monolog\KafkaHandler
Log handler sends log events to a kafka server.
Definition: KafkaHandler.php:49
MediaWiki\Logger\Monolog\KafkaHandlerTest\swallowsExceptionsWhenRequested
swallowsExceptionsWhenRequested()
Definition: KafkaHandlerTest.php:75
MediaWiki\Logger\Monolog\KafkaHandlerTest\testSendException
testSendException( $options, $expectException)
swallowsExceptionsWhenRequested
Definition: KafkaHandlerTest.php:120
$handler
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 it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
MediaWiki\Logger\Monolog\KafkaHandlerTest\setUp
setUp()
Definition: KafkaHandlerTest.php:32
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Logger\Monolog\KafkaHandlerTest\testBatchHandlesNullFormatterResult
testBatchHandlesNullFormatterResult()
Definition: KafkaHandlerTest.php:185
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
MediaWiki\Logger\Monolog\KafkaHandlerTest\testGetAvailablePartitionsException
testGetAvailablePartitionsException( $options, $expectException)
swallowsExceptionsWhenRequested
Definition: KafkaHandlerTest.php:89
$options
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 & $options
Definition: hooks.txt:1985
true
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:1985
class
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