From 521679a295f922eb8b740d4079cd24ae18e134a8 Mon Sep 17 00:00:00 2001 From: butschster Date: Tue, 3 Oct 2023 16:34:12 +0400 Subject: [PATCH 1/2] Sync query parameters between ServerRequest and Uri - Merge URI query parameters with provided query params. - Adjust `createRequest` method to ensure both Uri and ServerRequest objects have consistent query values. --- src/Http/FakeHttp.php | 66 ++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index 4e6d767..fcdc8f9 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -36,7 +36,7 @@ class FakeHttp public function __construct( private readonly Container $container, private readonly FileFactory $fileFactory, - private readonly \Closure $scope + private readonly \Closure $scope, ) { } @@ -77,7 +77,7 @@ public function withHeader(string $key, $value): self public function withAuthorizationToken(string $token, string $type = 'Bearer'): self { - return $this->withHeader('Authorization', $type.' '.$token); + return $this->withHeader('Authorization', $type . ' ' . $token); } public function flushHeaders(): self @@ -112,7 +112,7 @@ public function withSession( array $data, string $clientSignature = 'fake-session', int $lifetime = 3600, - ?string $id = null + ?string $id = null, ): self { $this->session = new FakeSession($data, $clientSignature, $lifetime, $id); @@ -144,11 +144,11 @@ public function withoutMiddleware(string ...$middleware): self new class implements MiddlewareInterface { public function process( ServerRequestInterface $request, - RequestHandlerInterface $handler + RequestHandlerInterface $handler, ): ResponseInterface { return $handler->handle($request); } - } + }, ); } @@ -168,7 +168,7 @@ public function getHttp(): Http public function get(string $uri, array $query = [], array $headers = [], array $cookies = []): TestResponse { return $this->handleRequest( - $this->createRequest($uri, 'GET', $query, $headers, $cookies) + $this->createRequest($uri, 'GET', $query, $headers, $cookies), ); } @@ -176,10 +176,10 @@ public function getJson( string $uri, array $query = [], array $headers = [], - array $cookies = [] + array $cookies = [], ): TestResponse { return $this->handleRequest( - $this->createJsonRequest($uri, 'GET', $query, $headers, $cookies) + $this->createJsonRequest($uri, 'GET', $query, $headers, $cookies), ); } @@ -201,7 +201,7 @@ public function post( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { $this->validateRequestData($data); @@ -210,7 +210,7 @@ public function post( return $this->handleRequest( $data instanceof StreamInterface ? $request->withBody($data) - : $request->withParsedBody($data) + : $request->withParsedBody($data), ); } @@ -222,10 +222,10 @@ public function postJson( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { return $this->handleRequest( - $this->createJsonRequest($uri, 'POST', $data, $headers, $cookies, $files) + $this->createJsonRequest($uri, 'POST', $data, $headers, $cookies, $files), ); } @@ -237,7 +237,7 @@ public function patch( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { $this->validateRequestData($data); @@ -246,7 +246,7 @@ public function patch( return $this->handleRequest( $data instanceof StreamInterface ? $request->withBody($data) - : $request->withParsedBody($data) + : $request->withParsedBody($data), ); } @@ -258,10 +258,10 @@ public function patchJson( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { return $this->handleRequest( - $this->createJsonRequest($uri, 'PATCH', $data, $headers, $cookies, $files) + $this->createJsonRequest($uri, 'PATCH', $data, $headers, $cookies, $files), ); } @@ -270,7 +270,7 @@ public function put( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { $this->validateRequestData($data); @@ -279,7 +279,7 @@ public function put( return $this->handleRequest( $data instanceof StreamInterface ? $request->withBody($data) - : $request->withParsedBody($data) + : $request->withParsedBody($data), ); } @@ -288,10 +288,10 @@ public function putJson( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { return $this->handleRequest( - $this->createJsonRequest($uri, 'PUT', $data, $headers, $cookies, $files) + $this->createJsonRequest($uri, 'PUT', $data, $headers, $cookies, $files), ); } @@ -300,7 +300,7 @@ public function delete( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { $this->validateRequestData($data); @@ -309,7 +309,7 @@ public function delete( return $this->handleRequest( $data instanceof StreamInterface ? $request->withBody($data) - : $request->withParsedBody($data) + : $request->withParsedBody($data), ); } @@ -318,10 +318,10 @@ public function deleteJson( $data = [], array $headers = [], array $cookies = [], - array $files = [] + array $files = [], ): TestResponse { return $this->handleRequest( - $this->createJsonRequest($uri, 'DELETE', $data, $headers, $cookies, $files) + $this->createJsonRequest($uri, 'DELETE', $data, $headers, $cookies, $files), ); } @@ -331,15 +331,15 @@ protected function createJsonRequest( $data, array $headers, array $cookies, - array $files = [] + array $files = [], ): ServerRequest { if (!\is_array($data) && !$data instanceof StreamInterface) { throw new \InvalidArgumentException( - \sprintf('$data should be array or instance of `%s` interface.', StreamInterface::class) + \sprintf('$data should be array or instance of `%s` interface.', StreamInterface::class), ); } - if (! $data instanceof StreamInterface) { + if (!$data instanceof StreamInterface) { $data = Stream::create(\json_encode($data)); } @@ -362,7 +362,7 @@ protected function createRequest( array $query, array $headers, array $cookies, - array $files = [] + array $files = [], ): ServerRequest { $cookies = \array_merge($this->defaultCookies, $cookies); $headers = \array_merge($this->defaultHeaders, $headers); @@ -373,9 +373,17 @@ protected function createRequest( $headers, 'php://input', '1.1', - $this->defaultServerVariables + $this->defaultServerVariables, ); + if ($request->getUri()->getQuery() !== '') { + $uriQuery = []; + \parse_str($request->getUri()->getQuery(), $uriQuery); + $query = \array_merge($uriQuery, $query); + } else { + $request = $request->getUri()->withQuery(\http_build_query($query)); + } + return $request ->withCookieParams($cookies) ->withQueryParams($query) From c6bc5fdf277c8174833a26c56615c879ab1c97e4 Mon Sep 17 00:00:00 2001 From: butschster Date: Tue, 3 Oct 2023 16:45:56 +0400 Subject: [PATCH 2/2] fix --- src/Http/FakeHttp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index fcdc8f9..e4884b9 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -381,7 +381,7 @@ protected function createRequest( \parse_str($request->getUri()->getQuery(), $uriQuery); $query = \array_merge($uriQuery, $query); } else { - $request = $request->getUri()->withQuery(\http_build_query($query)); + $request = $request->withUri($request->getUri()->withQuery(\http_build_query($query))); } return $request