|
4 | 4 |
|
5 | 5 | use GraphQL\Exception\ArgumentException; |
6 | 6 | use GraphQL\Exception\InvalidSelectionException; |
| 7 | +use GraphQL\Exception\InvalidVariableException; |
7 | 8 | use GraphQL\Query; |
8 | 9 | use GraphQL\RawObject; |
| 10 | +use GraphQL\Variable; |
9 | 11 | use PHPUnit\Framework\TestCase; |
10 | 12 |
|
11 | 13 | /** |
@@ -73,6 +75,165 @@ public function testQueryWithOperationType() |
73 | 75 | ); |
74 | 76 | } |
75 | 77 |
|
| 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 | + |
76 | 237 | /** |
77 | 238 | * @depends clone testEmptyArguments |
78 | 239 | * |
|
0 commit comments