Alex Kalineskou cc97b0ba45
Add support for php attributes (#1932)
* Add support for php attributes

* Fix tests for php 8.1

* Simplify the annotations

* Revert the changes to the tests

* CS

* Test FOSRest parsing of attributes

* CS

* typo

* CS

* Test fetchArticle php 8.1 attributes

* Fix namespaces

Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-12-21 16:16:14 +01:00

68 lines
2.3 KiB
PHP

<?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\ModelDescriber\Annotations;
use Doctrine\Common\Annotations\AnnotationReader;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\Annotations\OpenApiAnnotationsReader;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use PHPUnit\Framework\TestCase;
class AnnotationReaderTest extends TestCase
{
/**
* @param object $entity
* @dataProvider provideProperty
*/
public function testProperty($entity)
{
$schema = new OA\Schema([]);
$schema->merge([new OA\Property(['property' => 'property1'])]);
$schema->merge([new OA\Property(['property' => 'property2'])]);
$registry = new ModelRegistry([], new OA\OpenApi([]), []);
$symfonyConstraintAnnotationReader = new OpenApiAnnotationsReader(new AnnotationReader(), $registry, ['json']);
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property2'), $schema->properties[1]);
$this->assertEquals($schema->properties[0]->example, 1);
$this->assertEquals($schema->properties[0]->description, Generator::UNDEFINED);
$this->assertEquals($schema->properties[1]->example, 'some example');
$this->assertEquals($schema->properties[1]->description, 'some description');
}
public function provideProperty(): iterable
{
yield 'Annotations' => [new class() {
/**
* @OA\Property(example=1)
*/
private $property1;
/**
* @OA\Property(example="some example", description="some description")
*/
private $property2;
}];
if (\PHP_VERSION_ID >= 80100) {
yield 'Attributes' => [new class() {
#[OA\Property(example: 1)]
private $property1;
#[OA\Property(example: 'some example', description: 'some description')]
private $property2;
}];
}
}
}