forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarWarsValidationTest.php
151 lines (136 loc) · 3.59 KB
/
StarWarsValidationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php declare(strict_types=1);
namespace GraphQL\Tests;
use GraphQL\Error\Error;
use GraphQL\Error\SyntaxError;
use GraphQL\Language\Parser;
use GraphQL\Validator\DocumentValidator;
use PHPUnit\Framework\TestCase;
final class StarWarsValidationTest extends TestCase
{
// Star Wars Validation Tests
// Basic Queries
/** @see it('Validates a complex but valid query') */
public function testValidatesAComplexButValidQuery(): void
{
$query = '
query NestedQueryWithFragment {
hero {
...NameAndAppearances
friends {
...NameAndAppearances
friends {
...NameAndAppearances
}
}
}
}
fragment NameAndAppearances on Character {
name
appearsIn
}
';
$errors = $this->validationErrors($query);
self::assertCount(0, $errors);
}
/**
* Helper function to test a query and the expected response.
*
* @throws \Exception
* @throws \JsonException
* @throws SyntaxError
*
* @return array<int, Error>
*/
private function validationErrors(string $query): array
{
$ast = Parser::parse($query);
$schema = StarWarsSchema::build();
return DocumentValidator::validate($schema, $ast);
}
/** @see it('Notes that non-existent fields are invalid') */
public function testThatNonExistentFieldsAreInvalid(): void
{
$query = '
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
';
$errors = $this->validationErrors($query);
self::assertCount(1, $errors);
}
/** @see it('Requires fields on objects') */
public function testRequiresFieldsOnObjects(): void
{
$query = '
query HeroNoFieldsQuery {
hero
}
';
$errors = $this->validationErrors($query);
self::assertCount(1, $errors);
}
/** @see it('Disallows fields on scalars') */
public function testDisallowsFieldsOnScalars(): void
{
$query = '
query HeroFieldsOnScalarQuery {
hero {
name {
firstCharacterOfName
}
}
}
';
$errors = $this->validationErrors($query);
self::assertCount(1, $errors);
}
/** @see it('Disallows object fields on interfaces') */
public function testDisallowsObjectFieldsOnInterfaces(): void
{
$query = '
query DroidFieldOnCharacter {
hero {
name
primaryFunction
}
}
';
$errors = $this->validationErrors($query);
self::assertCount(1, $errors);
}
/** @see it('Allows object fields in fragments') */
public function testAllowsObjectFieldsInFragments(): void
{
$query = '
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
';
$errors = $this->validationErrors($query);
self::assertCount(0, $errors);
}
/** @see it('Allows object fields in inline fragments') */
public function testAllowsObjectFieldsInInlineFragments(): void
{
$query = '
query DroidFieldInFragment {
hero {
name
... on Droid {
primaryFunction
}
}
}
';
$errors = $this->validationErrors($query);
self::assertCount(0, $errors);
}
}