Skip to content

Commit d742994

Browse files
committed
add parser tests and GraphQL-type tests
1 parent 1d45088 commit d742994

File tree

7 files changed

+552
-0
lines changed

7 files changed

+552
-0
lines changed

tests/Parser/ParserTest.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
use \PHPUnit\Framework\TestCase;
4+
use \GraphQL\Parser\Parser;
5+
6+
class ParserTest extends TestCase
7+
{
8+
9+
/**
10+
* Allows us to check if comments are ignored
11+
*/
12+
public function testCheckCommentsAreIgnored()
13+
{
14+
$query = '{
15+
__schema {
16+
#}
17+
# __nonExistingField
18+
queryType {
19+
name
20+
}
21+
#}
22+
}
23+
}';
24+
25+
$parser = new Parser();
26+
$parser->parse($query);
27+
28+
self::assertEmpty($parser->getErrors());
29+
}
30+
31+
/**
32+
* Allows us to check if a complex but valid query can be parsed
33+
*/
34+
public function testComplexIntrospectionQuery()
35+
{
36+
$query = '
37+
query IntrospectionQuery {
38+
__schema {
39+
queryType { name }
40+
mutationType { name }
41+
subscriptionType { name }
42+
types {
43+
...FullType
44+
}
45+
directives {
46+
name
47+
description
48+
args {
49+
...InputValue
50+
}
51+
onOperation
52+
onFragment
53+
onField
54+
}
55+
}
56+
}
57+
58+
fragment FullType on __Type {
59+
kind
60+
name
61+
description
62+
fields(includeDeprecated: true) {
63+
name
64+
description
65+
args {
66+
...InputValue
67+
}
68+
type {
69+
...TypeRef
70+
}
71+
isDeprecated
72+
deprecationReason
73+
}
74+
inputFields {
75+
...InputValue
76+
}
77+
interfaces {
78+
...TypeRef
79+
}
80+
enumValues(includeDeprecated: true) {
81+
name
82+
description
83+
isDeprecated
84+
deprecationReason
85+
}
86+
possibleTypes {
87+
...TypeRef
88+
}
89+
}
90+
91+
fragment InputValue on __InputValue {
92+
name
93+
description
94+
type { ...TypeRef }
95+
defaultValue
96+
}
97+
98+
fragment TypeRef on __Type {
99+
kind
100+
name
101+
ofType {
102+
kind
103+
name
104+
ofType {
105+
kind
106+
name
107+
ofType {
108+
kind
109+
name
110+
}
111+
}
112+
}
113+
}';
114+
115+
$parser = new Parser();
116+
$parser->parse($query);
117+
118+
self::assertEmpty($parser->getErrors());
119+
}
120+
121+
122+
/**
123+
* Allows us to check if string quotes in arguments are required
124+
*/
125+
public function testCheckStringQuotesInArgument()
126+
{
127+
$query = '{
128+
search(query:"Harry Potter){ # missing "
129+
name
130+
}
131+
}';
132+
133+
$parser = new Parser();
134+
$parser->parse($query);
135+
136+
self::assertNotEmpty($parser->getErrors());
137+
}
138+
139+
140+
/**
141+
* Allows us to check if the parser checks for the variable type
142+
*/
143+
public function testCheckVariableTypeIsPresent()
144+
{
145+
$query = 'query QueryWithParams($param:){
146+
someField
147+
}';
148+
149+
$parser = new Parser();
150+
$parser->parse($query);
151+
152+
self::assertNotEmpty($parser->getErrors());
153+
}
154+
155+
156+
/**
157+
* Allows us to check if the parser can parse complex and nested variable types
158+
*/
159+
public function testCheckComplexNestedVariableType()
160+
{
161+
$query = 'query QueryWithParams($param:[[Boolean!]!]!, $param2:[[[[[[Int]!]]!]]!]){
162+
someField
163+
}';
164+
165+
$parser = new Parser();
166+
$parser->parse($query);
167+
168+
self::assertEmpty($parser->getErrors());
169+
}
170+
171+
172+
}

