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