GroupHandlerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\LogRecord;
  12. use Monolog\Level;
  13. class GroupHandlerTest extends \Monolog\Test\MonologTestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\GroupHandler::__construct
  17. */
  18. public function testConstructorOnlyTakesHandler()
  19. {
  20. $this->expectException(\InvalidArgumentException::class);
  21. new GroupHandler([new TestHandler(), "foo"]);
  22. }
  23. /**
  24. * @covers Monolog\Handler\GroupHandler::__construct
  25. * @covers Monolog\Handler\GroupHandler::handle
  26. */
  27. public function testHandle()
  28. {
  29. $testHandlers = [new TestHandler(), new TestHandler()];
  30. $handler = new GroupHandler($testHandlers);
  31. $handler->handle($this->getRecord(Level::Debug));
  32. $handler->handle($this->getRecord(Level::Info));
  33. foreach ($testHandlers as $test) {
  34. $this->assertTrue($test->hasDebugRecords());
  35. $this->assertTrue($test->hasInfoRecords());
  36. $this->assertCount(2, $test->getRecords());
  37. }
  38. }
  39. /**
  40. * @covers Monolog\Handler\GroupHandler::handleBatch
  41. */
  42. public function testHandleBatch()
  43. {
  44. $testHandlers = [new TestHandler(), new TestHandler()];
  45. $handler = new GroupHandler($testHandlers);
  46. $handler->handleBatch([$this->getRecord(Level::Debug), $this->getRecord(Level::Info)]);
  47. foreach ($testHandlers as $test) {
  48. $this->assertTrue($test->hasDebugRecords());
  49. $this->assertTrue($test->hasInfoRecords());
  50. $this->assertCount(2, $test->getRecords());
  51. }
  52. }
  53. /**
  54. * @covers Monolog\Handler\GroupHandler::isHandling
  55. */
  56. public function testIsHandling()
  57. {
  58. $testHandlers = [new TestHandler(Level::Error), new TestHandler(Level::Warning)];
  59. $handler = new GroupHandler($testHandlers);
  60. $this->assertTrue($handler->isHandling($this->getRecord(Level::Error)));
  61. $this->assertTrue($handler->isHandling($this->getRecord(Level::Warning)));
  62. $this->assertFalse($handler->isHandling($this->getRecord(Level::Debug)));
  63. }
  64. /**
  65. * @covers Monolog\Handler\GroupHandler::handle
  66. */
  67. public function testHandleUsesProcessors()
  68. {
  69. $test = new TestHandler();
  70. $handler = new GroupHandler([$test]);
  71. $handler->pushProcessor(function ($record) {
  72. $record->extra['foo'] = true;
  73. return $record;
  74. });
  75. $handler->handle($this->getRecord(Level::Warning));
  76. $this->assertTrue($test->hasWarningRecords());
  77. $records = $test->getRecords();
  78. $this->assertTrue($records[0]['extra']['foo']);
  79. }
  80. /**
  81. * @covers Monolog\Handler\GroupHandler::handle
  82. */
  83. public function testHandleBatchUsesProcessors()
  84. {
  85. $testHandlers = [new TestHandler(), new TestHandler()];
  86. $handler = new GroupHandler($testHandlers);
  87. $handler->pushProcessor(function ($record) {
  88. $record->extra['foo'] = true;
  89. return $record;
  90. });
  91. $handler->pushProcessor(function ($record) {
  92. $record->extra['foo2'] = true;
  93. return $record;
  94. });
  95. $handler->handleBatch([$this->getRecord(Level::Debug), $this->getRecord(Level::Info)]);
  96. foreach ($testHandlers as $test) {
  97. $this->assertTrue($test->hasDebugRecords());
  98. $this->assertTrue($test->hasInfoRecords());
  99. $this->assertCount(2, $test->getRecords());
  100. $records = $test->getRecords();
  101. $this->assertTrue($records[0]['extra']['foo']);
  102. $this->assertTrue($records[1]['extra']['foo']);
  103. $this->assertTrue($records[0]['extra']['foo2']);
  104. $this->assertTrue($records[1]['extra']['foo2']);
  105. }
  106. }
  107. public function testProcessorsDoNotInterfereBetweenHandlers()
  108. {
  109. $t1 = new TestHandler();
  110. $t2 = new TestHandler();
  111. $handler = new GroupHandler([$t1, $t2]);
  112. $t1->pushProcessor(function (LogRecord $record) {
  113. $record->extra['foo'] = 'bar';
  114. return $record;
  115. });
  116. $handler->handle($this->getRecord());
  117. self::assertSame([], $t2->getRecords()[0]->extra);
  118. }
  119. public function testProcessorsDoNotInterfereBetweenHandlersWithBatch()
  120. {
  121. $t1 = new TestHandler();
  122. $t2 = new TestHandler();
  123. $handler = new GroupHandler([$t1, $t2]);
  124. $t1->pushProcessor(function (LogRecord $record) {
  125. $record->extra['foo'] = 'bar';
  126. return $record;
  127. });
  128. $handler->handleBatch([$this->getRecord()]);
  129. self::assertSame([], $t2->getRecords()[0]->extra);
  130. }
  131. }