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

api-platform / core / 17937139726

23 Sep 2025 06:02AM UTC coverage: 21.926% (+0.006%) from 21.92%
17937139726

Pull #7399

github

web-flow
Merge 97d0442d2 into 510fa5523
Pull Request #7399: fix(schema): anyOf must contains an array, not an object

1 of 11 new or added lines in 2 files covered. (9.09%)

8 existing lines in 1 file now uncovered.

11878 of 54174 relevant lines covered (21.93%)

25.51 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\Enum\IntEnumAsIdentifier;
20
use ApiPlatform\Metadata\ApiProperty;
21
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Metadata\ResourceClassResolverInterface;
23
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
24
use PHPUnit\Framework\TestCase;
25
use Symfony\Component\PropertyInfo\Type as LegacyType;
26
use Symfony\Component\TypeInfo\Type;
27

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

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

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

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

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

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

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

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