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
;
25 use Wikimedia\TestingAccessWrapper
;
27 class KafkaHandlerTest
extends MediaWikiTestCase
{
29 protected function setUp() {
30 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
31 ||
!class_exists( 'Kafka\Produce' )
33 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
39 public function topicNamingProvider() {
41 [ [], 'monolog_foo' ],
42 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
47 * @dataProvider topicNamingProvider
49 public function testTopicNaming( $options, $expect ) {
50 $produce = $this->getMockBuilder( 'Kafka\Produce' )
51 ->disableOriginalConstructor()
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() )
61 ->will( $this->returnValue( true ) );
63 $handler = new KafkaHandler( $produce, $options );
66 'level' => Logger
::EMERGENCY
,
72 public function swallowsExceptionsWhenRequested() {
76 // also try false explicitly
77 [ [ 'swallowExceptions' => false ], true ],
79 [ [ 'swallowExceptions' => true ], false ],
84 * @dataProvider swallowsExceptionsWhenRequested
86 public function testGetAvailablePartitionsException( $options, $expectException ) {
87 $produce = $this->getMockBuilder( 'Kafka\Produce' )
88 ->disableOriginalConstructor()
90 $produce->expects( $this->any() )
91 ->method( 'getAvailablePartitions' )
92 ->will( $this->throwException( new \Kafka\Exception
) );
93 $produce->expects( $this->any() )
95 ->will( $this->returnValue( true ) );
97 if ( $expectException ) {
98 $this->setExpectedException( 'Kafka\Exception' );
101 $handler = new KafkaHandler( $produce, $options );
104 'level' => Logger
::EMERGENCY
,
109 if ( !$expectException ) {
110 $this->assertTrue( true, 'no exception was thrown' );
115 * @dataProvider swallowsExceptionsWhenRequested
117 public function testSendException( $options, $expectException ) {
118 $produce = $this->getMockBuilder( 'Kafka\Produce' )
119 ->disableOriginalConstructor()
121 $produce->expects( $this->any() )
122 ->method( 'getAvailablePartitions' )
123 ->will( $this->returnValue( [ 'A' ] ) );
124 $produce->expects( $this->any() )
126 ->will( $this->throwException( new \Kafka\Exception
) );
128 if ( $expectException ) {
129 $this->setExpectedException( 'Kafka\Exception' );
132 $handler = new KafkaHandler( $produce, $options );
135 'level' => Logger
::EMERGENCY
,
140 if ( !$expectException ) {
141 $this->assertTrue( true, 'no exception was thrown' );
145 public function testHandlesNullFormatterResult() {
146 $produce = $this->getMockBuilder( 'Kafka\Produce' )
147 ->disableOriginalConstructor()
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() )
156 ->will( $this->returnValue( true ) );
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' ] ]
164 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
165 $formatter->expects( $this->any() )
167 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
169 $handler = new KafkaHandler( $produce, [] );
170 $handler->setFormatter( $formatter );
171 for ( $i = 0; $i < 3; ++
$i ) {
174 'level' => Logger
::EMERGENCY
,
181 public function testBatchHandlesNullFormatterResult() {
182 $produce = $this->getMockBuilder( 'Kafka\Produce' )
183 ->disableOriginalConstructor()
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() )
193 ->will( $this->returnValue( true ) );
195 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
196 $formatter->expects( $this->any() )
198 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
200 $handler = new KafkaHandler( $produce, [] );
201 $handler->setFormatter( $formatter );
202 $handler->handleBatch( [
205 'level' => Logger
::EMERGENCY
,
211 'level' => Logger
::EMERGENCY
,
217 'level' => Logger
::EMERGENCY
,