Additional tests for variable coercion + use printSafeJson vs printSafe for input variables

This commit is contained in:
Vladimir Razuvaev 2017-09-20 18:40:45 +07:00
parent a1e06b2e61
commit 1e34982bda
2 changed files with 46 additions and 7 deletions

View File

@ -280,7 +280,7 @@ class Values
// a non-null value.
$parseResult = $type->parseValue($value);
if (null === $parseResult && !$type->isValidValue($value)) {
$v = Utils::printSafe($value);
$v = Utils::printSafeJson($value);
return [
"Expected type \"{$type->name}\", found $v."
];
@ -288,12 +288,12 @@ class Values
return [];
} catch (\Exception $e) {
return [
"Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' .
"Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' .
$e->getMessage()
];
} catch (\Throwable $e) {
return [
"Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' .
"Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' .
$e->getMessage()
];
}

View File

@ -3,12 +3,10 @@ namespace GraphQL\Tests\Executor;
require_once __DIR__ . '/TestClasses.php';
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Executor;
use GraphQL\Error\FormattedError;
use GraphQL\Language\Parser;
use GraphQL\Language\SourceLocation;
use GraphQL\Schema;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
@ -467,6 +465,47 @@ class VariablesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, Executor::execute($this->schema(), $ast)->toArray());
}
/**
* @it reports error for array passed into string input
*/
public function testReportsErrorForArrayPassedIntoStringInput()
{
$doc = '
query SetsNonNullable($value: String!) {
fieldWithNonNullableStringInput(input: $value)
}
';
$ast = Parser::parse($doc);
$variables = ['value' => [1, 2, 3]];
$expected = [
'errors' => [[
'message' =>
'Variable "$value" got invalid value [1,2,3].' . "\n" .
'Expected type "String", found array(3).',
'category' => 'graphql',
'locations' => [
['line' => 2, 'column' => 31]
]
]]
];
$this->assertEquals($expected, Executor::execute($this->schema(), $ast, null, null, $variables)->toArray());
}
/**
* @it serializing an array via GraphQLString throws TypeError
*/
public function testSerializingAnArrayViaGraphQLStringThrowsTypeError()
{
$this->setExpectedException(
InvariantViolation::class,
'String cannot represent non scalar value: array(3)'
);
Type::string()->serialize([1, 2, 3]);
}
/**
* @it reports error for non-provided variables for non-nullable inputs
*/