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() );
59 $handler = new KafkaHandler( $produce, $options );
62 'level' => Logger
::EMERGENCY
,
68 public function swallowsExceptionsWhenRequested() {
72 // also try false explicitly
73 [ [ 'swallowExceptions' => false ], true ],
75 [ [ 'swallowExceptions' => true ], false ],
80 * @dataProvider swallowsExceptionsWhenRequested
82 public function testGetAvailablePartitionsException( $options, $expectException ) {
83 $produce = $this->getMockBuilder( 'Kafka\Produce' )
84 ->disableOriginalConstructor()
86 $produce->expects( $this->any() )
87 ->method( 'getAvailablePartitions' )
88 ->will( $this->throwException( new \Kafka\Exception
) );
90 if ( $expectException ) {
91 $this->setExpectedException( 'Kafka\Exception' );
94 $handler = new KafkaHandler( $produce, $options );
97 'level' => Logger
::EMERGENCY
,
102 if ( !$expectException ) {
103 $this->assertTrue( true, 'no exception was thrown' );
108 * @dataProvider swallowsExceptionsWhenRequested
110 public function testSendException( $options, $expectException ) {
111 $produce = $this->getMockBuilder( 'Kafka\Produce' )
112 ->disableOriginalConstructor()
114 $produce->expects( $this->any() )
115 ->method( 'getAvailablePartitions' )
116 ->will( $this->returnValue( [ 'A' ] ) );
117 $produce->expects( $this->any() )
119 ->will( $this->throwException( new \Kafka\Exception
) );
121 if ( $expectException ) {
122 $this->setExpectedException( 'Kafka\Exception' );
125 $handler = new KafkaHandler( $produce, $options );
128 'level' => Logger
::EMERGENCY
,
133 if ( !$expectException ) {
134 $this->assertTrue( true, 'no exception was thrown' );
138 public function testHandlesNullFormatterResult() {
139 $produce = $this->getMockBuilder( 'Kafka\Produce' )
140 ->disableOriginalConstructor()
142 $produce->expects( $this->any() )
143 ->method( 'getAvailablePartitions' )
144 ->will( $this->returnValue( [ 'A' ] ) );
145 $mockMethod = $produce->expects( $this->exactly( 2 ) )
146 ->method( 'setMessages' );
148 \TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
->parametersMatcher
=
149 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
150 [ $this->anything(), $this->anything(), [ 'words' ] ],
151 [ $this->anything(), $this->anything(), [ 'lines' ] ]
154 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
155 $formatter->expects( $this->any() )
157 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
159 $handler = new KafkaHandler( $produce, [] );
160 $handler->setFormatter( $formatter );
161 for ( $i = 0; $i < 3; ++
$i ) {
164 'level' => Logger
::EMERGENCY
,
171 public function testBatchHandlesNullFormatterResult() {
172 $produce = $this->getMockBuilder( 'Kafka\Produce' )
173 ->disableOriginalConstructor()
175 $produce->expects( $this->any() )
176 ->method( 'getAvailablePartitions' )
177 ->will( $this->returnValue( [ 'A' ] ) );
178 $produce->expects( $this->once() )
179 ->method( 'setMessages' )
180 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
182 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
183 $formatter->expects( $this->any() )
185 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
187 $handler = new KafkaHandler( $produce, [] );
188 $handler->setFormatter( $formatter );
189 $handler->handleBatch( [
192 'level' => Logger
::EMERGENCY
,
198 'level' => Logger
::EMERGENCY
,
204 'level' => Logger
::EMERGENCY
,