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 $matcher = TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
;
162 TestingAccessWrapper
::newFromObject( $matcher )->parametersMatcher
=
163 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
164 [ $this->anything(), $this->anything(), [ 'words' ] ],
165 [ $this->anything(), $this->anything(), [ 'lines' ] ]
168 $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface
::class );
169 $formatter->expects( $this->any() )
171 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
173 $handler = new KafkaHandler( $produce, [] );
174 $handler->setFormatter( $formatter );
175 for ( $i = 0; $i < 3; ++
$i ) {
178 'level' => Logger
::EMERGENCY
,
185 public function testBatchHandlesNullFormatterResult() {
186 $produce = $this->getMockBuilder( 'Kafka\Produce' )
187 ->disableOriginalConstructor()
189 $produce->expects( $this->any() )
190 ->method( 'getAvailablePartitions' )
191 ->will( $this->returnValue( [ 'A' ] ) );
192 $produce->expects( $this->once() )
193 ->method( 'setMessages' )
194 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
195 $produce->expects( $this->any() )
197 ->will( $this->returnValue( true ) );
199 $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface
::class );
200 $formatter->expects( $this->any() )
202 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
204 $handler = new KafkaHandler( $produce, [] );
205 $handler->setFormatter( $formatter );
206 $handler->handleBatch( [
209 'level' => Logger
::EMERGENCY
,
215 'level' => Logger
::EMERGENCY
,
221 'level' => Logger
::EMERGENCY
,