MediaWiki  1.28.1
KafkaHandlerTest.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
25 
27 
28  protected function setUp() {
29  if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
30  || !class_exists( 'Kafka\Produce' )
31  ) {
32  $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
33  }
34 
35  parent::setUp();
36  }
37 
38  public function topicNamingProvider() {
39  return [
40  [ [], 'monolog_foo' ],
41  [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
42  ];
43  }
44 
48  public function testTopicNaming( $options, $expect ) {
49  $produce = $this->getMockBuilder( 'Kafka\Produce' )
50  ->disableOriginalConstructor()
51  ->getMock();
52  $produce->expects( $this->any() )
53  ->method( 'getAvailablePartitions' )
54  ->will( $this->returnValue( [ 'A' ] ) );
55  $produce->expects( $this->once() )
56  ->method( 'setMessages' )
57  ->with( $expect, $this->anything(), $this->anything() );
58  $produce->expects( $this->any() )
59  ->method( 'send' )
60  ->will( $this->returnValue( true ) );
61 
62  $handler = new KafkaHandler( $produce, $options );
63  $handler->handle( [
64  'channel' => 'foo',
65  'level' => Logger::EMERGENCY,
66  'extra' => [],
67  'context' => [],
68  ] );
69  }
70 
71  public function swallowsExceptionsWhenRequested() {
72  return [
73  // defaults to false
74  [ [], true ],
75  // also try false explicitly
76  [ [ 'swallowExceptions' => false ], true ],
77  // turn it on
78  [ [ 'swallowExceptions' => true ], false ],
79  ];
80  }
81 
85  public function testGetAvailablePartitionsException( $options, $expectException ) {
86  $produce = $this->getMockBuilder( 'Kafka\Produce' )
87  ->disableOriginalConstructor()
88  ->getMock();
89  $produce->expects( $this->any() )
90  ->method( 'getAvailablePartitions' )
91  ->will( $this->throwException( new \Kafka\Exception ) );
92  $produce->expects( $this->any() )
93  ->method( 'send' )
94  ->will( $this->returnValue( true ) );
95 
96  if ( $expectException ) {
97  $this->setExpectedException( 'Kafka\Exception' );
98  }
99 
100  $handler = new KafkaHandler( $produce, $options );
101  $handler->handle( [
102  'channel' => 'foo',
103  'level' => Logger::EMERGENCY,
104  'extra' => [],
105  'context' => [],
106  ] );
107 
108  if ( !$expectException ) {
109  $this->assertTrue( true, 'no exception was thrown' );
110  }
111  }
112 
116  public function testSendException( $options, $expectException ) {
117  $produce = $this->getMockBuilder( 'Kafka\Produce' )
118  ->disableOriginalConstructor()
119  ->getMock();
120  $produce->expects( $this->any() )
121  ->method( 'getAvailablePartitions' )
122  ->will( $this->returnValue( [ 'A' ] ) );
123  $produce->expects( $this->any() )
124  ->method( 'send' )
125  ->will( $this->throwException( new \Kafka\Exception ) );
126 
127  if ( $expectException ) {
128  $this->setExpectedException( 'Kafka\Exception' );
129  }
130 
131  $handler = new KafkaHandler( $produce, $options );
132  $handler->handle( [
133  'channel' => 'foo',
134  'level' => Logger::EMERGENCY,
135  'extra' => [],
136  'context' => [],
137  ] );
138 
139  if ( !$expectException ) {
140  $this->assertTrue( true, 'no exception was thrown' );
141  }
142  }
143 
144  public function testHandlesNullFormatterResult() {
145  $produce = $this->getMockBuilder( 'Kafka\Produce' )
146  ->disableOriginalConstructor()
147  ->getMock();
148  $produce->expects( $this->any() )
149  ->method( 'getAvailablePartitions' )
150  ->will( $this->returnValue( [ 'A' ] ) );
151  $mockMethod = $produce->expects( $this->exactly( 2 ) )
152  ->method( 'setMessages' );
153  $produce->expects( $this->any() )
154  ->method( 'send' )
155  ->will( $this->returnValue( true ) );
156  // evil hax
157  \TestingAccessWrapper::newFromObject( $mockMethod )->matcher->parametersMatcher =
158  new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
159  [ $this->anything(), $this->anything(), [ 'words' ] ],
160  [ $this->anything(), $this->anything(), [ 'lines' ] ]
161  ] );
162 
163  $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
164  $formatter->expects( $this->any() )
165  ->method( 'format' )
166  ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
167 
168  $handler = new KafkaHandler( $produce, [] );
169  $handler->setFormatter( $formatter );
170  for ( $i = 0; $i < 3; ++$i ) {
171  $handler->handle( [
172  'channel' => 'foo',
173  'level' => Logger::EMERGENCY,
174  'extra' => [],
175  'context' => [],
176  ] );
177  }
178  }
179 
181  $produce = $this->getMockBuilder( 'Kafka\Produce' )
182  ->disableOriginalConstructor()
183  ->getMock();
184  $produce->expects( $this->any() )
185  ->method( 'getAvailablePartitions' )
186  ->will( $this->returnValue( [ 'A' ] ) );
187  $produce->expects( $this->once() )
188  ->method( 'setMessages' )
189  ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
190  $produce->expects( $this->any() )
191  ->method( 'send' )
192  ->will( $this->returnValue( true ) );
193 
194  $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
195  $formatter->expects( $this->any() )
196  ->method( 'format' )
197  ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
198 
199  $handler = new KafkaHandler( $produce, [] );
200  $handler->setFormatter( $formatter );
201  $handler->handleBatch( [
202  [
203  'channel' => 'foo',
204  'level' => Logger::EMERGENCY,
205  'extra' => [],
206  'context' => [],
207  ],
208  [
209  'channel' => 'foo',
210  'level' => Logger::EMERGENCY,
211  'extra' => [],
212  'context' => [],
213  ],
214  [
215  'channel' => 'foo',
216  'level' => Logger::EMERGENCY,
217  'extra' => [],
218  'context' => [],
219  ],
220  ] );
221  }
222 }
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testSendException($options, $expectException)
swallowsExceptionsWhenRequested
testTopicNaming($options, $expect)
topicNamingProvider
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
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:1936
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
testGetAvailablePartitionsException($options, $expectException)
swallowsExceptionsWhenRequested
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
Log handler sends log events to a kafka server.
static newFromObject($object)
Return the same object, without access restrictions.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:802