Skip to content

Commit

Permalink
Merge pull request #63 from spiral/hotfix/promote-query-from-uri
Browse files Browse the repository at this point in the history
Synchronize Query Parameters Between `ServerRequest` and `Uri`
  • Loading branch information
butschster committed Oct 3, 2023
2 parents 333f209 + c6bc5fd commit fdbe8f0
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}
},
);
}

Expand All @@ -168,18 +168,18 @@ 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),
);
}

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),
);
}

Expand All @@ -201,7 +201,7 @@ public function post(
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

Expand All @@ -210,7 +210,7 @@ public function post(
return $this->handleRequest(
$data instanceof StreamInterface
? $request->withBody($data)
: $request->withParsedBody($data)
: $request->withParsedBody($data),
);
}

Expand All @@ -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),
);
}

Expand All @@ -237,7 +237,7 @@ public function patch(
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

Expand All @@ -246,7 +246,7 @@ public function patch(
return $this->handleRequest(
$data instanceof StreamInterface
? $request->withBody($data)
: $request->withParsedBody($data)
: $request->withParsedBody($data),
);
}

Expand All @@ -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),
);
}

Expand All @@ -270,7 +270,7 @@ public function put(
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

Expand All @@ -279,7 +279,7 @@ public function put(
return $this->handleRequest(
$data instanceof StreamInterface
? $request->withBody($data)
: $request->withParsedBody($data)
: $request->withParsedBody($data),
);
}

Expand All @@ -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),
);
}

Expand All @@ -300,7 +300,7 @@ public function delete(
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

Expand All @@ -309,7 +309,7 @@ public function delete(
return $this->handleRequest(
$data instanceof StreamInterface
? $request->withBody($data)
: $request->withParsedBody($data)
: $request->withParsedBody($data),
);
}

Expand All @@ -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),
);
}

Expand All @@ -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));
}

Expand All @@ -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);
Expand All @@ -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->withUri($request->getUri()->withQuery(\http_build_query($query)));
}

return $request
->withCookieParams($cookies)
->withQueryParams($query)
Expand Down

0 comments on commit fdbe8f0

Please sign in to comment.