Skip to content

Commit 4bea9d5

Browse files
authored
Merge pull request #3 from Micro-PHP/1.6.1
v1.6.1 up decoration priority to 1001
2 parents 258ce5c + 61473f7 commit 4bea9d5

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

src/Business/Executor/HttpExceptionPageExecutorDecorator.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public function execute(Request $request, bool $flush = true): Response
4545

4646
return $response;
4747
} catch (\Throwable $throwable) {
48-
if (!$flush) {
49-
throw $throwable;
50-
}
51-
5248
$content = $this->rendererFactory
5349
->create($request)
5450
->render($throwable);
@@ -59,21 +55,20 @@ public function execute(Request $request, bool $flush = true): Response
5955
}
6056

6157
$contentType = $request->get('_format', 'text/html');
62-
switch ($contentType) {
63-
case 'json':
64-
$contentType = 'application/json';
65-
break;
66-
default:
67-
$contentType = 'text/html';
68-
}
58+
$contentType = match ($contentType) {
59+
'json' => 'application/json',
60+
default => 'text/html',
61+
};
6962

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

74-
$response->send();
67+
if ($flush) {
68+
$response->send();
69+
}
7570

76-
throw $throwable;
71+
return $response;
7772
}
7873
}
7974
}

src/HttpExceptionResponseDevPluginConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class HttpExceptionResponseDevPluginConfiguration extends PluginConfiguration implements HttpExceptionResponseDevPluginConfigurationInterface
2323
{
2424
public const CFG_DECORATED_WEIGHT = 'MICRO_HTTP_EXCEPTION_DEV_PAGE_DECORATION_WEIGHT';
25-
public const DECORATED_DEFAULT = 200;
25+
public const DECORATED_DEFAULT = 1001;
2626

2727
public function getProjectDir(): string
2828
{

tests/Unit/HttpExceptionPagePluginTest.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
2222
use PHPUnit\Framework\TestCase;
2323
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\Response;
2425

2526
/**
2627
* @author Stanislau Komar <head.trackingsoft@gmail.com>
@@ -72,44 +73,40 @@ public function testDecorated()
7273
/**
7374
* @dataProvider dataProviderException
7475
*/
75-
public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format)
76+
public function testExceptionResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void
7677
{
7778
$kernel = $this->createKernel($env);
7879
$request = Request::create($uri);
7980
$request->request->set('_format', $format);
8081

8182
$isDev = str_starts_with($env, 'dev');
83+
if (!$isDev) {
84+
$this->expectException(HttpException::class);
85+
}
86+
8287
preg_match('/\d+/', $uri, $match);
8388
$exceptionCode = (int) $match[0];
8489

85-
$this->expectException(HttpException::class);
86-
$this->expectExceptionCode($exceptionCode);
87-
88-
ob_start();
89-
90-
try {
90+
if (!$isDev) {
9191
$response = $kernel->container()->get(HttpFacadeInterface::class)
9292
->execute($request, $isFlush);
93+
} else {
94+
ob_start();
95+
/** @var Response $response */
96+
$response = $kernel->container()->get(HttpFacadeInterface::class)
97+
->execute($request, $isFlush);
98+
$flushedContent = ob_get_contents();
99+
ob_end_clean();
100+
}
93101

94-
$flushedContent = ob_get_clean();
95-
} catch (HttpException $httpException) {
96-
$flushedContent = ob_get_clean();
97-
98-
if ($isFlush) {
99-
if ('json' === $format) {
100-
$this->assertJson($flushedContent);
101-
}
102+
$responseContent = $response->getContent();
102103

103-
if ('html' === $format) {
104-
$this->assertStringStartsWith('<!--', $flushedContent);
105-
$this->assertStringEndsWith('-->', $flushedContent);
106-
}
107-
}
104+
$this->assertEquals($exceptionCode, $response->getStatusCode());
108105

109-
throw $httpException;
106+
if ($isFlush) {
107+
$this->assertEquals($responseContent, $flushedContent);
110108
}
111109

112-
$responseContent = $response->getContent();
113110
if ('html' === $format) {
114111
$this->assertStringStartsWith('<!--', $responseContent);
115112
$this->assertStringEndsWith('-->', $responseContent);
@@ -118,15 +115,12 @@ public function testExceptionResponse(string $env, string $uri, bool $isFlush, m
118115
if ('json' === $format) {
119116
$this->assertJson($responseContent);
120117
}
121-
122-
$this->assertStringEndsWith($responseContent, $flushedContent);
123-
$this->assertStringStartsWith($responseContent, $flushedContent);
124118
}
125119

126120
/**
127121
* @dataProvider dataProviderSuccess
128122
*/
129-
public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format)
123+
public function testSuccessResponse(string $env, string $uri, bool $isFlush, mixed $result, string $format): void
130124
{
131125
$kernel = $this->createKernel($env);
132126
$request = Request::create($uri);
@@ -139,11 +133,11 @@ public function testSuccessResponse(string $env, string $uri, bool $isFlush, mix
139133

140134
$responseFlushedContent = ob_get_clean();
141135

142-
$this->assertEquals($isFlush ? $result : '', $responseFlushedContent);
136+
$this->assertEquals($isFlush ? $result : false, $responseFlushedContent);
143137
$this->assertEquals($result, $response->getContent());
144138
}
145139

146-
public function dataProviderSuccess()
140+
public function dataProviderSuccess(): array
147141
{
148142
return [
149143
['dev', '/', true, 'Hello, world', 'html'],
@@ -159,7 +153,7 @@ public function dataProviderSuccess()
159153
];
160154
}
161155

162-
public function dataProviderException()
156+
public function dataProviderException(): array
163157
{
164158
return [
165159
['dev', '/404', true, null, 'html'],

0 commit comments

Comments
 (0)