4
4
5
5
use Doctrine \Common \Annotations \AnnotationReader as DoctrineAnnotationReader ;
6
6
use GraphQL \Error \Debug ;
7
+ use GraphQL \Executor \ExecutionResult ;
7
8
use GraphQL \GraphQL ;
8
9
use Mouf \Picotainer \Picotainer ;
9
10
use PHPUnit \Framework \TestCase ;
66
67
use TheCodingMachine \GraphQLite \Types \ArgumentResolver ;
67
68
use TheCodingMachine \GraphQLite \Types \TypeResolver ;
68
69
use TheCodingMachine \GraphQLite \Utils \Namespaces \NamespaceFactory ;
70
+ use function json_encode ;
69
71
use function var_dump ;
70
72
use function var_export ;
73
+ use const JSON_PRETTY_PRINT ;
71
74
72
75
class EndToEndTest extends TestCase
73
76
{
@@ -275,6 +278,17 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
275
278
return $ container ;
276
279
}
277
280
281
+ /**
282
+ * @return mixed
283
+ */
284
+ private function getSuccessResult (ExecutionResult $ result , int $ debugFlag = Debug::RETHROW_INTERNAL_EXCEPTIONS ) {
285
+ $ array = $ result ->toArray ($ debugFlag );
286
+ if (isset ($ array ['errors ' ]) || !isset ($ array ['data ' ])) {
287
+ $ this ->fail ('Expected a successful answer. Got ' .json_encode ($ array , JSON_PRETTY_PRINT ));
288
+ }
289
+ return $ array ['data ' ];
290
+ }
291
+
278
292
public function testEndToEnd (): void
279
293
{
280
294
/**
@@ -325,7 +339,7 @@ public function testEndToEnd(): void
325
339
]
326
340
327
341
]
328
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
342
+ ], $ this -> getSuccessResult ( $ result ) );
329
343
330
344
// Let's redo this to test cache.
331
345
$ result = GraphQL::executeQuery (
@@ -354,7 +368,7 @@ public function testEndToEnd(): void
354
368
]
355
369
356
370
]
357
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
371
+ ], $ this -> getSuccessResult ( $ result ) );
358
372
}
359
373
360
374
public function testDeprecatedField (): void
@@ -400,7 +414,7 @@ public function testDeprecatedField(): void
400
414
]
401
415
402
416
]
403
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
417
+ ], $ this -> getSuccessResult ( $ result ) );
404
418
405
419
// Let's introspect to see if the field is marked as deprecated
406
420
// in the resulting GraphQL schema
@@ -423,7 +437,7 @@ public function testDeprecatedField(): void
423
437
new Context ()
424
438
);
425
439
426
- $ fields = $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] ['__type ' ]['fields ' ];
440
+ $ fields = $ this -> getSuccessResult ( $ result ) ['__type ' ]['fields ' ];
427
441
$ deprecatedFields = [
428
442
'deprecatedUppercaseName ' ,
429
443
'deprecatedName '
@@ -511,7 +525,7 @@ public function testEndToEndInputTypeDate()
511
525
'name ' => 'Bill ' ,
512
526
'birthDate ' => '1942-12-24T00:00:00+00:00 ' ,
513
527
]
514
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
528
+ ], $ this -> getSuccessResult ( $ result ) );
515
529
}
516
530
517
531
public function testEndToEndInputTypeDateAsParam ()
@@ -544,7 +558,7 @@ public function testEndToEndInputTypeDateAsParam()
544
558
'name ' => 'Bill ' ,
545
559
'birthDate ' => '1942-12-24T00:00:00+00:00 ' ,
546
560
]
547
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
561
+ ], $ this -> getSuccessResult ( $ result ) );
548
562
}
549
563
550
564
public function testEndToEndInputType ()
@@ -590,7 +604,7 @@ public function testEndToEndInputType()
590
604
]
591
605
]
592
606
]
593
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
607
+ ], $ this -> getSuccessResult ( $ result ) );
594
608
}
595
609
596
610
public function testEndToEndPorpaginas (): void
@@ -631,7 +645,7 @@ public function testEndToEndPorpaginas(): void
631
645
],
632
646
'count ' => 2
633
647
]
634
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
648
+ ], $ this -> getSuccessResult ( $ result ) );
635
649
636
650
// Let's redo this to test cache.
637
651
$ result = GraphQL::executeQuery (
@@ -650,7 +664,7 @@ public function testEndToEndPorpaginas(): void
650
664
],
651
665
'count ' => 2
652
666
]
653
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
667
+ ], $ this -> getSuccessResult ( $ result ) );
654
668
655
669
// Let's run a query with no limit but an offset
656
670
$ invalidQueryString = '
@@ -708,7 +722,7 @@ public function testEndToEndPorpaginas(): void
708
722
],
709
723
'count ' => 2
710
724
]
711
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
725
+ ], $ this -> getSuccessResult ( $ result ) );
712
726
}
713
727
714
728
public function testEndToEndPorpaginasOnScalarType (): void
@@ -737,7 +751,7 @@ public function testEndToEndPorpaginasOnScalarType(): void
737
751
'items ' => ['Bill ' ],
738
752
'count ' => 2
739
753
]
740
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
754
+ ], $ this -> getSuccessResult ( $ result ) );
741
755
}
742
756
743
757
/**
@@ -800,7 +814,7 @@ public function testEndToEnd2Iterators(): void
800
814
],
801
815
'count ' => 1
802
816
]
803
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
817
+ ], $ this -> getSuccessResult ( $ result ) );
804
818
805
819
}
806
820
@@ -824,7 +838,7 @@ public function testEndToEndStaticFactories(): void
824
838
825
839
$ this ->assertSame ([
826
840
'echoFilters ' => [ "foo " , "bar " , "12 " , "42 " , "62 " ]
827
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
841
+ ], $ this -> getSuccessResult ( $ result ) );
828
842
829
843
// Call again to test GlobTypeMapper cache
830
844
$ result = GraphQL::executeQuery (
@@ -834,7 +848,7 @@ public function testEndToEndStaticFactories(): void
834
848
835
849
$ this ->assertSame ([
836
850
'echoFilters ' => [ "foo " , "bar " , "12 " , "42 " , "62 " ]
837
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
851
+ ], $ this -> getSuccessResult ( $ result ) );
838
852
}
839
853
840
854
public function testNonNullableTypesWithOptionnalFactoryArguments (): void
@@ -857,7 +871,7 @@ public function testNonNullableTypesWithOptionnalFactoryArguments(): void
857
871
858
872
$ this ->assertSame ([
859
873
'echoFilters ' => []
860
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
874
+ ], $ this -> getSuccessResult ( $ result ) );
861
875
}
862
876
863
877
public function testNullableTypesWithOptionnalFactoryArguments (): void
@@ -880,7 +894,7 @@ public function testNullableTypesWithOptionnalFactoryArguments(): void
880
894
881
895
$ this ->assertSame ([
882
896
'echoNullableFilters ' => null
883
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
897
+ ], $ this -> getSuccessResult ( $ result ) );
884
898
}
885
899
886
900
public function testEndToEndResolveInfo (): void
@@ -903,7 +917,7 @@ public function testEndToEndResolveInfo(): void
903
917
904
918
$ this ->assertSame ([
905
919
'echoResolveInfo ' => 'echoResolveInfo '
906
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
920
+ ], $ this -> getSuccessResult ( $ result ) );
907
921
}
908
922
909
923
public function testEndToEndRightIssues (): void
@@ -976,7 +990,7 @@ public function testAutowireService(): void
976
990
]
977
991
978
992
]
979
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
993
+ ], $ this -> getSuccessResult ( $ result ) );
980
994
}
981
995
982
996
public function testParameterAnnotationsInSourceField (): void
@@ -1009,7 +1023,7 @@ public function testParameterAnnotationsInSourceField(): void
1009
1023
]
1010
1024
1011
1025
]
1012
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1026
+ ], $ this -> getSuccessResult ( $ result ) );
1013
1027
}
1014
1028
1015
1029
public function testEndToEndEnums (): void
@@ -1032,7 +1046,7 @@ public function testEndToEndEnums(): void
1032
1046
1033
1047
$ this ->assertSame ([
1034
1048
'echoProductType ' => 'NON_FOOD '
1035
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1049
+ ], $ this -> getSuccessResult ( $ result ) );
1036
1050
}
1037
1051
1038
1052
public function testEndToEndEnums2 (): void
@@ -1055,7 +1069,7 @@ public function testEndToEndEnums2(): void
1055
1069
1056
1070
$ this ->assertSame ([
1057
1071
'echoSomeProductType ' => 'FOOD '
1058
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1072
+ ], $ this -> getSuccessResult ( $ result ) );
1059
1073
}
1060
1074
1061
1075
public function testEndToEndEnums3 (): void
@@ -1085,7 +1099,7 @@ public function testEndToEndEnums3(): void
1085
1099
1086
1100
$ this ->assertSame ([
1087
1101
'echoProductType ' => 'NON_FOOD '
1088
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1102
+ ], $ this -> getSuccessResult ( $ result ) );
1089
1103
}
1090
1104
1091
1105
public function testEndToEndDateTime (): void
@@ -1108,7 +1122,7 @@ public function testEndToEndDateTime(): void
1108
1122
1109
1123
$ this ->assertSame ([
1110
1124
'echoDate ' => '2019-05-05T01:02:03+00:00 '
1111
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1125
+ ], $ this -> getSuccessResult ( $ result ) );
1112
1126
}
1113
1127
1114
1128
public function testEndToEndErrorHandlingOfInconstentTypesInArrays (): void
@@ -1164,7 +1178,7 @@ public function testEndToEndNonDefaultOutputType(): void
1164
1178
'fullName ' => 'JOE ' ,
1165
1179
'phone ' => '0123456789 '
1166
1180
]
1167
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1181
+ ], $ this -> getSuccessResult ( $ result ) );
1168
1182
}
1169
1183
1170
1184
public function testEndToEndSecurityAnnotation (): void
@@ -1187,7 +1201,7 @@ public function testEndToEndSecurityAnnotation(): void
1187
1201
1188
1202
$ this ->assertSame ([
1189
1203
'secretPhrase ' => 'you can see this secret only if passed parameter is "foo" '
1190
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1204
+ ], $ this -> getSuccessResult ( $ result ) );
1191
1205
1192
1206
$ queryString = '
1193
1207
query {
@@ -1226,7 +1240,7 @@ public function testEndToEndSecurityFailWithAnnotation(): void
1226
1240
1227
1241
$ this ->assertSame ([
1228
1242
'nullableSecretPhrase ' => null
1229
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1243
+ ], $ this -> getSuccessResult ( $ result ) );
1230
1244
1231
1245
// Test with @FailWith annotation
1232
1246
$ queryString = '
@@ -1242,7 +1256,7 @@ public function testEndToEndSecurityFailWithAnnotation(): void
1242
1256
1243
1257
$ this ->assertSame ([
1244
1258
'nullableSecretPhrase2 ' => null
1245
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1259
+ ], $ this -> getSuccessResult ( $ result ) );
1246
1260
}
1247
1261
1248
1262
public function testEndToEndSecurityWithUser (): void
@@ -1487,7 +1501,7 @@ public function testEndToEndMagicFieldWithPhpType(): void
1487
1501
]
1488
1502
],
1489
1503
]
1490
- ], $ result -> toArray (Debug:: RETHROW_INTERNAL_EXCEPTIONS )[ ' data ' ] );
1504
+ ], $ this -> getSuccessResult ( $ result ) );
1491
1505
}
1492
1506
1493
1507
public function testEndToEndInjectUser (): void
@@ -1545,4 +1559,31 @@ public function testInputOutputNameConflict(): void
1545
1559
1546
1560
$ schema ->validate ();
1547
1561
}
1562
+
1563
+ public function testEndToEndFieldAnnotationInProperty (): void
1564
+ {
1565
+ /**
1566
+ * @var Schema $schema
1567
+ */
1568
+ $ schema = $ this ->mainContainer ->get (Schema::class);
1569
+
1570
+ $ queryString = '
1571
+ query {
1572
+ contacts {
1573
+ age
1574
+ nickName
1575
+ }
1576
+ }
1577
+ ' ;
1578
+
1579
+ $ result = GraphQL::executeQuery (
1580
+ $ schema ,
1581
+ $ queryString
1582
+ );
1583
+
1584
+ $ data = $ this ->getSuccessResult ($ result );
1585
+
1586
+ $ this ->assertSame (42 , $ data ['contacts ' ][0 ]['age ' ]);
1587
+ $ this ->assertSame ('foo ' , $ data ['contacts ' ][0 ]['nickName ' ]);
1588
+ }
1548
1589
}
0 commit comments