• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15069419398

16 May 2025 01:19PM UTC coverage: 21.832%. First build
15069419398

Pull #6960

github

web-flow
Merge 88ff9fb4b into b6080d419
Pull Request #6960: feat(json-schema): mutualize json schema between formats

0 of 470 new or added lines in 25 files covered. (0.0%)

11116 of 50915 relevant lines covered (21.83%)

29.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Hydra/Tests/JsonSchema/SchemaFactoryTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Hydra\Tests\JsonSchema;
15

16
use ApiPlatform\Hydra\JsonSchema\SchemaFactory;
17
use ApiPlatform\Hydra\Tests\Fixtures\Dummy;
18
use ApiPlatform\JsonLd\ContextBuilder;
19
use ApiPlatform\JsonSchema\DefinitionNameFactory;
20
use ApiPlatform\JsonSchema\Schema;
21
use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory;
22
use ApiPlatform\Metadata\ApiProperty;
23
use ApiPlatform\Metadata\ApiResource;
24
use ApiPlatform\Metadata\Get;
25
use ApiPlatform\Metadata\GetCollection;
26
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
27
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
28
use ApiPlatform\Metadata\Property\PropertyNameCollection;
29
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
30
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
31
use PHPUnit\Framework\TestCase;
32
use Prophecy\Argument;
33
use Prophecy\PhpUnit\ProphecyTrait;
34

35
class SchemaFactoryTest extends TestCase
36
{
37
    use ProphecyTrait;
38

39
    private SchemaFactory $schemaFactory;
40

41
    protected function setUp(): void
42
    {
43
        $resourceMetadataFactoryCollection = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
44
        $resourceMetadataFactoryCollection->create(Dummy::class)->willReturn(
×
45
            new ResourceMetadataCollection(Dummy::class, [
×
46
                new ApiResource(operations: [
×
47
                    'get' => new Get(name: 'get'),
×
48
                ]),
×
49
            ])
×
50
        );
×
51

52
        $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
NEW
53
        $propertyNameCollectionFactory->create(Dummy::class, ['enable_getter_setter_extraction' => true, 'schema_type' => Schema::TYPE_OUTPUT])->willReturn(new PropertyNameCollection(['id', 'name']));
×
54
        $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
NEW
55
        $propertyMetadataFactory->create(Dummy::class, 'id', Argument::type('array'))->willReturn(new ApiProperty(identifier: true));
×
NEW
56
        $propertyMetadataFactory->create(Dummy::class, 'name', Argument::type('array'))->willReturn(new ApiProperty());
×
57

NEW
58
        $definitionNameFactory = new DefinitionNameFactory();
×
59

60
        $baseSchemaFactory = new BaseSchemaFactory(
×
61
            resourceMetadataFactory: $resourceMetadataFactoryCollection->reveal(),
×
62
            propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(),
×
63
            propertyMetadataFactory: $propertyMetadataFactory->reveal(),
×
64
            definitionNameFactory: $definitionNameFactory,
×
65
        );
×
66

NEW
67
        $this->schemaFactory = new SchemaFactory(
×
NEW
68
            $baseSchemaFactory,
×
NEW
69
            [],
×
NEW
70
            $definitionNameFactory,
×
NEW
71
            $resourceMetadataFactoryCollection->reveal(),
×
NEW
72
        );
×
73
    }
74

75
    public function testBuildSchema(): void
76
    {
77
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
×
78

79
        $this->assertTrue($resultSchema->isDefined());
×
80
        $this->assertSame('Dummy.jsonld', $resultSchema->getRootDefinitionKey());
×
81
    }
82

83
    public function testCustomFormatBuildSchema(): void
84
    {
85
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'json');
×
86

87
        $this->assertTrue($resultSchema->isDefined());
×
88
        $this->assertSame('Dummy', $resultSchema->getRootDefinitionKey());
×
89
    }
90

91
    public function testHasRootDefinitionKeyBuildSchema(): void
