-
-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c07abe0
commit c86bd62
Showing
6 changed files
with
551 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
Tests/RouteDescriber/SymfonyAnnotationDescriber/SymfonyMapQueryParameterDescriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\RouteDescriber\SymfonyAnnotationDescriber; | ||
|
||
use Nelmio\ApiDocBundle\Annotation\Operation; | ||
use Nelmio\ApiDocBundle\RouteDescriber\InlineParameterDescriber\SymfonyMapQueryParameterDescriber; | ||
use Nelmio\ApiDocBundle\Tests\Functional\WebTestCase; | ||
use OpenApi\Annotations\OpenApi; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionAttribute; | ||
use ReflectionParameter; | ||
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface; | ||
use const PHP_VERSION_ID; | ||
|
||
class SymfonyMapQueryParameterDescriberTest extends WebTestCase | ||
{ | ||
/** | ||
* @var SymfonyMapQueryParameterDescriber | ||
*/ | ||
private $symfonyMapQueryParameterDescriber; | ||
|
||
/** | ||
* @var ArgumentMetadataFactoryInterface | ||
*/ | ||
private $argumentMetadataFactory; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
self::markTestSkipped('Attributes require PHP 8'); | ||
} | ||
|
||
if (!class_exists(MapQueryParameter::class)) { | ||
self::markTestSkipped('Symfony 6.3 MapQueryParameter attribute not found'); | ||
} | ||
|
||
$this->argumentMetadataFactory = self::getContainer()->get('argument_metadata_factory'); | ||
|
||
$this->symfonyMapQueryParameterDescriber = new SymfonyMapQueryParameterDescriber(); | ||
} | ||
|
||
/** | ||
* @dataProvider provideMapQueryParameterTestData | ||
*/ | ||
public function testMapQueryParameter(callable $function): void | ||
{ | ||
$argumentMetaData = $this->argumentMetadataFactory->createArgumentMetadata($function)[0]; | ||
|
||
$this->symfonyMapQueryParameterDescriber->describe( | ||
new OpenApi([]), | ||
$operation = new Operation([]), | ||
$argumentMetaData | ||
); | ||
|
||
/** @var MapQueryParameter $mapQueryParameter */ | ||
$mapQueryParameter = $argumentMetaData->getAttributes(MapQueryParameter::class, ArgumentMetadata::IS_INSTANCEOF)[0]; | ||
|
||
$documentationParameter = $operation->parameters[0]; | ||
self::assertSame($mapQueryParameter->name ?? $argumentMetaData->getName(), $documentationParameter->name); | ||
self::assertSame('query', $documentationParameter->in); | ||
self::assertSame(!$argumentMetaData->hasDefaultValue() && !$argumentMetaData->isNullable(), $documentationParameter->required); | ||
|
||
$schema = $documentationParameter->schema; | ||
self::assertSame('integer', $schema->type); | ||
if ($argumentMetaData->hasDefaultValue()) { | ||
self::assertSame($argumentMetaData->getDefaultValue(), $schema->default); | ||
} | ||
|
||
if (FILTER_VALIDATE_REGEXP === $mapQueryParameter->filter) { | ||
self::assertSame($mapQueryParameter->options['regexp'], $schema->pattern); | ||
} | ||
} | ||
|
||
public static function provideMapQueryParameterTestData(): iterable | ||
{ | ||
yield 'it documents query parameters' => [ | ||
function ( | ||
#[MapQueryParameter] int $parameter1, | ||
) { | ||
}, | ||
]; | ||
|
||
yield 'it documents query parameters with default values' => [ | ||
function ( | ||
#[MapQueryParameter] int $parameter1 = 123, | ||
) { | ||
}, | ||
]; | ||
|
||
yield 'it documents query parameters with nullable types' => [ | ||
function ( | ||
#[MapQueryParameter] ?int $parameter1, | ||
) { | ||
}, | ||
]; | ||
|
||
yield 'it uses MapQueryParameter name argument as name' => [ | ||
function ( | ||
#[MapQueryParameter('someOtherParameter1Name')] int $parameter1, | ||
) { | ||
}, | ||
]; | ||
|
||
yield 'it documents regex pattern' => [ | ||
function ( | ||
#[MapQueryParameter(filter: FILTER_VALIDATE_REGEXP, options: ['regexp' => '/^\d+$/'])] int $parameter1, | ||
) { | ||
}, | ||
]; | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
Tests/RouteDescriber/SymfonyAnnotationDescriber/SymfonyMapQueryStringDescriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\RouteDescriber\SymfonyAnnotationDescriber; | ||
|
||
use Nelmio\ApiDocBundle\Annotation\Operation; | ||
use Nelmio\ApiDocBundle\Model\ModelRegistry; | ||
use Nelmio\ApiDocBundle\ModelDescriber\SelfDescribingModelDescriber; | ||
use Nelmio\ApiDocBundle\RouteDescriber\InlineParameterDescriber\SymfonyMapQueryStringDescriber; | ||
use Nelmio\ApiDocBundle\Tests\Functional\WebTestCase; | ||
use Nelmio\ApiDocBundle\Tests\RouteDescriber\Fixtures\DTO; | ||
use Nelmio\ApiDocBundle\Tests\RouteDescriber\Fixtures\SymfonyDescriberMapQueryStringClass; | ||
use OpenApi\Annotations\OpenApi; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionParameter; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\HttpKernel\Attribute\MapQueryString; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface; | ||
use const PHP_VERSION_ID; | ||
|
||
class SymfonyMapQueryStringDescriberTest extends WebTestCase | ||
{ | ||
/** | ||
* @var OpenApi | ||
*/ | ||
private $openApi; | ||
/** | ||
* @var SymfonyMapQueryStringDescriber | ||
*/ | ||
private $symfonyMapQueryStringDescriber; | ||
|
||
/** | ||
* @var ArgumentMetadataFactoryInterface | ||
*/ | ||
private $argumentMetadataFactory; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
self::markTestSkipped('Attributes require PHP 8'); | ||
} | ||
|
||
if (!class_exists(MapQueryString::class)) { | ||
self::markTestSkipped('Symfony 6.3 MapQueryString attribute not found'); | ||
} | ||
|
||
$this->argumentMetadataFactory = self::getContainer()->get('argument_metadata_factory'); | ||
|
||
$this->openApi = new OpenApi([]); | ||
|
||
$this->symfonyMapQueryStringDescriber = new SymfonyMapQueryStringDescriber([new SelfDescribingModelDescriber()]); | ||
|
||
$registry = new ModelRegistry([], $this->openApi, []); | ||
|
||
$this->symfonyMapQueryStringDescriber->setModelRegistry($registry); | ||
} | ||
|
||
/** | ||
* @dataProvider provideMapQueryStringTestData | ||
*/ | ||
public function testMapQueryString(callable $function, bool $required): void | ||
{ | ||
$argumentMetaData = $this->argumentMetadataFactory->createArgumentMetadata($function)[0]; | ||
|
||
$this->symfonyMapQueryStringDescriber->describe( | ||
$this->openApi, | ||
$operation = new Operation([]), | ||
$argumentMetaData | ||
); | ||
|
||
// Test it registers the model | ||
$modelSchema = $this->openApi->components->schemas[0]; | ||
$expectedModelProperties = SymfonyDescriberMapQueryStringClass::getProperties(); | ||
|
||
self::assertSame(SymfonyDescriberMapQueryStringClass::SCHEMA, $modelSchema->schema); | ||
self::assertSame(SymfonyDescriberMapQueryStringClass::TITLE, $modelSchema->title); | ||
self::assertSame(SymfonyDescriberMapQueryStringClass::TYPE, $modelSchema->type); | ||
self::assertEquals($expectedModelProperties, $modelSchema->properties); | ||
|
||
foreach ($expectedModelProperties as $key => $expectedModelProperty) { | ||
$queryParameter = $operation->parameters[$key]; | ||
|
||
self::assertSame('query', $queryParameter->in); | ||
self::assertSame($expectedModelProperty->property, $queryParameter->name); | ||
self::assertSame($required, $queryParameter->required); | ||
} | ||
} | ||
|
||
public static function provideMapQueryStringTestData(): iterable | ||
{ | ||
yield 'it documents query string parameters' => [ | ||
function ( | ||
#[MapQueryString] SymfonyDescriberMapQueryStringClass $parameter1, | ||
) { | ||
}, | ||
true | ||
]; | ||
|
||
yield 'it documents a nullable type as optional' => [ | ||
function ( | ||
#[MapQueryString] ?SymfonyDescriberMapQueryStringClass $parameter1, | ||
) { | ||
}, | ||
false | ||
]; | ||
|
||
yield 'it documents a default value as optional' => [ | ||
function ( | ||
#[MapQueryString] ?SymfonyDescriberMapQueryStringClass $parameter1, | ||
) { | ||
}, | ||
false | ||
]; | ||
} | ||
|
||
public function testItDescribesProperties(): void | ||
{ | ||
$function = function ( | ||
#[MapQueryString] DTO $DTO, | ||
) { | ||
}; | ||
|
||
$argumentMetaData = $this->argumentMetadataFactory->createArgumentMetadata($function)[0]; | ||
|
||
$this->symfonyMapQueryStringDescriber->describe( | ||
$this->openApi, | ||
$operation = new Operation([]), | ||
$argumentMetaData | ||
); | ||
|
||
// Test it registers the model | ||
$modelSchema = $this->openApi->components->schemas[0]; | ||
|
||
self::assertEquals('object', $modelSchema->type); | ||
self::assertEquals(DTO::getRequired(), $modelSchema->required); | ||
self::assertEquals(DTO::getProperties(), $modelSchema->properties); | ||
|
||
self::assertSame('id', $operation->parameters[0]->name); | ||
self::assertSame('int', $operation->parameters[0]->schema->type); | ||
|
||
self::assertSame('name', $operation->parameters[1]->name); | ||
|
||
self::assertSame('nullableName', $operation->parameters[2]->name); | ||
self::assertSame('string', $operation->parameters[2]->schema->type); | ||
self::assertSame(false, $operation->parameters[2]->required); | ||
self::assertSame(true, $operation->parameters[2]->schema->nullable); | ||
|
||
self::assertSame('nameWithExample', $operation->parameters[3]->name); | ||
self::assertSame('string', $operation->parameters[3]->schema->type); | ||
self::assertSame(true, $operation->parameters[3]->required); | ||
self::assertSame(DTO::EXAMPLE_NAME, $operation->parameters[3]->schema->example); | ||
self::assertSame(DTO::EXAMPLE_NAME, $operation->parameters[3]->example); | ||
|
||
self::assertSame('nameWithDescription', $operation->parameters[4]->name); | ||
self::assertSame('string', $operation->parameters[4]->schema->type); | ||
self::assertSame(true, $operation->parameters[4]->required); | ||
self::assertSame(DTO::DESCRIPTION, $operation->parameters[4]->schema->description); | ||
self::assertSame(DTO::DESCRIPTION, $operation->parameters[4]->description); | ||
} | ||
} |
Oops, something went wrong.