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__
. '/../../../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 array( array(), 'monolog_foo' ),
44 array( array( 'alias' => array( '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( array( 'A' ) ) );
58 $produce->expects($this->once())
59 ->method( 'setMessages' )
60 ->with( $expect, $this->anything(), $this->anything() );
62 $handler = new KafkaHandler( $produce, $options );
63 $handler->handle( array(
65 'level' => Logger
::EMERGENCY
,
70 public function swallowsExceptionsWhenRequested() {
73 array( array(), true ),
74 // also try false explicitly
75 array( array( 'swallowExceptions' => false ), true ),
77 array( array( 'swallowExceptions' => true ), false ),
82 * @dataProvider swallowsExceptionsWhenRequested
84 public function testGetAvailablePartitionsException( $options, $expectException ) {
85 $produce = $this->getMockBuilder( 'Kafka\Produce' )
86 ->disableOriginalConstructor()
88 $produce->expects( $this->any() )
89 ->method( 'getAvailablePartitions' )
90 ->will( $this->throwException( new \Kafka\Exception
) );
92 if ( $expectException ) {
93 $this->setExpectedException( 'Kafka\Exception' );
96 $handler = new KafkaHandler( $produce, $options );
97 $handler->handle( array(
99 'level' => Logger
::EMERGENCY
,
103 if ( !$expectException ) {
104 $this->assertTrue( true, 'no exception was thrown' );
109 * @dataProvider swallowsExceptionsWhenRequested
111 public function testSendException( $options, $expectException ) {
112 $produce = $this->getMockBuilder( 'Kafka\Produce' )
113 ->disableOriginalConstructor()
115 $produce->expects( $this->any() )
116 ->method( 'getAvailablePartitions' )
117 ->will( $this->returnValue( array( 'A' ) ) );
118 $produce->expects( $this->any() )
120 ->will( $this->throwException( new \Kafka\Exception
) );
122 if ( $expectException ) {
123 $this->setExpectedException( 'Kafka\Exception' );
126 $handler = new KafkaHandler( $produce, $options );
127 $handler->handle( array(
129 '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( array( 'A' ) ) );
145 $mockMethod = $produce->expects( $this->exactly( 2 ) )
146 ->method( 'setMessages' );
148 \TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
->parametersMatcher
=
149 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( array(
150 array( $this->anything(), $this->anything(), array( 'words' ) ),
151 array( $this->anything(), $this->anything(), array( '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, array() );
160 $handler->setFormatter( $formatter );
161 for ( $i = 0; $i < 3; ++
$i ) {
162 $handler->handle( array(
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( array( 'A' ) ) );
178 $produce->expects( $this->once() )
179 ->method( 'setMessages' )
180 ->with( $this->anything(), $this->anything(), array( '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, array() );
188 $handler->setFormatter( $formatter );
189 $handler->handleBatch( array(
192 'level' => Logger
::EMERGENCY
,
197 'level' => Logger
::EMERGENCY
,
202 'level' => Logger
::EMERGENCY
,