21
21
use ApiPlatform \Metadata \GraphQl \QueryCollection ;
22
22
use GraphQL \Type \Definition \ResolveInfo ;
23
23
use PHPUnit \Framework \TestCase ;
24
+ use Prophecy \Argument ;
24
25
use Prophecy \PhpUnit \ProphecyTrait ;
25
26
use Prophecy \Prophecy \ObjectProphecy ;
26
27
use Psr \Container \ContainerInterface ;
@@ -68,7 +69,9 @@ public function testResolve(): void
68
69
$ operation = (new QueryCollection ())->withName ($ operationName );
69
70
$ source = ['testField ' => 0 ];
70
71
$ args = ['args ' ];
71
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
72
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
73
+ $ infoProphecy ->getFieldSelection ()->willReturn (['testField ' => true ]);
74
+ $ info = $ infoProphecy ->reveal ();
72
75
$ info ->fieldName = 'testField ' ;
73
76
$ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
74
77
@@ -101,7 +104,9 @@ public function testResolveFieldNotInSource(): void
101
104
$ operation = (new QueryCollection ())->withName ($ operationName );
102
105
$ source = ['source ' ];
103
106
$ args = ['args ' ];
104
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
107
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
108
+ $ infoProphecy ->getFieldSelection ()->willReturn (['testField ' => true ]);
109
+ $ info = $ infoProphecy ->reveal ();
105
110
$ info ->fieldName = 'testField ' ;
106
111
$ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
107
112
@@ -132,7 +137,9 @@ public function testResolveNullSource(): void
132
137
$ operation = (new QueryCollection ())->withName ($ operationName );
133
138
$ source = null ;
134
139
$ args = ['args ' ];
135
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
140
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
141
+ $ infoProphecy ->getFieldSelection ()->willReturn ([]);
142
+ $ info = $ infoProphecy ->reveal ();
136
143
$ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
137
144
138
145
$ readStageCollection = [new \stdClass ()];
@@ -164,7 +171,9 @@ public function testResolveNullResourceClass(): void
164
171
$ operation = (new QueryCollection ())->withName ($ operationName );
165
172
$ source = ['source ' ];
166
173
$ args = ['args ' ];
167
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
174
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
175
+ $ infoProphecy ->getFieldSelection ()->willReturn ([]);
176
+ $ info = $ infoProphecy ->reveal ();
168
177
169
178
$ this ->assertNull (($ this ->collectionResolverFactory )($ resourceClass , $ rootClass , $ operation )($ source , $ args , null , $ info ));
170
179
}
@@ -177,7 +186,9 @@ public function testResolveNullRootClass(): void
177
186
$ operation = (new QueryCollection ())->withName ($ operationName );
178
187
$ source = ['source ' ];
179
188
$ args = ['args ' ];
180
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
189
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
190
+ $ infoProphecy ->getFieldSelection ()->willReturn ([]);
191
+ $ info = $ infoProphecy ->reveal ();
181
192
182
193
$ this ->assertNull (($ this ->collectionResolverFactory )($ resourceClass , $ rootClass , $ operation )($ source , $ args , null , $ info ));
183
194
}
@@ -190,7 +201,9 @@ public function testResolveBadReadStageCollection(): void
190
201
$ operation = (new QueryCollection ())->withName ($ operationName );
191
202
$ source = null ;
192
203
$ args = ['args ' ];
193
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
204
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
205
+ $ infoProphecy ->getFieldSelection ()->willReturn ([]);
206
+ $ info = $ infoProphecy ->reveal ();
194
207
$ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
195
208
196
209
$ readStageCollection = new \stdClass ();
@@ -210,7 +223,9 @@ public function testResolveCustom(): void
210
223
$ operation = (new QueryCollection ())->withResolver ('query_resolver_id ' )->withName ($ operationName );
211
224
$ source = null ;
212
225
$ args = ['args ' ];
213
- $ info = $ this ->prophesize (ResolveInfo::class)->reveal ();
226
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
227
+ $ infoProphecy ->getFieldSelection ()->willReturn ([]);
228
+ $ info = $ infoProphecy ->reveal ();
214
229
$ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
215
230
216
231
$ readStageCollection = [new \stdClass ()];
@@ -237,4 +252,44 @@ public function testResolveCustom(): void
237
252
238
253
$ this ->assertSame ($ serializeStageData , ($ this ->collectionResolverFactory )($ resourceClass , $ rootClass , $ operation )($ source , $ args , null , $ info ));
239
254
}
255
+
256
+ /**
257
+ * @dataProvider resolveAndPaginationInfoProvider
258
+ */
259
+ public function testResolveAndPaginationInfo (bool $ paginationClientPartialEnabled , bool $ paginationInfoRequested , ?bool $ shouldPaginationBePartial ): void
260
+ {
261
+ $ resourceClass = \stdClass::class;
262
+ $ rootClass = 'rootClass ' ;
263
+ $ operationName = 'collection_query ' ;
264
+ $ operation = (new QueryCollection ())->withName ($ operationName )->withPaginationClientPartial ($ paginationClientPartialEnabled );
265
+ $ source = ['testField ' => 0 ];
266
+ $ args = ['args ' ];
267
+ $ infoProphecy = $ this ->prophesize (ResolveInfo::class);
268
+ $ resolveInfoFields = ['testField ' => true ];
269
+ if ($ paginationInfoRequested ) {
270
+ $ resolveInfoFields ['paginationInfo ' ] = true ;
271
+ }
272
+ $ infoProphecy ->getFieldSelection ()->willReturn ($ resolveInfoFields );
273
+ $ info = $ infoProphecy ->reveal ();
274
+ $ info ->fieldName = 'testField ' ;
275
+ $ resolverContext = ['source ' => $ source , 'args ' => $ args , 'info ' => $ info , 'is_collection ' => true , 'is_mutation ' => false , 'is_subscription ' => false ];
276
+
277
+ $ readStageCollection = [new \stdClass ()];
278
+ $ this ->readStageProphecy ->__invoke ($ resourceClass , $ rootClass , Argument::type ($ operation ::class), $ resolverContext )->shouldBeCalled ()->willReturn ($ readStageCollection );
279
+
280
+ $ serializeStageData = ['serialized ' ];
281
+ $ this ->serializeStageProphecy ->__invoke ($ readStageCollection , $ resourceClass , Argument::that (function ($ operation ) use ($ shouldPaginationBePartial ) {
282
+ return $ operation ->getPaginationPartial () === $ shouldPaginationBePartial ;
283
+ }), $ resolverContext )->shouldBeCalled ()->willReturn ($ serializeStageData );
284
+
285
+ $ this ->assertSame ($ serializeStageData , ($ this ->collectionResolverFactory )($ resourceClass , $ rootClass , $ operation )($ source , $ args , null , $ info ));
286
+ }
287
+
288
+ public static function resolveAndPaginationInfoProvider (): iterable
289
+ {
290
+ yield 'paginationClientPartial disabled - paginationInfo requested ' => [false , true , null ];
291
+ yield 'paginationClientPartial enabled - paginationInfo requested ' => [true , true , null ];
292
+ yield 'paginationClientPartial disabled - paginationInfo not requested ' => [false , false , null ];
293
+ yield 'paginationClientPartial enabled - paginationInfo not requested ' => [true , false , true ];
294
+ }
240
295
}
0 commit comments