FingersCrossedHandlerTest.php 11 KB

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