From 3cfd616e77fb8dbc564387a67697b6af1f3f3f6b Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Thu, 17 Oct 2024 17:25:37 +0800 Subject: [PATCH] Adds `Request::getPsrRequest()` (#725) * Adds `Request::getPsrRequest()` * test: Add test for `getPsrRequest()` method in HttpServerRequest --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- .../HttpServer/Contract/RequestInterface.php | 2 ++ src/macros/src/RequestMixin.php | 6 ++++++ tests/Macros/HttpServerRequestTest.php | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/macros/output/Hyperf/HttpServer/Contract/RequestInterface.php b/src/macros/output/Hyperf/HttpServer/Contract/RequestInterface.php index 84d522583..1e8550afe 100644 --- a/src/macros/output/Hyperf/HttpServer/Contract/RequestInterface.php +++ b/src/macros/output/Hyperf/HttpServer/Contract/RequestInterface.php @@ -101,6 +101,8 @@ public function getHttpHost(): string; public function getPort(): int; + public function getPsrRequest(): ?ServerRequestInterface; + public function getScheme(): string; public function isSecure(): bool; diff --git a/src/macros/src/RequestMixin.php b/src/macros/src/RequestMixin.php index 2f8b6ee98..9b1675a94 100644 --- a/src/macros/src/RequestMixin.php +++ b/src/macros/src/RequestMixin.php @@ -15,6 +15,7 @@ use Carbon\Carbon; use Hyperf\Collection\Arr; use Hyperf\Context\Context; +use Hyperf\Context\RequestContext; use Hyperf\HttpMessage\Server\Request as ServerRequest; use Hyperf\HttpServer\Request; use Hyperf\Stringable\Str; @@ -170,6 +171,11 @@ public function getHttpHost() return fn () => $this->getHost() . ':' . $this->getPort(); } + public function getPsrRequest() + { + return fn () => RequestContext::getOrNull(); + } + public function getPort() { return function () { diff --git a/tests/Macros/HttpServerRequestTest.php b/tests/Macros/HttpServerRequestTest.php index ffc468503..941ee281a 100644 --- a/tests/Macros/HttpServerRequestTest.php +++ b/tests/Macros/HttpServerRequestTest.php @@ -46,7 +46,7 @@ expect($request->isEmptyString('id'))->toBeFalse(); }); -test('test get host', function () { +test('test getHost', function () { $request = new Request(); $host = 'foo.com'; @@ -75,7 +75,7 @@ expect($request->getHost())->toBe($host); }); -test('test get port', function () { +test('test getPort', function () { $request = new Request(); $port = 80; @@ -113,7 +113,7 @@ expect($request->getPort())->toBe($port); }); -test('test get scheme', function () { +test('test getScheme', function () { $request = new Request(); $psrRequest = m::mock(ServerRequestPlusInterface::class, function ($mock) { @@ -168,3 +168,12 @@ expect($request->getMethod())->toBe('POST'); expect($request->getUri()->getPath())->toBe('/foo'); }); + +test('test getPsrRequest', function () { + $request = new Request(); + expect($request->getPsrRequest())->toBeNull(); + + $psrRequest = Request::fake(); + Context::set(ServerRequestInterface::class, $psrRequest); + expect($request->getPsrRequest())->toBe($psrRequest); +});