2020use ApiPlatform \Metadata \ApiProperty ;
2121use ApiPlatform \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
2222use ApiPlatform \Metadata \ResourceClassResolverInterface ;
23+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
2324use PHPUnit \Framework \TestCase ;
24- use Symfony \Component \PropertyInfo \Type ;
25+ use Symfony \Component \PropertyInfo \Type as LegacyType ;
26+ use Symfony \Component \TypeInfo \Type ;
2527
2628class SchemaPropertyMetadataFactoryTest extends TestCase
2729{
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+
2843 public function testEnum (): void
2944 {
3045 $ 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
3247 $ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
3348 $ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithEnum::class, 'intEnumAsIdentifier ' )->willReturn ($ apiProperty );
3449 $ schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory ($ resourceClassResolver , $ decorated );
3550 $ apiProperty = $ schemaPropertyMetadataFactory ->create (DummyWithEnum::class, 'intEnumAsIdentifier ' );
3651 $ this ->assertEquals (['type ' => ['integer ' , 'null ' ], 'enum ' => [1 , 2 , null ]], $ apiProperty ->getSchema ());
3752 }
3853
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+
3970 public function testWithCustomOpenApiContext (): void
4071 {
4172 $ resourceClassResolver = $ this ->createMock (ResourceClassResolverInterface::class);
4273 $ 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
4475 openapiContext: ['type ' => 'object ' , 'properties ' => ['alpha ' => ['type ' => 'integer ' ]]],
4576 );
4677 $ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
@@ -50,12 +81,43 @@ public function testWithCustomOpenApiContext(): void
5081 $ this ->assertEquals ([], $ apiProperty ->getSchema ());
5182 }
5283
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+
53115 public function testWithCustomOpenApiContextWithoutTypeDefinition (): void
54116 {
55117 $ resourceClassResolver = $ this ->createMock (ResourceClassResolverInterface::class);
56118 $ apiProperty = new ApiProperty (
57119 openapiContext: ['description ' => 'My description ' ],
58- builtinTypes: [ new Type (builtinType: ' bool ' )] ,
120+ nativeType: Type:: bool () ,
59121 );
60122 $ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
61123 $ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithCustomOpenApiContext::class, 'foo ' )->willReturn ($ apiProperty );
@@ -67,7 +129,7 @@ public function testWithCustomOpenApiContextWithoutTypeDefinition(): void
67129
68130 $ apiProperty = new ApiProperty (
69131 openapiContext: ['iris ' => 'https://schema.org/Date ' ],
70- builtinTypes: [ new Type (builtinType: ' object ' , class: \DateTimeImmutable::class)] ,
132+ nativeType: Type:: object ( \DateTimeImmutable::class),
71133 );
72134 $ decorated = $ this ->createMock (PropertyMetadataFactoryInterface::class);
73135 $ decorated ->expects ($ this ->once ())->method ('create ' )->with (DummyWithCustomOpenApiContext::class, 'bar ' )->willReturn ($ apiProperty );
0 commit comments