GroupHandlerTest.php 4.8 KB

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