92
    {
93
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
×
94
        $definitions = $resultSchema->getDefinitions();
×
95
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
96

97
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
NEW
98
        $this->assertTrue(isset($definitions[$rootDefinitionKey]['allOf'][1]['properties']));
×
NEW
99
        $this->assertEquals($definitions[$rootDefinitionKey]['allOf'][0], ['$ref' => '#/definitions/HydraItemBaseSchema']);
×
100

NEW
101
        $properties = $definitions['HydraItemBaseSchema']['properties'];
×
102
        $this->assertArrayHasKey('@context', $properties);
×
103
        $this->assertEquals(
×
104
            [
×
105
                'oneOf' => [
×
106
                    ['type' => 'string'],
×
107
                    [
×
108
                        'type' => 'object',
×
109
                        'properties' => [
×
110
                            '@vocab' => [
×
111
                                'type' => 'string',
×
112
                            ],
×
113
                            'hydra' => [
×
114
                                'type' => 'string',
×
115
                                'enum' => [ContextBuilder::HYDRA_NS],
×
116
                            ],
×
117
                        ],
×
118
                        'required' => ['@vocab', 'hydra'],
×
119
                        'additionalProperties' => true,
×
120
                    ],
×
121
                ],
×
122
            ],
×
123
            $properties['@context']
×
124
        );
×
125
        $this->assertArrayHasKey('@type', $properties);
×
126
        $this->assertArrayHasKey('@id', $properties);
×
127
    }
128

129
    public function testSchemaTypeBuildSchema(): void
130
    {
131
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', Schema::TYPE_OUTPUT, new GetCollection());
×
132
        $this->assertNull($resultSchema->getRootDefinitionKey());
×
NEW
133
        $hydraCollectionSchema = $resultSchema['definitions']['HydraCollectionBaseSchema'];
×
NEW
134
        $properties = $hydraCollectionSchema['properties'];
×
NEW
135
        $this->assertTrue(isset($properties['hydra:member']));
×
NEW
136
        $this->assertArrayHasKey('hydra:totalItems', $properties);
×
NEW
137
        $this->assertArrayHasKey('hydra:view', $properties);
×
NEW
138
        $this->assertArrayHasKey('hydra:search', $properties);
×
139
        $this->assertArrayNotHasKey('@context', $properties);
×
140

NEW
141
        $this->assertTrue(isset($properties['hydra:view']));
×
NEW
142
        $this->assertArrayHasKey('properties', $properties['hydra:view']);
×
NEW
143
        $this->assertArrayHasKey('hydra:first', $properties['hydra:view']['properties']);
×
NEW
144
        $this->assertArrayHasKey('hydra:last', $properties['hydra:view']['properties']);
×
NEW
145
        $this->assertArrayHasKey('hydra:previous', $properties['hydra:view']['properties']);
×
NEW
146
        $this->assertArrayHasKey('hydra:next', $properties['hydra:view']['properties']);
×
147

NEW
148
        $forcedCollection = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', Schema::TYPE_OUTPUT, null, null, null, true);
×
NEW
149
        $this->assertEquals($resultSchema['allOf'][0]['$ref'], $forcedCollection['allOf'][0]['$ref']);
×
150
    }
151

152
    public function testSchemaTypeBuildSchemaWithoutPrefix(): void
153
    {
154
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', Schema::TYPE_OUTPUT, new GetCollection(), null, [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false]);
×
155
        $this->assertNull($resultSchema->getRootDefinitionKey());
×
NEW
156
        $hydraCollectionSchema = $resultSchema['definitions']['HydraCollectionBaseSchema'];
×
NEW
157
        $properties = $hydraCollectionSchema['properties'];
×
NEW
158
        $this->assertArrayHasKey('totalItems', $properties);
×
NEW
159
        $this->assertArrayHasKey('view', $properties);
×
NEW
160
        $this->assertArrayHasKey('search', $properties);
×
161
    }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc