Skip to content

Commit 14e42c7

Browse files
committed
GQL-47: Completed tests for newly added functionality
- Added tests for the Variable class - Added tests for adding variables to Query objects - Added tests for setting variables using the QueryBuilder object
1 parent 1f59ccc commit 14e42c7

File tree

7 files changed

+294
-35
lines changed

7 files changed

+294
-35
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
66

77
- Add support for variables attachment to query
88
- Add support for variables passing query
9+
- Support for input objects variables
910

1011
## 1.1: 2019-04-26
1112

src/Query.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GraphQL;
44

5+
use Exception;
56
use GraphQL\Exception\ArgumentException;
67
use GraphQL\Exception\InvalidSelectionException;
78
use GraphQL\Exception\InvalidVariableException;
@@ -110,7 +111,7 @@ public function setVariables(array $variables)
110111
return !$e instanceof Variable;
111112
});
112113
if (count($nonVarElements) > 0) {
113-
throw new InvalidVariableException('One or more of the elements of the variables array provided is not an instance of GraphQL\\Variable');
114+
throw new InvalidVariableException('At least one of the elements of the variables array provided is not an instance of GraphQL\\Variable');
114115
}
115116

116117
$this->variables = $variables;

src/QueryBuilder/QueryBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ public function setArgument(string $argumentName, $argumentValue)
3838
}
3939

