Fix typo in KafkaHandlerTest
[lhc/web/wiklou.git] / tests / phpunit / includes / debug / logger / monolog / KafkaHandlerTest.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger\Monolog;
22
23 use MediaWikiTestCase;
24 use Monolog\Logger;
25
26 // not available in the version of phpunit mw uses, so copied into repo
27 require_once __DIR__ . '/../../../ConsecutiveParametersMatcher.php';
28
29 class KafkaHandlerTest extends MediaWikiTestCase {
30
31 public function setUp() {
32 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' ) ) {
33 $this->markTestSkipped( 'Monolog is required for the KafkaHandlerTest' );
34 }
35 parent::setUp();
36 }
37
38 public function topicNamingProvider() {
39 return array(
40 array( array(), 'monolog_foo' ),
41 array( array( 'alias' => array( 'foo' => 'bar' ) ), 'bar' )
42 );
43 }
44
45 /**
46 * @dataProvider topicNamingProvider
47 */
48 public function testTopicNaming( $options, $expect ) {
49 $produce = $this->getMockBuilder( 'Kafka\Produce' )
50 ->disableOriginalConstructor()
51 ->getMock();
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() );
58
59 $handler = new KafkaHandler( $produce, $options );
60 $handler->handle( array(
61 'channel' => 'foo',
62 'level' => Logger::EMERGENCY,
63 'extra' => array(),
64 ) );
65 }
66
67 public function swallowsExceptionsWhenRequested() {
68 return array(
69 // defaults to false
70 array( array(), true ),
71 // also try false explicitly
72 array( array( 'swallowExceptions' => false ), true ),
73 // turn it on
74 array( array( 'swallowExceptions' => true ), false ),
75 );
76 }
77
78 /**
79 * @dataProvider swallowsExceptionsWhenRequested
80 */
81 public function testGetAvailablePartitionsException( $options, $expectException ) {
82 $produce = $this->getMockBuilder( 'Kafka\Produce' )
83 ->disableOriginalConstructor()
84 ->getMock();
85 $produce->expects( $this->any() )
86 ->method( 'getAvailablePartitions' )
87 ->will( $this->throwException( new \Kafka\Exception ) );
88
89 if ( $expectException ) {
90 $this->setExpectedException( 'Kafka\Exception' );
91 }
92
93 $handler = new KafkaHandler( $produce, $options );
94 $handler->handle( array(
95 'channel' => 'foo',
96 'level' => Logger::EMERGENCY,
97 'extra' => array(),
98 ) );
99
100 if ( !$expectException ) {
101 $this->assertTrue( true, 'no exception was thrown' );
102 }
103 }
104
105 /**
106 * @dataProvider swallowsExceptionsWhenRequested
107 */
108 public function testSendException( $options, $expectException ) {
109 $produce = $this->getMockBuilder( 'Kafka\Produce' )
110 ->disableOriginalConstructor()
111 ->getMock();
112 $produce->expects( $this->any() )
113 ->method( 'getAvailablePartitions' )
114 ->will( $this->returnValue( array( 'A' ) ) );
115 $produce->expects( $this->any() )
116 ->method( 'send' )
117 ->will( $this->throwException( new \Kafka\Exception ) );
118
119 if ( $expectException ) {
120 $this->setExpectedException( 'Kafka\Exception' );
121 }
122
123 $handler = new KafkaHandler( $produce, $options );
124 $handler->handle( array(
125 'channel' => 'foo',
126 'level' => Logger::EMERGENCY,
127 'extra' => array(),
128 ) );
129
130 if ( !$expectException ) {
131 $this->assertTrue( true, 'no exception was thrown' );
132 }
133 }
134
135 public function testHandlesNullFormatterResult() {
136 $produce = $this->getMockBuilder( 'Kafka\Produce' )
137 ->disableOriginalConstructor()
138 ->getMock();
139 $produce->expects( $this->any() )
140 ->method( 'getAvailablePartitions' )
141 ->will( $this->returnValue( array( 'A' ) ) );
142 $mockMethod = $produce->expects( $this->exactly( 2 ) )
143 ->method( 'setMessages' );
144 // evil hax
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' ) )
149 ) );
150
151 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
152 $formatter->expects( $this->any() )
153 ->method( 'format' )
154 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
155
156 $handler = new KafkaHandler( $produce, array() );
157 $handler->setFormatter( $formatter );
158 for ( $i = 0; $i < 3; ++$i ) {
159 $handler->handle( array(
160 'channel' => 'foo',
161 'level' => Logger::EMERGENCY,
162 'extra' => array(),
163 ) );
164 }
165 }
166
167
168 public function testBatchHandlesNullFormatterResult() {
169 $produce = $this->getMockBuilder( 'Kafka\Produce' )
170 ->disableOriginalConstructor()
171 ->getMock();
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' ) );
178
179 $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
180 $formatter->expects( $this->any() )
181 ->method( 'format' )
182 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
183
184 $handler = new KafkaHandler( $produce, array() );
185 $handler->setFormatter( $formatter );
186 $handler->handleBatch( array(
187 array(
188 'channel' => 'foo',
189 'level' => Logger::EMERGENCY,
190 'extra' => array(),
191 ),
192 array(
193 'channel' => 'foo',
194 'level' => Logger::EMERGENCY,
195 'extra' => array(),
196 ),
197 array(
198 'channel' => 'foo',
199 'level' => Logger::EMERGENCY,
200 'extra' => array(),
201 ),
202 ) );
203 }
204 }