Skip to content

Commit a83be60

Browse files
committed
Add dev dependency httpsoft/http-server-request
1 parent e854f88 commit a83be60

9 files changed

+84
-25
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"psr/http-server-middleware": "^1.0"
2727
},
2828
"require-dev": {
29-
"httpsoft/http-request": "^1.0",
29+
"httpsoft/http-server-request": "^1.0",
3030
"phpunit/phpunit": "^9.3",
3131
"squizlabs/php_codesniffer": "^3.5",
3232
"vimeo/psalm": "^3.14"

psalm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
</projectFiles>
1515

1616
<issueHandlers>
17+
<MixedArgument errorLevel="info" />
18+
<MixedArrayAccess errorLevel="info" />
1719
<MixedArrayOffset errorLevel="info" />
1820
<MixedReturnTypeCoercion errorLevel="info" />
1921
</issueHandlers>

src/ErrorResponseGenerator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use HttpSoft\Response\HtmlResponse;
88
use HttpSoft\Response\JsonResponse;
9-
use HttpSoft\Response\ResponseStatusCodeInterface;
109
use HttpSoft\Response\TextResponse;
1110
use HttpSoft\Response\XmlResponse;
1211
use Psr\Http\Message\ResponseInterface;
@@ -21,7 +20,7 @@
2120
use function trim;
2221
use function uasort;
2322

24-
final class ErrorResponseGenerator implements ErrorResponseGeneratorInterface, ResponseStatusCodeInterface
23+
final class ErrorResponseGenerator implements ErrorResponseGeneratorInterface
2524
{
2625
/**
2726
* {@inheritDoc}
@@ -31,12 +30,12 @@ public function generate(Throwable $error, ServerRequestInterface $request): Res
3130
$errorCode = (int) $error->getCode();
3231
$responseCode = self::STATUS_INTERNAL_SERVER_ERROR;
3332

34-
if ($errorCode >= 400 && $errorCode < 600 && array_key_exists($errorCode, self::PHRASES)) {
33+
if (array_key_exists($errorCode, self::ERROR_PHRASES)) {
3534
$responseCode = $errorCode;
3635
}
3736

3837
$requestMimeTypes = $this->getSortedMimeTypesByRequest($request);
39-
return $this->getResponse($responseCode, self::PHRASES[$responseCode], $requestMimeTypes);
38+
return $this->getResponse($responseCode, self::ERROR_PHRASES[$responseCode], $requestMimeTypes);
4039
}
4140

4241
/**

src/ErrorResponseGeneratorInterface.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,63 @@
44

55
namespace HttpSoft\ErrorHandler;
66

7+
use HttpSoft\Response\ResponseStatusCodeInterface;
78
use Psr\Http\Message\ResponseInterface;
89
use Psr\Http\Message\ServerRequestInterface;
910
use Throwable;
1011

11-
interface ErrorResponseGeneratorInterface
12+
interface ErrorResponseGeneratorInterface extends ResponseStatusCodeInterface
1213
{
14+
/**
15+
* Map of error HTTP status code and reason phrases.
16+
*
17+
* @link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
18+
*/
19+
public const ERROR_PHRASES = [
20+
// Client Errors 4xx
21+
self::STATUS_BAD_REQUEST => 'Bad Request',
22+
self::STATUS_UNAUTHORIZED => 'Unauthorized',
23+
self::STATUS_PAYMENT_REQUIRED => 'Payment Required',
24+
self::STATUS_FORBIDDEN => 'Forbidden',
25+
self::STATUS_NOT_FOUND => 'Not Found',
26+
self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed',
27+
self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable',
28+
self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
29+
self::STATUS_REQUEST_TIMEOUT => 'Request Timeout',
30+
self::STATUS_CONFLICT => 'Conflict',
31+
self::STATUS_GONE => 'Gone',
32+
self::STATUS_LENGTH_REQUIRED => 'Length Required',
33+
self::STATUS_PRECONDITION_FAILED => 'Precondition Failed',
34+
self::STATUS_PAYLOAD_TOO_LARGE => 'Payload Too Large',
35+
self::STATUS_URI_TOO_LONG => 'URI Too Long',
36+
self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
37+
self::STATUS_RANGE_NOT_SATISFIABLE => 'Range Not Satisfiable',
38+
self::STATUS_EXPECTATION_FAILED => 'Expectation Failed',
39+
self::STATUS_IM_A_TEAPOT => 'I\'m a teapot',
40+
self::STATUS_MISDIRECTED_REQUEST => 'Misdirected Request',
41+
self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
42+
self::STATUS_LOCKED => 'Locked',
43+
self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency',
44+
self::STATUS_TOO_EARLY => 'Too Early',
45+
self::STATUS_UPGRADE_REQUIRED => 'Upgrade Required',
46+
self::STATUS_PRECONDITION_REQUIRED => 'Precondition Required',
47+
self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests',
48+
self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
49+
self::STATUS_UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
50+
// Server Errors 5xx
51+
self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error',
52+
self::STATUS_NOT_IMPLEMENTED => 'Not Implemented',
53+
self::STATUS_BAD_GATEWAY => 'Bad Gateway',
54+
self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable',
55+
self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout',
56+
self::STATUS_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
57+
self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
58+
self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage',
59+
self::STATUS_LOOP_DETECTED => 'Loop Detected',
60+
self::STATUS_NOT_EXTENDED => 'Not Extended',
61+
self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
62+
];
63+
1364
/**
1465
* Generates an instance of `Psr\Http\Message\ResponseInterface` with information about the handled error.
1566
*

tests/ErrorHandlerMiddlewareTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use HttpSoft\ErrorHandler\ErrorHandlerMiddleware;
88
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
9-
use HttpSoft\Request\ServerRequestFactory;
9+
use HttpSoft\ServerRequest\ServerRequestCreator;
1010
use HttpSoft\Response\ResponseStatusCodeInterface;
1111
use HttpSoft\Tests\ErrorHandler\TestAsset\ErrorRequestHandler;
1212
use HttpSoft\Tests\ErrorHandler\TestAsset\ThrowableRequestHandler;
@@ -25,6 +25,8 @@
2525

2626
class ErrorHandlerMiddlewareTest extends TestCase implements ResponseStatusCodeInterface
2727
{
28+
private const PHRASES = ErrorResponseGeneratorInterface::ERROR_PHRASES;
29+
2830
/**
2931
* @var int
3032
*/
@@ -43,7 +45,7 @@ class ErrorHandlerMiddlewareTest extends TestCase implements ResponseStatusCodeI
4345
public function setUp(): void
4446
{
4547
$this->errorHandler = new ErrorHandlerMiddleware();
46-
$this->request = ServerRequestFactory::create();
48+
$this->request = ServerRequestCreator::create();
4749
$this->errorReporting = error_reporting();
4850
}
4951

@@ -57,15 +59,15 @@ public function testWithRequestHandlerAndWithStatusOk(): void
5759
$response = $this->errorHandler->process($this->request, $this->createRequestHandler());
5860
$this->assertInstanceOf(ResponseInterface::class, $response);
5961
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
60-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
62+
$this->assertSame('OK', $response->getReasonPhrase());
6163
}
6264

6365
public function testWithRequestHandlerAndWithStatusCreated(): void
6466
{
6567
$response = $this->errorHandler->process($this->request, $this->createRequestHandler(self::STATUS_CREATED));
6668
$this->assertInstanceOf(ResponseInterface::class, $response);
6769
$this->assertSame(self::STATUS_CREATED, $response->getStatusCode());
68-
$this->assertSame(self::PHRASES[self::STATUS_CREATED], $response->getReasonPhrase());
70+
$this->assertSame('Created', $response->getReasonPhrase());
6971
}
7072

7173
public function testWithRequestHandlerAndWithErrorResponseGeneratorMock(): void
@@ -74,7 +76,7 @@ public function testWithRequestHandlerAndWithErrorResponseGeneratorMock(): void
7476
$response = $errorHandler->process($this->request, $this->createRequestHandler());
7577
$this->assertInstanceOf(ResponseInterface::class, $response);
7678
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
77-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
79+
$this->assertSame('OK', $response->getReasonPhrase());
7880
}
7981

