|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Tempest\Integration\Route; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Tempest\Http\Status; |
| 9 | +use Tempest\Router\GenericRouter; |
| 10 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +final class InferredConstraintsTest extends FrameworkIntegrationTestCase |
| 16 | +{ |
| 17 | + #[Test] |
| 18 | + public function int_parameter_accepts_numeric_values(): void |
| 19 | + { |
| 20 | + $router = $this->container->get(GenericRouter::class); |
| 21 | + |
| 22 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/int/123')); |
| 23 | + |
| 24 | + $this->assertEquals(Status::OK, $response->status); |
| 25 | + $this->assertEquals('int: 123', $response->body); |
| 26 | + } |
| 27 | + |
| 28 | + #[Test] |
| 29 | + public function int_parameter_rejects_non_numeric_values(): void |
| 30 | + { |
| 31 | + $this->http |
| 32 | + ->get('/inferred/int/abc') |
| 33 | + ->assertNotFound(); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function string_parameter_accepts_any_value(): void |
| 38 | + { |
| 39 | + $router = $this->container->get(GenericRouter::class); |
| 40 | + |
| 41 | + $response1 = $router->dispatch($this->http->makePsrRequest('/inferred/string/test')); |
| 42 | + $this->assertEquals(Status::OK, $response1->status); |
| 43 | + $this->assertEquals('string: test', $response1->body); |
| 44 | + |
| 45 | + $response2 = $router->dispatch($this->http->makePsrRequest('/inferred/string/123')); |
| 46 | + $this->assertEquals(Status::OK, $response2->status); |
| 47 | + $this->assertEquals('string: 123', $response2->body); |
| 48 | + } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + public function float_parameter_accepts_decimal_values(): void |
| 52 | + { |
| 53 | + $router = $this->container->get(GenericRouter::class); |
| 54 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/float/12.34')); |
| 55 | + |
| 56 | + $this->assertEquals(Status::OK, $response->status); |
| 57 | + $this->assertEquals('float: 12.34', $response->body); |
| 58 | + } |
| 59 | + |
| 60 | + #[Test] |
| 61 | + public function float_parameter_accepts_integer_values(): void |
| 62 | + { |
| 63 | + $router = $this->container->get(GenericRouter::class); |
| 64 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/float/42')); |
| 65 | + |
| 66 | + $this->assertEquals(Status::OK, $response->status); |
| 67 | + $this->assertEquals('float: 42', $response->body); |
| 68 | + } |
| 69 | + |
| 70 | + #[Test] |
| 71 | + public function optional_int_parameter_accepts_numeric_values(): void |
| 72 | + { |
| 73 | + $router = $this->container->get(GenericRouter::class); |
| 74 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/optional-int/456')); |
| 75 | + |
| 76 | + $this->assertEquals(Status::OK, $response->status); |
| 77 | + $this->assertEquals('int: 456', $response->body); |
| 78 | + } |
| 79 | + |
| 80 | + #[Test] |
| 81 | + public function optional_int_parameter_rejects_non_numeric_values(): void |
| 82 | + { |
| 83 | + $this->http |
| 84 | + ->get('/inferred/optional-int/xyz') |
| 85 | + ->assertNotFound(); |
| 86 | + } |
| 87 | + |
| 88 | + #[Test] |
| 89 | + public function optional_int_parameter_accepts_no_value(): void |
| 90 | + { |
| 91 | + $router = $this->container->get(GenericRouter::class); |
| 92 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/optional-int')); |
| 93 | + |
| 94 | + $this->assertEquals(Status::OK, $response->status); |
| 95 | + $this->assertEquals('no id', $response->body); |
| 96 | + } |
| 97 | + |
| 98 | + #[Test] |
| 99 | + public function explicit_constraint_is_preserved(): void |
| 100 | + { |
| 101 | + $this->http |
| 102 | + ->get('/inferred/explicit/123') |
| 103 | + ->assertOk() |
| 104 | + ->assertSee('explicit: 123'); |
| 105 | + |
| 106 | + $this->http |
| 107 | + ->get('/inferred/explicit/1234') |
| 108 | + ->assertNotFound(); |
| 109 | + |
| 110 | + $this->http |
| 111 | + ->get('/inferred/explicit/12') |
| 112 | + ->assertNotFound(); |
| 113 | + } |
| 114 | + |
| 115 | + #[Test] |
| 116 | + public function mixed_parameter_types(): void |
| 117 | + { |
| 118 | + $router = $this->container->get(GenericRouter::class); |
| 119 | + $response = $router->dispatch($this->http->makePsrRequest('/inferred/mixed/42/john')); |
| 120 | + |
| 121 | + $this->assertEquals(Status::OK, $response->status); |
| 122 | + $this->assertEquals('id: 42, name: john', $response->body); |
| 123 | + } |
| 124 | + |
| 125 | + #[Test] |
| 126 | + public function mixed_parameter_types_reject_invalid_int(): void |
| 127 | + { |
| 128 | + $this->http |
| 129 | + ->get('/inferred/mixed/abc/john') |
| 130 | + ->assertNotFound(); |
| 131 | + } |
| 132 | +} |
0 commit comments