1
0
Files
breadcrumb-the-shire/tests/Http/ErrorHandlerTest.php

79 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Tests\Http;
use MintyPHP\Debugger;
use MintyPHP\Http\ErrorHandler;
use MintyPHP\Http\RequestContext;
use PHPUnit\Framework\TestCase;
class ErrorHandlerTest extends TestCase
{
private array $serverBackup = [];
protected function setUp(): void
{
$this->serverBackup = $_SERVER;
RequestContext::resetForTests();
Debugger::$enabled = false;
header_remove();
http_response_code(200);
}
protected function tearDown(): void
{
$_SERVER = $this->serverBackup;
RequestContext::resetForTests();
Debugger::$enabled = false;
header_remove();
}
/**
* @runInSeparateProcess
*/
public function testHandleExceptionReturnsJsonForApiRequestsWithRequestId(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/ping';
$_SERVER['HTTP_X_REQUEST_ID'] = '123e4567-e89b-12d3-a456-426614174000';
$this->expectOutputRegex('/^(?:(?!<html).)*"error_code":"internal_error".*"request_id":"123e4567-e89b-12d3-a456-426614174000".*$/s');
ErrorHandler::handleException(new \RuntimeException('API failure'));
$this->assertSame(500, http_response_code());
}
/**
* @runInSeparateProcess
*/
public function testHandleExceptionRendersProdPageInWebWhenDebugDisabled(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/admin/users';
$_SERVER['HTTP_X_REQUEST_ID'] = '123e4567-e89b-12d3-a456-426614174111';
$this->expectOutputRegex('/^(?=.*<html lang="en">)(?=.*Back to start)(?=.*123e4567-e89b-12d3-a456-426614174111)(?!.*panel-exception).*$/s');
ErrorHandler::handleException(new \RuntimeException('Web failure'));
$this->assertSame(500, http_response_code());
}
/**
* @runInSeparateProcess
*/
public function testHandleExceptionKeepsExistingErrorStatusCode(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/admin/settings';
http_response_code(500);
$this->expectOutputRegex('/500/');
ErrorHandler::handleException(new \RuntimeException('Status flow'));
$this->assertSame(500, http_response_code());
}
}