Skip to content

Commit

Permalink
Merge pull request #3 from Micro-PHP/1.6.1
Browse files Browse the repository at this point in the history
v1.6.1 up decoration priority to 1001
  • Loading branch information
Asisyas authored Nov 7, 2023
2 parents 258ce5c + 61473f7 commit 4bea9d5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
21 changes: 8 additions & 13 deletions src/Business/Executor/HttpExceptionPageExecutorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public function execute(Request $request, bool $flush = true): Response

return $response;
} catch (\Throwable $throwable) {
if (!$flush) {
throw $throwable;
}

$content = $this->rendererFactory
->create($request)
->render($throwable);
Expand All @@ -59,21 +55,20 @@ public function execute(Request $request, bool $flush = true): Response
}

$contentType = $request->get('_format', 'text/html');
switch ($contentType) {
case 'json':
$contentType = 'application/json';
break;
default:
$contentType = 'text/html';
}
$contentType = match ($contentType) {
'json' => 'application/json',
default => 'text/html',
};

$response = new Response($content, $statusCode, [
'content-type' => $contentType,
]);

$response->send();
if ($flush) {
$response->send();
}

throw $throwable;
return $response;
}
}
}
2 changes: 1 addition & 1 deletion src/HttpExceptionResponseDevPluginConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class HttpExceptionResponseDevPluginConfiguration extends PluginConfiguration implements HttpExceptionResponseDevPluginConfigurationInterface
{
public const CFG_DECORATED_WEIGHT = 'MICRO_HTTP_EXCEPTION_DEV_PAGE_DECORATION_WEIGHT';
public const DECORATED_DEFAULT = 200;
public const DECORATED_DEFAULT = 1001;

public function getProjectDir(): string
{
Expand Down
52 changes: 23 additions & 29 deletions tests/Unit/HttpExceptionPagePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Stanislau Komar <[email protected]>
Expand Down Expand Up @@ -72,44 +73,40 @@ public function testDecorated()
/**
* @dataProvider dataProviderException
*/
public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format)
public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void
{
$kernel = $this->createKernel($env);
$request = Request::create($uri);
$request->request->set('_format', $format);

$isDev = str_starts_with($env, 'dev');
if (!$isDev) {
$this->expectException(HttpException::class);
}

preg_match('/\d+/', $uri, $match);
$exceptionCode = (int) $match[0];

$this->expectException(HttpException::class);
$this->expectExceptionCode($exceptionCode);

ob_start();

try {
if (!$isDev) {
$response = $kernel->container()->get(HttpFacadeInterface::class)
->execute($request, $isFlush);
} else {
ob_start();
/** @var Response $response */
$response = $kernel->container()->get(HttpFacadeInterface::class)
->execute($request, $isFlush);
$flushedContent = ob_get_contents();
ob_end_clean();
}

$flushedContent = ob_get_clean();
} catch (HttpException $httpException) {
$flushedContent = ob_get_clean();

if ($isFlush) {
if ('json' === $format) {
$this->assertJson($flushedContent);
}
$responseContent = $response->getContent();

if ('html' === $format) {
$this->assertStringStartsWith('<!--', $flushedContent);
$this->assertStringEndsWith('-->', $flushedContent);
}
}
$this->assertEquals($exceptionCode, $response->getStatusCode());

throw $httpException;
if ($isFlush) {
$this->assertEquals($responseContent, $flushedContent);
}

$responseContent = $response->getContent();
if ('html' === $format) {
$this->assertStringStartsWith('<!--', $responseContent);
$this->assertStringEndsWith('-->', $responseContent);
Expand All @@ -118,15 +115,12 @@ public function testExceptionResponse(string $env, string $uri, bool $isFlush, m
if ('json' === $format) {
$this->assertJson($responseContent);
}

$this->assertStringEndsWith($responseContent, $flushedContent);
$this->assertStringStartsWith($responseContent, $flushedContent);
}

/**
* @dataProvider dataProviderSuccess
*/
public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format)
public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void
{
$kernel = $this->createKernel($env);
$request = Request::create($uri);
Expand All @@ -139,11 +133,11 @@ public function testSuccessResponse(string $env, string $uri, bool $isFlush, mix

$responseFlushedContent = ob_get_clean();

$this->assertEquals($isFlush ? $result : '', $responseFlushedContent);
$this->assertEquals($isFlush ? $result : false, $responseFlushedContent);
$this->assertEquals($result, $response->getContent());
}

public function dataProviderSuccess()
public function dataProviderSuccess(): array
{
return [
['dev', '/', true, 'Hello, world', 'html'],
Expand All @@ -159,7 +153,7 @@ public function dataProviderSuccess()
];
}

public function dataProviderException()
public function dataProviderException(): array
{
return [
['dev', '/404', true, null, 'html'],
Expand Down

0 comments on commit 4bea9d5

Please sign in to comment.