1+ <?php
2+ namespace GraphQL \Tests \Executor ;
3+
4+ use GraphQL \Executor \Values ;
5+ use GraphQL \Language \AST \NamedTypeNode ;
6+ use GraphQL \Language \AST \NameNode ;
7+ use GraphQL \Language \AST \VariableDefinitionNode ;
8+ use GraphQL \Language \AST \VariableNode ;
9+ use GraphQL \Type \Definition \ObjectType ;
10+ use GraphQL \Type \Definition \Type ;
11+ use GraphQL \Type \Schema ;
12+
13+ class ValuesTest extends \PHPUnit_Framework_Testcase {
14+
15+ public function testGetIDVariableValues ()
16+ {
17+ $ this ->expectInputVariablesMatchOutputVariables (['idInput ' => '123456789 ' ]);
18+ $ this ->assertEquals (
19+ ['idInput ' => '123456789 ' ],
20+ self ::runTestCase (['idInput ' => 123456789 ]),
21+ 'Integer ID was not converted to string '
22+ );
23+ }
24+
25+ public function testGetBooleanVariableValues ()
26+ {
27+ $ this ->expectInputVariablesMatchOutputVariables (['boolInput ' => true ]);
28+ $ this ->expectInputVariablesMatchOutputVariables (['boolInput ' => false ]);
29+ }
30+
31+ public function testGetIntVariableValues ()
32+ {
33+ $ this ->expectInputVariablesMatchOutputVariables (['intInput ' => -1 ]);
34+ $ this ->expectInputVariablesMatchOutputVariables (['intInput ' => 0 ]);
35+ $ this ->expectInputVariablesMatchOutputVariables (['intInput ' => 1 ]);
36+
37+ // Test the int size limit
38+ $ this ->expectInputVariablesMatchOutputVariables (['intInput ' => 2147483647 ]);
39+ $ this ->expectInputVariablesMatchOutputVariables (['intInput ' => -2147483648 ]);
40+ }
41+
42+ public function testGetStringVariableValues ()
43+ {
44+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => 'meow ' ]);
45+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => '' ]);
46+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => '1 ' ]);
47+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => '0 ' ]);
48+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => 'false ' ]);
49+ $ this ->expectInputVariablesMatchOutputVariables (['stringInput ' => '1.2 ' ]);
50+ }
51+
52+ public function testGetFloatVariableValues ()
53+ {
54+ $ this ->expectInputVariablesMatchOutputVariables (['floatInput ' => 1.2 ]);
55+ $ this ->expectInputVariablesMatchOutputVariables (['floatInput ' => 1.0 ]);
56+ $ this ->expectInputVariablesMatchOutputVariables (['floatInput ' => 1 ]);
57+ $ this ->expectInputVariablesMatchOutputVariables (['floatInput ' => 0 ]);
58+ $ this ->expectInputVariablesMatchOutputVariables (['floatInput ' => 1e3 ]);
59+ }
60+
61+ public function testBooleanForIDVariableThrowsError ()
62+ {
63+ $ this ->expectGraphQLError (['idInput ' => true ]);
64+ }
65+
66+ public function testFloatForIDVariableThrowsError ()
67+ {
68+ $ this ->expectGraphQLError (['idInput ' => 1.0 ]);
69+ }
70+
71+ public function testStringForBooleanVariableThrowsError ()
72+ {
73+ $ this ->expectGraphQLError (['boolInput ' => 'true ' ]);
74+ }
75+
76+ public function testIntForBooleanVariableThrowsError ()
77+ {
78+ $ this ->expectGraphQLError (['boolInput ' => 1 ]);
79+ }
80+
81+ public function testFloatForBooleanVariableThrowsError ()
82+ {
83+ $ this ->expectGraphQLError (['boolInput ' => 1.0 ]);
84+ }
85+
86+ public function testBooleanForIntVariableThrowsError ()
87+ {
88+ $ this ->expectGraphQLError (['intInput ' => true ]);
89+ }
90+
91+ public function testStringForIntVariableThrowsError ()
92+ {
93+ $ this ->expectGraphQLError (['intInput ' => 'true ' ]);
94+ }
95+
96+ public function testFloatForIntVariableThrowsError ()
97+ {
98+ $ this ->expectGraphQLError (['intInput ' => 1.0 ]);
99+ }
100+
101+ public function testPositiveBigIntForIntVariableThrowsError ()
102+ {
103+ $ this ->expectGraphQLError (['intInput ' => 2147483648 ]);
104+ }
105+
106+ public function testNegativeBigIntForIntVariableThrowsError ()
107+ {
108+ $ this ->expectGraphQLError (['intInput ' => -2147483649 ]);
109+ }
110+
111+ public function testBooleanForStringVariableThrowsError ()
112+ {
113+ $ this ->expectGraphQLError (['stringInput ' => true ]);
114+ }
115+
116+ public function testIntForStringVariableThrowsError ()
117+ {
118+ $ this ->expectGraphQLError (['stringInput ' => 1 ]);
119+ }
120+
121+ public function testFloatForStringVariableThrowsError ()
122+ {
123+ $ this ->expectGraphQLError (['stringInput ' => 1.0 ]);
124+ }
125+
126+ public function testBooleanForFloatVariableThrowsError ()
127+ {
128+ $ this ->expectGraphQLError (['floatInput ' => true ]);
129+ }
130+
131+ public function testStringForFloatVariableThrowsError ()
132+ {
133+ $ this ->expectGraphQLError (['floatInput ' => '1.0 ' ]);
134+ }
135+
136+ // Helpers for running test cases and making assertions
137+
138+ private function expectInputVariablesMatchOutputVariables ($ variables )
139+ {
140+ $ this ->assertEquals (
141+ $ variables ,
142+ self ::runTestCase ($ variables ),
143+ 'Output variables did not match input variables ' . PHP_EOL . var_export ($ variables , true ) . PHP_EOL
144+ );
145+ }
146+
147+ private function expectGraphQLError ($ variables )
148+ {
149+ $ this ->setExpectedException (\GraphQL \Error \Error::class);
150+ self ::runTestCase ($ variables );
151+ }
152+
153+ private static $ schema ;
154+
155+ private static function getSchema ()
156+ {
157+ if (!self ::$ schema ) {
158+ self ::$ schema = new Schema ([
159+ 'query ' => new ObjectType ([
160+ 'name ' => 'Query ' ,
161+ 'fields ' => [
162+ 'test ' => [
163+ 'type ' => Type::boolean (),
164+ 'args ' => [
165+ 'idInput ' => Type::id (),
166+ 'boolInput ' => Type::boolean (),
167+ 'intInput ' => Type::int (),
168+ 'stringInput ' => Type::string (),
169+ 'floatInput ' => Type::float ()
170+ ]
171+ ],
172+ ]
173+ ])
174+ ]);
175+ }
176+ return self ::$ schema ;
177+ }
178+
179+ private static function getVariableDefinitionNodes ()
180+ {
181+ $ idInputDefinition = new VariableDefinitionNode ([
182+ 'variable ' => new VariableNode (['name ' => new NameNode (['value ' => 'idInput ' ])]),
183+ 'type ' => new NamedTypeNode (['name ' => new NameNode (['value ' => 'ID ' ])])
184+ ]);
185+ $ boolInputDefinition = new VariableDefinitionNode ([
186+ 'variable ' => new VariableNode (['name ' => new NameNode (['value ' => 'boolInput ' ])]),
187+ 'type ' => new NamedTypeNode (['name ' => new NameNode (['value ' => 'Boolean ' ])])
188+ ]);
189+ $ intInputDefinition = new VariableDefinitionNode ([
190+ 'variable ' => new VariableNode (['name ' => new NameNode (['value ' => 'intInput ' ])]),
191+ 'type ' => new NamedTypeNode (['name ' => new NameNode (['value ' => 'Int ' ])])
192+ ]);
193+ $ stringInputDefintion = new VariableDefinitionNode ([
194+ 'variable ' => new VariableNode (['name ' => new NameNode (['value ' => 'stringInput ' ])]),
195+ 'type ' => new NamedTypeNode (['name ' => new NameNode (['value ' => 'String ' ])])
196+ ]);
197+ $ floatInputDefinition = new VariableDefinitionNode ([
198+ 'variable ' => new VariableNode (['name ' => new NameNode (['value ' => 'floatInput ' ])]),
199+ 'type ' => new NamedTypeNode (['name ' => new NameNode (['value ' => 'Float ' ])])
200+ ]);
201+ return [$ idInputDefinition , $ boolInputDefinition , $ intInputDefinition , $ stringInputDefintion , $ floatInputDefinition ];
202+ }
203+
204+ private function runTestCase ($ variables )
205+ {
206+ return Values::getVariableValues (self ::getSchema (), self ::getVariableDefinitionNodes (), $ variables );
207+ }
208+ }
0 commit comments