8082
public function testWithThrowableRequestHandlerAndWithDefaultError(): void
@@ -112,7 +114,7 @@ public function testWithErrorRequestHandlerAndWithoutCaughtError(): void
112114
$response = $this->errorHandler->process($this->request, $this->createErrorRequestHandler(E_ERROR));
113115
$this->assertInstanceOf(ResponseInterface::class, $response);
114116
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
115-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
117+
$this->assertSame('OK', $response->getReasonPhrase());
116118
}
117119

118120
public function testWithErrorRequestHandlerAndWithCaughtError(): void
@@ -134,7 +136,7 @@ public function testWithRequestHandlerAndWithAdditionOfListeners(): void
134136

135137
$this->assertInstanceOf(ResponseInterface::class, $response);
136138
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
137-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
139+
$this->assertSame('OK', $response->getReasonPhrase());
138140

139141
$this->assertFalse($firstListener->triggered());
140142
$this->assertFalse($secondListener->triggered());

tests/ErrorHandlerTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use HttpSoft\ErrorHandler\ErrorHandler;
88
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
9-
use HttpSoft\Request\ServerRequestFactory;
9+
use HttpSoft\ServerRequest\ServerRequestCreator;
1010
use HttpSoft\Response\ResponseStatusCodeInterface;
1111
use HttpSoft\Tests\ErrorHandler\TestAsset\ErrorRequestHandler;
1212
use HttpSoft\Tests\ErrorHandler\TestAsset\ThrowableRequestHandler;
@@ -25,6 +25,8 @@
2525

