Skip to content

Commit bb69621

Browse files
committed
Adding a integration test to test @field annotation in properties.
1 parent 0005802 commit bb69621

File tree

2 files changed

+88
-28
lines changed

2 files changed

+88
-28
lines changed

tests/Fixtures/Integration/Models/Contact.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;
55

66

7+
use DateTimeImmutable;
78
use TheCodingMachine\GraphQLite\Annotations\MagicField;
89
use function array_search;
910
use DateTimeInterface;
@@ -46,6 +47,16 @@ class Contact
4647
* @var string
4748
*/
4849
private $company;
50+
/**
51+
* @Field()
52+
* @var int
53+
*/
54+
private $age = 42;
55+
/**
56+
* @Field()
57+
* @var string
58+
*/
59+
public $nickName = 'foo';
4960

5061
public function __construct(string $name)
5162
{
@@ -182,6 +193,14 @@ public function secret(): string
182193
return 'you can see this only if you have the good right';
183194
}
184195

196+
/**
197+
* @return int
198+
*/
199+
public function getAge(): int
200+
{
201+
return $this->age;
202+
}
203+
185204
/**
186205
* @Field()
187206
* @Autowire(for="testService", identifier="testService")

tests/Integration/EndToEndTest.php

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
66
use GraphQL\Error\Debug;
7+
use GraphQL\Executor\ExecutionResult;
78
use GraphQL\GraphQL;
89
use Mouf\Picotainer\Picotainer;
910
use PHPUnit\Framework\TestCase;
@@ -66,8 +67,10 @@
6667
use TheCodingMachine\GraphQLite\Types\ArgumentResolver;
6768
use TheCodingMachine\GraphQLite\Types\TypeResolver;
6869
use TheCodingMachine\GraphQLite\Utils\Namespaces\NamespaceFactory;
70+
use function json_encode;
6971
use function var_dump;
7072
use function var_export;
73+
use const JSON_PRETTY_PRINT;
7174

7275
class EndToEndTest extends TestCase
7376
{
@@ -275,6 +278,17 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
275278
return $container;
276279
}
277280

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+
278292
public function testEndToEnd(): void
279293
{
280294
/**
@@ -325,7 +339,7 @@ public function testEndToEnd(): void
325339
]
326340

327341
]
328-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
342+
], $this->getSuccessResult($result));
329343

330344
// Let's redo this to test cache.
331345
$result = GraphQL::executeQuery(
@@ -354,7 +368,7 @@ public function testEndToEnd(): void
354368
]
355369

356370
]
357-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
371+
], $this->getSuccessResult($result));
358372
}
359373

360374
public function testDeprecatedField(): void
@@ -400,7 +414,7 @@ public function testDeprecatedField(): void
400414
]
401415

402416
]
403-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
417+
], $this->getSuccessResult($result));
404418

405419
// Let's introspect to see if the field is marked as deprecated
406420
// in the resulting GraphQL schema
@@ -423,7 +437,7 @@ public function testDeprecatedField(): void
423437
new Context()
424438
);
425439

426-
$fields = $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']['__type']['fields'];
440+
$fields = $this->getSuccessResult($result)['__type']['fields'];
427441
$deprecatedFields = [
428442
'deprecatedUppercaseName',
429443
'deprecatedName'
@@ -511,7 +525,7 @@ public function testEndToEndInputTypeDate()
511525
'name' => 'Bill',
512526
'birthDate' => '1942-12-24T00:00:00+00:00',
513527
]
514-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
528+
], $this->getSuccessResult($result));
515529
}
516530

517531
public function testEndToEndInputTypeDateAsParam()
@@ -544,7 +558,7 @@ public function testEndToEndInputTypeDateAsParam()
544558
'name' => 'Bill',
545559
'birthDate' => '1942-12-24T00:00:00+00:00',
546560
]
547-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
561+
], $this->getSuccessResult($result));
548562
}
549563

550564
public function testEndToEndInputType()
@@ -590,7 +604,7 @@ public function testEndToEndInputType()
590604
]
591605
]
592606
]
593-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
607+
], $this->getSuccessResult($result));
594608
}
595609

596610
public function testEndToEndPorpaginas(): void
@@ -631,7 +645,7 @@ public function testEndToEndPorpaginas(): void
631645
],
632646
'count' => 2
633647
]
634-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
648+
], $this->getSuccessResult($result));
635649

636650
// Let's redo this to test cache.
637651
$result = GraphQL::executeQuery(
@@ -650,7 +664,7 @@ public function testEndToEndPorpaginas(): void
650664
],
651665
'count' => 2
652666
]
653-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
667+
], $this->getSuccessResult($result));
654668

655669
// Let's run a query with no limit but an offset
656670
$invalidQueryString = '
@@ -708,7 +722,7 @@ public function testEndToEndPorpaginas(): void
708722
],
709723
'count' => 2
710724
]
711-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
725+
], $this->getSuccessResult($result));
712726
}
713727

714728
public function testEndToEndPorpaginasOnScalarType(): void
@@ -737,7 +751,7 @@ public function testEndToEndPorpaginasOnScalarType(): void
737751
'items' => ['Bill'],
738752
'count' => 2
739753
]
740-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
754+
], $this->getSuccessResult($result));
741755
}
742756

743757
/**
@@ -800,7 +814,7 @@ public function testEndToEnd2Iterators(): void
800814
],
801815
'count' => 1
802816
]
803-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
817+
], $this->getSuccessResult($result));
804818

805819
}
806820

@@ -824,7 +838,7 @@ public function testEndToEndStaticFactories(): void
824838

825839
$this->assertSame([
826840
'echoFilters' => [ "foo", "bar", "12", "42", "62" ]
827-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
841+
], $this->getSuccessResult($result));
828842

829843
// Call again to test GlobTypeMapper cache
830844
$result = GraphQL::executeQuery(
@@ -834,7 +848,7 @@ public function testEndToEndStaticFactories(): void
834848

835849
$this->assertSame([
836850
'echoFilters' => [ "foo", "bar", "12", "42", "62" ]
837-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
851+
], $this->getSuccessResult($result));
838852
}
839853

840854
public function testNonNullableTypesWithOptionnalFactoryArguments(): void
@@ -857,7 +871,7 @@ public function testNonNullableTypesWithOptionnalFactoryArguments(): void
857871

858872
$this->assertSame([
859873
'echoFilters' => []
860-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
874+
], $this->getSuccessResult($result));
861875
}
862876

863877
public function testNullableTypesWithOptionnalFactoryArguments(): void
@@ -880,7 +894,7 @@ public function testNullableTypesWithOptionnalFactoryArguments(): void
880894

881895
$this->assertSame([
882896
'echoNullableFilters' => null
883-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
897+
], $this->getSuccessResult($result));
884898
}
885899

886900
public function testEndToEndResolveInfo(): void
@@ -903,7 +917,7 @@ public function testEndToEndResolveInfo(): void
903917

904918
$this->assertSame([
905919
'echoResolveInfo' => 'echoResolveInfo'
906-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
920+
], $this->getSuccessResult($result));
907921
}
908922

909923
public function testEndToEndRightIssues(): void
@@ -976,7 +990,7 @@ public function testAutowireService(): void
976990
]
977991

978992
]
979-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
993+
], $this->getSuccessResult($result));
980994
}
981995

982996
public function testParameterAnnotationsInSourceField(): void
@@ -1009,7 +1023,7 @@ public function testParameterAnnotationsInSourceField(): void
10091023
]
10101024

10111025
]
1012-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1026+
], $this->getSuccessResult($result));
10131027
}
10141028

10151029
public function testEndToEndEnums(): void
@@ -1032,7 +1046,7 @@ public function testEndToEndEnums(): void
10321046

10331047
$this->assertSame([
10341048
'echoProductType' => 'NON_FOOD'
1035-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1049+
], $this->getSuccessResult($result));
10361050
}
10371051

10381052
public function testEndToEndEnums2(): void
@@ -1055,7 +1069,7 @@ public function testEndToEndEnums2(): void
10551069

10561070
$this->assertSame([
10571071
'echoSomeProductType' => 'FOOD'
1058-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1072+
], $this->getSuccessResult($result));
10591073
}
10601074

10611075
public function testEndToEndEnums3(): void
@@ -1085,7 +1099,7 @@ public function testEndToEndEnums3(): void
10851099

10861100
$this->assertSame([
10871101
'echoProductType' => 'NON_FOOD'
1088-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1102+
], $this->getSuccessResult($result));
10891103
}
10901104

10911105
public function testEndToEndDateTime(): void
@@ -1108,7 +1122,7 @@ public function testEndToEndDateTime(): void
11081122

11091123
$this->assertSame([
11101124
'echoDate' => '2019-05-05T01:02:03+00:00'
1111-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1125+
], $this->getSuccessResult($result));
11121126
}
11131127

11141128
public function testEndToEndErrorHandlingOfInconstentTypesInArrays(): void
@@ -1164,7 +1178,7 @@ public function testEndToEndNonDefaultOutputType(): void
11641178
'fullName' => 'JOE',
11651179
'phone' => '0123456789'
11661180
]
1167-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1181+
], $this->getSuccessResult($result));
11681182
}
11691183

11701184
public function testEndToEndSecurityAnnotation(): void
@@ -1187,7 +1201,7 @@ public function testEndToEndSecurityAnnotation(): void
11871201

11881202
$this->assertSame([
11891203
'secretPhrase' => 'you can see this secret only if passed parameter is "foo"'
1190-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1204+
], $this->getSuccessResult($result));
11911205

11921206
$queryString = '
11931207
query {
@@ -1226,7 +1240,7 @@ public function testEndToEndSecurityFailWithAnnotation(): void
12261240

12271241
$this->assertSame([
12281242
'nullableSecretPhrase' => null
1229-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1243+
], $this->getSuccessResult($result));
12301244

12311245
// Test with @FailWith annotation
12321246
$queryString = '
@@ -1242,7 +1256,7 @@ public function testEndToEndSecurityFailWithAnnotation(): void
12421256

12431257
$this->assertSame([
12441258
'nullableSecretPhrase2' => null
1245-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1259+
], $this->getSuccessResult($result));
12461260
}
12471261

12481262
public function testEndToEndSecurityWithUser(): void
@@ -1487,7 +1501,7 @@ public function testEndToEndMagicFieldWithPhpType(): void
14871501
]
14881502
],
14891503
]
1490-
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
1504+
], $this->getSuccessResult($result));
14911505
}
14921506

14931507
public function testEndToEndInjectUser(): void
@@ -1545,4 +1559,31 @@ public function testInputOutputNameConflict(): void
15451559

15461560
$schema->validate();
15471561
}
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+
}
15481589
}

0 commit comments

Comments
 (0)