BufferHandlerTest.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 BufferHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\BufferHandler::__construct
  17. * @covers Monolog\Handler\BufferHandler::handle
  18. * @covers Monolog\Handler\BufferHandler::close
  19. */
  20. public function testHandleBuffers()
  21. {
  22. $test = new TestHandler();
  23. $handler = new BufferHandler($test);
  24. $handler->handle($this->getRecord(Logger::DEBUG));
  25. $handler->handle($this->getRecord(Logger::INFO));
  26. $this->assertFalse($test->hasDebugRecords());
  27. $this->assertFalse($test->hasInfoRecords());
  28. $handler->close();
  29. $this->assertTrue($test->hasInfoRecords());
  30. $this->assertTrue(count($test->getRecords()) === 2);
  31. }
  32. /**
  33. * @covers Monolog\Handler\BufferHandler::close
  34. */
  35. public function testDestructPropagatesRecords()
  36. {
  37. $test = new TestHandler();
  38. $handler = new BufferHandler($test);
  39. $handler->handle($this->getRecord(Logger::WARNING));
  40. $handler->handle($this->getRecord(Logger::DEBUG));
  41. unset($handler);
  42. $this->assertTrue($test->hasWarningRecords());
  43. $this->assertTrue($test->hasDebugRecords());
  44. }
  45. /**
  46. * @covers Monolog\Handler\BufferHandler::handle
  47. */
  48. public function testHandleBufferLimit()
  49. {
  50. $test = new TestHandler();
  51. $handler = new BufferHandler($test, 2);
  52. $handler->handle($this->getRecord(Logger::DEBUG));
  53. $handler->handle($this->getRecord(Logger::DEBUG));
  54. $handler->handle($this->getRecord(Logger::INFO));
  55. $handler->handle($this->getRecord(Logger::WARNING));
  56. $handler->close();
  57. $this->assertTrue($test->hasWarningRecords());
  58. $this->assertTrue($test->hasInfoRecords());
  59. $this->assertFalse($test->hasDebugRecords());
  60. }
  61. /**
  62. * @covers Monolog\Handler\BufferHandler::handle
  63. */
  64. public function testHandleLevel()
  65. {
  66. $test = new TestHandler();
  67. $handler = new BufferHandler($test, 0, Logger::INFO);
  68. $handler->handle($this->getRecord(Logger::DEBUG));
  69. $handler->handle($this->getRecord(Logger::INFO));
  70. $handler->handle($this->getRecord(Logger::WARNING));
  71. $handler->handle($this->getRecord(Logger::DEBUG));
  72. $handler->close();
  73. $this->assertTrue($test->hasWarningRecords());
  74. $this->assertTrue($test->hasInfoRecords());
  75. $this->assertFalse($test->hasDebugRecords());
  76. }
  77. }