tests/Types/GraphQLBooleanTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use \PHPUnit\Framework\TestCase;
4+
use \GraphQL\Types\GraphQLBoolean;
5+
use \GraphQL\Errors\GraphQLError;
6+
7+
class GraphQLBooleanTest extends TestCase
8+
{
9+
10+
public function getType()
11+
{
12+
return new GraphQLBoolean();
13+
}
14+
15+
/**
16+
* Allows us to test if input can be serialized by the type
17+
*/
18+
public function testSerialize()
19+
{
20+
$type = $this->getType();
21+
22+
self::assertSame(true, $type->serialize(true));
23+
24+
self::expectException(GraphQLError::class);
25+
$type->serialize("true");
26+
}
27+
28+
/**
29+
* Allows us to test if string input can be literalized by the type
30+
*/
31+
public function testParseLiteral()
32+
{
33+
$type = $this->getType();
34+
35+
$node = [
36+
"kind" => "BooleanValue",
37+
"value" => "true"
38+
];
39+
40+
self::assertSame("true", $type->parseLiteral($node, null));
41+
}
42+
43+
44+
/**
45+
* Allows us to test if value can be parsed
46+
*/
47+
public function testParseValue()
48+
{
49+
$type = $this->getType();
50+
51+
self::assertSame(false, $type->parseValue(false));
52+
53+
self::expectException(GraphQLError::class);
54+
$type->parseValue("false");
55+
}
56+
}

tests/Types/GraphQLEnumTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
use \PHPUnit\Framework\TestCase;
4+
use \GraphQL\Types\GraphQLEnum;
5+
use \GraphQL\Types\GraphQLEnumValue;
6+
use \GraphQL\Errors\GraphQLError;
7+
8+
class GraphQLEnumTest extends TestCase
9+
{
10+
11+
public function getType()
12+
{
13+
return new GraphQLEnum("enum", "", [
14+
new GraphQLEnumValue("VAL1", "Value #1."),
15+
new GraphQLEnumValue("VAL2", "Value #2."),
16+
]);
17+
}
18+
19+
/**
20+
* Allows us to test if input can be serialized by the type
21+
*/
22+
public function testSerialize()
23+
{
24+
$type = $this->getType();
25+
26+
self::assertSame("VAL1", $type->serialize("VAL1"));
27+
28+
self::expectException(GraphQLError::class);
29+
$type->serialize("42");
30+
}
31+
32+
/**
33+
* Allows us to test if string input can be literalized by the type
34+
*/
35+
public function testParseLiteral()
36+
{
37+
$type = $this->getType();
38+
39+
$node = [
40+
"kind" => "EnumValue",
41+
"value" => "VAL1"
42+
];
43+
44+
self::assertSame("VAL1", $type->parseLiteral($node, null));
45+
46+
$node = [
47+
"kind" => "EnumValue",
48+
"value" => "223245"
49+
];
50+
51+
self::expectException(GraphQLError::class);
52+
$type->parseLiteral($node, null);
53+
}
54+
55+
56+
/**
57+
* Allows us to test if value can be parsed
58+
*/
59+
public function testParseValue()
60+
{
61+
$type = $this->getType();
62+
63+
self::assertSame("VAL2", $type->parseValue("VAL2"));
64+
65+
self::expectException(GraphQLError::class);
66+
$type->parseValue(42);
67+
}
68+
}

tests/Types/GraphQLFloatTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
use \PHPUnit\Framework\TestCase;
4+
use \GraphQL\Types\GraphQLFloat;
5+
use \GraphQL\Errors\GraphQLError;
6+
7+
class GraphQLFloatTest extends TestCase
8+
{
9+
10+
public function getType()
11+
{
12+
return new GraphQLFloat();
13+
}
14+
15+
/**
16+
* Allows us to test if input can be serialized by the type
17+
*/
18+
public function testSerialize()
19+
{
20+
$type = $this->getType();
21+
22+
self::assertSame(42.24, $type->serialize(42.24));
23+
24+
self::expectException(GraphQLError::class);
25+
$type->serialize("42.24");
26+
}
27+
28+
/**
29+
* Allows us to test if string input can be literalized by the type
30+
*/
31+
public function testParseLiteral()
32+
{
33+
$type = $this->getType();
34+
35+
$node = [
36+
"kind" => "FloatValue",
37+
"value" => "42.24"
38+
];
39+
40+
self::assertSame(42.24, $type->parseLiteral($node, null));
41+
42+
$node = [
43+
"kind" => "FloatValue",
44+
"value" => "hello world!"
45+
];
46+
47+
self::expectException(GraphQLError::class);
48+
$type->parseLiteral($node, null);
49+
}
50+
51+
52+
/**
53+
* Allows us to test if value can be parsed
54+
*/
55+
public function testParseValue()
56+
{
57+
$type = $this->getType();
58+
59+
self::assertSame(42.24, $type->parseValue(42.24));
60+
61+
self::expectException(GraphQLError::class);
62+
$type->parseValue("42.24");
63+
}
64+
}

0 commit comments

Comments
 (0)