|
| 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\Hal\JsonSchema; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Api\OperationType; |
| 17 | +use ApiPlatform\Core\Hal\JsonSchema\SchemaFactory; |
| 18 | +use ApiPlatform\Core\Hydra\JsonSchema\SchemaFactory as HydraSchemaFactory; |
| 19 | +use ApiPlatform\Core\JsonSchema\Schema; |
| 20 | +use ApiPlatform\Core\JsonSchema\SchemaFactory as BaseSchemaFactory; |
| 21 | +use ApiPlatform\Core\JsonSchema\TypeFactoryInterface; |
| 22 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 23 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 24 | +use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; |
| 25 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 26 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
| 27 | +use ApiPlatform\Core\Tests\ProphecyTrait; |
| 28 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | + |
| 31 | +/** |
| 32 | + * @group legacy |
| 33 | + */ |
| 34 | +class SchemaFactoryTest extends TestCase |
| 35 | +{ |
| 36 | + use ProphecyTrait; |
| 37 | + |
| 38 | + private $schemaFactory; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $typeFactory = $this->prophesize(TypeFactoryInterface::class); |
| 43 | + $resourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 44 | + $resourceMetadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(Dummy::class)); |
| 45 | + $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 46 | + $propertyNameCollectionFactory->create(Dummy::class, ['enable_getter_setter_extraction' => true])->willReturn(new PropertyNameCollection()); |
| 47 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 48 | + |
| 49 | + $baseSchemaFactory = new BaseSchemaFactory( |
| 50 | + $typeFactory->reveal(), |
| 51 | + $resourceMetadataFactory->reveal(), |
| 52 | + $propertyNameCollectionFactory->reveal(), |
| 53 | + $propertyMetadataFactory->reveal() |
| 54 | + ); |
| 55 | + |
| 56 | + $hydraSchemaFactory = new HydraSchemaFactory($baseSchemaFactory); |
| 57 | + |
| 58 | + $this->schemaFactory = new SchemaFactory($hydraSchemaFactory); |
| 59 | + } |
| 60 | + |
| 61 | + public function testBuildSchema(): void |
| 62 | + { |
| 63 | + $resultSchema = $this->schemaFactory->buildSchema(Dummy::class); |
| 64 | + |
| 65 | + $this->assertTrue($resultSchema->isDefined()); |
| 66 | + $this->assertEquals(str_replace('\\', '.', Dummy::class).'.jsonhal', $resultSchema->getRootDefinitionKey()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testCustomFormatBuildSchema(): void |
| 70 | + { |
| 71 | + $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'json'); |
| 72 | + |
| 73 | + $this->assertTrue($resultSchema->isDefined()); |
| 74 | + $this->assertEquals(str_replace('\\', '.', Dummy::class), $resultSchema->getRootDefinitionKey()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testHasRootDefinitionKeyBuildSchema(): void |
| 78 | + { |
| 79 | + $resultSchema = $this->schemaFactory->buildSchema(Dummy::class); |
| 80 | + $definitions = $resultSchema->getDefinitions(); |
| 81 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 82 | + |
| 83 | + $this->assertArrayHasKey($rootDefinitionKey, $definitions); |
| 84 | + $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]); |
| 85 | + $properties = $resultSchema['definitions'][$rootDefinitionKey]['properties']; |
| 86 | + $this->assertArrayHasKey('_links', $properties); |
| 87 | + $this->assertSame( |
| 88 | + [ |
| 89 | + 'type' => 'object', |
| 90 | + 'properties' => [ |
| 91 | + 'self' => [ |
| 92 | + 'type' => 'object', |
| 93 | + 'properties' => [ |
| 94 | + 'href' => [ |
| 95 | + 'type' => 'string', |
| 96 | + 'format' => 'iri-reference', |
| 97 | + ], |
| 98 | + ], |
| 99 | + ], |
| 100 | + ], |
| 101 | + ], |
| 102 | + $properties['_links'] |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function testSchemaTypeBuildSchema(): void |
| 107 | + { |
| 108 | + $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonhal', Schema::TYPE_OUTPUT, OperationType::COLLECTION); |
| 109 | + $definitionName = str_replace('\\', '.', Dummy::class).'.jsonhal'; |
| 110 | + |
| 111 | + $this->assertNull($resultSchema->getRootDefinitionKey()); |
| 112 | + $this->assertArrayHasKey('properties', $resultSchema); |
| 113 | + $this->assertArrayHasKey('_embedded', $resultSchema['properties']); |
| 114 | + $this->assertArrayHasKey('totalItems', $resultSchema['properties']); |
| 115 | + $this->assertArrayHasKey('itemsPerPage', $resultSchema['properties']); |
| 116 | + $this->assertArrayHasKey('_links', $resultSchema['properties']); |
| 117 | + $properties = $resultSchema['definitions'][$definitionName]['properties']; |
| 118 | + $this->assertArrayHasKey('_links', $properties); |
| 119 | + |
| 120 | + $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonhal', Schema::TYPE_OUTPUT, null, null, null, null, true); |
| 121 | + |
| 122 | + $this->assertNull($resultSchema->getRootDefinitionKey()); |
| 123 | + $this->assertArrayHasKey('properties', $resultSchema); |
| 124 | + $this->assertArrayHasKey('_embedded', $resultSchema['properties']); |
| 125 | + $this->assertArrayHasKey('totalItems', $resultSchema['properties']); |
| 126 | + $this->assertArrayHasKey('itemsPerPage', $resultSchema['properties']); |
| 127 | + $this->assertArrayHasKey('_links', $resultSchema['properties']); |
| 128 | + $properties = $resultSchema['definitions'][$definitionName]['properties']; |
| 129 | + $this->assertArrayHasKey('_links', $properties); |
| 130 | + } |
| 131 | +} |
0 commit comments