scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 use Monolog\TestCase;
15 use Monolog\Logger;
16 use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
17 use Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy;
18 use Psr\Log\LogLevel;
19  
20 class FingersCrossedHandlerTest extends TestCase
21 {
22 /**
23 * @covers Monolog\Handler\FingersCrossedHandler::__construct
24 * @covers Monolog\Handler\FingersCrossedHandler::handle
25 * @covers Monolog\Handler\FingersCrossedHandler::activate
26 */
27 public function testHandleBuffers()
28 {
29 $test = new TestHandler();
30 $handler = new FingersCrossedHandler($test);
31 $handler->handle($this->getRecord(Logger::DEBUG));
32 $handler->handle($this->getRecord(Logger::INFO));
33 $this->assertFalse($test->hasDebugRecords());
34 $this->assertFalse($test->hasInfoRecords());
35 $handler->handle($this->getRecord(Logger::WARNING));
36 $handler->close();
37 $this->assertTrue($test->hasInfoRecords());
38 $this->assertTrue(count($test->getRecords()) === 3);
39 }
40  
41 /**
42 * @covers Monolog\Handler\FingersCrossedHandler::handle
43 * @covers Monolog\Handler\FingersCrossedHandler::activate
44 */
45 public function testHandleStopsBufferingAfterTrigger()
46 {
47 $test = new TestHandler();
48 $handler = new FingersCrossedHandler($test);
49 $handler->handle($this->getRecord(Logger::WARNING));
50 $handler->handle($this->getRecord(Logger::DEBUG));
51 $handler->close();
52 $this->assertTrue($test->hasWarningRecords());
53 $this->assertTrue($test->hasDebugRecords());
54 }
55  
56 /**
57 * @covers Monolog\Handler\FingersCrossedHandler::handle
58 * @covers Monolog\Handler\FingersCrossedHandler::activate
59 * @covers Monolog\Handler\FingersCrossedHandler::reset
60 */
61 public function testHandleRestartBufferingAfterReset()
62 {
63 $test = new TestHandler();
64 $handler = new FingersCrossedHandler($test);
65 $handler->handle($this->getRecord(Logger::WARNING));
66 $handler->handle($this->getRecord(Logger::DEBUG));
67 $handler->reset();
68 $handler->handle($this->getRecord(Logger::INFO));
69 $handler->close();
70 $this->assertTrue($test->hasWarningRecords());
71 $this->assertTrue($test->hasDebugRecords());
72 $this->assertFalse($test->hasInfoRecords());
73 }
74  
75 /**
76 * @covers Monolog\Handler\FingersCrossedHandler::handle
77 * @covers Monolog\Handler\FingersCrossedHandler::activate
78 */
79 public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
80 {
81 $test = new TestHandler();
82 $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false);
83 $handler->handle($this->getRecord(Logger::DEBUG));
84 $handler->handle($this->getRecord(Logger::WARNING));
85 $handler->handle($this->getRecord(Logger::INFO));
86 $handler->close();
87 $this->assertTrue($test->hasWarningRecords());
88 $this->assertTrue($test->hasDebugRecords());
89 $this->assertFalse($test->hasInfoRecords());
90 }
91  
92 /**
93 * @covers Monolog\Handler\FingersCrossedHandler::handle
94 * @covers Monolog\Handler\FingersCrossedHandler::activate
95 */
96 public function testHandleBufferLimit()
97 {
98 $test = new TestHandler();
99 $handler = new FingersCrossedHandler($test, Logger::WARNING, 2);
100 $handler->handle($this->getRecord(Logger::DEBUG));
101 $handler->handle($this->getRecord(Logger::DEBUG));
102 $handler->handle($this->getRecord(Logger::INFO));
103 $handler->handle($this->getRecord(Logger::WARNING));
104 $this->assertTrue($test->hasWarningRecords());
105 $this->assertTrue($test->hasInfoRecords());
106 $this->assertFalse($test->hasDebugRecords());
107 }
108  
109 /**
110 * @covers Monolog\Handler\FingersCrossedHandler::handle
111 * @covers Monolog\Handler\FingersCrossedHandler::activate
112 */
113 public function testHandleWithCallback()
114 {
115 $test = new TestHandler();
116 $handler = new FingersCrossedHandler(function ($record, $handler) use ($test) {
117 return $test;
118 });
119 $handler->handle($this->getRecord(Logger::DEBUG));
120 $handler->handle($this->getRecord(Logger::INFO));
121 $this->assertFalse($test->hasDebugRecords());
122 $this->assertFalse($test->hasInfoRecords());
123 $handler->handle($this->getRecord(Logger::WARNING));
124 $this->assertTrue($test->hasInfoRecords());
125 $this->assertTrue(count($test->getRecords()) === 3);
126 }
127  
128 /**
129 * @covers Monolog\Handler\FingersCrossedHandler::handle
130 * @covers Monolog\Handler\FingersCrossedHandler::activate
131 * @expectedException RuntimeException
132 */
133 public function testHandleWithBadCallbackThrowsException()
134 {
135 $handler = new FingersCrossedHandler(function ($record, $handler) {
136 return 'foo';
137 });
138 $handler->handle($this->getRecord(Logger::WARNING));
139 }
140  
141 /**
142 * @covers Monolog\Handler\FingersCrossedHandler::isHandling
143 */
144 public function testIsHandlingAlways()
145 {
146 $test = new TestHandler();
147 $handler = new FingersCrossedHandler($test, Logger::ERROR);
148 $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
149 }
150  
151 /**
152 * @covers Monolog\Handler\FingersCrossedHandler::__construct
153 * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
154 * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
155 */
156 public function testErrorLevelActivationStrategy()
157 {
158 $test = new TestHandler();
159 $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
160 $handler->handle($this->getRecord(Logger::DEBUG));
161 $this->assertFalse($test->hasDebugRecords());
162 $handler->handle($this->getRecord(Logger::WARNING));
163 $this->assertTrue($test->hasDebugRecords());
164 $this->assertTrue($test->hasWarningRecords());
165 }
166  
167 /**
168 * @covers Monolog\Handler\FingersCrossedHandler::__construct
169 * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
170 * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
171 */
172 public function testErrorLevelActivationStrategyWithPsrLevel()
173 {
174 $test = new TestHandler();
175 $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
176 $handler->handle($this->getRecord(Logger::DEBUG));
177 $this->assertFalse($test->hasDebugRecords());
178 $handler->handle($this->getRecord(Logger::WARNING));
179 $this->assertTrue($test->hasDebugRecords());
180 $this->assertTrue($test->hasWarningRecords());
181 }
182  
183 /**
184 * @covers Monolog\Handler\FingersCrossedHandler::__construct
185 * @covers Monolog\Handler\FingersCrossedHandler::activate
186 */
187 public function testOverrideActivationStrategy()
188 {
189 $test = new TestHandler();
190 $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
191 $handler->handle($this->getRecord(Logger::DEBUG));
192 $this->assertFalse($test->hasDebugRecords());
193 $handler->activate();
194 $this->assertTrue($test->hasDebugRecords());
195 $handler->handle($this->getRecord(Logger::INFO));
196 $this->assertTrue($test->hasInfoRecords());
197 }
198  
199 /**
200 * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
201 * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
202 */
203 public function testChannelLevelActivationStrategy()
204 {
205 $test = new TestHandler();
206 $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG)));
207 $handler->handle($this->getRecord(Logger::WARNING));
208 $this->assertFalse($test->hasWarningRecords());
209 $record = $this->getRecord(Logger::DEBUG);
210 $record['channel'] = 'othertest';
211 $handler->handle($record);
212 $this->assertTrue($test->hasDebugRecords());
213 $this->assertTrue($test->hasWarningRecords());
214 }
215  
216 /**
217 * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
218 * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
219 */
220 public function testChannelLevelActivationStrategyWithPsrLevels()
221 {
222 $test = new TestHandler();
223 $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', array('othertest' => 'debug')));
224 $handler->handle($this->getRecord(Logger::WARNING));
225 $this->assertFalse($test->hasWarningRecords());
226 $record = $this->getRecord(Logger::DEBUG);
227 $record['channel'] = 'othertest';
228 $handler->handle($record);
229 $this->assertTrue($test->hasDebugRecords());
230 $this->assertTrue($test->hasWarningRecords());
231 }
232  
233 /**
234 * @covers Monolog\Handler\FingersCrossedHandler::handle
235 * @covers Monolog\Handler\FingersCrossedHandler::activate
236 */
237 public function testHandleUsesProcessors()
238 {
239 $test = new TestHandler();
240 $handler = new FingersCrossedHandler($test, Logger::INFO);
241 $handler->pushProcessor(function ($record) {
242 $record['extra']['foo'] = true;
243  
244 return $record;
245 });
246 $handler->handle($this->getRecord(Logger::WARNING));
247 $this->assertTrue($test->hasWarningRecords());
248 $records = $test->getRecords();
249 $this->assertTrue($records[0]['extra']['foo']);
250 }
251  
252 /**
253 * @covers Monolog\Handler\FingersCrossedHandler::close
254 */
255 public function testPassthruOnClose()
256 {
257 $test = new TestHandler();
258 $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, Logger::INFO);
259 $handler->handle($this->getRecord(Logger::DEBUG));
260 $handler->handle($this->getRecord(Logger::INFO));
261 $handler->close();
262 $this->assertFalse($test->hasDebugRecords());
263 $this->assertTrue($test->hasInfoRecords());
264 }
265  
266 /**
267 * @covers Monolog\Handler\FingersCrossedHandler::close
268 */
269 public function testPsrLevelPassthruOnClose()
270 {
271 $test = new TestHandler();
272 $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, LogLevel::INFO);
273 $handler->handle($this->getRecord(Logger::DEBUG));
274 $handler->handle($this->getRecord(Logger::INFO));
275 $handler->close();
276 $this->assertFalse($test->hasDebugRecords());
277 $this->assertTrue($test->hasInfoRecords());
278 }
279 }