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 // not available in the version of phpunit mw uses, so copied into repo
27 require_once __DIR__
. '/../../../phpunit/ConsecutiveParametersMatcher.php';
29 class KafkaHandlerTest
extends MediaWikiTestCase
{
31 protected function setUp() {
32 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
33 ||
!class_exists( 'Kafka\Produce' )
35 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
41 public function topicNamingProvider() {
43 [ [], 'monolog_foo' ],
44 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
49 * @dataProvider topicNamingProvider
51 public function testTopicNaming( $options, $expect ) {
52 $produce = $this->getMockBuilder( 'Kafka\Produce' )
53 ->disableOriginalConstructor()
55 $produce->expects( $this->any() )
56 ->method( 'getAvailablePartitions' )
57 ->will( $this->returnValue( [ 'A' ] ) );
58 $produce->expects( $this->once() )
59 ->method( 'setMessages' )
60 ->with( $expect, $this->anything(), $this->anything() );
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
) );
93 if ( $expectException ) {
94 $this->setExpectedException( 'Kafka\Exception' );
97 $handler = new KafkaHandler( $produce, $options );
100 'level' => Logger
::EMERGENCY
,
105 if ( !$expectException ) {
106 $this->assertTrue( true, 'no exception was thrown' );
111 * @dataProvider swallowsExceptionsWhenRequested
113 public function testSendException( $options, $expectException ) {
114 $produce = $this->getMockBuilder( 'Kafka\Produce' )
115 ->disableOriginalConstructor()
117 $produce->expects( $this->any() )
118 ->method( 'getAvailablePartitions' )
119 ->will( $this->returnValue( [ 'A' ] ) );
120 $produce->expects( $this->any() )
122 ->will( $this->throwException( new \Kafka\Exception
) );
124 if ( $expectException ) {
125 $this->setExpectedException( 'Kafka\Exception' );
128 $handler = new KafkaHandler( $produce, $options );
131 'level' => Logger
::EMERGENCY
,
136 if ( !$expectException ) {
137 $this->assertTrue( true, 'no exception was thrown' );
141 public function testHandlesNullFormatterResult() {
142 $produce = $this->getMockBuilder( 'Kafka\Produce' )
143 ->disableOriginalConstructor()
145 $produce->expects( $this->any() )
146 ->method( 'getAvailablePartitions' )
147 ->will( $this->returnValue( [ 'A' ] ) );
148 $mockMethod = $produce->expects( $this->exactly( 2 ) )
149 ->method( 'setMessages' );
151 \TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
->parametersMatcher
=
152 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
153 [ $this->anything(), $this->anything(), [ 'words' ] ],
154 [ $this->anything(), $this->anything(), [ 'lines' ] ]
157 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
158 $formatter->expects( $this->any() )
160 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
162 $handler = new KafkaHandler( $produce, [] );
163 $handler->setFormatter( $formatter );
164 for ( $i = 0; $i < 3; ++
$i ) {
167 'level' => Logger
::EMERGENCY
,
174 public function testBatchHandlesNullFormatterResult() {
175 $produce = $this->getMockBuilder( 'Kafka\Produce' )
176 ->disableOriginalConstructor()
178 $produce->expects( $this->any() )
179 ->method( 'getAvailablePartitions' )
180 ->will( $this->returnValue( [ 'A' ] ) );
181 $produce->expects( $this->once() )
182 ->method( 'setMessages' )
183 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
185 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
186 $formatter->expects( $this->any() )
188 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
190 $handler = new KafkaHandler( $produce, [] );
191 $handler->setFormatter( $formatter );
192 $handler->handleBatch( [
195 'level' => Logger
::EMERGENCY
,
201 'level' => Logger
::EMERGENCY
,
207 'level' => Logger
::EMERGENCY
,