graphql-php/tests/Utils/IsValidLiteralValueTest.php
Daniel Tschinder 58e0c7a178 Validate literals in a single rule with finer precision
This generalizes the "arguments of correct type" and "default values of correct type" to a single rule "values of correct type" which has been re-written to rely on a traversal rather than the utility function `isValidLiteralValue`. To reduce breaking scope, this does not remove that utility even though it's no longer used directly within the library. Since the default values rule included another validation rule that rule was renamed to a more apt "variable default value allowed".

This also includes the original errors from custom scalars in the validation error output, solving the remainder of graphql/graphql-js#821.

ref: graphql/graphql-js#1144
2018-02-15 21:29:14 +01:00

38 lines
1.1 KiB
PHP

<?php
namespace GraphQL\Tests\Utils;
use GraphQL\Language\Parser;
use GraphQL\Language\SourceLocation;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\Utils;
use GraphQL\Validator\DocumentValidator;
class IsValidLiteralValueTest extends \PHPUnit_Framework_TestCase
{
// DESCRIBE: isValidLiteralValue
/**
* @it Returns no errors for a valid value
*/
public function testReturnsNoErrorsForAValidValue()
{
$this->assertEquals(
[],
DocumentValidator::isValidLiteralValue(Type::int(), Parser::parseValue('123'))
);
}
/**
* @it Returns errors for an invalid value
*/
public function testReturnsErrorsForForInvalidValue()
{
$errors = DocumentValidator::isValidLiteralValue(Type::int(), Parser::parseValue('"abc"'));
$this->assertCount(1, $errors);
$this->assertEquals('Expected type Int, found "abc".', $errors[0]->getMessage());
$this->assertEquals([new SourceLocation(1, 1)], $errors[0]->getLocations());
$this->assertEquals(null, $errors[0]->getPath());
}
}