Fix KafkaHandlerTest hack to work with PHPUnit 6
[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 use Wikimedia\TestingAccessWrapper;
26
27 /**
28 * @covers \MediaWiki\Logger\Monolog\KafkaHandler
29 */
30 class KafkaHandlerTest extends MediaWikiTestCase {
31
32 protected function setUp() {
33 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
34 || !class_exists( 'Kafka\Produce' )
35 ) {
36 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
37 }
38
39 parent::setUp();
40 }
41
42 public function topicNamingProvider() {
43 return [
44 [ [], 'monolog_foo' ],
45 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
46 ];
47 }
48
49 /**
50 * @dataProvider topicNamingProvider
51 */
52 public function testTopicNaming( $options, $expect ) {
53 $produce = $this->getMockBuilder( 'Kafka\Produce' )
54 ->disableOriginalConstructor()
55 ->getMock();
56 $produce->expects( $this->any() )
57 ->method( 'getAvailablePartitions' )
58 ->will( $this->returnValue( [ 'A' ] ) );
59 $produce->expects( $this->once() )
60 ->method( 'setMessages' )
61 ->with( $expect, $this->anything(), $this->anything() );
62 $produce->expects( $this->any() )
63 ->method( 'send' )
64 ->will( $this->returnValue( true ) );
65
66 $handler = new KafkaHandler( $produce, $options );
67 $handler->handle( [
68 'channel' => 'foo',
69 'level' => Logger::EMERGENCY,
70 'extra' => [],
71 'context' => [],
72 ] );
73 }
74
75 public function swallowsExceptionsWhenRequested() {
76 return [
77 // defaults to false
78 [ [], true ],
79 // also try false explicitly
80 [ [ 'swallowExceptions' => false ], true ],
81 // turn it on
82 [ [ 'swallowExceptions' => true ], false ],
83 ];
84 }
85
86 /**
87 * @dataProvider swallowsExceptionsWhenRequested
88 */
89 public function testGetAvailablePartitionsException( $options, $expectException ) {
90 $produce = $this->getMockBuilder( 'Kafka\Produce' )
91 ->disableOriginalConstructor()
92 ->getMock();
93 $produce->expects( $this->any() )
94 ->method( 'getAvailablePartitions' )
95 ->will( $this->throwException( new \Kafka\Exception ) );
96 $produce->expects( $this->any() )
97 ->method( 'send' )
98 ->will( $this->returnValue( true ) );
99
100 if ( $expectException ) {
101 $this->setExpectedException( 'Kafka\Exception' );
102 }
103
104 $handler = new KafkaHandler( $produce, $options );
105 $handler->handle( [
106 'channel' => 'foo',
107 'level' => Logger::EMERGENCY,
108 'extra' => [],
109 'context' => [],
110 ] );
111
112 if ( !$expectException ) {
113 $this->assertTrue( true, 'no exception was thrown' );
114 }
115 }
116
117 /**
118 * @dataProvider swallowsExceptionsWhenRequested
119 */
120 public function testSendException( $options, $expectException ) {
121 $produce = $this->getMockBuilder( 'Kafka\Produce' )
122 ->disableOriginalConstructor()
123 ->getMock();
124 $produce->expects( $this->any() )
125 ->method( 'getAvailablePartitions' )
126 ->will( $this->returnValue( [ 'A' ] ) );
127 $produce->expects( $this->any() )
128 ->method( 'send' )
129 ->will( $this->throwException( new \Kafka\Exception ) );
130
131 if ( $expectException ) {
132 $this->setExpectedException( 'Kafka\Exception' );
133 }
134
135 $handler = new KafkaHandler( $produce, $options );
136 $handler->handle( [
137 'channel' => 'foo',
138 'level' => Logger::EMERGENCY,
139 'extra' => [],
140 'context' => [],
141 ] );
142
143 if ( !$expectException ) {
144 $this->assertTrue( true, 'no exception was thrown' );
145 }
146 }
147
148 public function testHandlesNullFormatterResult() {
149 $produce = $this->getMockBuilder( 'Kafka\Produce' )
150 ->disableOriginalConstructor()
151 ->getMock();
152 $produce->expects( $this->any() )
153 ->method( 'getAvailablePartitions' )
154 ->will( $this->returnValue( [ 'A' ] ) );
155 $mockMethod = $produce->expects( $this->exactly( 2 ) )
156 ->method( 'setMessages' );
157 $produce->expects( $this->any() )
158 ->method( 'send' )
159 ->will( $this->returnValue( true ) );
160 // evil hax
161 $matcher = TestingAccessWrapper::newFromObject( $mockMethod )->matcher;
162 TestingAccessWrapper::newFromObject( $matcher )->parametersMatcher =
163 new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
164 [ $this->anything(), $this->anything(), [ 'words' ] ],
165 [ $this->anything(), $this->anything(), [ 'lines' ] ]
166 ] );
167
168 $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface::class );
169 $formatter->expects( $this->any() )
170 ->method( 'format' )
171 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
172
173 $handler = new KafkaHandler( $produce, [] );
174 $handler->setFormatter( $formatter );
175 for ( $i = 0; $i < 3; ++$i ) {
176 $handler->handle( [
177 'channel' => 'foo',
178 'level' => Logger::EMERGENCY,
179 'extra' => [],
180 'context' => [],
181 ] );
182 }
183 }
184
185 public function testBatchHandlesNullFormatterResult() {
186 $produce = $this->getMockBuilder( 'Kafka\Produce' )
187 ->disableOriginalConstructor()
188 ->getMock();
189 $produce->expects( $this->any() )
190 ->method( 'getAvailablePartitions' )
191 ->will( $this->returnValue( [ 'A' ] ) );
192 $produce->expects( $this->once() )
193 ->method( 'setMessages' )
194 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
195 $produce->expects( $this->any() )
196 ->method( 'send' )
197 ->will( $this->returnValue( true ) );
198
199 $formatter = $this->createMock( \Monolog\Formatter\FormatterInterface::class );
200 $formatter->expects( $this->any() )
201 ->method( 'format' )
202 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
203
204 $handler = new KafkaHandler( $produce, [] );
205 $handler->setFormatter( $formatter );
206 $handler->handleBatch( [
207 [
208 'channel' => 'foo',
209 'level' => Logger::EMERGENCY,
210 'extra' => [],
211 'context' => [],
212 ],
213 [
214 'channel' => 'foo',
215 'level' => Logger::EMERGENCY,
216 'extra' => [],
217 'context' => [],
218 ],
219 [
220 'channel' => 'foo',
221 'level' => Logger::EMERGENCY,
222 'extra' => [],
223 'context' => [],
224 ],
225 ] );
226 }
227 }