Set visibility of monolog tests setUp method to protected
[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__ . '/../../../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 array(
43 array( array(), 'monolog_foo' ),
44 array( array( 'alias' => array( '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( array( '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( array(
64 'channel' => 'foo',
65 'level' => Logger::EMERGENCY,
66 'extra' => array(),
67 ) );
68 }
69
70 public function swallowsExceptionsWhenRequested() {
71 return array(
72 // defaults to false
73 array( array(), true ),
74 // also try false explicitly
75 array( array( 'swallowExceptions' => false ), true ),
76 // turn it on
77 array( array( 'swallowExceptions' => true ), false ),
78 );
79 }
80
81 /**
82 * @dataProvider swallowsExceptionsWhenRequested
83 */
84 public function testGetAvailablePartitionsException( $options, $expectException ) {
85 $produce = $this->getMockBuilder( 'Kafka\Produce' )
86 ->disableOriginalConstructor()
87 ->getMock();
88 $produce->expects( $this->any() )
89 ->method( 'getAvailablePartitions' )
90 ->will( $this->throwException( new \Kafka\Exception ) );
91
92 if ( $expectException ) {
93 $this->setExpectedException( 'Kafka\Exception' );
94 }
95
96 $handler = new KafkaHandler( $produce, $options );
97 $handler->handle( array(
98 'channel' => 'foo',
99 'level' => Logger::EMERGENCY,
100 'extra' => array(),
101 ) );
102
103 if ( !$expectException ) {
104 $this->assertTrue( true, 'no exception was thrown' );
105 }
106 }
107
108 /**
109 * @dataProvider swallowsExceptionsWhenRequested
110 */
111 public function testSendException( $options, $expectException ) {
112 $produce = $this->getMockBuilder( 'Kafka\Produce' )
113 ->disableOriginalConstructor()
114 ->getMock();
115 $produce->expects( $this->any() )
116 ->method( 'getAvailablePartitions' )
117 ->will( $this->returnValue( array( 'A' ) ) );
118 $produce->expects( $this->any() )
119 ->method( 'send' )
120 ->will( $this->throwException( new \Kafka\Exception ) );
121
122 if ( $expectException ) {
123 $this->setExpectedException( 'Kafka\Exception' );
124 }
125
126 $handler = new KafkaHandler( $produce, $options );
127 $handler->handle( array(
128 'channel' => 'foo',
129 'level' => Logger::EMERGENCY,
130 'extra' => array(),
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( array( '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( array(
150 array( $this->anything(), $this->anything(), array( 'words' ) ),
151 array( $this->anything(), $this->anything(), array( '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, array() );
160 $handler->setFormatter( $formatter );
161 for ( $i = 0; $i < 3; ++$i ) {
162 $handler->handle( array(
163 'channel' => 'foo',
164 'level' => Logger::EMERGENCY,
165 'extra' => array(),
166 ) );
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( array( 'A' ) ) );
178 $produce->expects( $this->once() )
179 ->method( 'setMessages' )
180 ->with( $this->anything(), $this->anything(), array( '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, array() );
188 $handler->setFormatter( $formatter );
189 $handler->handleBatch( array(
190 array(
191 'channel' => 'foo',
192 'level' => Logger::EMERGENCY,
193 'extra' => array(),
194 ),
195 array(
196 'channel' => 'foo',
197 'level' => Logger::EMERGENCY,
198 'extra' => array(),
199 ),
200 array(
201 'channel' => 'foo',
202 'level' => Logger::EMERGENCY,
203 'extra' => array(),
204 ),
205 ) );
206 }
207 }