22
22
use ApiPlatform \Core \Upgrade \SubresourceTransformer ;
23
23
use ApiPlatform \Metadata \Resource \Factory \ResourceNameCollectionFactoryInterface ;
24
24
use ApiPlatform \Metadata \Resource \ResourceNameCollection ;
25
- use ApiPlatform \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
25
+ use ApiPlatform \Tests \Fixtures \TestBundle \Entity \DummyToUpgradeWithOnlyAnnotation ;
26
+ use ApiPlatform \Tests \Fixtures \TestBundle \Entity \DummyToUpgradeWithOnlyAttribute ;
26
27
use Doctrine \Common \Annotations \AnnotationReader ;
27
28
use PHPUnit \Framework \TestCase ;
28
29
use Symfony \Component \Console \Application ;
@@ -49,75 +50,117 @@ private function getCommandTester(ResourceNameCollectionFactoryInterface $resour
49
50
50
51
/**
51
52
* @requires PHP 8.1
53
+ *
54
+ * @dataProvider debugResourceProvider
52
55
*/
53
- public function testDebugResource ()
56
+ public function testDebugResource (string $ entityClass , array $ subresourceOperationFactoryReturn , array $ expectedStrings )
54
57
{
55
58
$ resourceNameCollectionFactoryProphecy = $ this ->prophesize (ResourceNameCollectionFactoryInterface::class);
56
- $ resourceNameCollectionFactoryProphecy ->create ()->willReturn (new ResourceNameCollection ([RelatedDummy::class ]));
59
+ $ resourceNameCollectionFactoryProphecy ->create ()->willReturn (new ResourceNameCollection ([$ entityClass ]));
57
60
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
58
- $ resourceMetadataFactoryProphecy ->create (RelatedDummy::class )->willReturn (new ResourceMetadata ());
61
+ $ resourceMetadataFactoryProphecy ->create ($ entityClass )->willReturn (new ResourceMetadata ());
59
62
$ subresourceOperationFactoryProphecy = $ this ->prophesize (SubresourceOperationFactoryInterface::class);
60
- $ subresourceOperationFactoryProphecy ->create (RelatedDummy::class)->willReturn ([[
61
- 'property ' => 'id ' ,
62
- 'collection ' => false ,
63
- 'resource_class ' => RelatedDummy::class,
64
- 'shortNames ' => [
65
- 'RelatedDummy ' ,
66
- ],
67
- 'legacy_filters ' => [
68
- 'related_dummy.friends ' ,
69
- 'related_dummy.complex_sub_query ' ,
70
- ],
71
- 'legacy_normalization_context ' => [
72
- 'groups ' => [
73
- 'friends ' ,
74
- ],
75
- ],
76
- 'legacy_type ' => 'https://schema.org/Product ' ,
77
- 'identifiers ' => [
78
- 'id ' => [
79
- RelatedDummy::class,
80
- 'id ' ,
81
- true ,
82
- ],
83
- ],
84
- 'operation_name ' => 'id_get_subresource ' ,
85
- 'route_name ' => 'api_related_dummies_id_get_subresource ' ,
86
- 'path ' => '/related_dummies/{id}/id.{_format} ' ,
87
- ]]);
63
+ $ subresourceOperationFactoryProphecy ->create ($ entityClass )->willReturn ($ subresourceOperationFactoryReturn );
88
64
89
65
$ commandTester = $ this ->getCommandTester ($ resourceNameCollectionFactoryProphecy ->reveal (), $ resourceMetadataFactoryProphecy ->reveal (), $ subresourceOperationFactoryProphecy ->reveal ());
90
66
$ commandTester ->execute ([]);
91
67
92
- $ expectedStrings = [
93
- '-use ApiPlatform \\Core \\Annotation \\ApiSubresource ' ,
94
- '-use ApiPlatform \\Core \\Annotation \\ApiProperty ' ,
95
- '-use ApiPlatform \\Core \\Annotation \\ApiResource ' ,
96
- '-use ApiPlatform \\Core \\Annotation \\ApiFilter ' ,
97
- '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\SearchFilter; ' ,
98
- '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\ExistsFilter; ' ,
99
- '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\DateFilter; ' ,
100
- '+use ApiPlatform \\Metadata \\ApiProperty ' ,
101
- '+use ApiPlatform \\Metadata \\ApiResource ' ,
102
- '+use ApiPlatform \\Metadata \\ApiFilter ' ,
103
- '+use ApiPlatform \\Doctrine \\Orm \\Filter \\SearchFilter ' ,
104
- '+use ApiPlatform \\Doctrine \\Orm \\Filter \\ExistsFilter ' ,
105
- '+use ApiPlatform \\Doctrine \\Orm \\Filter \\DateFilter ' ,
106
- '+use ApiPlatform \\Metadata \\Get ' ,
107
- "+#[ApiResource(graphQlOperations: [new Query(name: 'item_query'), new Mutation(name: 'update', normalizationContext: ['groups' => ['chicago', 'fakemanytomany']], denormalizationContext: ['groups' => ['friends']])], types: ['https://schema.org/Product'], normalizationContext: ['groups' => ['friends']], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'])] " ,
108
- "#[ApiResource(uriTemplate: '/related_dummies/{id}/id.{_format}', uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])] " ,
109
- "+#[ApiFilter(filterClass: SearchFilter::class, properties: ['id'])] " ,
110
- '+ #[ApiFilter(filterClass: SearchFilter::class)] ' ,
111
- '+ #[ApiFilter(filterClass: ExistsFilter::class)] ' ,
112
- '+ #[ApiFilter(filterClass: DateFilter::class)] ' ,
113
- '+ #[ApiProperty(writable: false)] ' ,
114
- "+ #[ApiProperty(iris: ['RelatedDummy.name'])] " ,
115
- "+ #[ApiProperty(deprecationReason: 'This property is deprecated for upgrade test')] " ,
116
- ];
117
-
118
68
$ display = $ commandTester ->getDisplay ();
119
69
foreach ($ expectedStrings as $ expectedString ) {
120
70
$ this ->assertStringContainsString ($ expectedString , $ display );
121
71
}
122
72
}
73
+
74
+ public function debugResourceProvider (): array
75
+ {
76
+ $ entityClasses = [
77
+ 'only_annotation ' => DummyToUpgradeWithOnlyAnnotation::class,
78
+ 'only_attribute ' => DummyToUpgradeWithOnlyAttribute::class,
79
+ ];
80
+
81
+ return array_map (function ($ key , $ entityClass ) {
82
+ $ expectedStrings = [
83
+ '+#[ApiResource] ' ,
84
+ '-use ApiPlatform \\Core \\Annotation \\ApiSubresource ' ,
85
+ '-use ApiPlatform \\Core \\Annotation \\ApiProperty ' ,
86
+ '-use ApiPlatform \\Core \\Annotation \\ApiResource ' ,
87
+ '+use ApiPlatform \\Metadata \\ApiProperty ' ,
88
+ '+use ApiPlatform \\Metadata \\ApiResource ' ,
89
+ '+use ApiPlatform \\Metadata \\ApiFilter ' ,
90
+ '+use ApiPlatform \\Metadata \\Get ' ,
91
+ sprintf ("#[ApiResource(uriTemplate: '/%s/{id}/name.{_format}', uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])] " , $ key ),
92
+ ];
93
+
94
+ if (DummyToUpgradeWithOnlyAnnotation::class === $ entityClass ) {
95
+ array_push ($ expectedStrings ,
96
+ '+use ApiPlatform \\Doctrine \\Orm \\Filter \\SearchFilter ' ,
97
+ '+use ApiPlatform \\Doctrine \\Orm \\Filter \\ExistsFilter ' ,
98
+ '+use ApiPlatform \\Doctrine \\Orm \\Filter \\DateFilter ' ,
99
+ '-use ApiPlatform \\Core \\Annotation \\ApiFilter ' ,
100
+ '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\SearchFilter; ' ,
101
+ '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\ExistsFilter; ' ,
102
+ '-use ApiPlatform \\Core \\Bridge \\Doctrine \\Orm \\Filter \\DateFilter; ' ,
103
+ '- * @ApiResource ' ,
104
+ '- * @ApiFilter(SearchFilter::class, properties={"id"}) ' ,
105
+ "+#[ApiFilter(filterClass: SearchFilter::class, properties: ['id'])] " ,
106
+ '- * @ApiProperty(writable=false) ' ,
107
+ '+ #[ApiProperty(writable: false)] ' ,
108
+ '- * @ApiSubresource ' ,
109
+ '- * @ApiFilter(DateFilter::class) ' ,
110
+ '- * @ApiProperty(iri="DummyToUpgradeWithOnlyAnnotation.dummyToUpgradeProduct") ' ,
111
+ "+ #[ApiProperty(iris: ['DummyToUpgradeWithOnlyAnnotation.dummyToUpgradeProduct'])] " ,
112
+ '- * @ApiFilter(SearchFilter::class) ' ,
113
+ '- * @ApiFilter(ExistsFilter::class) ' ,
114
+ '+ #[ApiFilter(filterClass: SearchFilter::class)] ' ,
115
+ '+ #[ApiFilter(filterClass: ExistsFilter::class)] ' ,
116
+ '+ #[ApiFilter(filterClass: DateFilter::class)] '
117
+ );
118
+ }
119
+
120
+ if (DummyToUpgradeWithOnlyAttribute::class === $ entityClass ) {
121
+ array_push ($ expectedStrings ,
122
+ '-#[ApiResource()] ' ,
123
+ "+#[ApiResource(uriTemplate: '/only_attribute/{id}/name.{_format}', uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])] " ,
124
+ '- #[ApiSubresource] ' ,
125
+ "- #[ApiProperty(iri: 'DummyToUpgradeWithOnlyAttribute.dummyToUpgradeProduct')] " ,
126
+ "+ #[ApiProperty(iris: ['DummyToUpgradeWithOnlyAttribute.dummyToUpgradeProduct'])] "
127
+ );
128
+ }
129
+
130
+ return [
131
+ $ entityClass ,
132
+ [
133
+ [
134
+ 'property ' => 'id ' ,
135
+ 'collection ' => false ,
136
+ 'resource_class ' => $ entityClass ,
137
+ 'shortNames ' => [
138
+ substr ($ entityClass , (\strlen ($ entityClass ) - strrpos ($ entityClass , '\\' ) - 1 ) * (-1 )),
139
+ ],
140
+ 'legacy_filters ' => [
141
+ 'related_dummy.friends ' ,
142
+ 'related_dummy.complex_sub_query ' ,
143
+ ],
144
+ 'legacy_normalization_context ' => [
145
+ 'groups ' => [
146
+ 'friends ' ,
147
+ ],
148
+ ],
149
+ 'legacy_type ' => 'https://schema.org/Product ' ,
150
+ 'identifiers ' => [
151
+ 'id ' => [
152
+ $ entityClass ,
153
+ 'id ' ,
154
+ true ,
155
+ ],
156
+ ],
157
+ 'operation_name ' => 'name_get_subresource ' ,
158
+ 'route_name ' => sprintf ('api_%s_name_get_subresource ' , $ key ),
159
+ 'path ' => sprintf ('/%s/{id}/name.{_format} ' , $ key ),
160
+ ],
161
+ ],
162
+ array_merge ($ expectedStrings ),
163
+ ];
164
+ }, array_keys ($ entityClasses ), array_values ($ entityClasses ));
165
+ }
123
166
}
0 commit comments