Skip to content

Commit 1bee909

Browse files
committed
Add test for Hydra json schema factory
1 parent e8f26ca commit 1bee909

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Hydra\JsonSchema;
15+
16+
use ApiPlatform\Core\Api\OperationType;
17+
use ApiPlatform\Core\Hydra\JsonSchema\SchemaFactory;
18+
use ApiPlatform\Core\JsonSchema\SchemaFactory as BaseSchemaFactory;
19+
use ApiPlatform\Core\JsonSchema\TypeFactoryInterface;
20+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22+
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
23+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
24+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
25+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
26+
use PHPUnit\Framework\TestCase;
27+
28+
class SchemaFactoryTest extends TestCase
29+
{
30+
private $schemaFactory;
31+
32+
protected function setUp(): void
33+
{
34+
$typeFactory = $this->prophesize(TypeFactoryInterface::class);
35+
$resourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
36+
$resourceMetadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(Dummy::class));
37+
$propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
38+
$propertyNameCollectionFactory->create(Dummy::class, [])->willReturn(new PropertyNameCollection());
39+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
40+
41+
$baseSchemaFactory = new BaseSchemaFactory(
42+
$typeFactory->reveal(),
43+
$resourceMetadataFactory->reveal(),
44+
$propertyNameCollectionFactory->reveal(),
45+
$propertyMetadataFactory->reveal()
46+
);
47+
48+
$this->schemaFactory = new SchemaFactory($baseSchemaFactory);
49+
}
50+
51+
public function testBuildSchema(): void
52+
{
53+
$resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
54+
55+
$this->assertTrue($resultSchema->isDefined());
56+
$this->assertEquals(Dummy::class.':jsonld', $resultSchema->getRootDefinitionKey());
57+
}
58+
59+
public function testCustomFormatBuildSchema(): void
60+
{
61+
$resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'json');
62+
63+
$this->assertTrue($resultSchema->isDefined());
64+
$this->assertEquals(Dummy::class, $resultSchema->getRootDefinitionKey());
65+
}
66+
67+
public function testHasRootDefinitionKeyBuildSchema(): void
68+
{
69+
$resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
70+
$definitions = $resultSchema->getDefinitions();
71+
$rootDefinitionKey = $resultSchema->getRootDefinitionKey();
72+
73+
$this->assertEquals(Dummy::class.':jsonld', $rootDefinitionKey);
74+
$this->assertArrayHasKey($rootDefinitionKey, $definitions);
75+
$this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
76+
}
77+
78+
public function testSchemaTypeBuildSchema(): void
79+
{
80+
$resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', true, OperationType::COLLECTION);
81+
82+
$this->assertNull($resultSchema->getRootDefinitionKey());
83+
$this->assertArrayHasKey('properties', $resultSchema);
84+
$this->assertArrayHasKey('hydra:member', $resultSchema['properties']);
85+
$this->assertArrayHasKey('hydra:totalItems', $resultSchema['properties']);
86+
$this->assertArrayHasKey('hydra:view', $resultSchema['properties']);
87+
$this->assertArrayHasKey('hydra:search', $resultSchema['properties']);
88+
89+
$resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', true, null, null, null, null, true);
90+
91+
$this->assertNull($resultSchema->getRootDefinitionKey());
92+
$this->assertArrayHasKey('properties', $resultSchema);
93+
$this->assertArrayHasKey('hydra:member', $resultSchema['properties']);
94+
$this->assertArrayHasKey('hydra:totalItems', $resultSchema['properties']);
95+
$this->assertArrayHasKey('hydra:view', $resultSchema['properties']);
96+
$this->assertArrayHasKey('hydra:search', $resultSchema['properties']);
97+
}
98+
}

0 commit comments

Comments
 (0)