MediaWiki REL1_30
KafkaHandlerTest.php
Go to the documentation of this file.
1<?php
22
24use Monolog\Logger;
25use Wikimedia\TestingAccessWrapper;
26
28
29 protected function setUp() {
30 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
31 || !class_exists( 'Kafka\Produce' )
32 ) {
33 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
34 }
35
36 parent::setUp();
37 }
38
39 public function topicNamingProvider() {
40 return [
41 [ [], 'monolog_foo' ],
42 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
43 ];
44 }
45
49 public function testTopicNaming( $options, $expect ) {
50 $produce = $this->getMockBuilder( 'Kafka\Produce' )
51 ->disableOriginalConstructor()
52 ->getMock();
53 $produce->expects( $this->any() )
54 ->method( 'getAvailablePartitions' )
55 ->will( $this->returnValue( [ 'A' ] ) );
56 $produce->expects( $this->once() )
57 ->method( 'setMessages' )
58 ->with( $expect, $this->anything(), $this->anything() );
59 $produce->expects( $this->any() )
60 ->method( 'send' )
61 ->will( $this->returnValue( true ) );
62
63 $handler = new KafkaHandler( $produce, $options );
64 $handler->handle( [
65 'channel' => 'foo',
66 'level' => Logger::EMERGENCY,
67 'extra' => [],
68 'context' => [],
69 ] );
70 }
71
73 return [
74 // defaults to false
75 [ [], true ],
76 // also try false explicitly
77 [ [ 'swallowExceptions' => false ], true ],
78 // turn it on
79 [ [ 'swallowExceptions' => true ], false ],
80 ];
81 }
82
86 public function testGetAvailablePartitionsException( $options, $expectException ) {
87 $produce = $this->getMockBuilder( 'Kafka\Produce' )
88 ->disableOriginalConstructor()
89 ->getMock();
90 $produce->expects( $this->any() )
91 ->method( 'getAvailablePartitions' )
92 ->will( $this->throwException( new \Kafka\Exception ) );
93 $produce->expects( $this->any() )
94 ->method( 'send' )
95 ->will( $this->returnValue( true ) );
96
97 if ( $expectException ) {
98 $this->setExpectedException( 'Kafka\Exception' );
99 }
100
101 $handler = new KafkaHandler( $produce, $options );
102 $handler->handle( [
103 'channel' => 'foo',
104 'level' => Logger::EMERGENCY,
105 'extra' => [],
106 'context' => [],
107 ] );
108
109 if ( !$expectException ) {
110 $this->assertTrue( true, 'no exception was thrown' );
111 }
112 }
113
117 public function testSendException( $options, $expectException ) {
118 $produce = $this->getMockBuilder( 'Kafka\Produce' )
119 ->disableOriginalConstructor()
120 ->getMock();
121 $produce->expects( $this->any() )
122 ->method( 'getAvailablePartitions' )
123 ->will( $this->returnValue( [ 'A' ] ) );
124 $produce->expects( $this->any() )
125 ->method( 'send' )
126 ->will( $this->throwException( new \Kafka\Exception ) );
127
128 if ( $expectException ) {
129 $this->setExpectedException( 'Kafka\Exception' );
130 }
131
132 $handler = new KafkaHandler( $produce, $options );
133 $handler->handle( [
134 'channel' => 'foo',
135 'level' => Logger::EMERGENCY,
136 'extra' => [],
137 'context' => [],
138 ] );
139
140 if ( !$expectException ) {
141 $this->assertTrue( true, 'no exception was thrown' );
142 }
143 }
144
146 $produce = $this->getMockBuilder( 'Kafka\Produce' )
147 ->disableOriginalConstructor()
148 ->getMock();
149 $produce->expects( $this->any() )
150 ->method( 'getAvailablePartitions' )
151 ->will( $this->returnValue( [ 'A' ] ) );
152 $mockMethod = $produce->expects( $this->exactly( 2 ) )
153 ->method( 'setMessages' );
154 $produce->expects( $this->any() )
155 ->method( 'send' )
156 ->will( $this->returnValue( true ) );
157 // evil hax
158 TestingAccessWrapper::newFromObject( $mockMethod )->matcher->parametersMatcher =
159 new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
160 [ $this->anything(), $this->anything(), [ 'words' ] ],
161 [ $this->anything(), $this->anything(), [ 'lines' ] ]
162 ] );
163
164 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
165 $formatter->expects( $this->any() )
166 ->method( 'format' )
167 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
168
169 $handler = new KafkaHandler( $produce, [] );
170 $handler->setFormatter( $formatter );
171 for ( $i = 0; $i < 3; ++$i ) {
172 $handler->handle( [
173 'channel' => 'foo',
174 'level' => Logger::EMERGENCY,
175 'extra' => [],
176 'context' => [],
177 ] );
178 }
179 }
180
182 $produce = $this->getMockBuilder( 'Kafka\Produce' )
183 ->disableOriginalConstructor()
184 ->getMock();
185 $produce->expects( $this->any() )
186 ->method( 'getAvailablePartitions' )
187 ->will( $this->returnValue( [ 'A' ] ) );
188 $produce->expects( $this->once() )
189 ->method( 'setMessages' )
190 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
191 $produce->expects( $this->any() )
192 ->method( 'send' )
193 ->will( $this->returnValue( true ) );
194
195 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
196 $formatter->expects( $this->any() )
197 ->method( 'format' )
198 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
199
200 $handler = new KafkaHandler( $produce, [] );
201 $handler->setFormatter( $formatter );
202 $handler->handleBatch( [
203 [
204 'channel' => 'foo',
205 'level' => Logger::EMERGENCY,
206 'extra' => [],
207 'context' => [],
208 ],
209 [
210 'channel' => 'foo',
211 'level' => Logger::EMERGENCY,
212 'extra' => [],
213 'context' => [],
214 ],
215 [
216 'channel' => 'foo',
217 'level' => Logger::EMERGENCY,
218 'extra' => [],
219 'context' => [],
220 ],
221 ] );
222 }
223}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
testTopicNaming( $options, $expect)
topicNamingProvider
testSendException( $options, $expectException)
swallowsExceptionsWhenRequested
testGetAvailablePartitionsException( $options, $expectException)
swallowsExceptionsWhenRequested
Log handler sends log events to a kafka server.
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 & $options
Definition hooks.txt:1971
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:1976
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:901
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187