Add @covers tags to includes/debug tests
[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 TestingAccessWrapper::newFromObject( $mockMethod )->matcher->parametersMatcher =
162 new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
163 [ $this->anything(), $this->anything(), [ 'words' ] ],
164 [ $this->anything(), $this->anything(), [ 'lines' ] ]
165 ] );
166
167 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
168 $formatter->expects( $this->any() )
169 ->method( 'format' )
170 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
171
172 $handler = new KafkaHandler( $produce, [] );
173 $handler->setFormatter( $formatter );
174 for ( $i = 0; $i < 3; ++$i ) {
175 $handler->handle( [
176 'channel' => 'foo',
177 'level' => Logger::EMERGENCY,
178 'extra' => [],
179 'context' => [],
180 ] );
181 }
182 }
183
184 public function testBatchHandlesNullFormatterResult() {
185 $produce = $this->getMockBuilder( 'Kafka\Produce' )
186 ->disableOriginalConstructor()
187 ->getMock();
188 $produce->expects( $this->any() )
189 ->method( 'getAvailablePartitions' )
190 ->will( $this->returnValue( [ 'A' ] ) );
191 $produce->expects( $this->once() )
192 ->method( 'setMessages' )
193 ->with( $this->anything(), $this->anything(), [ 'words', 'lines' ] );
194 $produce->expects( $this->any() )
195 ->method( 'send' )
196 ->will( $this->returnValue( true ) );
197
198 $formatter = $this->createMock( 'Monolog\Formatter\FormatterInterface' );
199 $formatter->expects( $this->any() )
200 ->method( 'format' )
201 ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
202
203 $handler = new KafkaHandler( $produce, [] );
204 $handler->setFormatter( $formatter );
205 $handler->handleBatch( [
206 [
207 'channel' => 'foo',
208 'level' => Logger::EMERGENCY,
209 'extra' => [],
210 'context' => [],
211 ],
212 [
213 'channel' => 'foo',
214 'level' => Logger::EMERGENCY,
215 'extra' => [],
216 'context' => [],
217 ],
218 [
219 'channel' => 'foo',
220 'level' => Logger::EMERGENCY,
221 'extra' => [],
222 'context' => [],
223 ],
224 ] );
225 }
226 }