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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 hits per line

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

0.0
/src/Hal/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\Hal\Tests\JsonSchema;
15

16
use ApiPlatform\Hal\JsonSchema\SchemaFactory;
17
use ApiPlatform\Hal\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 PHPUnit\Framework\TestCase;
31

32
class SchemaFactoryTest extends TestCase
33
{
34
    private SchemaFactory $schemaFactory;
35

36
    protected function setUp(): void
37
    {
38
        $resourceMetadataFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
×
39
        $resourceMetadataFactory->method('create')
×
40
            ->with(Dummy::class)
×
41
            ->willReturn(
×
42
                new ResourceMetadataCollection(Dummy::class, [
×
43
                    (new ApiResource())->withOperations(new Operations([
×
44
                        'get' => (new Get())->withName('get'),
×
45
                    ])),
×
46
                ])
×
47
            );
×
48

49
        $propertyNameCollectionFactory = $this->createMock(PropertyNameCollectionFactoryInterface::class);
×
50
        $propertyNameCollectionFactory->method('create')
×
51
            ->with(Dummy::class, ['enable_getter_setter_extraction' => true, 'schema_type' => Schema::TYPE_OUTPUT])
×
52
            ->willReturn(new PropertyNameCollection());
×
53

54
        $propertyMetadataFactory = $this->createMock(PropertyMetadataFactoryInterface::class);
×
55

56
        $definitionNameFactory = new DefinitionNameFactory();
×
57

58
        $baseSchemaFactory = new BaseSchemaFactory(
×
59
            resourceMetadataFactory: $resourceMetadataFactory,
×
60
            propertyNameCollectionFactory: $propertyNameCollectionFactory,
×
61
            propertyMetadataFactory: $propertyMetadataFactory,
×
62
            definitionNameFactory: $definitionNameFactory,
×
63
        );
×
64

65
        $this->schemaFactory = new SchemaFactory($baseSchemaFactory);
×
66
    }
67

68
    public function testBuildSchema(): void
69
    {
70
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
×
71

72
        $this->assertTrue($resultSchema->isDefined());
×
73
        $this->assertSame('Dummy.jsonhal', $resultSchema->getRootDefinitionKey());
×
74
    }
75

76
    public function testCustomFormatBuildSchema(): void
77
    {
78
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'json');
×
79

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

84
    public function testHasRootDefinitionKeyBuildSchema(): void
85
    {
86
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class);
×
87
        $definitions = $resultSchema->getDefinitions();
×
88
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
89

90
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
91
        $this->assertTrue(isset($definitions[$rootDefinitionKey]['allOf'][0]['properties']));
×
92
        $properties = $resultSchema['definitions'][$rootDefinitionKey]['allOf'][0]['properties'];
×
93
        $this->assertArrayHasKey('_links', $properties);
×
94
        $this->assertEquals(
×
95
            [
×
96
                'type' => 'object',
×
97
                'properties' => [
×
98
                    'self' => [
×
99
                        'type' => 'object',
×
100
                        'properties' => [
×
101
                            'href' => [
×
102
                                'type' => 'string',
×
103
                                'format' => 'iri-reference',
×
104
                            ],
×
105
                        ],
×
106
                    ],
×
107
                ],
×
108
            ],
×
109
            $properties['_links']
×
110
        );
×
111
    }
112

113
    public function testCollection(): void
114
    {
115
        $resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonhal', Schema::TYPE_OUTPUT, new GetCollection());
×
116
        $this->assertNull($resultSchema->getRootDefinitionKey());
×
117

118
        $this->assertTrue(isset($resultSchema['definitions']['Dummy.jsonhal']));
×
119
        $this->assertTrue(isset($resultSchema['definitions']['HalCollectionBaseSchema']));
×
120
        $this->assertTrue(isset($resultSchema['definitions']['Dummy.jsonhal']));
×
121

122
        foreach ($resultSchema['allOf'] as $schema) {
×
123
            if (isset($schema['$ref'])) {
×
124
                $this->assertEquals($schema['$ref'], '#/definitions/HalCollectionBaseSchema');
×
125
                continue;
×
126
            }
127

UNCOV
128
            $this->assertArrayHasKey('_embedded', $schema['properties']);
×
129
            $this->assertEquals('#/definitions/Dummy.jsonhal', $schema['properties']['_embedded']['additionalProperties']['items']['$ref']);
×
130
        }
131

132
        $forceCollectionSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonhal', Schema::TYPE_OUTPUT, null, null, null, true);
×
133
        $this->assertEquals($forceCollectionSchema, $resultSchema);
×
134
    }
135
}
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

© 2026 Coveralls, Inc