Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize Query Parameters Between ServerRequest and Uri #63

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 withAuthorizationToken(string $token, string $type = 'Bearer'): self
{
return $this->withHeader('Authorization', $type.' '.$token);
return $this->withHeader('Authorization', $type . ' ' . $token);

Check warning on line 80 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L80

Added line #L80 was not covered by tests
}

public function flushHeaders(): self
Expand Down Expand Up @@ -112,7 +112,7 @@
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 @@
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 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 @@
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

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

Check warning on line 213 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L213

Added line #L213 was not covered by tests
);
}

Expand All @@ -222,10 +222,10 @@
$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 @@
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

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

Check warning on line 249 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L249

Added line #L249 was not covered by tests
);
}

Expand All @@ -258,10 +258,10 @@
$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),

Check warning on line 264 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L264

Added line #L264 was not covered by tests
);
}

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

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

Check warning on line 282 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L282

Added line #L282 was not covered by tests
);
}

Expand All @@ -288,10 +288,10 @@
$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 @@
$data = [],
array $headers = [],
array $cookies = [],
array $files = []
array $files = [],
): TestResponse {
$this->validateRequestData($data);

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

Check warning on line 312 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L312

Added line #L312 was not covered by tests
);
}

Expand All @@ -318,10 +318,10 @@
$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 @@
$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),

Check warning on line 338 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L338

Added line #L338 was not covered by tests
);
}

if (! $data instanceof StreamInterface) {
if (!$data instanceof StreamInterface) {
$data = Stream::create(\json_encode($data));
}

Expand All @@ -362,7 +362,7 @@
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 @@
$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);

Check warning on line 382 in src/Http/FakeHttp.php

View check run for this annotation

Codecov / codecov/patch

src/Http/FakeHttp.php#L380-L382

Added lines #L380 - L382 were not covered by tests
} else {
$request = $request->withUri($request->getUri()->withQuery(\http_build_query($query)));
}

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