FingersCrossedHandlerTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. class FingersCrossedHandlerTest extends TestCase
  16. {
  17. /**
  18. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  19. * @covers Monolog\Handler\FingersCrossedHandler::handle
  20. */
  21. public function testHandleBuffers()
  22. {
  23. $test = new TestHandler();
  24. $handler = new FingersCrossedHandler($test);
  25. $handler->handle($this->getRecord(Logger::DEBUG));
  26. $handler->handle($this->getRecord(Logger::INFO));
  27. $this->assertFalse($test->hasDebugRecords());
  28. $this->assertFalse($test->hasInfoRecords());
  29. $handler->handle($this->getRecord(Logger::WARNING));
  30. $handler->close();
  31. $this->assertTrue($test->hasInfoRecords());
  32. $this->assertTrue(count($test->getRecords()) === 3);
  33. }
  34. /**
  35. * @covers Monolog\Handler\FingersCrossedHandler::handle
  36. */
  37. public function testHandleStopsBufferingAfterTrigger()
  38. {
  39. $test = new TestHandler();
  40. $handler = new FingersCrossedHandler($test);
  41. $handler->handle($this->getRecord(Logger::WARNING));
  42. $handler->handle($this->getRecord(Logger::DEBUG));
  43. $handler->close();
  44. $this->assertTrue($test->hasWarningRecords());
  45. $this->assertTrue($test->hasDebugRecords());
  46. }
  47. /**
  48. * @covers Monolog\Handler\FingersCrossedHandler::handle
  49. * @covers Monolog\Handler\FingersCrossedHandler::reset
  50. */
  51. public function testHandleRestartBufferingAfterReset()
  52. {
  53. $test = new TestHandler();
  54. $handler = new FingersCrossedHandler($test);
  55. $handler->handle($this->getRecord(Logger::WARNING));
  56. $handler->handle($this->getRecord(Logger::DEBUG));
  57. $handler->reset();
  58. $handler->handle($this->getRecord(Logger::INFO));
  59. $handler->close();
  60. $this->assertTrue($test->hasWarningRecords());
  61. $this->assertTrue($test->hasDebugRecords());
  62. $this->assertFalse($test->hasInfoRecords());
  63. }
  64. /**
  65. * @covers Monolog\Handler\FingersCrossedHandler::handle
  66. */
  67. public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
  68. {
  69. $test = new TestHandler();
  70. $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false);
  71. $handler->handle($this->getRecord(Logger::DEBUG));
  72. $handler->handle($this->getRecord(Logger::WARNING));
  73. $handler->handle($this->getRecord(Logger::INFO));
  74. $handler->close();
  75. $this->assertTrue($test->hasWarningRecords());
  76. $this->assertTrue($test->hasDebugRecords());
  77. $this->assertFalse($test->hasInfoRecords());
  78. }
  79. /**
  80. * @covers Monolog\Handler\FingersCrossedHandler::handle
  81. */
  82. public function testHandleBufferLimit()
  83. {
  84. $test = new TestHandler();
  85. $handler = new FingersCrossedHandler($test, Logger::WARNING, 2);
  86. $handler->handle($this->getRecord(Logger::DEBUG));
  87. $handler->handle($this->getRecord(Logger::DEBUG));
  88. $handler->handle($this->getRecord(Logger::INFO));
  89. $handler->handle($this->getRecord(Logger::WARNING));
  90. $this->assertTrue($test->hasWarningRecords());
  91. $this->assertTrue($test->hasInfoRecords());
  92. $this->assertFalse($test->hasDebugRecords());
  93. }
  94. /**
  95. * @covers Monolog\Handler\FingersCrossedHandler::handle
  96. */
  97. public function testHandleWithCallback()
  98. {
  99. $test = new TestHandler();
  100. $handler = new FingersCrossedHandler(function ($record, $handler) use ($test) {
  101. return $test;
  102. });
  103. $handler->handle($this->getRecord(Logger::DEBUG));
  104. $handler->handle($this->getRecord(Logger::INFO));
  105. $this->assertFalse($test->hasDebugRecords());
  106. $this->assertFalse($test->hasInfoRecords());
  107. $handler->handle($this->getRecord(Logger::WARNING));
  108. $this->assertTrue($test->hasInfoRecords());
  109. $this->assertTrue(count($test->getRecords()) === 3);
  110. }
  111. /**
  112. * @covers Monolog\Handler\FingersCrossedHandler::handle
  113. * @expectedException RuntimeException
  114. */
  115. public function testHandleWithBadCallbackThrowsException()
  116. {
  117. $handler = new FingersCrossedHandler(function ($record, $handler) {
  118. return 'foo';
  119. });
  120. $handler->handle($this->getRecord(Logger::WARNING));
  121. }
  122. /**
  123. * @covers Monolog\Handler\FingersCrossedHandler::isHandling
  124. */
  125. public function testIsHandlingAlways()
  126. {
  127. $test = new TestHandler();
  128. $handler = new FingersCrossedHandler($test, Logger::ERROR);
  129. $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
  130. }
  131. /**
  132. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  133. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
  134. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
  135. */
  136. public function testErrorLevelActivationStrategy()
  137. {
  138. $test = new TestHandler();
  139. $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
  140. $handler->handle($this->getRecord(Logger::DEBUG));
  141. $this->assertFalse($test->hasDebugRecords());
  142. $handler->handle($this->getRecord(Logger::WARNING));
  143. $this->assertTrue($test->hasDebugRecords());
  144. $this->assertTrue($test->hasWarningRecords());
  145. }
  146. /**
  147. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  148. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
  149. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
  150. */
  151. public function testErrorLevelActivationStrategyWithPsrLevel()
  152. {
  153. $test = new TestHandler();
  154. $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
  155. $handler->handle($this->getRecord(Logger::DEBUG));
  156. $this->assertFalse($test->hasDebugRecords());
  157. $handler->handle($this->getRecord(Logger::WARNING));
  158. $this->assertTrue($test->hasDebugRecords());
  159. $this->assertTrue($test->hasWarningRecords());
  160. }
  161. /**
  162. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
  163. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
  164. */
  165. public function testChannelLevelActivationStrategy()
  166. {
  167. $test = new TestHandler();
  168. $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG)));
  169. $handler->handle($this->getRecord(Logger::WARNING));
  170. $this->assertFalse($test->hasWarningRecords());
  171. $record = $this->getRecord(Logger::DEBUG);
  172. $record['channel'] = 'othertest';
  173. $handler->handle($record);
  174. $this->assertTrue($test->hasDebugRecords());
  175. $this->assertTrue($test->hasWarningRecords());
  176. }
  177. /**
  178. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
  179. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
  180. */
  181. public function testChannelLevelActivationStrategyWithPsrLevels()
  182. {
  183. $test = new TestHandler();
  184. $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', array('othertest' => 'debug')));
  185. $handler->handle($this->getRecord(Logger::WARNING));
  186. $this->assertFalse($test->hasWarningRecords());
  187. $record = $this->getRecord(Logger::DEBUG);
  188. $record['channel'] = 'othertest';
  189. $handler->handle($record);
  190. $this->assertTrue($test->hasDebugRecords());
  191. $this->assertTrue($test->hasWarningRecords());
  192. }
  193. /**
  194. * @covers Monolog\Handler\FingersCrossedHandler::handle
  195. */
  196. public function testHandleUsesProcessors()
  197. {
  198. $test = new TestHandler();
  199. $handler = new FingersCrossedHandler($test, Logger::INFO);
  200. $handler->pushProcessor(function ($record) {
  201. $record['extra']['foo'] = true;
  202. return $record;
  203. });
  204. $handler->handle($this->getRecord(Logger::WARNING));
  205. $this->assertTrue($test->hasWarningRecords());
  206. $records = $test->getRecords();
  207. $this->assertTrue($records[0]['extra']['foo']);
  208. }
  209. /**
  210. * @covers Monolog\Handler\FingersCrossedHandler::close
  211. */
  212. public function testPassthruOnClose()
  213. {
  214. $test = new TestHandler();
  215. $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, Logger::INFO);
  216. $handler->handle($this->getRecord(Logger::DEBUG));
  217. $handler->handle($this->getRecord(Logger::INFO));
  218. $handler->close();
  219. $this->assertFalse($test->hasDebugRecords());
  220. $this->assertTrue($test->hasInfoRecords());
  221. }
  222. }