20
20
use ApiPlatform \Metadata \ApiProperty ;
21
21
use ApiPlatform \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
22
22
use ApiPlatform \Metadata \ResourceClassResolverInterface ;
23
+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
23
24
use PHPUnit \Framework \TestCase ;
24
- use Symfony \Component \PropertyInfo \Type ;
25
+ use Symfony \Component \PropertyInfo \Type as LegacyType ;
26
+ use Symfony \Component \TypeInfo \Type ;
25
27
26
28
class SchemaPropertyMetadataFactoryTest extends TestCase
27
29
{
30
+ #[IgnoreDeprecations]
31
+ public function testEnumLegacy (): void
32
+ {
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
+
28
43
public function testEnum (): void
29
44
{
30
45
$ resourceClassResolver = $ this ->createMock (ResourceClassResolverInterface::class);
31
- $ apiProperty = new ApiProperty (builtinTypes: [ new Type (builtinType: ' object ' , nullable: true , class: IntEnumAsIdentifier::class)]);
46
+ $ apiProperty = new ApiProperty (nativeType: Type:: nullable (Type:: enum ( IntEnumAsIdentifier::class))); // @phpstan-ignore-line
32
47
$ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
33
48
$ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithEnum::class, 'intEnumAsIdentifier ' )->willReturn ($ apiProperty );
34
49
$ schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory ($ resourceClassResolver , $ decorated );
35
50
$ apiProperty = $ schemaPropertyMetadataFactory ->create (DummyWithEnum::class, 'intEnumAsIdentifier ' );
36
51
$ this ->assertEquals (['type ' => ['integer ' , 'null ' ], 'enum ' => [1 , 2 , null ]], $ apiProperty ->getSchema ());
37
52
}
38
53
54
+ #[IgnoreDeprecations]
55
+ public function testWithCustomOpenApiContextLegacy (): void
56
+ {
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
+
39
70
public function testWithCustomOpenApiContext (): void
40
71
{
41
72
$ resourceClassResolver = $ this ->createMock (ResourceClassResolverInterface::class);
42
73
$ apiProperty = new ApiProperty (
43
- builtinTypes: [ new Type (builtinType: ' object ' , nullable: true , class: IntEnumAsIdentifier::class)],
74
+ nativeType: Type:: nullable (Type:: enum ( IntEnumAsIdentifier::class)), // @phpstan-ignore-line
44
75
openapiContext: ['type ' => 'object ' , 'properties ' => ['alpha ' => ['type ' => 'integer ' ]]],
45
76
);
46
77
$ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
@@ -50,12 +81,43 @@ public function testWithCustomOpenApiContext(): void
50
81
$ this ->assertEquals ([], $ apiProperty ->getSchema ());
51
82
}
52
83
84
+ #[IgnoreDeprecations]
85
+ public function testWithCustomOpenApiContextWithoutTypeDefinitionLegacy (): void
86
+ {
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
+
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
+
53
115
public function testWithCustomOpenApiContextWithoutTypeDefinition (): void
54
116
{
55
117
$ resourceClassResolver = $ this ->createMock (ResourceClassResolverInterface::class);
56
118
$ apiProperty = new ApiProperty (
57
119
openapiContext: ['description ' => 'My description ' ],
58
- builtinTypes: [ new Type (builtinType: ' bool ' )] ,
120
+ nativeType: Type:: bool () ,
59
121
);
60
122
$ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
61
123
$ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithCustomOpenApiContext::class, 'foo ' )->willReturn ($ apiProperty );
@@ -67,7 +129,7 @@ public function testWithCustomOpenApiContextWithoutTypeDefinition(): void
67
129
68
130
$ apiProperty = new ApiProperty (
69
131
openapiContext: ['iris ' => 'https://schema.org/Date ' ],
70
- builtinTypes: [ new Type (builtinType: ' object ' , class: \DateTimeImmutable::class)] ,
132
+ nativeType: Type:: object ( \DateTimeImmutable::class),
71
133
);
72
134
$ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
73
135
$ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithCustomOpenApiContext::class, 'bar ' )->willReturn ($ apiProperty );
0 commit comments