2626
class ErrorHandlerTest extends TestCase implements ResponseStatusCodeInterface
2727
{
28+
private const PHRASES = ErrorResponseGeneratorInterface::ERROR_PHRASES;
29+
2830
/**
2931
* @var int
3032
*/
@@ -37,7 +39,7 @@ class ErrorHandlerTest extends TestCase implements ResponseStatusCodeInterface
3739

3840
public function setUp(): void
3941
{
40-
$this->request = ServerRequestFactory::create();
42+
$this->request = ServerRequestCreator::create();
4143
$this->errorReporting = error_reporting();
4244
}
4345

@@ -52,7 +54,7 @@ public function testWithRequestHandlerAndWithStatusOk(): void
5254
$response = $errorHandler->handle($this->request);
5355
$this->assertInstanceOf(ResponseInterface::class, $response);
5456
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
55-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
57+
$this->assertSame('OK', $response->getReasonPhrase());
5658
}
5759

5860
public function testWithRequestHandlerAndWithStatusCreated(): void
@@ -61,7 +63,7 @@ public function testWithRequestHandlerAndWithStatusCreated(): void
6163
$response = $errorHandler->handle($this->request);
6264
$this->assertInstanceOf(ResponseInterface::class, $response);
6365
$this->assertSame(self::STATUS_CREATED, $response->getStatusCode());
64-
$this->assertSame(self::PHRASES[self::STATUS_CREATED], $response->getReasonPhrase());
66+
$this->assertSame('Created', $response->getReasonPhrase());
6567
}
6668

6769
public function testWithRequestHandlerAndWithErrorResponseGeneratorMock(): void
@@ -71,7 +73,7 @@ public function testWithRequestHandlerAndWithErrorResponseGeneratorMock(): void
7173
$response = $errorHandler->handle($this->request);
7274
$this->assertInstanceOf(ResponseInterface::class, $response);
7375
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
74-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
76+
$this->assertSame('OK', $response->getReasonPhrase());
7577
}
7678

7779
public function testWithThrowableRequestHandlerAndWithDefaultError(): void
@@ -107,7 +109,7 @@ public function testWithErrorRequestHandlerAndWithoutCaughtError(): void
107109
$response = $errorHandler->handle($this->request);
108110
$this->assertInstanceOf(ResponseInterface::class, $response);
109111
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
110-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
112+
$this->assertSame('OK', $response->getReasonPhrase());
111113
}
112114

113115
public function testWithErrorRequestHandlerAndWithCaughtError(): void
@@ -128,7 +130,7 @@ public function testWithRequestHandlerAndWithAdditionOfListeners(): void
128130

129131
$this->assertInstanceOf(ResponseInterface::class, $response);
130132
$this->assertSame(self::STATUS_OK, $response->getStatusCode());
131-
$this->assertSame(self::PHRASES[self::STATUS_OK], $response->getReasonPhrase());
133+
$this->assertSame('OK', $response->getReasonPhrase());
132134

133135
$this->assertFalse($firstListener->triggered());
134136
$this->assertFalse($secondListener->triggered());

tests/ErrorResponseGeneratorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use Exception;
88
use HttpSoft\ErrorHandler\ErrorResponseGenerator;
9-
use HttpSoft\Request\ServerRequest;
9+
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
10+
use HttpSoft\Message\ServerRequest;
1011
use HttpSoft\Response\HtmlResponse;
1112
use HttpSoft\Response\JsonResponse;
1213
use HttpSoft\Response\ResponseStatusCodeInterface;
@@ -19,6 +20,8 @@
1920

2021
class ErrorResponseGeneratorTest extends TestCase implements ResponseStatusCodeInterface
2122
{
23+
private const PHRASES = ErrorResponseGeneratorInterface::ERROR_PHRASES;
24+
2225
private ErrorResponseGenerator $generator;
2326

2427
public function setUp(): void

tests/TestAsset/ErrorRequestHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace HttpSoft\Tests\ErrorHandler\TestAsset;
66

7-
use HttpSoft\Response\ResponseFactory;
7+
use HttpSoft\Message\Response;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Psr\Http\Server\RequestHandlerInterface;
@@ -21,6 +21,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2121
{
2222
$array = [];
2323
$array['undefined'];
24-
return ResponseFactory::create();
24+
return new Response();
2525
}
2626
}

tests/TestAsset/RequestHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace HttpSoft\Tests\ErrorHandler\TestAsset;
66

7-
use HttpSoft\Response\ResponseFactory;
7+
use HttpSoft\Message\Response;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Psr\Http\Server\RequestHandlerInterface;
@@ -30,6 +30,6 @@ public function __construct(int $code = null)
3030
*/
3131
public function handle(ServerRequestInterface $request): ResponseInterface
3232
{
33-
return $this->code ? ResponseFactory::create($this->code) : ResponseFactory::create();
33+
return $this->code ? new Response($this->code) : new Response();
3434
}
3535
}

0 commit comments

Comments
 (0)