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 ;
2020-05-28 13:19:11 +02:00
use OpenApi\Annotations as OA ;
2017-06-26 10:34:42 +02:00
use PHPUnit\Framework\TestCase ;
2021-08-17 21:51:11 +02:00
use Psr\Log\LoggerInterface ;
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
{
2018-06-10 09:56:38 +02:00
public function testNameAliasingNotAppliedForCollections ()
{
$alternativeNames = [
'Foo1' => [
'type' => self :: class ,
'groups' => [ 'group1' ],
],
];
2020-05-28 13:19:11 +02:00
$registry = new ModelRegistry ([], new OA\OpenApi ([]), $alternativeNames );
2018-06-10 09:56:38 +02:00
$type = new Type ( Type :: BUILTIN_TYPE_ARRAY , false , null , true );
2020-05-28 13:19:11 +02:00
$this -> assertEquals ( '#/components/schemas/array' , $registry -> register ( new Model ( $type , [ 'group1' ])));
2018-06-10 09:56:38 +02:00
}
2021-11-30 13:06:32 +01:00
/**
* @ dataProvider provideNameCollisionsTypes
*/
public function testNameCollisionsAreLogged ( Type $type , array $arrayType )
2021-08-17 21:51:11 +02:00
{
$logger = $this -> createMock ( LoggerInterface :: class );
$logger
-> expects ( self :: once ())
-> method ( 'info' )
-> with (
'Can not assign a name for the model, the name "ModelRegistryTest" has already been taken.' , [
'model' => [
2021-11-30 13:06:32 +01:00
'type' => $arrayType ,
2021-08-17 21:51:11 +02:00
'options' => null ,
'groups' => [ 'group2' ],
],
'taken_by' => [
2021-11-30 13:06:32 +01:00
'type' => $arrayType ,
2021-08-17 21:51:11 +02:00
'options' => null ,
'groups' => [ 'group1' ],
],
]);
$registry = new ModelRegistry ([], new OA\OpenApi ([]), []);
$registry -> setLogger ( $logger );
$registry -> register ( new Model ( $type , [ 'group1' ]));
$registry -> register ( new Model ( $type , [ 'group2' ]));
}
2021-11-30 13:06:32 +01:00
public function provideNameCollisionsTypes ()
{
yield [
new Type ( Type :: BUILTIN_TYPE_OBJECT , false , self :: class ),
[
'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest' ,
'built_in_type' => 'object' ,
'nullable' => false ,
'collection' => false ,
'collection_key_types' => null ,
'collection_value_types' => null ,
],
];
yield [
new Type ( Type :: BUILTIN_TYPE_OBJECT , false , self :: class , true , new Type ( Type :: BUILTIN_TYPE_OBJECT )),
[
'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest' ,
'built_in_type' => 'object' ,
'nullable' => false ,
'collection' => true ,
'collection_key_types' => [
[
'class' => null ,
'built_in_type' => 'object' ,
'nullable' => false ,
'collection' => false ,
'collection_key_types' => null ,
'collection_value_types' => null ,
],
],
'collection_value_types' => [],
],
];
}
2021-08-17 21:51:11 +02:00
public function testNameCollisionsAreLoggedWithAlternativeNames ()
{
$ref = new \ReflectionClass ( self :: class );
$alternativeNames = [
$ref -> getShortName () => [
'type' => $ref -> getName (),
'groups' => [ 'group1' ],
],
];
$logger = $this -> createMock ( LoggerInterface :: class );
$logger
-> expects ( self :: once ())
-> method ( 'info' )
-> with (
'Can not assign a name for the model, the name "ModelRegistryTest" has already been taken.' , [
'model' => [
'type' => [
'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest' ,
'built_in_type' => 'object' ,
'nullable' => false ,
'collection' => false ,
'collection_key_types' => null ,
'collection_value_types' => null ,
],
'options' => null ,
'groups' => [ 'group2' ],
],
'taken_by' => [
'type' => [
'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest' ,
'built_in_type' => 'object' ,
'nullable' => false ,
'collection' => false ,
'collection_key_types' => null ,
'collection_value_types' => null ,
],
'options' => null ,
'groups' => [ 'group1' ],
],
]);
$registry = new ModelRegistry ([], new OA\OpenApi ([]), $alternativeNames );
$registry -> setLogger ( $logger );
$type = new Type ( Type :: BUILTIN_TYPE_OBJECT , false , self :: class );
$registry -> register ( new Model ( $type , [ 'group2' ]));
}
2018-06-10 09:56:38 +02:00
/**
* @ dataProvider getNameAlternatives
*
* @ param $expected
*/
public function testNameAliasingForObjects ( string $expected , $groups , array $alternativeNames )
{
2020-05-28 13:19:11 +02:00
$registry = new ModelRegistry ([], new OA\OpenApi ([]), $alternativeNames );
2018-06-10 09:56:38 +02:00
$type = new Type ( Type :: BUILTIN_TYPE_OBJECT , false , self :: class );
$this -> assertEquals ( $expected , $registry -> register ( new Model ( $type , $groups )));
}
public function getNameAlternatives ()
{
return [
[
2020-05-28 13:19:11 +02:00
'#/components/schemas/ModelRegistryTest' ,
2018-06-10 09:56:38 +02:00
null ,
[
'Foo1' => [
'type' => self :: class ,
'groups' => [ 'group1' ],
],
],
],
[
2020-05-28 13:19:11 +02:00
'#/components/schemas/Foo1' ,
2018-06-10 09:56:38 +02:00
[ 'group1' ],
[
'Foo1' => [
'type' => self :: class ,
'groups' => [ 'group1' ],
],
],
],
[
2020-05-28 13:19:11 +02:00
'#/components/schemas/Foo1' ,
2018-06-10 09:56:38 +02:00
[ 'group1' , 'group2' ],
[
'Foo1' => [
'type' => self :: class ,
'groups' => [ 'group1' , 'group2' ],
],
],
],
[
2020-05-28 13:19:11 +02:00
'#/components/schemas/ModelRegistryTest' ,
2018-06-10 09:56:38 +02:00
null ,
[
'Foo1' => [
'type' => self :: class ,
'groups' => [],
],
],
],
2018-08-26 22:15:44 +02:00
[
2020-05-28 13:19:11 +02:00
'#/components/schemas/Foo1' ,
2018-08-26 22:15:44 +02:00
[],
[
'Foo1' => [
'type' => self :: class ,
'groups' => [],
],
],
],
2018-06-10 09:56:38 +02:00
];
}
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
2020-05-28 13:19:11 +02:00
$registry = new ModelRegistry ([], new OA\OpenApi ([]));
2017-01-14 17:36:56 +01:00
$registry -> register ( new Model ( $type ));
2020-05-28 13:19:11 +02:00
$registry -> registerSchemas ();
2017-01-14 17:36:56 +01:00
}
public function unsupportedTypesProvider ()
{
return [
[ new Type ( Type :: BUILTIN_TYPE_ARRAY , false , null , true ), 'mixed[]' ],
2022-04-13 19:54:31 +02:00
[ new Type ( Type :: BUILTIN_TYPE_OBJECT , false , self :: class ), '\\' . self :: class ],
2017-01-14 17:36:56 +01:00
];
}
2022-04-13 19:54:31 +02:00
public function testUnsupportedTypeExceptionWithNonExistentClass ()
{
$className = DoesNotExist :: class ;
$type = new Type ( Type :: BUILTIN_TYPE_OBJECT , false , $className );
$this -> expectException ( '\LogicException' );
$this -> expectExceptionMessage ( sprintf ( 'Schema of type "\%s" can\'t be generated, no describer supports it. Class "\Nelmio\ApiDocBundle\Tests\Model\DoesNotExist" does not exist, did you forget a use statement, or typed it wrong?' , $className ));
$registry = new ModelRegistry ([], new OA\OpenApi ([]));
$registry -> register ( new Model ( $type ));
$registry -> registerSchemas ();
}
2017-01-14 17:36:56 +01:00
}