Skip to content

Commit

Permalink
Added tests for UuidPropertyDescriber
Browse files Browse the repository at this point in the history
  • Loading branch information
stollr committed Apr 2, 2024
1 parent 72830ba commit e5f22fb
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions tests/Functional/Controller/ApiController80.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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"})
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Functional/Controller/ApiController81.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/Functional/Entity/EntityWithUuid.php
@@ -0,0 +1,17 @@
<?php

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;

use Symfony\Component\Uid\Uuid;

class EntityWithUuid
{
public ?Uuid $id;
private string $name;

public function __construct(string $name)
{
$this->id = Uuid::v1();
$this->name = $name;
}
}
12 changes: 12 additions & 0 deletions tests/Functional/FunctionalTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
82 changes: 82 additions & 0 deletions tests/PropertyDescriber/UuidPropertyDescriberTest.php
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle\Tests\PropertyDescriber;

use Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\Uuid;

/**
* @covers \Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber::supports
* @covers \Nelmio\ApiDocBundle\PropertyDescriber\UuidPropertyDescriber::describe
*/
class UuidPropertyDescriberTest extends TestCase
{
public function testSupportsUuidPropertyType(): void
{
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, Uuid::class);

$describer = new UuidPropertyDescriber();

$this->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
}
}

0 comments on commit e5f22fb

Please sign in to comment.