Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / unit / 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 Monolog\Logger;
24 use Wikimedia\TestingAccessWrapper;
25
26 /**
27 * @covers \MediaWiki\Logger\Monolog\KafkaHandler
28 */
29 class KafkaHandlerTest extends \MediaWikiUnitTestCase {
30
31 protected function setUp() {
32 if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
33 || !class_exists( 'Kafka\Produce' )
34 ) {
35 $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
36 }
37
38 parent::setUp();
39 }
40
41 public function topicNamingProvider() {
42 return [
43 [ [], 'monolog_foo' ],
44 [ [ 'alias' => [ 'foo' => 'bar' ] ], 'bar' ]
45 ];
46 }
47
48 /**
49 * @dataProvider topicNamingProvider
50 */
51 public function testTopicNaming( $options, $expect ) {
52 $produce = $this->getMockBuilder( 'Kafka\Produce' )
53 ->disableOriginalConstructor()
54 ->getMock();
55 $produce->expects( $this->any() )
56 ->method( 'getAvailablePartitions' )
57 ->will( $this->returnValue( [ 'A' ] ) );
58 $produce->expects( $this->once() )
59 ->method( 'setMessages' )
60 ->with( $expect, $this->anything(), $this->anything() );
61 $produce->expects( $this->any() )
62 ->method( 'send' )
63 ->will( $this->returnValue( true ) );
64
65 $handler = new KafkaHandler( $produce, $options );
66 $handler->handle( [
67 'channel' => 'foo',
68 'level' => Logger::EMERGENCY,
69 'extra' => [],
70 'context' => [],
71 ] );
72 }
73
74 public function swallowsExceptionsWhenRequested() {
75 return [
76 // defaults to false
77 [ [], true ],
78 // also try false explicitly
79 [ [ 'swallowExceptions' => false ], true ],
80 // turn it on
81 [ [ 'swallowExceptions' => true ], false ],
82 ];
83 }
84
85 /**
86 * @dataProvider swallowsExceptionsWhenRequested
87 */
88 public function testGetAvailablePartitionsException( $options, $expectException ) {
89 $produce = $this->getMockBuilder( 'Kafka\Produce' )
90 ->disableOriginalConstructor()
91 ->getMock();
92 $produce->expects( $this->any() )
93 ->method( 'getAvailablePartitions' )
94 ->will( $this->throwException( new \Kafka\Exception ) );
95 $produce->expects( $this->any() )
96 ->method( 'send' )
97 ->will( $this->returnValue( true ) );
98
99 if ( $expectException ) {
100 $this->setExpectedException( 'Kafka\Exception' );
101 }
102
103 $handler = new KafkaHandler( $produce, $options );
104 $handler->handle( [
105 'channel' => 'foo',
106 'level' => Logger::EMERGENCY,
107 'extra' => [],
108 'context' => [],
109 ] );
110
111 if ( !$expectException ) {
112 $this->assertTrue( true, 'no exception was thrown' );
113 }
114 }
115
116 /**
117 * @dataProvider swallowsExceptionsWhenRequested
118 */
119 public function testSendException( $options, $expectException ) {
120 $produce = $this->getMockBuilder( 'Kafka\Produce' )
121 ->disableOriginalConstructor()
122 ->getMock();
123 $produce->expects( $this->any() )
124 ->method( 'getAvailablePartitions' )
125 ->will( $this->returnValue( [ 'A' ] ) );
126 $produce->expects( $this->any() )
127 ->method( 'send' )
128 ->will( $this->throwException( new \Kafka\Exception ) );
129
130 if ( $expectException ) {
131 $this->setExpectedException( 'Kafka\Exception' );
132 }
133
134 $handler = new KafkaHandler( $produce, $options );
135 $handler->handle( [
136 'channel' => 'foo',
137 'level' => Logger::EMERGENCY,
138 'extra' => [],
139 'context' => [],
140 ] );
141
142 if ( !$expectException ) {
143 $this->assertTrue( true, 'no exception was thrown' );
144 }
145 }
146
147 public function testHandlesNullFormatterResult() {
148 $produce = $this->getMockBuilder( 'Kafka\Produce' )
149 ->disableOriginalConstructor()
150 ->getMock();
151 $produce->expects( $this->any() )
152 ->method( 'getAvailablePartitions' )
153 ->will( $this->returnValue( [ 'A' ] ) );
154 $mockMethod = $produce->expects( $this->exactly( 2 ) )
155 ->method( 'setMessages' );
156 $produce->expects( $this->any() )
157 ->method( 'send' )
158 ->will( $this->returnValue( true ) );
159 // evil hax
160 $matcher = TestingAccessWrapper::newFromObject( $mockMethod )->matcher;
161 TestingAccessWrapper::newFromObject( $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::class );
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::class );
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 }