mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 22:36:02 +03:00
d22385cc93
specification. The framework currently coerces query variables similar to the way it treats output values, which means it attempts to coerce the value into the field's corresponding data type regardless of the received value. According to items 3f and 3g in section 6.1.2 (http://facebook.github.io/graphql/#sec-Validating-Requests) of Facebook's GraphQL specification query variables should be coerced according to their type's input coercion rules laid out in section 3.1.1 (http://facebook.github.io/graphql/#sec-Scalars). If the value can not be coerced into the correct type according the the input coercion rules for the type a query error should be thrown. This ensures that client provided query variables were of the correct format and will be a valid format and type by the time they are passed into an implementing resolver. This patch fixes the above issue by updating the way query variables are sanitized during the process of parsing the query. It directly follows the rules for scalar input coercion laid out by the specification and throws query errors when a value that cannot be coerced to the correct type is given. Tests for isValidPHPValue will also be updated to ensure that it is doing the correct type checks on Values::isValidPHPValue for the given type and value provided. A new test case will also be added to test Values::getVariableValues and make sure it is also enforcing the scalar input coercion rules and throwing errors for invalid values.
133 lines
4.8 KiB
PHP
133 lines
4.8 KiB
PHP
<?php
|
|
namespace GraphQL\Tests\Utils;
|
|
|
|
|
|
use GraphQL\Executor\Values;
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
class IsValidPHPValueTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
public function testValidIntValue()
|
|
{
|
|
// returns no error for positive int value
|
|
$result = Values::isValidPHPValue(1, Type::int());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for negative int value
|
|
$result = Values::isValidPHPValue(-1, Type::int());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for null value
|
|
$result = Values::isValidPHPValue(null, Type::int());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns a single error for positive int string value
|
|
$result = Values::isValidPHPValue('1', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for negative int string value
|
|
$result = Values::isValidPHPValue('-1', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns errors for exponential int string value
|
|
$result = Values::isValidPHPValue('1e3', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
$result = Values::isValidPHPValue('0e3', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for empty value
|
|
$result = Values::isValidPHPValue('', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns error for float value
|
|
$result = Values::isValidPHPValue(1.5, Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
$result = Values::isValidPHPValue(1e3, Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns error for float string value
|
|
$result = Values::isValidPHPValue('1.5', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for char input
|
|
$result = Values::isValidPHPValue('a', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for char input
|
|
$result = Values::isValidPHPValue('meow', Type::int());
|
|
$this->expectErrorResult($result, 1);
|
|
}
|
|
|
|
public function testValidFloatValue()
|
|
{
|
|
// returns no error for positive float value
|
|
$result = Values::isValidPHPValue(1.2, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for exponential float value
|
|
$result = Values::isValidPHPValue(1e3, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for negative float value
|
|
$result = Values::isValidPHPValue(-1.2, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for a positive int value
|
|
$result = Values::isValidPHPValue(1, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no errors for a negative int value
|
|
$result = Values::isValidPHPValue(-1, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns no error for null value:
|
|
$result = Values::isValidPHPValue(null, Type::float());
|
|
$this->expectNoErrors($result);
|
|
|
|
// returns error for positive float string value
|
|
$result = Values::isValidPHPValue('1.2', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns error for negative float string value
|
|
$result = Values::isValidPHPValue('-1.2', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns error for a positive int string value
|
|
$result = Values::isValidPHPValue('1', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns errors for a negative int string value
|
|
$result = Values::isValidPHPValue('-1', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns error for exponent input
|
|
$result = Values::isValidPHPValue('1e3', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
$result = Values::isValidPHPValue('0e3', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for empty value
|
|
$result = Values::isValidPHPValue('', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for char input
|
|
$result = Values::isValidPHPValue('a', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
|
|
// returns a single error for char input
|
|
$result = Values::isValidPHPValue('meow', Type::float());
|
|
$this->expectErrorResult($result, 1);
|
|
}
|
|
|
|
private function expectNoErrors($result)
|
|
{
|
|
$this->assertInternalType('array', $result);
|
|
$this->assertEquals([], $result);
|
|
}
|
|
|
|
private function expectErrorResult($result, $size) {
|
|
$this->assertInternalType('array', $result);
|
|
$this->assertEquals($size, count($result));
|
|
}
|
|
}
|