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.
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.
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
21 namespace MediaWiki\Logger\Monolog
;
23 use MediaWikiTestCase
;
26 class KafkaHandlerTest
extends MediaWikiTestCase
{
28 protected function setUp() {
29 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
30 ||
!class_exists( 'Kafka\Produce' )
32 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
38 public function topicNamingProvider() {
40 [ [], 'monolog_foo' ],
41 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
46 * @dataProvider topicNamingProvider
48 public function testTopicNaming( $options, $expect ) {
49 $produce = $this->getMockBuilder( 'Kafka\Produce' )
50 ->disableOriginalConstructor()
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() )
60 ->will( $this->returnValue( true ) );
62 $handler = new KafkaHandler( $produce, $options );
65 'level' => Logger
::EMERGENCY
,
71 public function swallowsExceptionsWhenRequested() {
75 // also try false explicitly
76 [ [ 'swallowExceptions' => false ], true ],
78 [ [ 'swallowExceptions' => true ], false ],
83 * @dataProvider swallowsExceptionsWhenRequested
85 public function testGetAvailablePartitionsException( $options, $expectException ) {
86 $produce = $this->getMockBuilder( 'Kafka\Produce' )
87 ->disableOriginalConstructor()
89 $produce->expects( $this->any() )
90 ->method( 'getAvailablePartitions' )
91 ->will( $this->throwException( new \Kafka\Exception
) );
92 $produce->expects( $this->any() )
94 ->will( $this->returnValue( true ) );
96 if ( $expectException ) {
97 $this->setExpectedException( 'Kafka\Exception' );
100 $handler = new KafkaHandler( $produce, $options );
103 'level' => Logger
::EMERGENCY
,
108 if ( !$expectException ) {
109 $this->assertTrue( true, 'no exception was thrown' );
114 * @dataProvider swallowsExceptionsWhenRequested
116 public function testSendException( $options, $expectException ) {
117 $produce = $this->getMockBuilder( 'Kafka\Produce' )
118 ->disableOriginalConstructor()
120 $produce->expects( $this->any() )
121 ->method( 'getAvailablePartitions' )
122 ->will( $this->returnValue( [ 'A' ] ) );
123 $produce->expects( $this->any() )
125 ->will( $this->throwException( new \Kafka\Exception
) );
127 if ( $expectException ) {
128 $this->setExpectedException( 'Kafka\Exception' );
131 $handler = new KafkaHandler( $produce, $options );
134 'level' => Logger
::EMERGENCY
,
139 if ( !$expectException ) {
140 $this->assertTrue( true, 'no exception was thrown' );
144 public function testHandlesNullFormatterResult() {
145 $produce = $this->getMockBuilder( 'Kafka\Produce' )
146 ->disableOriginalConstructor()
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() )
155 ->will( $this->returnValue( true ) );
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' ] ]
163 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
164 $formatter->expects( $this->any() )
166 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
168 $handler = new KafkaHandler( $produce, [] );
169 $handler->setFormatter( $formatter );
170 for ( $i = 0; $i < 3; ++
$i ) {
173 'level' => Logger
::EMERGENCY
,
180 public function testBatchHandlesNullFormatterResult() {
181 $produce = $this->getMockBuilder( 'Kafka\Produce' )
182 ->disableOriginalConstructor()
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() )
192 ->will( $this->returnValue( true ) );
194 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
195 $formatter->expects( $this->any() )
197 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
199 $handler = new KafkaHandler( $produce, [] );
200 $handler->setFormatter( $formatter );
201 $handler->handleBatch( [
204 'level' => Logger
::EMERGENCY
,
210 'level' => Logger
::EMERGENCY
,
216 'level' => Logger
::EMERGENCY
,