2
0

FallbackGroupHandlerTest.php 6.0 KB

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