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

api-platform / core / 18370496742

09 Oct 2025 08:35AM UTC coverage: 0.0% (-21.9%) from 21.92%
18370496742

push

github

soyuka
docs: changelog 4.2.2

0 of 56436 relevant lines covered (0.0%)

0.0 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)]);
×
37
        $decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
×
38
        $decorated->expects($this->once())->method('create')->with(DummyWithEnum::class, 'intEnumAsIdentifier')->willReturn($apiProperty);
×
39
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
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);
×
51
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
×
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);
×
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);
×
80
        $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
×
81
        $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
×
82
        $this->assertEquals([], $apiProperty->getSchema());
×
83
    }
84

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

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

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

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

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

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

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

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

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