• 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/JsonApi/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\JsonApi\Tests\JsonSchema;
15

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

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

38
    private SchemaFactory $schemaFactory;
39

40
    protected function setUp(): void
41
    {
42
        $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
43
        $resourceMetadataFactory->create(Dummy::class)->willReturn(
×
44
            new ResourceMetadataCollection(Dummy::class, [
×
45
                (new ApiResource())->withOperations(new Operations([
×
46
                    'get' => (new Get())->withName('get'),
×
47
                ])),
×
NEW
48
            ])
×
NEW
49
        );
×
50
        $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
51
        $propertyNameCollectionFactory->create(Dummy::class, ['enable_getter_setter_extraction' => true, 'schema_type' => Schema::TYPE_OUTPUT])->willReturn(new PropertyNameCollection());
×
52
        $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
53

NEW
54
        $definitionNameFactory = new DefinitionNameFactory();
×
55

56
        $baseSchemaFactory = new BaseSchemaFactory(
×
57
            resourceMetadataFactory: $resourceMetadataFactory->reveal(),
×
58
            propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(),
×
59
            propertyMetadataFactory: $propertyMetadataFactory->reveal(),
×
60
            definitionNameFactory: $definitionNameFactory,
×
61
        );
×
62

63
        $resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class);
×
NEW
64
        $resourceClassResolver->isResourceClass(Dummy::class)->willReturn(true);
×
65

66
        $this->schemaFactory = new SchemaFactory(
×
67
            schemaFactory: $baseSchemaFactory,
×
68
            propertyMetadataFactory: $propertyMetadataFactory->reveal(),
×
69
            resourceClassResolver: $resourceClassResolver->reveal(),
×
70
            resourceMetadataFactory: $resourceMetadataFactory->reveal(),
×
71
            definitionNameFactory: $definitionNameFactory,
×
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.jsonapi', $resultSchema->getRootDefinitionKey());
×
81
    }
82

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

87
        $this->assertTrue($resultSchema->isDefined());
×
88
        $this->assertSame('Dummy.jsonapi', $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]));
×
98
        $this->assertTrue(isset($definitions[$rootDefinitionKey]['properties']));
×
99
        $properties = $resultSchema['definitions'][$rootDefinitionKey]['properties'];
×
100
        $this->assertArrayHasKey('data', $properties);
×
101
        $this->assertEquals(
×
102
            [
×
103
                'type' => 'object',
×
104
                'properties' => [
×
105
                    'id' => [
×
106
                        'type' => 'string',
×
107
                    ],
×
108
                    'type' => [
×
109
                        'type' => 'string',
×
110
                    ],
×
111
                    'attributes' => [
×
NEW
112
                        '$ref' => '#/definitions/Dummy',
×
113
                    ],
×
114
                ],
×
115
                'required' => [
×
116
                    'type',
×
117
                    'id',
×
118
                ],
×
119
            ],
×
120
            $properties['data']
×
121
        );
×
122
    }
123

124
    public function testSchemaTypeBuildSchema(): void
125
    {
126
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonapi', Schema::TYPE_OUTPUT, new GetCollection());
×
127

128
        $this->assertNull($resultSchema->getRootDefinitionKey());
×
NEW
129
        $this->assertTrue(isset($resultSchema['allOf'][0]['$ref']));
×
NEW
130
        $this->assertEquals($resultSchema['allOf'][0]['$ref'], '#/definitions/JsonApiCollectionBaseSchema');
×
131

NEW
132
        $jsonApiCollectionBaseSchema = $resultSchema['definitions']['JsonApiCollectionBaseSchema'];
×
NEW
133
        $this->assertTrue(isset($jsonApiCollectionBaseSchema['properties']));
×
NEW
134
        $this->assertArrayHasKey('links', $jsonApiCollectionBaseSchema['properties']);
×
NEW
135
        $this->assertArrayHasKey('self', $jsonApiCollectionBaseSchema['properties']['links']['properties']);
×
NEW
136
        $this->assertArrayHasKey('first', $jsonApiCollectionBaseSchema['properties']['links']['properties']);
×
NEW
137
        $this->assertArrayHasKey('prev', $jsonApiCollectionBaseSchema['properties']['links']['properties']);
×
NEW
138
        $this->assertArrayHasKey('next', $jsonApiCollectionBaseSchema['properties']['links']['properties']);
×
NEW
139
        $this->assertArrayHasKey('last', $jsonApiCollectionBaseSchema['properties']['links']['properties']);
×
140

NEW
141
        $this->assertArrayHasKey('meta', $jsonApiCollectionBaseSchema['properties']);
×
NEW
142
        $this->assertArrayHasKey('totalItems', $jsonApiCollectionBaseSchema['properties']['meta']['properties']);
×
NEW
143
        $this->assertArrayHasKey('itemsPerPage', $jsonApiCollectionBaseSchema['properties']['meta']['properties']);
×
NEW
144
        $this->assertArrayHasKey('currentPage', $jsonApiCollectionBaseSchema['properties']['meta']['properties']);
×
145

NEW
146
        $objectSchema = $resultSchema['allOf'][1];
×
NEW
147
        $this->assertArrayHasKey('data', $objectSchema['properties']);
×
148

NEW
149
        $this->assertArrayHasKey('items', $objectSchema['properties']['data']);
×
NEW
150
        $this->assertArrayHasKey('$ref', $objectSchema['properties']['data']['items']['properties']['attributes']);
×
151

NEW
152
        $properties = $objectSchema['properties'];
×
153
        $this->assertArrayHasKey('data', $properties);
×
NEW
154
        $this->assertArrayHasKey('items', $properties['data']);
×
NEW
155
        $this->assertArrayHasKey('id', $properties['data']['items']['properties']);
×
NEW
156
        $this->assertArrayHasKey('type', $properties['data']['items']['properties']);
×
NEW
157
        $this->assertArrayHasKey('attributes', $properties['data']['items']['properties']);
×
158

NEW
159
        $forcedCollection = $this->schemaFactory->buildSchema(Dummy::class, 'jsonapi', Schema::TYPE_OUTPUT, forceCollection: true);
×
NEW
160
        $this->assertEquals($resultSchema['allOf'][0]['$ref'], $forcedCollection['allOf'][0]['$ref']);
×
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