Skip to content

Commit a819979

Browse files
authored
feat(http)!: add query parameter to route testing utilities (#1583)
1 parent cd39b60 commit a819979

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
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
}

tests/Integration/Testing/Http/TestResponseHelperTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
1010
use Tempest\Framework\Testing\Http\TestResponseHelper;
11+
use Tempest\Http\GenericRequest;
1112
use Tempest\Http\GenericResponse;
13+
use Tempest\Http\Method;
1214
use Tempest\Http\Status;
1315

1416
/**
@@ -19,7 +21,7 @@ final class TestResponseHelperTest extends TestCase
1921
public function test_get_response(): void
2022
{
2123
$response = new GenericResponse(status: Status::OK);
22-
$helper = new TestResponseHelper($response);
24+
$helper = new TestResponseHelper($response, new GenericRequest(Method::GET, '/'));
2325

2426
$this->assertSame($response, $helper->response);
2527
}
@@ -31,6 +33,7 @@ public function test_assert_has_header(): void
3133
status: Status::OK,
3234
headers: ['Location' => '/new-location'],
3335
),
36+
new GenericRequest(Method::GET, '/'),
3437
);
3538

3639
$helper->assertHasHeader('Location');
@@ -39,9 +42,8 @@ public function test_assert_has_header(): void
3942
public function test_assert_has_header_failure(): void
4043
{
4144
$helper = new TestResponseHelper(
42-
new GenericResponse(
43-
status: Status::OK,
44-
),
45+
new GenericResponse(status: Status::OK),
46+
new GenericRequest(Method::GET, '/'),
4547
);
4648

4749
$this->expectException(AssertionFailedError::class);
@@ -56,6 +58,7 @@ public function test_assert_header_value_equals(): void
5658
status: Status::OK,
5759
headers: ['Content-Type' => ['application/json']],
5860
),
61+
new GenericRequest(Method::GET, '/'),
5962
);
6063

6164
$helper->assertHeaderContains('Content-Type', 'application/json');
@@ -65,6 +68,7 @@ public function test_assert_header_value_equals_failure(): void
6568
{
6669
$helper = new TestResponseHelper(
6770
new GenericResponse(status: Status::OK),
71+
new GenericRequest(Method::GET, '/'),
6872
);
6973

7074
$this->expectException(AssertionFailedError::class);
@@ -81,6 +85,7 @@ public function test_assert_redirect(): void
8185
'Location' => ['/other-location'],
8286
],
8387
),
88+
new GenericRequest(Method::GET, '/'),
8489
);
8590

8691
$helper->assertRedirect();
@@ -90,6 +95,7 @@ public function test_assert_redirect_without_location_header(): void
9095
{
9196
$helper = new TestResponseHelper(
9297
new GenericResponse(status: Status::FOUND),
98+
new GenericRequest(Method::GET, '/'),
9399
);
94100

95101
$this->expectException(AssertionFailedError::class);
@@ -104,6 +110,7 @@ public function test_assert_redirect_without_3xx_status_code(): void
104110
status: Status::OK,
105111
headers: ['Location' => '/other-location'],
106112
),
113+
new GenericRequest(Method::GET, '/'),
107114
);
108115

109116
$this->expectException(AssertionFailedError::class);
@@ -118,6 +125,7 @@ public function test_assert_redirect_to(): void
118125
status: Status::FOUND,
119126
headers: ['Location' => ['/other-location']],
120127
),
128+
new GenericRequest(Method::GET, '/'),
121129
);
122130

123131
$helper->assertRedirect('/other-location');
@@ -127,6 +135,7 @@ public function test_assert_ok(): void
127135
{
128136
$helper = new TestResponseHelper(
129137
new GenericResponse(status: Status::OK),
138+
new GenericRequest(Method::GET, '/'),
130139
);
131140

132141
$helper->assertOk();
@@ -136,6 +145,7 @@ public function test_assert_ok_fails_with_not_okay_response(): void
136145
{
137146
$helper = new TestResponseHelper(
138147
new GenericResponse(status: Status::INTERNAL_SERVER_ERROR),
148+
new GenericRequest(Method::GET, '/'),
139149
);
140150

141151
$this->expectException(AssertionFailedError::class);
@@ -147,6 +157,7 @@ public function test_assert_not_found(): void
147157
{
148158
$helper = new TestResponseHelper(
149159
new GenericResponse(status: Status::NOT_FOUND),
160+
new GenericRequest(Method::GET, '/'),
150161
);
151162

152163
$helper->assertNotFound();
@@ -156,6 +167,7 @@ public function test_assert_not_found_fails_with_okay_response(): void
156167
{
157168
$helper = new TestResponseHelper(
158169
new GenericResponse(status: Status::OK),
170+
new GenericRequest(Method::GET, '/'),
159171
);
160172

161173
$this->expectException(AssertionFailedError::class);
@@ -166,15 +178,15 @@ public function test_assert_not_found_fails_with_okay_response(): void
166178
#[DataProvider('provide_assert_status_cases')]
167179
public function test_assert_status(Status $expectedStatus, GenericResponse $response): void
168180
{
169-
$helper = new TestResponseHelper($response);
181+
$helper = new TestResponseHelper($response, new GenericRequest(Method::GET, '/'));
170182

171183
$helper->assertStatus($expectedStatus);
172184
}
173185

174186
#[DataProvider('provide_assert_status_fails_when_status_does_not_match_cases')]
175187
public function test_assert_status_fails_when_status_does_not_match(Status $expectedStatus, GenericResponse $response): void
176188
{
177-
$helper = new TestResponseHelper($response);
189+
$helper = new TestResponseHelper($response, new GenericRequest(Method::GET, '/'));
178190

179191
$this->expectException(AssertionFailedError::class);
180192

@@ -185,6 +197,7 @@ public function test_assert_json_has_keys(): void
185197
{
186198
$helper = new TestResponseHelper(
187199
new GenericResponse(status: Status::OK, body: ['title' => 'Timeline Taxi', 'author' => ['name' => 'John']]),
200+
new GenericRequest(Method::GET, '/'),
188201
);
189202

190203
$helper->assertJsonHasKeys('title', 'author.name');
@@ -194,6 +207,7 @@ public function test_assert_json_contains(): void
194207
{
195208
$helper = new TestResponseHelper(
196209
new GenericResponse(status: Status::OK, body: ['title' => 'Timeline Taxi', 'author' => ['name' => 'John']]),
210+
new GenericRequest(Method::GET, '/'),
197211
);
198212

199213
$helper->assertJsonContains(['title' => 'Timeline Taxi']);
@@ -205,6 +219,7 @@ public function test_assert_json(): void
205219
{
206220
$helper = new TestResponseHelper(
207221
new GenericResponse(status: Status::OK, body: ['title' => 'Timeline Taxi', 'author' => ['name' => 'John']]),
222+
new GenericRequest(Method::GET, '/'),
208223
);
209224

210225
$helper->assertJson(['title' => 'Timeline Taxi', 'author' => ['name' => 'John']]);

0 commit comments

Comments
 (0)