File tree Expand file tree Collapse file tree 2 files changed +90
-0
lines changed
Expand file tree Collapse file tree 2 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tests \Tempest \Fixtures \Controllers ;
6+
7+ use Tempest \Http \Response ;
8+ use Tempest \Http \Responses \Ok ;
9+ use Tempest \Router \Get ;
10+
11+ final readonly class OptionalParametersController
12+ {
13+ #[Get('/articles/{id}/{?slug} ' )]
14+ public function articles (string $ id , ?string $ slug = null ): Response
15+ {
16+ if ($ slug === null ) {
17+ return new Ok ("Article {$ id } without slug " );
18+ }
19+
20+ return new Ok ("Article {$ id } with slug {$ slug }" );
21+ }
22+
23+ #[Get('/users/{?id} ' )]
24+ public function users (?string $ id = null ): Response
25+ {
26+ if ($ id === null ) {
27+ return new Ok ('All users ' );
28+ }
29+
30+ return new Ok ("User {$ id }" );
31+ }
32+
33+ #[Get('/posts/{?id}/{?category} ' )]
34+ public function posts (?string $ id = null , ?string $ category = null ): Response
35+ {
36+ if ($ id === null && $ category === null ) {
37+ return new Ok ('All posts ' );
38+ }
39+
40+ if ($ category === null ) {
41+ return new Ok ("Post {$ id }" );
42+ }
43+
44+ return new Ok ("Post {$ id } in category {$ category }" );
45+ }
46+ }
Original file line number Diff line number Diff line change @@ -291,4 +291,48 @@ public function test_without_middleware_decorator(): void
291291 ->assertOk ()
292292 ->assertDoesNotHaveCookie (VerifyCsrfMiddleware::CSRF_COOKIE_KEY );
293293 }
294+
295+ public function test_optional_parameter_with_required_parameter (): void
296+ {
297+ $ this ->http
298+ ->get ('/articles/123 ' )
299+ ->assertOk ()
300+ ->assertSee ('Article 123 without slug ' );
301+
302+ $ this ->http
303+ ->get ('/articles/123/my-article ' )
304+ ->assertOk ()
305+ ->assertSee ('Article 123 with slug my-article ' );
306+ }
307+
308+ public function test_optional_parameter_only (): void
309+ {
310+ $ this ->http
311+ ->get ('/users ' )
312+ ->assertOk ()
313+ ->assertSee ('All users ' );
314+
315+ $ this ->http
316+ ->get ('/users/456 ' )
317+ ->assertOk ()
318+ ->assertSee ('User 456 ' );
319+ }
320+
321+ public function test_multiple_optional_parameters (): void
322+ {
323+ $ this ->http
324+ ->get ('/posts ' )
325+ ->assertOk ()
326+ ->assertSee ('All posts ' );
327+
328+ $ this ->http
329+ ->get ('/posts/789 ' )
330+ ->assertOk ()
331+ ->assertSee ('Post 789 ' );
332+
333+ $ this ->http
334+ ->get ('/posts/789/tech ' )
335+ ->assertOk ()
336+ ->assertSee ('Post 789 in category tech ' );
337+ }
294338}
You can’t perform that action at this time.
0 commit comments