NelmioApiDocBundle/Tests/Model/ModelRegistryTest.php

126 lines
3.6 KiB
PHP
Raw Normal View History

2017-01-14 17:36:56 +01:00
<?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\Model;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use OpenApi\Annotations as OA;
use PHPUnit\Framework\TestCase;
2017-01-14 17:36:56 +01:00
use Symfony\Component\PropertyInfo\Type;
2017-05-31 19:36:17 +02:00
class ModelRegistryTest extends TestCase
2017-01-14 17:36:56 +01:00
{
public function testNameAliasingNotAppliedForCollections()
{
$alternativeNames = [
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
],
];
$registry = new ModelRegistry([], new OA\OpenApi([]), $alternativeNames);
$type = new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true);
$this->assertEquals('#/components/schemas/array', $registry->register(new Model($type, ['group1'])));
}
/**
* @dataProvider getNameAlternatives
*
* @param $expected
*/
public function testNameAliasingForObjects(string $expected, $groups, array $alternativeNames)
{
$registry = new ModelRegistry([], new OA\OpenApi([]), $alternativeNames);
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class);
$this->assertEquals($expected, $registry->register(new Model($type, $groups)));
}
public function getNameAlternatives()
{
return [
[
'#/components/schemas/ModelRegistryTest',
null,
[
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
],
],
],
[
'#/components/schemas/Foo1',
['group1'],
[
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
],
],
],
[
'#/components/schemas/Foo1',
['group1', 'group2'],
[
'Foo1' => [
'type' => self::class,
'groups' => ['group1', 'group2'],
],
],
],
[
'#/components/schemas/ModelRegistryTest',
null,
[
'Foo1' => [
'type' => self::class,
'groups' => [],
],
],
],
[
'#/components/schemas/Foo1',
[],
[
'Foo1' => [
'type' => self::class,
'groups' => [],
],
],
],
];
}
2017-01-14 17:36:56 +01:00
/**
* @dataProvider unsupportedTypesProvider
*/
public function testUnsupportedTypeException(Type $type, string $stringType)
{
2017-05-31 19:36:17 +02:00
$this->expectException('\LogicException');
$this->expectExceptionMessage(sprintf('Schema of type "%s" can\'t be generated, no describer supports it.', $stringType));
2017-01-14 17:36:56 +01:00
$registry = new ModelRegistry([], new OA\OpenApi([]));
2017-01-14 17:36:56 +01:00
$registry->register(new Model($type));
$registry->registerSchemas();
2017-01-14 17:36:56 +01:00
}
public function unsupportedTypesProvider()
{
return [
[new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true), 'mixed[]'],
[new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class), self::class],
];
}
}