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 public function setUo() {
32 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' ) ) {
33 $this->markTestSkipped( 'Monolog is required for the KafkaHandlerTest' );
38 public function topicNamingProvider() {
40 array( array(), 'monolog_foo' ),
41 array( array( 'alias' => array( '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( array( 'A' ) ) );
55 $produce->expects($this->once())
56 ->method( 'setMessages' )
57 ->with( $expect, $this->anything(), $this->anything() );
59 $handler = new KafkaHandler( $produce, $options );
60 $handler->handle( array(
62 'level' => Logger
::EMERGENCY
,
67 public function swallowsExceptionsWhenRequested() {
70 array( array(), true ),
71 // also try false explicitly
72 array( array( 'swallowExceptions' => false ), true ),
74 array( array( 'swallowExceptions' => true ), false ),
79 * @dataProvider swallowsExceptionsWhenRequested
81 public function testGetAvailablePartitionsException( $options, $expectException ) {
82 $produce = $this->getMockBuilder( 'Kafka\Produce' )
83 ->disableOriginalConstructor()
85 $produce->expects( $this->any() )
86 ->method( 'getAvailablePartitions' )
87 ->will( $this->throwException( new \Kafka\Exception
) );
89 if ( $expectException ) {
90 $this->setExpectedException( 'Kafka\Exception' );
93 $handler = new KafkaHandler( $produce, $options );
94 $handler->handle( array(
96 'level' => Logger
::EMERGENCY
,
100 if ( !$expectException ) {
101 $this->assertTrue( true, 'no exception was thrown' );
106 * @dataProvider swallowsExceptionsWhenRequested
108 public function testSendException( $options, $expectException ) {
109 $produce = $this->getMockBuilder( 'Kafka\Produce' )
110 ->disableOriginalConstructor()
112 $produce->expects( $this->any() )
113 ->method( 'getAvailablePartitions' )
114 ->will( $this->returnValue( array( 'A' ) ) );
115 $produce->expects( $this->any() )
117 ->will( $this->throwException( new \Kafka\Exception
) );
119 if ( $expectException ) {
120 $this->setExpectedException( 'Kafka\Exception' );
123 $handler = new KafkaHandler( $produce, $options );
124 $handler->handle( array(
126 'level' => Logger
::EMERGENCY
,
130 if ( !$expectException ) {
131 $this->assertTrue( true, 'no exception was thrown' );
135 public function testHandlesNullFormatterResult() {
136 $produce = $this->getMockBuilder( 'Kafka\Produce' )
137 ->disableOriginalConstructor()
139 $produce->expects( $this->any() )
140 ->method( 'getAvailablePartitions' )
141 ->will( $this->returnValue( array( 'A' ) ) );
142 $mockMethod = $produce->expects( $this->exactly( 2 ) )
143 ->method( 'setMessages' );
145 \TestingAccessWrapper
::newFromObject( $mockMethod )->matcher
->parametersMatcher
=
146 new \
PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( array(
147 array( $this->anything(), $this->anything(), array( 'words' ) ),
148 array( $this->anything(), $this->anything(), array( 'lines' ) )
151 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
152 $formatter->expects( $this->any() )
154 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
156 $handler = new KafkaHandler( $produce, array() );
157 $handler->setFormatter( $formatter );
158 for ( $i = 0; $i < 3; ++
$i ) {
159 $handler->handle( array(
161 'level' => Logger
::EMERGENCY
,
168 public function testBatchHandlesNullFormatterResult() {
169 $produce = $this->getMockBuilder( 'Kafka\Produce' )
170 ->disableOriginalConstructor()
172 $produce->expects( $this->any() )
173 ->method( 'getAvailablePartitions' )
174 ->will( $this->returnValue( array( 'A' ) ) );
175 $produce->expects( $this->once() )
176 ->method( 'setMessages' )
177 ->with( $this->anything(), $this->anything(), array( 'words', 'lines' ) );
179 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
180 $formatter->expects( $this->any() )
182 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
184 $handler = new KafkaHandler( $produce, array() );
185 $handler->setFormatter( $formatter );
186 $handler->handleBatch( array(
189 'level' => Logger
::EMERGENCY
,
194 'level' => Logger
::EMERGENCY
,
199 'level' => Logger
::EMERGENCY
,