|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Tests\JsonSchema; |
| 15 | + |
| 16 | +use ApiPlatform\Core\JsonSchema\Schema; |
| 17 | +use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface; |
| 18 | +use ApiPlatform\Core\JsonSchema\TypeFactory; |
| 19 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Prophecy\Argument; |
| 22 | +use Symfony\Component\PropertyInfo\Type; |
| 23 | + |
| 24 | +class TypeFactoryTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @dataProvider typeProvider |
| 28 | + */ |
| 29 | + public function testGetType(array $schema, Type $type): void |
| 30 | + { |
| 31 | + $typeFactory = new TypeFactory(); |
| 32 | + $this->assertSame($schema, $typeFactory->getType($type)); |
| 33 | + } |
| 34 | + |
| 35 | + public function typeProvider(): iterable |
| 36 | + { |
| 37 | + yield [['type' => 'integer'], new Type(Type::BUILTIN_TYPE_INT)]; |
| 38 | + yield [['type' => 'number'], new Type(Type::BUILTIN_TYPE_FLOAT)]; |
| 39 | + yield [['type' => 'boolean'], new Type(Type::BUILTIN_TYPE_BOOL)]; |
| 40 | + yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT)]; |
| 41 | + yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)]; |
| 42 | + yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]; |
| 43 | + yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)]; |
| 44 | + } |
| 45 | + |
| 46 | + public function testGetClassType(): void |
| 47 | + { |
| 48 | + $schemaFactoryProphecy = $this->prophesize(SchemaFactoryInterface::class); |
| 49 | + |
| 50 | + $schemaFactoryProphecy->buildSchema(Dummy::class, 'jsonld', true, null, null, Argument::type(Schema::class), ['foo' => 'bar'])->will(function (array $args) { |
| 51 | + $args[5]['$ref'] = 'ref'; |
| 52 | + |
| 53 | + return $args[5]; |
| 54 | + }); |
| 55 | + |
| 56 | + $typeFactory = new TypeFactory(); |
| 57 | + $typeFactory->setSchemaFactory($schemaFactoryProphecy->reveal()); |
| 58 | + |
| 59 | + $this->assertSame(['$ref' => 'ref'], $typeFactory->getType(new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class), 'jsonld', true, ['foo' => 'bar'], new Schema())); |
| 60 | + } |
| 61 | +} |
0 commit comments