FingersCrossedHandlerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php declare(strict_types=1);
  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\Level;
  12. use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
  13. use Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy;
  14. use Psr\Log\LogLevel;
  15. class FingersCrossedHandlerTest extends \Monolog\Test\MonologTestCase
  16. {
  17. /**
  18. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  19. * @covers Monolog\Handler\FingersCrossedHandler::handle
  20. * @covers Monolog\Handler\FingersCrossedHandler::activate
  21. */
  22. public function testHandleBuffers()
  23. {
  24. $test = new TestHandler();
  25. $handler = new FingersCrossedHandler($test);
  26. $handler->handle($this->getRecord(Level::Debug));
  27. $handler->handle($this->getRecord(Level::Info));
  28. $this->assertFalse($test->hasDebugRecords());
  29. $this->assertFalse($test->hasInfoRecords());
  30. $handler->handle($this->getRecord(Level::Warning));
  31. $handler->close();
  32. $this->assertTrue($test->hasInfoRecords());
  33. $this->assertCount(3, $test->getRecords());
  34. }
  35. /**
  36. * @covers Monolog\Handler\FingersCrossedHandler::handle
  37. * @covers Monolog\Handler\FingersCrossedHandler::activate
  38. */
  39. public function testHandleStopsBufferingAfterTrigger()
  40. {
  41. $test = new TestHandler();
  42. $handler = new FingersCrossedHandler($test);
  43. $handler->handle($this->getRecord(Level::Warning));
  44. $handler->handle($this->getRecord(Level::Debug));
  45. $handler->close();
  46. $this->assertTrue($test->hasWarningRecords());
  47. $this->assertTrue($test->hasDebugRecords());
  48. }
  49. /**
  50. * @covers Monolog\Handler\FingersCrossedHandler::handle
  51. * @covers Monolog\Handler\FingersCrossedHandler::activate
  52. * @covers Monolog\Handler\FingersCrossedHandler::reset
  53. */
  54. public function testHandleResetBufferingAfterReset()
  55. {
  56. $test = new TestHandler();
  57. $test->setSkipReset(true);
  58. $handler = new FingersCrossedHandler($test);
  59. $handler->handle($this->getRecord(Level::Warning));
  60. $handler->handle($this->getRecord(Level::Debug));
  61. $handler->reset();
  62. $handler->handle($this->getRecord(Level::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 testHandleResetBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
  73. {
  74. $test = new TestHandler();
  75. $handler = new FingersCrossedHandler($test, Level::Warning, 0, false, false);
  76. $handler->handle($this->getRecord(Level::Debug));
  77. $handler->handle($this->getRecord(Level::Warning));
  78. $handler->handle($this->getRecord(Level::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, Level::Warning, 2);
  92. $handler->handle($this->getRecord(Level::Debug));
  93. $handler->handle($this->getRecord(Level::Debug));
  94. $handler->handle($this->getRecord(Level::Info));
  95. $handler->handle($this->getRecord(Level::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(Level::Debug));
  111. $handler->handle($this->getRecord(Level::Info));
  112. $this->assertFalse($test->hasDebugRecords());
  113. $this->assertFalse($test->hasInfoRecords());
  114. $handler->handle($this->getRecord(Level::Warning));
  115. $this->assertTrue($test->hasInfoRecords());
  116. $this->assertCount(3, $test->getRecords());
  117. }
  118. /**
  119. * @covers Monolog\Handler\FingersCrossedHandler::handle
  120. * @covers Monolog\Handler\FingersCrossedHandler::activate
  121. */
  122. public function testHandleWithBadCallbackThrowsException()
  123. {
  124. $handler = new FingersCrossedHandler(function ($record, $handler) {
  125. return 'foo';
  126. });
  127. $this->expectException(\RuntimeException::class);
  128. $handler->handle($this->getRecord(Level::Warning));
  129. }
  130. /**
  131. * @covers Monolog\Handler\FingersCrossedHandler::isHandling
  132. */
  133. public function testIsHandlingAlways()
  134. {
  135. $test = new TestHandler();
  136. $handler = new FingersCrossedHandler($test, Level::Error);
  137. $this->assertTrue($handler->isHandling($this->getRecord(Level::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(Level::Warning));
  148. $handler->handle($this->getRecord(Level::Debug));
  149. $this->assertFalse($test->hasDebugRecords());
  150. $handler->handle($this->getRecord(Level::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(Level::Debug));
  164. $this->assertFalse($test->hasDebugRecords());
  165. $handler->handle($this->getRecord(Level::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(Level::Debug));
  178. $this->assertFalse($test->hasDebugRecords());
  179. $handler->activate();
  180. $this->assertTrue($test->hasDebugRecords());
  181. $handler->handle($this->getRecord(Level::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(Level::Error, ['othertest' => Level::Debug]));
  192. $handler->handle($this->getRecord(Level::Warning));
  193. $this->assertFalse($test->hasWarningRecords());
  194. $record = $this->getRecord(Level::Debug, channel: 'othertest');
  195. $handler->handle($record);
  196. $this->assertTrue($test->hasDebugRecords());
  197. $this->assertTrue($test->hasWarningRecords());
  198. }
  199. /**
  200. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
  201. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
  202. */
  203. public function testChannelLevelActivationStrategyWithPsrLevels()
  204. {
  205. $test = new TestHandler();
  206. $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', ['othertest' => 'debug']));
  207. $handler->handle($this->getRecord(Level::Warning));
  208. $this->assertFalse($test->hasWarningRecords());
  209. $record = $this->getRecord(Level::Debug, channel: 'othertest');
  210. $handler->handle($record);
  211. $this->assertTrue($test->hasDebugRecords());
  212. $this->assertTrue($test->hasWarningRecords());
  213. }
  214. /**
  215. * @covers Monolog\Handler\FingersCrossedHandler::handle
  216. * @covers Monolog\Handler\FingersCrossedHandler::activate
  217. */
  218. public function testHandleUsesProcessors()
  219. {
  220. $test = new TestHandler();
  221. $handler = new FingersCrossedHandler($test, Level::Info);
  222. $handler->pushProcessor(function ($record) {
  223. $record->extra['foo'] = true;
  224. return $record;
  225. });
  226. $handler->handle($this->getRecord(Level::Warning));
  227. $this->assertTrue($test->hasWarningRecords());
  228. $records = $test->getRecords();
  229. $this->assertTrue($records[0]['extra']['foo']);
  230. }
  231. /**
  232. * @covers Monolog\Handler\FingersCrossedHandler::close
  233. */
  234. public function testPassthruOnClose()
  235. {
  236. $test = new TestHandler();
  237. $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Level::Warning), 0, true, true, Level::Info);
  238. $handler->handle($this->getRecord(Level::Debug));
  239. $handler->handle($this->getRecord(Level::Info));
  240. $handler->handle($this->getRecord(Level::Notice));
  241. $handler->close();
  242. $this->assertFalse($test->hasDebugRecords());
  243. $this->assertTrue($test->hasInfoRecords());
  244. $this->assertTrue($test->hasNoticeRecords());
  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(Level::Warning), 0, true, true, LogLevel::INFO);
  253. $handler->handle($this->getRecord(Level::Debug));
  254. $handler->handle($this->getRecord(Level::Info));
  255. $handler->handle($this->getRecord(Level::Notice));
  256. $handler->close();
  257. $this->assertFalse($test->hasDebugRecords());
  258. $this->assertTrue($test->hasInfoRecords());
  259. $this->assertTrue($test->hasNoticeRecords());
  260. }
  261. }