Skip to content

Commit 1e34982

Browse files
committed
Additional tests for variable coercion + use printSafeJson vs printSafe for input variables
1 parent a1e06b2 commit 1e34982

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/Executor/Values.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,20 @@ public static function isValidPHPValue($value, InputType $type)
280280
// a non-null value.
281281
$parseResult = $type->parseValue($value);
282282
if (null === $parseResult && !$type->isValidValue($value)) {
283-
$v = Utils::printSafe($value);
283+
$v = Utils::printSafeJson($value);
284284
return [
285285
"Expected type \"{$type->name}\", found $v."
286286
];
287287
}
288288
return [];
289289
} catch (\Exception $e) {
290290
return [
291-
"Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' .
291+
"Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' .
292292
$e->getMessage()
293293
];
294294
} catch (\Throwable $e) {
295295
return [
296-
"Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' .
296+
"Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' .
297297
$e->getMessage()
298298
];
299299
}

tests/Executor/VariablesTest.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
require_once __DIR__ . '/TestClasses.php';
55

6-
use GraphQL\Error\Error;
6+
use GraphQL\Error\InvariantViolation;
77
use GraphQL\Executor\Executor;
8-
use GraphQL\Error\FormattedError;
98
use GraphQL\Language\Parser;
10-
use GraphQL\Language\SourceLocation;
11-
use GraphQL\Schema;
9+
use GraphQL\Type\Schema;
1210
use GraphQL\Type\Definition\InputObjectType;
1311
use GraphQL\Type\Definition\ObjectType;
1412
use GraphQL\Type\Definition\Type;
@@ -467,6 +465,47 @@ public function testReportsErrorForMissingNonNullableInputs()
467465
$this->assertEquals($expected, Executor::execute($this->schema(), $ast)->toArray());
468466
}
469467

468+
/**
469+
* @it reports error for array passed into string input
470+
*/
471+
public function testReportsErrorForArrayPassedIntoStringInput()
472+
{
473+
474+
$doc = '
475+
query SetsNonNullable($value: String!) {
476+
fieldWithNonNullableStringInput(input: $value)
477+
}
478+
';
479+
$ast = Parser::parse($doc);
480+
$variables = ['value' => [1, 2, 3]];
481+
482+
$expected = [
483+
'errors' => [[
484+
'message' =>
485+
'Variable "$value" got invalid value [1,2,3].' . "\n" .
486+
'Expected type "String", found array(3).',
487+
'category' => 'graphql',
488+
'locations' => [
489+
['line' => 2, 'column' => 31]
490+
]
491+
]]
492+
];
493+
494+
$this->assertEquals($expected, Executor::execute($this->schema(), $ast, null, null, $variables)->toArray());
495+
}
496+
497+
/**
498+
* @it serializing an array via GraphQLString throws TypeError
499+
*/
500+
public function testSerializingAnArrayViaGraphQLStringThrowsTypeError()
501+
{
502+
$this->setExpectedException(
503+
InvariantViolation::class,
504+
'String cannot represent non scalar value: array(3)'
505+
);
506+
Type::string()->serialize([1, 2, 3]);
507+
}
508+
470509
/**
471510
* @it reports error for non-provided variables for non-nullable inputs
472511
*/

0 commit comments

Comments
 (0)