|
14 | 14 | namespace ApiPlatform\Tests\Symfony\Bundle\DependencyInjection; |
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 17 | +use ApiPlatform\Metadata\HeaderParameter; |
17 | 18 | use ApiPlatform\Symfony\Bundle\DependencyInjection\Configuration; |
18 | 19 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
19 | 20 | use Doctrine\ORM\OptimisticLockException; |
@@ -540,4 +541,43 @@ public function testOauthScopeNaming(): void |
540 | 541 |
|
541 | 542 | $this->assertEquals(['resource:read-write' => 'Read and write resource data'], $config['oauth']['scopes']); |
542 | 543 | } |
| 544 | + |
| 545 | + public function testDefaultParameters(): void |
| 546 | + { |
| 547 | + $config = $this->processor->processConfiguration($this->configuration, [ |
| 548 | + 'api_platform' => [ |
| 549 | + 'defaults' => [ |
| 550 | + 'parameters' => [ |
| 551 | + HeaderParameter::class => ['key' => 'API-Token'], |
| 552 | + 'request_id' => ['class' => HeaderParameter::class, 'key' => 'Request-ID'], |
| 553 | + 'api_version' => ['key' => 'api_version'], |
| 554 | + ], |
| 555 | + ], |
| 556 | + ], |
| 557 | + ]); |
| 558 | + |
| 559 | + $this->assertCount(3, $config['defaults']['parameters']); |
| 560 | + } |
| 561 | + |
| 562 | + public static function invalidDefaultParameterProvider(): iterable |
| 563 | + { |
| 564 | + yield 'unknown class option' => ['api_token', ['class' => 'ApiPlatform\Metadata\HeaderParamter'], 'ApiPlatform\Metadata\HeaderParamter']; |
| 565 | + yield 'unknown class as key' => ['ApiPlatform\Metadata\HeaderParamter', ['key' => 'API-Token'], 'ApiPlatform\Metadata\HeaderParamter']; |
| 566 | + yield 'not a parameter' => ['api_token', ['class' => \stdClass::class], \stdClass::class]; |
| 567 | + } |
| 568 | + |
| 569 | + #[DataProvider('invalidDefaultParameterProvider')] |
| 570 | + public function testDefaultParametersWithInvalidClass(string $name, array $parameter, string $class): void |
| 571 | + { |
| 572 | + $this->expectException(InvalidConfigurationException::class); |
| 573 | + $this->expectExceptionMessage(\sprintf('The default parameter "%s" must reference a class extending "ApiPlatform\Metadata\Parameter", "%s" given.', $name, $class)); |
| 574 | + |
| 575 | + $this->processor->processConfiguration($this->configuration, [ |
| 576 | + 'api_platform' => [ |
| 577 | + 'defaults' => [ |
| 578 | + 'parameters' => [$name => $parameter], |
| 579 | + ], |
| 580 | + ], |
| 581 | + ]); |
| 582 | + } |
543 | 583 | } |
0 commit comments