FallbackGroupHandlerTest.php 6.0 KB

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