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