• 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/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.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\JsonSchema\Tests\Metadata\Property\Factory;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithCustomOpenApiContext;
18
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum;
19
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithUnionTypeProperty;
20
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
21
use ApiPlatform\Metadata\ApiProperty;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\ResourceClassResolverInterface;
24
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
25
use PHPUnit\Framework\TestCase;
26
use Symfony\Component\PropertyInfo\Type as LegacyType;
27
use Symfony\Component\TypeInfo\Type;
28

29
class SchemaPropertyMetadataFactoryTest extends TestCase
30
{
31
    #[IgnoreDeprecations]
32
    public function testEnumLegacy(): void
33
    {
34
        $this->expectUserDeprecationMessage('Since api_platform/metadata 4.2: The "builtinTypes" argument of "ApiPlatform\Metadata\ApiProperty" is deprecated, use "nativeType" instead.');
×
35
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
36
        $apiProperty = new ApiProperty(builtinTypes: [new LegacyType(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)]);
×
UNCOV
37
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
38
        $decorated->expects($this->once())->method('create')->with(DummyWithEnum::class, 'intEnumAsIdentifier')->willReturn($apiProperty);
×
UNCOV
39
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
40
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
×
41
        $this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema());
×
42
    }
43

44
    public function testEnum(): void
45
    {
46
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
47
        $apiProperty = new ApiProperty(nativeType: Type::nullable(Type::enum(IntEnumAsIdentifier::class)));
×
48
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
49
        $decorated->expects($this->once())->method('create')->with(DummyWithEnum::class, 'intEnumAsIdentifier')->willReturn($apiProperty);
×
50
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
51
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
×
UNCOV
52
        $this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema());
×
53
    }
54

55
    #[IgnoreDeprecations]
56
    public function testWithCustomOpenApiContextLegacy(): void
57
    {
58
        $this->expectUserDeprecationMessage('Since api_platform/metadata 4.2: The "builtinTypes" argument of "ApiPlatform\Metadata\ApiProperty" is deprecated, use "nativeType" instead.');
×
59
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
60
        $apiProperty = new ApiProperty(
×
61
            builtinTypes: [new LegacyType(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)],
×
62
            openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]],
×
63
        );
×
64
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
65
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'acme')->willReturn($apiProperty);
×
66
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
67
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
×
68
        $this->assertEquals([], $apiProperty->getSchema());
×
69
    }
70

71
    public function testWithCustomOpenApiContext(): void
72
    {
73
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
74
        $apiProperty = new ApiProperty(
×
75
            nativeType: Type::nullable(Type::enum(IntEnumAsIdentifier::class)),
×
76
            openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]],
×
77
        );
×
78
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
79
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'acme')->willReturn($apiProperty);
×
UNCOV
80
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
81
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
×
UNCOV
82
        $this->assertEquals([], $apiProperty->getSchema());
×
83
    }
84

85
    #[IgnoreDeprecations]
86
    public function testWithCustomOpenApiContextWithoutTypeDefinitionLegacy(): void
87
    {
UNCOV
88
        $this->expectUserDeprecationMessage('Since api_platform/metadata 4.2: The "builtinTypes" argument of "ApiPlatform\Metadata\ApiProperty" is deprecated, use "nativeType" instead.');
×
UNCOV
89
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
UNCOV
90
        $apiProperty = new ApiProperty(
×
UNCOV
91
            openapiContext: ['description' => 'My description'],
×
UNCOV
92
            builtinTypes: [new LegacyType(builtinType: 'bool')],
×
UNCOV
93
        );
×
UNCOV
94
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
95
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'foo')->willReturn($apiProperty);
×
UNCOV
96
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
97
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'foo');
×
UNCOV
98
        $this->assertEquals([
×
UNCOV
99
            'type' => 'boolean',
×
UNCOV
100
        ], $apiProperty->getSchema());
×
101

UNCOV
102
        $apiProperty = new ApiProperty(
×
UNCOV
103
            openapiContext: ['iris' => 'https://schema.org/Date'],
×
UNCOV
104
            builtinTypes: [new LegacyType(builtinType: 'object', class: \DateTimeImmutable::class)],
×
UNCOV
105
        );
×
UNCOV
106
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
107
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'bar')->willReturn($apiProperty);
×
UNCOV
108
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
109
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'bar');
×
UNCOV
110
        $this->assertEquals([
×
UNCOV
111
            'type' => 'string',
×
UNCOV
112
            'format' => 'date-time',
×
UNCOV
113
        ], $apiProperty->getSchema());
×
114
    }
115

116
    public function testWithCustomOpenApiContextWithoutTypeDefinition(): void
117
    {
UNCOV
118
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
UNCOV
119
        $apiProperty =
×
UNCOV
120
            new ApiProperty(
×
UNCOV
121
                openapiContext: ['description' => 'My description'],
×
UNCOV
122
                nativeType: Type::bool(),
×
UNCOV
123
            );
×
UNCOV
124
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
125
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'foo')->willReturn($apiProperty);
×
UNCOV
126
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
127
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'foo');
×
UNCOV
128
        $this->assertEquals([
×
UNCOV
129
            'type' => 'boolean',
×
UNCOV
130
        ], $apiProperty->getSchema());
×
131

UNCOV
132
        $apiProperty =
×
UNCOV
133
            new ApiProperty(
×
UNCOV
134
                openapiContext: ['iris' => 'https://schema.org/Date'],
×
UNCOV
135
                nativeType: Type::object(\DateTimeImmutable::class),
×
UNCOV
136
            );
×
UNCOV
137
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
138
        $decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'bar')->willReturn($apiProperty);
×
UNCOV
139
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
140
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'bar');
×
UNCOV
141
        $this->assertEquals([
×
UNCOV
142
            'type' => 'string',
×
UNCOV
143
            'format' => 'date-time',
×
UNCOV
144
        ], $apiProperty->getSchema());
×
145
    }
146

147
    public function testUnionTypeAnyOfIsArray(): void
148
    {
UNCOV
149
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) { // @phpstan-ignore-line symfony/property-info 6.4 is still allowed and this may be true
×
UNCOV
150
            $this->markTestSkipped('This test only supports type-info component');
×
151
        }
152

UNCOV
153
        $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
×
UNCOV
154
        $apiProperty = new ApiProperty(nativeType: Type::union(Type::string(), Type::int()));
×
UNCOV
155
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
UNCOV
156
        $decorated->expects($this->once())->method('create')->with(DummyWithUnionTypeProperty::class, 'unionProperty')->willReturn($apiProperty);
×
157

UNCOV
158
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
UNCOV
159
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithUnionTypeProperty::class, 'unionProperty');
×
160

UNCOV
161
        $expectedSchema = [
×
UNCOV
162
            'anyOf' => [
×
UNCOV
163
                ['type' => 'integer'],
×
UNCOV
164
                ['type' => 'string'],
×
UNCOV
165
            ],
×
UNCOV
166
        ];
×
167

UNCOV
168
        $this->assertEquals($expectedSchema, $apiProperty->getSchema());
×
169
    }
170
}
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