| Conditions | 1 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 135 | public function testArgumentsValidation() |
||
| 136 | { |
||
| 137 | $variable = new Variable('year', 'Int'); |
||
| 138 | $variableWrongType = new Variable('year', 'String'); |
||
| 139 | $variable->setValue(2016); |
||
| 140 | |||
| 141 | |||
| 142 | $field = new Field([ |
||
| 143 | 'name' => 'hero', |
||
| 144 | 'type' => new ObjectType([ |
||
| 145 | 'name' => 'User', |
||
| 146 | 'fields' => [ |
||
| 147 | 'name' => new StringType() |
||
| 148 | ] |
||
| 149 | ]), |
||
| 150 | 'args' => [ |
||
| 151 | 'planet' => new StringType(), |
||
| 152 | 'year' => new IntType(), |
||
| 153 | ] |
||
| 154 | ]); |
||
| 155 | $validator = new ResolveValidator(new ExecutionContext()); |
||
| 156 | $request = new Request([]); |
||
| 157 | |||
| 158 | |||
| 159 | $validQuery = new Query('hero', null, [ |
||
| 160 | new Argument('planet', new Literal('earth')) |
||
| 161 | ]); |
||
| 162 | $invalidArgumentQuery = new Query('hero', null, [ |
||
| 163 | new Argument('planets', new Literal('earth')) |
||
| 164 | ]); |
||
| 165 | $invalidArgumentTypeQuery = new Query('hero', null, [ |
||
| 166 | new Argument('year', new Literal('invalid type')) |
||
| 167 | ]); |
||
| 168 | $argumentWithVariable = new Query('hero', null, [ |
||
| 169 | new Argument('year', $variable) |
||
| 170 | ]); |
||
| 171 | $argumentWithVariableWrongType = new Query('hero', null, [ |
||
| 172 | new Argument('year', $variableWrongType) |
||
| 173 | ]); |
||
| 174 | |||
| 175 | |||
| 176 | $this->assertFalse($validator->getExecutionContext()->hasErrors()); |
||
| 177 | |||
| 178 | $validator->validateArguments($field, $validQuery, $request); |
||
| 179 | $this->assertFalse($validator->getExecutionContext()->hasErrors()); |
||
| 180 | |||
| 181 | $validator->validateArguments($field, $invalidArgumentQuery, $request); |
||
| 182 | $this->assertEquals([ |
||
| 183 | ['message' => 'Unknown argument "planets" on field "hero"'] |
||
| 184 | ], $validator->getExecutionContext()->getErrorsArray()); |
||
| 185 | $validator->getExecutionContext()->clearErrors(); |
||
| 186 | |||
| 187 | $validator->validateArguments($field, $invalidArgumentTypeQuery, $request); |
||
| 188 | $this->assertEquals([ |
||
| 189 | ['message' => 'Not valid type for argument "year" in query "hero"'] |
||
| 190 | ], $validator->getExecutionContext()->getErrorsArray()); |
||
| 191 | $validator->getExecutionContext()->clearErrors(); |
||
| 192 | |||
| 193 | $validator->validateArguments($field, $argumentWithVariable, $request); |
||
| 194 | $this->assertEquals([ |
||
| 195 | ['message' => 'Variable "year" does not exist for query "hero"'] |
||
| 196 | ], $validator->getExecutionContext()->getErrorsArray()); |
||
| 197 | $validator->getExecutionContext()->clearErrors(); |
||
| 198 | |||
| 199 | $request->setVariables(['year' => '2016']); |
||
| 200 | $validator->validateArguments($field, $argumentWithVariable, $request); |
||
| 201 | $this->assertFalse($validator->getExecutionContext()->hasErrors()); |
||
| 202 | |||
| 203 | $validator->validateArguments($field, $argumentWithVariableWrongType, $request); |
||
| 204 | $this->assertEquals([ |
||
| 205 | ['message' => 'Invalid variable "year" type, allowed type is "Int"'] |
||
| 206 | ], $validator->getExecutionContext()->getErrorsArray()); |
||
| 207 | $validator->getExecutionContext()->clearErrors(); |
||
| 208 | |||
| 209 | |||
| 210 | } |
||
| 211 | } |
||
| 212 |