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
;
28 * @covers \MediaWiki\Logger\Monolog\KafkaHandler
30 class KafkaHandlerTest
extends MediaWikiTestCase
{
32 protected function setUp() {
33 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
34 ||
!class_exists( 'Kafka\Produce' )
36 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
42 public function topicNamingProvider() {
44 [ [], 'monolog_foo' ],
45 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
50 * @dataProvider topicNamingProvider
52 public function testTopicNaming( $options, $expect ) {
53 $produce = $this->getMockBuilder( 'Kafka\Produce' )
54 ->disableOriginalConstructor()
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() )
64 ->will( $this->returnValue( true ) );
66 $handler = new KafkaHandler( $produce, $options );
69 'level' => Logger
::EMERGENCY
,
75 public function swallowsExceptionsWhenRequested() {
79 // also try false explicitly
80 [ [ 'swallowExceptions' => false ], true ],
82 [ [ 'swallowExceptions' => true ], false ],
87 * @dataProvider swallowsExceptionsWhenRequested
89 public function testGetAvailablePartitionsException( $options, $expectException ) {
90 $produce = $this->getMockBuilder( 'Kafka\Produce' )
91 ->disableOriginalConstructor()
93 $produce->expects( $this->any() )
94 ->method( 'getAvailablePartitions' )
95 ->will( $this->throwException( new \Kafka\Exception
) );
96 $produce->expects( $this->any() )
98 ->will( $this->returnValue( true ) );
100 if ( $expectException ) {
101 $this->setExpectedException( 'Kafka\Exception' );
104 $handler = new KafkaHandler( $produce, $options );
107 'level' => Logger
::EMERGENCY
,
112 if ( !$expectException ) {
113 $this->assertTrue( true, 'no exception was thrown' );
118 * @dataProvider swallowsExceptionsWhenRequested
120 public function testSendException( $options, $expectException ) {
121 $produce = $this->getMockBuilder( 'Kafka\Produce' )
122 ->disableOriginalConstructor()
124 $produce->expects( $this->any() )
125 ->method( 'getAvailablePartitions' )
126 ->will( $this->returnValue( [ 'A' ] ) );
127 $produce->expects( $this->any() )
129 ->will( $this->throwException( new \Kafka\Exception
) );
131 if ( $expectException ) {
132 $this->setExpectedException( 'Kafka\Exception' );
135 $handler = new KafkaHandler( $produce, $options );
138 'level' => Logger
::EMERGENCY
,
143 if ( !$expectException ) {
144 $this->assertTrue( true, 'no exception was thrown' );
148 public function testHandlesNullFormatterResult() {
149 $produce = $this->getMockBuilder( 'Kafka\Produce' )
150 ->disableOriginalConstructor()
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() )
159 ->will( $this->returnValue( true ) );
161 TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
->parametersMatcher
=
162 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
163 [ $this->anything(), $this->anything(), [ 'words' ] ],
164 [ $this->anything(), $this->anything(), [ 'lines' ] ]
167 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
168 $formatter->expects( $this->any() )
170 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
172 $handler = new KafkaHandler( $produce, [] );
173 $handler->setFormatter( $formatter );
174 for ( $i = 0; $i < 3; ++
$i ) {
177 'level' => Logger
::EMERGENCY
,
184 public function testBatchHandlesNullFormatterResult() {
185 $produce = $this->getMockBuilder( 'Kafka\Produce' )
186 ->disableOriginalConstructor()
188 $produce->expects( $this->any() )
189 ->method( 'getAvailablePartitions' )
190 ->will( $this->returnValue( [ 'A' ] ) );
191 $produce->expects( $this->once() )
192 ->method( 'setMessages' )
193 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
194 $produce->expects( $this->any() )
196 ->will( $this->returnValue( true ) );
198 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
199 $formatter->expects( $this->any() )
201 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
203 $handler = new KafkaHandler( $produce, [] );
204 $handler->setFormatter( $formatter );
205 $handler->handleBatch( [
208 'level' => Logger
::EMERGENCY
,
214 'level' => Logger
::EMERGENCY
,
220 'level' => Logger
::EMERGENCY
,