4040
/**
41+
* Changing method visibility to public
42+
*
4143
* @param Variable $variable
4244
*
43-
* @return AbstractQueryBuilder
45+
* @return AbstractQueryBuilder|QueryBuilder
4446
*/
4547
public function setVariable(Variable $variable)
4648
{

src/Variable.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace GraphQL;
44

5+
use GraphQL\Util\StringLiteralFormatter;
6+
57
/**
68
* Class Variable
79
*
@@ -45,38 +47,6 @@ public function __construct(string $name, string $type, bool $isRequired = false
4547
$this->defaultValue = $defaultValue;
4648
}
4749

48-
/**
49-
* @return string
50-
*/
51-
public function getName(): string
52-
{
53-
return $this->name;
54-
}
55-
56-
/**
57-
* @return string
58-
*/
59-
public function getType(): string
60-
{
61-
return $this->type;
62-
}
63-
64-
/**
65-
* @return bool
66-
*/
67-
public function isRequired(): bool
68-
{
69-
return $this->required;
70-
}
71-
72-
/**
73-
* @return bool|float|int|string|null
74-
*/
75-
public function getDefaultValue()
76-
{
77-
return $this->defaultValue;
78-
}
79-
8050
/**
8151
* @return string
8252
*/
@@ -86,7 +56,7 @@ public function __toString(): string
8656
if ($this->required) {
8757
$varString .= '!';
8858
} elseif (!empty($this->defaultValue)) {
89-
$varString .= "=$this->defaultValue";
59+
$varString .= '=' . StringLiteralFormatter::formatValueForRHS($this->defaultValue);
9060
}
9161

9262
return $varString;

tests/QueryBuilderTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GraphQL\Query;
77
use GraphQL\QueryBuilder\QueryBuilder;
88
use GraphQL\RawObject;
9+
use GraphQL\Variable;
910
use PHPUnit\Framework\TestCase;
1011

1112
/**
@@ -22,6 +23,9 @@ class QueryBuilderTest extends TestCase
2223
*/
2324
protected $queryBuilder;
2425

26+
/**
27+
*
28+
*/
2529
public function setUp(): void
2630
{
2731
$this->queryBuilder = new QueryBuilder('Object');
@@ -56,6 +60,53 @@ public function testEmptySelectionSet()
5660
$this->queryBuilder->getQuery();
5761
}
5862

63+
/**
64+
* @covers \GraphQL\QueryBuilder\QueryBuilder::setVariable
65+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::setVariable
66+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::getQuery
67+
*/
68+
public function testAddVariables()
69+
{
70+
$this->queryBuilder
71+
->setVariable(new Variable('var', 'String'))
72+
->setVariable(new Variable('intVar', 'Int', false, 4))
73+
->selectField('fieldOne');
74+
$this->assertEquals(
75+
'query($var: String $intVar: Int=4) {
76+
Object {
77+
fieldOne
78+
}
79+
}',
80+
(string) $this->queryBuilder->getQuery()
81+
);
82+
}
83+
84+
/**
85+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::getQuery
86+
*/
87+
public function testAddVariablesToSecondLevelQueryDoesNothing()
88+
{
89+
$this->queryBuilder
90+
->setVariable(new Variable('var', 'String'))
91+
->selectField('fieldOne')
92+
->selectField(
93+
(new QueryBuilder('Nested'))
94+
->setVariable(new Variable('var', 'String'))
95+
->selectField('fieldTwo')
96+
);
97+
$this->assertEquals(
98+
'query($var: String) {
99+
Object {
100+
fieldOne
101+
Nested {
102+
fieldTwo
103+
}
104+
}
105+
}',
106+
(string) $this->queryBuilder->getQuery()
107+
);
108+
}
109+
59110
/**
60111
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
61112
* @covers \GraphQL\QueryBuilder\QueryBuilder::selectField
@@ -98,6 +149,10 @@ public function testSelectNestedQuery()
98149
);
99150
}
100151

152+
/**
153+
* @covers \GraphQL\QueryBuilder\QueryBuilder::getQuery
154+
* @covers \GraphQL\QueryBuilder\QueryBuilder::selectField
155+
*/
101156
public function testSelectNestedQueryBuilder()
102157
{
103158
$this->queryBuilder->selectField(

tests/QueryTest.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use GraphQL\Exception\ArgumentException;
66
use GraphQL\Exception\InvalidSelectionException;
7+
use GraphQL\Exception\InvalidVariableException;
78
use GraphQL\Query;
89
use GraphQL\RawObject;
10+
use GraphQL\Variable;
911
use PHPUnit\Framework\TestCase;
1012

1113
/**
@@ -73,6 +75,165 @@ public function testQueryWithOperationType()
7375
);
7476
}
7577

78+
/**
79+
* @depends testConvertsToString
80+
*
81+
* @covers \GraphQL\Query::generateSignature
82+
* @covers \GraphQL\Query::setOperationName
83+
* @covers \GraphQL\Query::__toString
84+
*/
85+
public function testQueryWithOperationName()
86+
{
87+
$query = (new Query('Object'))
88+
->setOperationName('retrieveObject');
89+
$this->assertEquals(
90+
'query retrieveObject {
91+
Object {
92+
93+
}
94+
}',
95+
(string) $query
96+
);
97+
}
98+
99+
// public function testQueryWithOperationNameAndOperationType()
100+
// {
101+
// $query = (new Query('query'))
102+
// ->setOperationName('retrieveObject')
103+
// ->setSelectionSet([new Query('Object')]);
104+
// $this->assertEquals(
105+
// 'query retrieveObject {
106+
//Object {
107+
//
108+
//}
109+
//}',
110+
// (string) $query
111+
// );
112+
// }
113+
114+
/**
115+
* @depends testQueryWithOperationName
116+
*
117+
* @covers \GraphQL\Query::generateSignature
118+
* @covers \GraphQL\Query::setOperationName
119+
* @covers \GraphQL\Query::__toString
120+
*/
121+
public function testQueryWithOperationNameInSecondLevelDoesNothing()
122+
{
123+
$query = (new Query('Object'))
124+
->setOperationName('retrieveObject')
125+
->setSelectionSet([(new Query('Nested'))->setOperationName('opName')]);
126+
$this->assertEquals(
127+
'query retrieveObject {
128+
Object {
129+
Nested {
130+
131+
}
132+
}
133+
}',
134+
(string) $query
135+
);
136+
}
137+
138+
/**
139+
* @covers \GraphQL\Query::setVariables
140+
* @covers \GraphQL\Exception\InvalidVariableException
141+
*/
142+
public function testSetVariablesWithoutVariableObjects()
143+
{
144+
$this->expectException(InvalidVariableException::class);
145+
(new Query('Object'))->setVariables(['one', 'two']);
146+
}
147+
148+
/**
149+
* @depends testConvertsToString
150+
*
151+
* @covers \GraphQL\Query::setVariables
152+
* @covers \GraphQL\Query::generateSignature
153+
* @covers \GraphQL\Query::constructVariables
154+
* @covers \GraphQL\Query::__toString
155+
*/
156+
public function testQueryWithOneVariable()
157+
{
158+
$query = (new Query('Object'))
159+
->setVariables([new Variable('var', 'String')]);
160+
$this->assertEquals(
161+
'query($var: String) {
162+
Object {
163+
164+
}
165+
}',
166+
(string) $query
167+
);
168+
}
169+
170+
/**
171+
* @depends testQueryWithOneVariable
172+
*
173+
* @covers \GraphQL\Query::setVariables
174+
* @covers \GraphQL\Query::generateSignature
175+
* @covers \GraphQL\Query::constructVariables
176+
* @covers \GraphQL\Query::__toString
177+
*/
178+
public function testQueryWithMultipleVariables()
179+
{
180+
$query = (new Query('Object'))
181+
->setVariables([new Variable('var', 'String'), new Variable('intVar', 'Int', false, 4)]);
182+
$this->assertEquals(
183+
'query($var: String $intVar: Int=4) {
184+
Object {
185+
186+
}
187+
}',
188+
(string) $query
189+
);
190+
}
191+
192+
/**
193+
* @depends testConvertsToString
194+
*
195+
* @covers \GraphQL\Query::__toString
196+
*/
197+
public function testQueryWithVariablesInSecondLevelDoesNothing()
198+
{
199+
$query = (new Query('Object'))
200+
->setVariables([new Variable('var', 'String'), new Variable('intVar', 'Int', false, 4)])
201+
->setSelectionSet([(new Query('Nested'))])
202+
->setVariables([new Variable('var', 'String'), new Variable('intVar', 'Int', false, 4)]);
203+
$this->assertEquals(
204+
'query($var: String $intVar: Int=4) {
205+
Object {
206+
Nested {
207+
208+
}
209+
}
210+
}',
211+
(string) $query
212+
);
213+
}
214+
215+
/**
216+
* @depends testQueryWithMultipleVariables
217+
* @depends testQueryWithOperationName
218+
*
219+
* @covers \GraphQL\Query::generateSignature
220+
* @covers \GraphQL\Query::__toString
221+
*/
222+
public function testQueryWithOperationNameAndVariables()
223+
{
224+
$query = (new Query('Object'))
225+
->setOperationName('retrieveObject')
226+
->setVariables([new Variable('var', 'String')]);
227+
$this->assertEquals(
228+
'query retrieveObject($var: String) {
229+
Object {
230+
231+
}
232+
}',
233+
(string) $query
234+
);
235+
}
236+
76237
/**
77238
* @depends clone testEmptyArguments
78239
*

0 commit comments

Comments
 (0)