From e5f22fb164e7a487649f832ff8fd61b3c1ac79c7 Mon Sep 17 00:00:00 2001 From: stollr Date: Wed, 3 Apr 2024 00:02:00 +0200 Subject: [PATCH] Added tests for UuidPropertyDescriber --- composer.json | 1 + .../Functional/Controller/ApiController80.php | 14 ++++ .../Functional/Controller/ApiController81.php | 14 ++++ tests/Functional/Entity/EntityWithUuid.php | 17 ++++ tests/Functional/FunctionalTest.php | 12 +++ .../UuidPropertyDescriberTest.php | 82 +++++++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 tests/Functional/Entity/EntityWithUuid.php create mode 100644 tests/PropertyDescriber/UuidPropertyDescriberTest.php diff --git a/composer.json b/composer.json index 7bd76fbfd..29fd57b65 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "symfony/validator": "^5.4|^6.0|^7.0", "symfony/expression-language": "^5.4|^6.0|^7.0", "symfony/security-csrf": "^5.4|^6.0|^7.0", + "symfony/uid": "^5.1|^6.0|^7.0", "api-platform/core": "^2.7.0|^3", "friendsofsymfony/rest-bundle": "^2.8|^3.0", diff --git a/tests/Functional/Controller/ApiController80.php b/tests/Functional/Controller/ApiController80.php index ee4d05ff0..7b61a3ade 100644 --- a/tests/Functional/Controller/ApiController80.php +++ b/tests/Functional/Controller/ApiController80.php @@ -26,6 +26,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithNullableSchemaSet; use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType; use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid; use Nelmio\ApiDocBundle\Tests\Functional\Entity\RangeInteger; use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraints80; use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraintsWithValidationGroups; @@ -414,6 +415,19 @@ public function entityWithObjectType() { } + /** + * @Route("/entity-with-uuid", methods={"GET", "POST"}) + * + * @OA\Get(operationId="entity-with-uuid") + * + * @OA\Response(response=200, description="success", @OA\JsonContent( + * ref=@Model(type=EntityWithUuid::class), + * )) + */ + public function entityWithUuid() + { + } + /** * @Route("/form-with-alternate-type", methods={"POST"}) * diff --git a/tests/Functional/Controller/ApiController81.php b/tests/Functional/Controller/ApiController81.php index 3b80bce89..18d74e11b 100644 --- a/tests/Functional/Controller/ApiController81.php +++ b/tests/Functional/Controller/ApiController81.php @@ -27,6 +27,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithNullableSchemaSet; use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType; use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid; use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\ArrayQueryModel; use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\FilterQueryModel; use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\PaginationQueryModel; @@ -345,6 +346,19 @@ public function entityWithObjectType() { } + #[Route('/entity-with-uuid', methods: ['GET', 'POST'])] + #[OA\Get(operationId: 'entity-with-uuid')] + #[OA\Response( + response: 200, + description: 'success', + content: new OA\JsonContent( + ref: new Model(type: EntityWithUuid::class), + ), + )] + public function entityWithUuid() + { + } + #[Route('/form-with-alternate-type', methods: ['POST'])] #[OA\Response( response: 204, diff --git a/tests/Functional/Entity/EntityWithUuid.php b/tests/Functional/Entity/EntityWithUuid.php new file mode 100644 index 000000000..3b643330e --- /dev/null +++ b/tests/Functional/Entity/EntityWithUuid.php @@ -0,0 +1,17 @@ +id = Uuid::v1(); + $this->name = $name; + } +} diff --git a/tests/Functional/FunctionalTest.php b/tests/Functional/FunctionalTest.php index 7bd63a605..a228ab724 100644 --- a/tests/Functional/FunctionalTest.php +++ b/tests/Functional/FunctionalTest.php @@ -17,6 +17,7 @@ use OpenApi\Annotations as OAAnnotations; use OpenApi\Attributes as OAAttributes; use OpenApi\Generator; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithUuid; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Serializer\Annotation\SerializedName; use const PHP_VERSION_ID; @@ -893,6 +894,17 @@ public function testEntitiesWithOverriddenSchemaTypeDoNotReadOtherProperties() $this->assertSame(Generator::UNDEFINED, $model->properties); } + public function testEntityWithUuid() + { + $model = $this->getModel('EntityWithUuid'); + + $this->assertSame('object', $model->type); + + $this->assertSame('id', $model->properties[0]->property); + $this->assertSame('string', $model->properties[0]->type); + $this->assertSame('uuid', $model->properties[0]->format); + } + public function testEntitiesWithRefInSchemaDoNoReadOtherProperties() { $model = $this->getModel('EntityWithRef'); diff --git a/tests/PropertyDescriber/UuidPropertyDescriberTest.php b/tests/PropertyDescriber/UuidPropertyDescriberTest.php new file mode 100644 index 000000000..a23e2da4c --- /dev/null +++ b/tests/PropertyDescriber/UuidPropertyDescriberTest.php @@ -0,0 +1,82 @@ +assertTrue($describer->supports([$type])); + } + + public function testSupportsNoIntPropertyType(): void + { + $type = new Type(Type::BUILTIN_TYPE_INT, false); + + $describer = new UuidPropertyDescriber(); + + $this->assertFalse($describer->supports([$type])); + } + + public function testSupportsNoDifferentObjectPropertyType(): void + { + $type = new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeInterface::class); + + $describer = new UuidPropertyDescriber(); + + $this->assertFalse($describer->supports([$type])); + } + + public function testDescribeUuidPropertyType(): void + { + $property = $this->initProperty(); + $schema = $this->initSchema(); + + $describer = new UuidPropertyDescriber(); + $describer->describe([], $property, [], $schema); + + $this->assertSame('string', $property->type); + $this->assertSame('uuid', $property->format); + } + + + private function initProperty(): \OpenApi\Annotations\Property + { + if (PHP_VERSION_ID < 80000) { + return new \OpenApi\Annotations\Property([]); + } + + return new \OpenApi\Attributes\Property(); // union types, used in schema attribute require PHP >= 8.0.0 + } + + private function initSchema(): \OpenApi\Annotations\Schema + { + if (PHP_VERSION_ID < 80000) { + return new \OpenApi\Annotations\Schema([]); + } + + return new \OpenApi\Attributes\Schema(); // union types, used in schema attribute require PHP >= 8.0.0 + } +}