FingersCrossedHandlerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $this->assertTrue($test->hasInfoRecords());
  31. $this->assertTrue(count($test->getRecords()) === 3);
  32. }
  33. /**
  34. * @covers Monolog\Handler\FingersCrossedHandler::handle
  35. */
  36. public function testHandleStopsBufferingAfterTrigger()
  37. {
  38. $test = new TestHandler();
  39. $handler = new FingersCrossedHandler($test);
  40. $handler->handle($this->getRecord(Logger::WARNING));
  41. $handler->handle($this->getRecord(Logger::DEBUG));
  42. $this->assertTrue($test->hasWarningRecords());
  43. $this->assertTrue($test->hasDebugRecords());
  44. }
  45. /**
  46. * @covers Monolog\Handler\FingersCrossedHandler::handle
  47. * @covers Monolog\Handler\FingersCrossedHandler::reset
  48. */
  49. public function testHandleRestartBufferingAfterReset()
  50. {
  51. $test = new TestHandler();
  52. $handler = new FingersCrossedHandler($test);
  53. $handler->handle($this->getRecord(Logger::WARNING));
  54. $handler->handle($this->getRecord(Logger::DEBUG));
  55. $handler->reset();
  56. $handler->handle($this->getRecord(Logger::INFO));
  57. $this->assertTrue($test->hasWarningRecords());
  58. $this->assertTrue($test->hasDebugRecords());
  59. $this->assertFalse($test->hasInfoRecords());
  60. }
  61. /**
  62. * @covers Monolog\Handler\FingersCrossedHandler::handle
  63. */
  64. public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
  65. {
  66. $test = new TestHandler();
  67. $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false);
  68. $handler->handle($this->getRecord(Logger::DEBUG));
  69. $handler->handle($this->getRecord(Logger::WARNING));
  70. $handler->handle($this->getRecord(Logger::INFO));
  71. $this->assertTrue($test->hasWarningRecords());
  72. $this->assertTrue($test->hasDebugRecords());
  73. $this->assertFalse($test->hasInfoRecords());
  74. }
  75. /**
  76. * @covers Monolog\Handler\FingersCrossedHandler::handle
  77. */
  78. public function testHandleBufferLimit()
  79. {
  80. $test = new TestHandler();
  81. $handler = new FingersCrossedHandler($test, Logger::WARNING, 2);
  82. $handler->handle($this->getRecord(Logger::DEBUG));
  83. $handler->handle($this->getRecord(Logger::DEBUG));
  84. $handler->handle($this->getRecord(Logger::INFO));
  85. $handler->handle($this->getRecord(Logger::WARNING));
  86. $this->assertTrue($test->hasWarningRecords());
  87. $this->assertTrue($test->hasInfoRecords());
  88. $this->assertFalse($test->hasDebugRecords());
  89. }
  90. /**
  91. * @covers Monolog\Handler\FingersCrossedHandler::handle
  92. */
  93. public function testHandleWithCallback()
  94. {
  95. $test = new TestHandler();
  96. $handler = new FingersCrossedHandler(function($record, $handler) use ($test) {
  97. return $test;
  98. });
  99. $handler->handle($this->getRecord(Logger::DEBUG));
  100. $handler->handle($this->getRecord(Logger::INFO));
  101. $this->assertFalse($test->hasDebugRecords());
  102. $this->assertFalse($test->hasInfoRecords());
  103. $handler->handle($this->getRecord(Logger::WARNING));
  104. $this->assertTrue($test->hasInfoRecords());
  105. $this->assertTrue(count($test->getRecords()) === 3);
  106. }
  107. /**
  108. * @covers Monolog\Handler\FingersCrossedHandler::handle
  109. * @expectedException RuntimeException
  110. */
  111. public function testHandleWithBadCallbackThrowsException()
  112. {
  113. $handler = new FingersCrossedHandler(function($record, $handler) {
  114. return 'foo';
  115. });
  116. $handler->handle($this->getRecord(Logger::WARNING));
  117. }
  118. /**
  119. * @covers Monolog\Handler\FingersCrossedHandler::isHandling
  120. */
  121. public function testIsHandlingAlways()
  122. {
  123. $test = new TestHandler();
  124. $handler = new FingersCrossedHandler($test, Logger::ERROR);
  125. $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
  126. }
  127. /**
  128. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  129. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
  130. * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
  131. */
  132. public function testErrorLevelActivationStrategy()
  133. {
  134. $test = new TestHandler();
  135. $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
  136. $handler->handle($this->getRecord(Logger::DEBUG));
  137. $this->assertFalse($test->hasDebugRecords());
  138. $handler->handle($this->getRecord(Logger::WARNING));
  139. $this->assertTrue($test->hasDebugRecords());
  140. $this->assertTrue($test->hasWarningRecords());
  141. }
  142. /**
  143. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
  144. * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
  145. */
  146. public function testChannelLevelActivationStrategy()
  147. {
  148. $test = new TestHandler();
  149. $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG)));
  150. $handler->handle($this->getRecord(Logger::WARNING));
  151. $this->assertFalse($test->hasWarningRecords());
  152. $record = $this->getRecord(Logger::DEBUG);
  153. $record['channel'] = 'othertest';
  154. $handler->handle($record);
  155. $this->assertTrue($test->hasDebugRecords());
  156. $this->assertTrue($test->hasWarningRecords());
  157. }
  158. /**
  159. * @covers Monolog\Handler\FingersCrossedHandler::handle
  160. */
  161. public function testHandleUsesProcessors()
  162. {
  163. $test = new TestHandler();
  164. $handler = new FingersCrossedHandler($test, Logger::INFO);
  165. $handler->pushProcessor(function ($record) {
  166. $record['extra']['foo'] = true;
  167. return $record;
  168. });
  169. $handler->handle($this->getRecord(Logger::WARNING));
  170. $this->assertTrue($test->hasWarningRecords());
  171. $records = $test->getRecords();
  172. $this->assertTrue($records[0]['extra']['foo']);
  173. }
  174. }