|
22 | 22 | use CodeIgniter\Test\StreamFilterTrait; |
23 | 23 | use Config\Exceptions as ExceptionsConfig; |
24 | 24 | use Config\Services; |
| 25 | +use PHPUnit\Framework\Attributes\DataProvider; |
25 | 26 | use PHPUnit\Framework\Attributes\Group; |
26 | 27 | use stdClass; |
27 | 28 |
|
@@ -168,6 +169,76 @@ public function testHandleCLIPageNotFoundException(): void |
168 | 169 | $this->resetStreamFilterBuffer(); |
169 | 170 | } |
170 | 171 |
|
| 172 | + /** |
| 173 | + * The handler can only exit outside the testing environment, so it |
| 174 | + * runs in a separate process with the production environment. |
| 175 | + */ |
| 176 | + #[DataProvider('provideHandleWebExceptionExitsOutsideWorkerMode')] |
| 177 | + public function testHandleWebExceptionExitsOutsideWorkerMode( |
| 178 | + string $accept, |
| 179 | + bool $workerMode, |
| 180 | + string $expectedOutput, |
| 181 | + int $expectedStatus, |
| 182 | + ): void { |
| 183 | + $escapedHomePath = var_export(HOMEPATH, true); |
| 184 | + $escapedBootstrap = var_export(SYSTEMPATH . 'util_bootstrap.php', true); |
| 185 | + $escapedAccept = var_export($accept, true); |
| 186 | + $escapedWorker = var_export($workerMode, true); |
| 187 | + |
| 188 | + $code = <<<PHP |
| 189 | + chdir({$escapedHomePath}); |
| 190 | + define('ENVIRONMENT', 'production'); |
| 191 | + require {$escapedBootstrap}; |
| 192 | +
|
| 193 | + use CodeIgniter\\Boot; |
| 194 | + use CodeIgniter\\Debug\\ExceptionHandler; |
| 195 | + use CodeIgniter\\Exceptions\\PageNotFoundException; |
| 196 | + use Config\\Exceptions as ExceptionsConfig; |
| 197 | +
|
| 198 | + (new \\ReflectionProperty(Boot::class, 'workerMode'))->setValue(null, {$escapedWorker}); |
| 199 | +
|
| 200 | + \$request = service('incomingrequest', null, false); |
| 201 | + \$request->setHeader('accept', {$escapedAccept}); |
| 202 | +
|
| 203 | + (new ExceptionHandler(new ExceptionsConfig()))->handle( |
| 204 | + PageNotFoundException::forPageNotFound(), |
| 205 | + \$request, |
| 206 | + service('response'), |
| 207 | + 404, |
| 208 | + EXIT_UNKNOWN_FILE, |
| 209 | + ); |
| 210 | +
|
| 211 | + echo 'HANDLER RETURNED'; |
| 212 | + PHP; |
| 213 | + |
| 214 | + $cmd = sprintf('%s -r %s 2>&1', escapeshellarg(PHP_BINARY), escapeshellarg($code)); |
| 215 | + exec($cmd, $output, $status); |
| 216 | + $output = implode(PHP_EOL, $output); |
| 217 | + |
| 218 | + $this->assertSame($expectedStatus, $status, $output); |
| 219 | + $this->assertStringContainsString($expectedOutput, $output); |
| 220 | + |
| 221 | + if ($workerMode) { |
| 222 | + $this->assertStringEndsWith('HANDLER RETURNED', $output); |
| 223 | + } else { |
| 224 | + $this->assertStringNotContainsString('HANDLER RETURNED', $output); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @return iterable<string, array{string, bool, string, int}> |
| 230 | + */ |
| 231 | + public static function provideHandleWebExceptionExitsOutsideWorkerMode(): iterable |
| 232 | + { |
| 233 | + yield 'non-HTML, worker mode' => ['application/json', true, '""', 0]; |
| 234 | + |
| 235 | + yield 'non-HTML, traditional' => ['application/json', false, '""', EXIT_UNKNOWN_FILE]; |
| 236 | + |
| 237 | + yield 'HTML, worker mode' => ['text/html', true, '<title>404 - Page Not Found</title>', 0]; |
| 238 | + |
| 239 | + yield 'HTML, traditional' => ['text/html', false, '<title>404 - Page Not Found</title>', EXIT_UNKNOWN_FILE]; |
| 240 | + } |
| 241 | + |
171 | 242 | public function testMaskSensitiveData(): void |
172 | 243 | { |
173 | 244 | $maskSensitiveData = self::getPrivateMethodInvoker($this->handler, 'maskSensitiveData'); |
|
0 commit comments