FingersCrossedHandlerTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. class FingersCrossedHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\FingersCrossedHandler::__construct
  17. * @covers Monolog\Handler\FingersCrossedHandler::handle
  18. */
  19. public function testHandleBuffers()
  20. {
  21. $test = new TestHandler();
  22. $handler = new FingersCrossedHandler($test);
  23. $handler->handle($this->getRecord(Logger::DEBUG));
  24. $handler->handle($this->getRecord(Logger::INFO));
  25. $this->assertFalse($test->hasDebugRecords());
  26. $this->assertFalse($test->hasInfoRecords());
  27. $handler->handle($this->getRecord(Logger::WARNING));
  28. $this->assertTrue($test->hasInfoRecords());
  29. $this->assertTrue(count($test->getRecords()) === 3);
  30. }
  31. /**
  32. * @covers Monolog\Handler\FingersCrossedHandler::handle
  33. */
  34. public function testHandleStopsBufferingAfterTrigger()
  35. {
  36. $test = new TestHandler();
  37. $handler = new FingersCrossedHandler($test);
  38. $handler->handle($this->getRecord(Logger::WARNING));
  39. $handler->handle($this->getRecord(Logger::DEBUG));
  40. $this->assertTrue($test->hasWarningRecords());
  41. $this->assertTrue($test->hasDebugRecords());
  42. }
  43. /**
  44. * @covers Monolog\Handler\FingersCrossedHandler::handle
  45. * @covers Monolog\Handler\FingersCrossedHandler::reset
  46. */
  47. public function testHandleRestartBufferingAfterReset()
  48. {
  49. $test = new TestHandler();
  50. $handler = new FingersCrossedHandler($test);
  51. $handler->handle($this->getRecord(Logger::WARNING));
  52. $handler->handle($this->getRecord(Logger::DEBUG));
  53. $handler->reset();
  54. $handler->handle($this->getRecord(Logger::INFO));
  55. $this->assertTrue($test->hasWarningRecords());
  56. $this->assertTrue($test->hasDebugRecords());
  57. $this->assertFalse($test->hasInfoRecords());
  58. }
  59. /**
  60. * @covers Monolog\Handler\FingersCrossedHandler::handle
  61. */
  62. public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
  63. {
  64. $test = new TestHandler();
  65. $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false);
  66. $handler->handle($this->getRecord(Logger::DEBUG));
  67. $handler->handle($this->getRecord(Logger::WARNING));
  68. $handler->handle($this->getRecord(Logger::INFO));
  69. $this->assertTrue($test->hasWarningRecords());
  70. $this->assertTrue($test->hasDebugRecords());
  71. $this->assertFalse($test->hasInfoRecords());
  72. }
  73. /**
  74. * @covers Monolog\Handler\FingersCrossedHandler::handle
  75. */
  76. public function testHandleBufferLimit()
  77. {
  78. $test = new TestHandler();
  79. $handler = new FingersCrossedHandler($test, Logger::WARNING, 2);
  80. $handler->handle($this->getRecord(Logger::DEBUG));
  81. $handler->handle($this->getRecord(Logger::DEBUG));
  82. $handler->handle($this->getRecord(Logger::INFO));
  83. $handler->handle($this->getRecord(Logger::WARNING));
  84. $this->assertTrue($test->hasWarningRecords());
  85. $this->assertTrue($test->hasInfoRecords());
  86. $this->assertFalse($test->hasDebugRecords());
  87. }
  88. /**
  89. * @covers Monolog\Handler\FingersCrossedHandler::handle
  90. */
  91. public function testHandleWithCallback()
  92. {
  93. $test = new TestHandler();
  94. $handler = new FingersCrossedHandler(function($record, $handler) use ($test) {
  95. return $test;
  96. });
  97. $handler->handle($this->getRecord(Logger::DEBUG));
  98. $handler->handle($this->getRecord(Logger::INFO));
  99. $this->assertFalse($test->hasDebugRecords());
  100. $this->assertFalse($test->hasInfoRecords());
  101. $handler->handle($this->getRecord(Logger::WARNING));
  102. $this->assertTrue($test->hasInfoRecords());
  103. $this->assertTrue(count($test->getRecords()) === 3);
  104. }
  105. /**
  106. * @covers Monolog\Handler\FingersCrossedHandler::handle
  107. * @expectedException RuntimeException
  108. */
  109. public function testHandleWithBadCallbackThrowsException()
  110. {
  111. $handler = new FingersCrossedHandler(function($record, $handler) {
  112. return 'foo';
  113. });
  114. $handler->handle($this->getRecord(Logger::WARNING));
  115. }
  116. /**
  117. * @covers Monolog\Handler\FingersCrossedHandler::isHandling
  118. */
  119. public function testIsHandlingAlways()
  120. {
  121. $test = new TestHandler();
  122. $handler = new FingersCrossedHandler($test, Logger::ERROR);
  123. $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
  124. }
  125. }