|
18 | 18 | use CodeIgniter\Exceptions\RuntimeException; |
19 | 19 | use CodeIgniter\I18n\Time; |
20 | 20 | use CodeIgniter\Log\Exceptions\LogException; |
| 21 | +use CodeIgniter\Log\Handlers\BaseHandler; |
| 22 | +use CodeIgniter\Log\Handlers\HandlerInterface; |
21 | 23 | use CodeIgniter\Test\CIUnitTestCase; |
22 | 24 | use CodeIgniter\Test\Mock\MockLogger as LoggerConfig; |
23 | 25 | use PHPUnit\Framework\Attributes\Group; |
@@ -95,6 +97,67 @@ public function testLogActuallyLogs(): void |
95 | 97 | $this->assertSame($expected, $logs[0]); |
96 | 98 | } |
97 | 99 |
|
| 100 | + public function testLogRunsRemainingHandlersWhenAHandlerReturnsContinue(): void |
| 101 | + { |
| 102 | + $config = new LoggerConfig(); |
| 103 | + $config->handlers = [ |
| 104 | + $this->getResultHandlerClass() => ['handles' => ['debug'], 'result' => HandlerInterface::RESULT_CONTINUE], |
| 105 | + TestHandler::class => ['handles' => ['debug']], |
| 106 | + ]; |
| 107 | + |
| 108 | + $logger = new Logger($config); |
| 109 | + $logger->log('debug', 'Test message'); |
| 110 | + |
| 111 | + $this->assertCount(1, TestHandler::getLogs()); |
| 112 | + } |
| 113 | + |
| 114 | + public function testLogStopsRunningHandlersWhenAHandlerReturnsStop(): void |
| 115 | + { |
| 116 | + $config = new LoggerConfig(); |
| 117 | + $config->handlers = [ |
| 118 | + $this->getResultHandlerClass() => ['handles' => ['debug'], 'result' => HandlerInterface::RESULT_STOP], |
| 119 | + TestHandler::class => ['handles' => ['debug']], |
| 120 | + ]; |
| 121 | + |
| 122 | + // Handlers are created lazily and TestHandler resets its logs when |
| 123 | + // created, so reset them here as it will never be reached. |
| 124 | + new TestHandler([]); |
| 125 | + |
| 126 | + $logger = new Logger($config); |
| 127 | + $logger->log('debug', 'Test message'); |
| 128 | + |
| 129 | + $this->assertCount(0, TestHandler::getLogs()); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns the class of a handler that returns the `result` value from its config. |
| 134 | + * |
| 135 | + * @return class-string<HandlerInterface> |
| 136 | + */ |
| 137 | + private function getResultHandlerClass(): string |
| 138 | + { |
| 139 | + $handler = new class ([]) extends BaseHandler { |
| 140 | + private int $result; |
| 141 | + |
| 142 | + /** |
| 143 | + * @param array{handles?: list<string>, result?: int} $config |
| 144 | + */ |
| 145 | + public function __construct(array $config) |
| 146 | + { |
| 147 | + parent::__construct($config); |
| 148 | + |
| 149 | + $this->result = $config['result'] ?? HandlerInterface::RESULT_CONTINUE; |
| 150 | + } |
| 151 | + |
| 152 | + public function handle($level, $message, array $context = []): int |
| 153 | + { |
| 154 | + return $this->result; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + return $handler::class; |
| 159 | + } |
| 160 | + |
98 | 161 | public function testLogDoesnotLogUnhandledLevels(): void |
99 | 162 | { |
100 | 163 | $config = new LoggerConfig(); |
|
0 commit comments