Skip to content

Commit 16598e4

Browse files
committed
feat(http): add query parameter to route testing utilities
1 parent 83320ab commit 16598e4

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/Tempest/Framework/Testing/Http/HttpRouterTester.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Tempest\Http\Request;
1414
use Tempest\Router\RouteConfig;
1515
use Tempest\Router\Router;
16-
use Tempest\Validation\Validator;
16+
use Tempest\Support\Uri;
1717

1818
use function Tempest\map;
1919

@@ -25,108 +25,108 @@ public function __construct(
2525
private Container $container,
2626
) {}
2727

28-
public function get(string $uri, array $headers = []): TestResponseHelper
28+
public function get(string $uri, array $query = [], array $headers = []): TestResponseHelper
2929
{
3030
return $this->sendRequest(
3131
new GenericRequest(
3232
method: Method::GET,
33-
uri: $uri,
33+
uri: Uri\merge_query($uri, ...$query),
3434
body: [],
3535
headers: $headers,
3636
),
3737
);
3838
}
3939

40-
public function head(string $uri, array $headers = []): TestResponseHelper
40+
public function head(string $uri, array $query = [], array $headers = []): TestResponseHelper
4141
{
4242
return $this->sendRequest(
4343
new GenericRequest(
4444
method: Method::HEAD,
45-
uri: $uri,
45+
uri: Uri\merge_query($uri, ...$query),
4646
body: [],
4747
headers: $headers,
4848
),
4949
);
5050
}
5151

52-
public function post(string $uri, array $body = [], array $headers = []): TestResponseHelper
52+
public function post(string $uri, array $body = [], array $query = [], array $headers = []): TestResponseHelper
5353
{
5454
return $this->sendRequest(
5555
new GenericRequest(
5656
method: Method::POST,
57-
uri: $uri,
57+
uri: Uri\merge_query($uri, ...$query),
5858
body: $body,
5959
headers: $headers,
6060
),
6161
);
6262
}
6363

64-
public function put(string $uri, array $body = [], array $headers = []): TestResponseHelper
64+
public function put(string $uri, array $body = [], array $query = [], array $headers = []): TestResponseHelper
6565
{
6666
return $this->sendRequest(
6767
new GenericRequest(
6868
method: Method::PUT,
69-
uri: $uri,
69+
uri: Uri\merge_query($uri, ...$query),
7070
body: $body,
7171
headers: $headers,
7272
),
7373
);
7474
}
7575

76-
public function delete(string $uri, array $body = [], array $headers = []): TestResponseHelper
76+
public function delete(string $uri, array $body = [], array $query = [], array $headers = []): TestResponseHelper
7777
{
7878
return $this->sendRequest(
7979
new GenericRequest(
8080
method: Method::DELETE,
81-
uri: $uri,
81+
uri: Uri\merge_query($uri, ...$query),
8282
body: $body,
8383
headers: $headers,
8484
),
8585
);
8686
}
8787

88-
public function connect(string $uri, array $body = [], array $headers = []): TestResponseHelper
88+
public function connect(string $uri, array $query = [], array $headers = []): TestResponseHelper
8989
{
9090
return $this->sendRequest(
9191
new GenericRequest(
9292
method: Method::CONNECT,
93-
uri: $uri,
94-
body: $body,
93+
uri: Uri\merge_query($uri, ...$query),
94+
body: [],
9595
headers: $headers,
9696
),
9797
);
9898
}
9999

100-
public function options(string $uri, array $body = [], array $headers = []): TestResponseHelper
100+
public function options(string $uri, array $query = [], array $headers = []): TestResponseHelper
101101
{
102102
return $this->sendRequest(
103103
new GenericRequest(
104104
method: Method::OPTIONS,
105-
uri: $uri,
106-
body: $body,
105+
uri: Uri\merge_query($uri, ...$query),
106+
body: [],
107107
headers: $headers,
108108
),
109109
);
110110
}
111111

112-
public function trace(string $uri, array $body = [], array $headers = []): TestResponseHelper
112+
public function trace(string $uri, array $query = [], array $headers = []): TestResponseHelper
113113
{
114114
return $this->sendRequest(
115115
new GenericRequest(
116116
method: Method::TRACE,
117-
uri: $uri,
118-
body: $body,
117+
uri: Uri\merge_query($uri, ...$query),
118+
body: [],
119119
headers: $headers,
120120
),
121121
);
122122
}
123123

124-
public function patch(string $uri, array $body = [], array $headers = []): TestResponseHelper
124+
public function patch(string $uri, array $body = [], array $query = [], array $headers = []): TestResponseHelper
125125
{
126126
return $this->sendRequest(
127127
new GenericRequest(
128128
method: Method::PATCH,
129-
uri: $uri,
129+
uri: Uri\merge_query($uri, ...$query),
130130
body: $body,
131131
headers: $headers,
132132
),
@@ -142,6 +142,7 @@ public function sendRequest(Request $request): TestResponseHelper
142142

143143
return new TestResponseHelper(
144144
response: $router->dispatch(map($request)->with(RequestToPsrRequestMapper::class)->do()),
145+
request: $request,
145146
container: $this->container,
146147
);
147148
}

src/Tempest/Framework/Testing/Http/TestResponseHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Tempest\Container\Container;
1313
use Tempest\Cryptography\Encryption\Encrypter;
1414
use Tempest\Http\Cookie\Cookie;
15+
use Tempest\Http\Request;
1516
use Tempest\Http\Response;
1617
use Tempest\Http\Responses\Invalid;
1718
use Tempest\Http\Session\Session;
@@ -22,13 +23,13 @@
2223
use Tempest\View\View;
2324
use Tempest\View\ViewRenderer;
2425

25-
use function Tempest\get;
2626
use function Tempest\Support\arr;
2727

2828
final class TestResponseHelper
2929
{
3030
public function __construct(
3131
private(set) Response $response,
32+
private(set) Request $request,
3233
private(set) ?Container $container = null,
3334
) {}
3435

tests/Integration/Testing/Http/HttpRouterTesterIntegrationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,18 @@ public function test_patch_requests_failure(): void
164164
->patch('/this-route-does-not-exist')
165165
->assertOk();
166166
}
167+
168+
public function test_query(): void
169+
{
170+
$this->assertSame($this->http->get('/test?foo=baz', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
171+
$this->assertSame($this->http->get('/test?jon=doe', query: ['foo' => 'bar'])->request->uri, '/test?jon=doe&foo=bar');
172+
$this->assertSame($this->http->get('/test?jon=doe', query: ['foo' => ['bar' => 'baz']])->request->uri, '/test?jon=doe&foo%5Bbar%5D=baz');
173+
174+
$this->assertSame($this->http->get('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
175+
$this->assertSame($this->http->post('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
176+
$this->assertSame($this->http->put('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
177+
$this->assertSame($this->http->delete('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
178+
$this->assertSame($this->http->patch('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
179+
$this->assertSame($this->http->head('/test', query: ['foo' => 'bar'])->request->uri, '/test?foo=bar');
180+
}
167181
}

0 commit comments

Comments
 (0)