|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Metadata\Tests; |
15 | 15 |
|
| 16 | +use ApiPlatform\Metadata\Parameter; |
16 | 17 | use ApiPlatform\Metadata\QueryParameter; |
17 | 18 | use ApiPlatform\State\ParameterNotFound; |
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
18 | 20 | use PHPUnit\Framework\TestCase; |
19 | 21 |
|
20 | 22 | class ParameterTest extends TestCase |
21 | 23 | { |
22 | 24 | public function testDefaultValue(): void |
23 | 25 | { |
24 | | - $parameter = new QueryParameter(); |
25 | | - $this->assertSame('test', $parameter->getValue('test')); |
26 | | - $this->assertInstanceOf(ParameterNotFound::class, $parameter->getValue()); |
| 26 | + $this->assertInstanceOf(ParameterNotFound::class, (new QueryParameter())->getValue()); |
| 27 | + } |
| 28 | + |
| 29 | + #[DataProvider('provideDefaultValueCases')] |
| 30 | + public function testDefaultValueWithFallbackValue(Parameter $parameter, mixed $fallbackValue, mixed $expectedDefault): void |
| 31 | + { |
| 32 | + $this->assertSame($expectedDefault, $parameter->getValue($fallbackValue)); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + /** @return iterable<[Parameter, mixed, mixed]> */ |
| 37 | + public static function provideDefaultValueCases(): iterable |
| 38 | + { |
| 39 | + $fallbackValue = new ParameterNotFound(); |
| 40 | + |
| 41 | + yield 'no default specified' => [ |
| 42 | + new QueryParameter(), $fallbackValue, $fallbackValue, |
| 43 | + ]; |
| 44 | + |
| 45 | + yield 'with a default specified' => [ |
| 46 | + new QueryParameter(default: false), $fallbackValue, false, |
| 47 | + ]; |
| 48 | + |
| 49 | + yield 'with null as default' => [ |
| 50 | + new QueryParameter(default: null), $fallbackValue, $fallbackValue, |
| 51 | + ]; |
27 | 52 | } |
28 | 53 | } |
0 commit comments