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

82 lines
2.7 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\Functional;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
class FOSRestTest extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
static::createClient([], ['HTTP_HOST' => 'api.example.com']);
}
/**
* @dataProvider provideRoute
*/
public function testFOSRestAction(string $route)
{
$operation = $this->getOperation($route, 'post');
$this->assertHasParameter('foo', 'query', $operation);
$this->assertInstanceOf(OA\RequestBody::class, $operation->requestBody);
$bodySchema = $operation->requestBody->content['application/json']->schema;
$this->assertHasProperty('bar', $bodySchema);
$this->assertHasProperty('baz', $bodySchema);
$fooParameter = $this->getParameter($operation, 'foo', 'query');
$this->assertInstanceOf(OA\Schema::class, $fooParameter->schema);
$this->assertEquals('\d+', $fooParameter->schema->pattern);
$this->assertEquals(Generator::UNDEFINED, $fooParameter->schema->format);
$mappedParameter = $this->getParameter($operation, 'mapped[]', 'query');
$this->assertTrue($mappedParameter->explode);
$barProperty = $this->getProperty($bodySchema, 'bar');
$this->assertEquals('\d+', $barProperty->pattern);
$this->assertEquals(Generator::UNDEFINED, $barProperty->format);
$bazProperty = $this->getProperty($bodySchema, 'baz');
$this->assertEquals(Generator::UNDEFINED, $bazProperty->pattern);
$this->assertEquals('IsTrue', $bazProperty->format);
$dateTimeProperty = $this->getProperty($bodySchema, 'datetime');
$this->assertEquals('date-time', $dateTimeProperty->format);
$dateTimeAltProperty = $this->getProperty($bodySchema, 'datetimeAlt');
$this->assertEquals('date-time', $dateTimeAltProperty->format);
$dateTimeNoFormatProperty = $this->getProperty($bodySchema, 'datetimeNoFormat');
$this->assertEquals(Generator::UNDEFINED, $dateTimeNoFormatProperty->format);
$dateProperty = $this->getProperty($bodySchema, 'date');
$this->assertEquals('date', $dateProperty->format);
// The _format path attribute should be removed
$this->assertNotHasParameter('_format', 'path', $operation);
}
public function provideRoute(): iterable
{
yield 'Annotations' => ['/api/fosrest'];
if (\PHP_VERSION_ID >= 80100) {
yield 'Attributes' => ['/api/fosrest_attributes'];
}
}
}