2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Executor;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
require_once __DIR__ . '/TestClasses.php';
|
|
|
|
|
2018-02-13 18:51:44 +03:00
|
|
|
use GraphQL\Error\Error;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Executor\Executor;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\Parser;
|
2017-09-20 14:40:45 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class VariablesTest extends TestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
// Execute: Handles inputs
|
2015-07-15 20:05:46 +03:00
|
|
|
// Handles objects and nullability
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
|
|
|
* @describe using inline structs
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsingInlineStructs()
|
|
|
|
{
|
|
|
|
// executes with complex input:
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"})
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}'
|
|
|
|
]
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
// properly parses single value to list:
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"})
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-11-18 19:59:28 +03:00
|
|
|
|
|
|
|
// properly parses null value to null
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2016-11-18 19:59:28 +03:00
|
|
|
{
|
|
|
|
fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null})
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-11-18 19:59:28 +03:00
|
|
|
$expected = ['data' => ['fieldWithObjectInput' => '{"a":null,"b":null,"c":"C","d":null}']];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-11-18 19:59:28 +03:00
|
|
|
|
|
|
|
// properly parses null value in list
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2016-11-18 19:59:28 +03:00
|
|
|
{
|
|
|
|
fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"})
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-11-18 19:59:28 +03:00
|
|
|
$expected = ['data' => ['fieldWithObjectInput' => '{"b":["A",null,"C"],"c":"C"}']];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
// does not use incorrect value
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
fieldWithObjectInput(input: ["foo", "bar", "baz"])
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-08-17 17:01:55 +03:00
|
|
|
|
|
|
|
$expected = [
|
2016-11-19 00:15:40 +03:00
|
|
|
'data' => ['fieldWithObjectInput' => null],
|
|
|
|
'errors' => [[
|
2018-02-15 23:29:14 +03:00
|
|
|
'message' => 'Argument "input" has invalid value ["foo", "bar", "baz"].',
|
|
|
|
'path' => ['fieldWithObjectInput'],
|
|
|
|
'locations' => [['line' => 3, 'column' => 39]]
|
2016-11-19 00:15:40 +03:00
|
|
|
]]
|
2015-08-17 17:01:55 +03:00
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertArraySubset($expected, $result->toArray());
|
2016-05-02 00:42:05 +03:00
|
|
|
|
|
|
|
// properly runs parseLiteral on complex scalar types
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2016-05-02 00:42:05 +03:00
|
|
|
{
|
2016-11-19 00:15:40 +03:00
|
|
|
fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"})
|
2016-05-02 00:42:05 +03:00
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-05-02 00:42:05 +03:00
|
|
|
$this->assertEquals(
|
2016-11-19 00:15:40 +03:00
|
|
|
['data' => ['fieldWithObjectInput' => '{"c":"foo","d":"DeserializedValue"}']],
|
2018-08-22 14:40:15 +03:00
|
|
|
$result->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
|
|
|
* @describe using variables
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsingVariables()
|
|
|
|
{
|
|
|
|
$doc = '
|
2018-08-22 14:40:15 +03:00
|
|
|
query q($input:TestInputObject) {
|
|
|
|
fieldWithObjectInput(input: $input)
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
|
|
|
|
// executes with complex input:
|
2015-07-15 20:05:46 +03:00
|
|
|
$params = ['input' => ['a' => 'foo', 'b' => ['bar'], 'c' => 'baz']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']],
|
2018-08-22 14:40:15 +03:00
|
|
|
$result->toArray()
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
// uses default value when not provided:
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
|
|
|
query ($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {
|
2015-08-17 17:01:55 +03:00
|
|
|
fieldWithObjectInput(input: $input)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2018-02-13 18:51:44 +03:00
|
|
|
// properly parses single value to list:
|
2015-07-15 20:05:46 +03:00
|
|
|
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2015-07-15 20:05:46 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']],
|
2018-08-22 14:40:15 +03:00
|
|
|
$result->toArray()
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
// executes with complex scalar input:
|
|
|
|
$params = [ 'input' => [ 'c' => 'foo', 'd' => 'SerializedValue' ] ];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = [
|
2015-08-17 17:01:55 +03:00
|
|
|
'data' => [
|
|
|
|
'fieldWithObjectInput' => '{"c":"foo","d":"DeserializedValue"}'
|
|
|
|
]
|
2015-07-15 20:05:46 +03:00
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-08-17 17:01:55 +03:00
|
|
|
|
|
|
|
// errors on null for nested non-null:
|
|
|
|
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => null]];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value ' .
|
|
|
|
'{"a":"foo","b":"bar","c":null}; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type String! not to be null at value.c.',
|
2018-08-22 14:40:15 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 21]],
|
2017-08-07 22:00:17 +03:00
|
|
|
'category' => 'graphql'
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2018-02-13 18:51:44 +03:00
|
|
|
|
2017-07-05 13:31:35 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-08-17 17:01:55 +03:00
|
|
|
|
|
|
|
// errors on incorrect type:
|
|
|
|
$params = [ 'input' => 'foo bar' ];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value "foo bar"; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected type TestInputObject to be an object.',
|
2018-08-22 14:40:15 +03:00
|
|
|
'locations' => [ [ 'line' => 2, 'column' => 21 ] ],
|
2017-08-07 22:00:17 +03:00
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
// errors on omission of nested non-null:
|
|
|
|
$params = ['input' => ['a' => 'foo', 'b' => 'bar']];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value {"a":"foo","b":"bar"}; '.
|
|
|
|
'Field value.c of required type String! was not provided.',
|
2018-08-22 14:40:15 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 21]],
|
2017-08-07 22:00:17 +03:00
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
// errors on deep nested errors and with many errors
|
|
|
|
$nestedDoc = '
|
|
|
|
query q($input: TestNestedInputObject) {
|
|
|
|
fieldWithNestedObjectInput(input: $input)
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$params = [ 'input' => [ 'na' => [ 'a' => 'foo' ] ] ];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($nestedDoc, $params);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value {"na":{"a":"foo"}}; ' .
|
|
|
|
'Field value.na.c of required type String! was not provided.',
|
|
|
|
'locations' => [['line' => 2, 'column' => 19]],
|
|
|
|
'category' => 'graphql',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'message' =>
|
|
|
|
'Variable "$input" got invalid value {"na":{"a":"foo"}}; ' .
|
|
|
|
'Field value.nb of required type String! was not provided.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 19]],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
// errors on addition of unknown input field
|
2018-02-13 18:51:44 +03:00
|
|
|
$params = ['input' => [ 'a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'extra' => 'dog' ]];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $params);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value ' .
|
|
|
|
'{"a":"foo","b":"bar","c":"baz","extra":"dog"}; ' .
|
|
|
|
'Field "extra" is not defined by type TestInputObject.',
|
2018-08-22 14:40:15 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 21]],
|
2017-08-07 22:00:17 +03:00
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
// Describe: Handles nullable scalars
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be omitted')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeOmitted()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithNullableStringInput
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = [
|
2015-08-17 17:01:55 +03:00
|
|
|
'data' => ['fieldWithNullableStringInput' => null]
|
2015-07-15 20:05:46 +03:00
|
|
|
];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be omitted in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeOmittedInAVariable()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
query SetsNullable($value: String) {
|
|
|
|
fieldWithNullableStringInput(input: $value)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-08-17 17:01:55 +03:00
|
|
|
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be omitted in an unlisted variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeOmittedInAnUnlistedVariable()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
query SetsNullable {
|
|
|
|
fieldWithNullableStringInput(input: $value)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-08-17 17:01:55 +03:00
|
|
|
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be set to null in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeSetToNullInAVariable()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
query SetsNullable($value: String) {
|
|
|
|
fieldWithNullableStringInput(input: $value)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-08-17 17:01:55 +03:00
|
|
|
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be set to a value in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeSetToAValueInAVariable()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query SetsNullable($value: String) {
|
|
|
|
fieldWithNullableStringInput(input: $value)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['value' => 'a']);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['fieldWithNullableStringInput' => '"a"']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows nullable inputs to be set to a value directly')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNullableInputsToBeSetToAValueDirectly()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithNullableStringInput(input: "a")
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['fieldWithNullableStringInput' => '"a"']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
// Describe: Handles non-nullable scalars
|
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-nullable inputs to be omitted given a default')
|
2016-11-19 00:15:40 +03:00
|
|
|
*/
|
|
|
|
public function testAllowsNonNullableInputsToBeOmittedGivenADefault()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2016-11-19 00:15:40 +03:00
|
|
|
query SetsNonNullable($value: String = "default") {
|
|
|
|
fieldWithNonNullableStringInput(input: $value)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-11-19 00:15:40 +03:00
|
|
|
$expected = [
|
|
|
|
'data' => ['fieldWithNonNullableStringInput' => '"default"']
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-11-19 00:15:40 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow non-nullable inputs to be omitted in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesntAllowNonNullableInputsToBeOmittedInAVariable()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
query SetsNonNullable($value: String!) {
|
|
|
|
fieldWithNonNullableStringInput(input: $value)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2017-07-05 13:31:35 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' => 'Variable "$value" of required type "String!" was not provided.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 31]],
|
|
|
|
'category' => 'graphql'
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow non-nullable inputs to be set to null in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesNotAllowNonNullableInputsToBeSetToNullInAVariable()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query SetsNonNullable($value: String!) {
|
|
|
|
fieldWithNonNullableStringInput(input: $value)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['value' => null]);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$value" got invalid value null; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type String! not to be null.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 31]],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-nullable inputs to be set to a value in a variable')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNonNullableInputsToBeSetToAValueInAVariable()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query SetsNonNullable($value: String!) {
|
|
|
|
fieldWithNonNullableStringInput(input: $value)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['value' => 'a']);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['fieldWithNonNullableStringInput' => '"a"']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-nullable inputs to be set to a value directly')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNonNullableInputsToBeSetToAValueDirectly()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithNonNullableStringInput(input: "a")
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['fieldWithNonNullableStringInput' => '"a"']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('reports error for missing non-nullable inputs')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2016-11-19 00:15:40 +03:00
|
|
|
public function testReportsErrorForMissingNonNullableInputs()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
fieldWithNonNullableStringInput
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-11-19 00:15:40 +03:00
|
|
|
$expected = [
|
|
|
|
'data' => ['fieldWithNonNullableStringInput' => null],
|
|
|
|
'errors' => [[
|
|
|
|
'message' => 'Argument "input" of required type "String!" was not provided.',
|
|
|
|
'locations' => [ [ 'line' => 3, 'column' => 9 ] ],
|
2017-08-07 22:00:17 +03:00
|
|
|
'path' => [ 'fieldWithNonNullableStringInput' ],
|
|
|
|
'category' => 'graphql',
|
2016-11-19 00:15:40 +03:00
|
|
|
]]
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-11-19 00:15:40 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:40:45 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('reports error for array passed into string input')
|
2017-09-20 14:40:45 +03:00
|
|
|
*/
|
|
|
|
public function testReportsErrorForArrayPassedIntoStringInput()
|
|
|
|
{
|
|
|
|
|
|
|
|
$doc = '
|
|
|
|
query SetsNonNullable($value: String!) {
|
|
|
|
fieldWithNonNullableStringInput(input: $value)
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$variables = ['value' => [1, 2, 3]];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $variables);
|
2017-09-20 14:40:45 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$value" got invalid value [1,2,3]; Expected type ' .
|
|
|
|
'String; String cannot represent an array value: [1,2,3]',
|
2017-09-20 14:40:45 +03:00
|
|
|
'category' => 'graphql',
|
|
|
|
'locations' => [
|
|
|
|
['line' => 2, 'column' => 31]
|
|
|
|
]
|
|
|
|
]]
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2017-09-20 14:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('serializing an array via GraphQLString throws TypeError')
|
2017-09-20 14:40:45 +03:00
|
|
|
*/
|
|
|
|
public function testSerializingAnArrayViaGraphQLStringThrowsTypeError()
|
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(Error::class);
|
|
|
|
$this->expectExceptionMessage('String cannot represent non scalar value: [1,2,3]');
|
2017-09-20 14:40:45 +03:00
|
|
|
Type::string()->serialize([1, 2, 3]);
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('reports error for non-provided variables for non-nullable inputs')
|
2016-11-19 00:15:40 +03:00
|
|
|
*/
|
|
|
|
public function testReportsErrorForNonProvidedVariablesForNonNullableInputs()
|
|
|
|
{
|
|
|
|
// Note: this test would typically fail validation before encountering
|
|
|
|
// this execution error, however for queries which previously validated
|
|
|
|
// and are being run against a new schema which have introduced a breaking
|
|
|
|
// change to make a formerly non-required argument required, this asserts
|
|
|
|
// failure before allowing the underlying code to receive a non-null value.
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('
|
2016-11-19 00:15:40 +03:00
|
|
|
{
|
|
|
|
fieldWithNonNullableStringInput(input: $foo)
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
');
|
2016-11-19 00:15:40 +03:00
|
|
|
$expected = [
|
|
|
|
'data' => ['fieldWithNonNullableStringInput' => null],
|
|
|
|
'errors' => [[
|
|
|
|
'message' =>
|
|
|
|
'Argument "input" of required type "String!" was provided the ' .
|
|
|
|
'variable "$foo" which was not provided a runtime value.',
|
|
|
|
'locations' => [['line' => 3, 'column' => 48]],
|
2017-08-07 22:00:17 +03:00
|
|
|
'path' => ['fieldWithNonNullableStringInput'],
|
|
|
|
'category' => 'graphql',
|
2016-11-19 00:15:40 +03:00
|
|
|
]]
|
|
|
|
];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
// Describe: Handles lists and nullability
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows lists to be null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsListsToBeNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]) {
|
|
|
|
list(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => null]);
|
2015-08-17 17:01:55 +03:00
|
|
|
$expected = ['data' => ['list' => null]];
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows lists to contain values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsListsToContainValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]) {
|
|
|
|
list(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['list' => '["A"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows lists to contain null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsListsToContainNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]) {
|
|
|
|
list(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A',null,'B']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['list' => '["A",null,"B"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow non-null lists to be null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesNotAllowNonNullListsToBeNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]!) {
|
|
|
|
nnList(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => null]);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value null; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type [String]! not to be null.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 17]],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-null lists to contain values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNonNullListsToContainValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]!) {
|
|
|
|
nnList(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['nnList' => '["A"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-null lists to contain null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNonNullListsToContainNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String]!) {
|
|
|
|
nnList(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A',null,'B']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['nnList' => '["A",null,"B"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows lists of non-nulls to be null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsListsOfNonNullsToBeNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]) {
|
|
|
|
listNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => null]);
|
2015-08-17 17:01:55 +03:00
|
|
|
$expected = ['data' => ['listNN' => null]];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows lists of non-nulls to contain values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsListsOfNonNullsToContainValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]) {
|
|
|
|
listNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['listNN' => '["A"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow lists of non-nulls to contain null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesNotAllowListsOfNonNullsToContainNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]) {
|
|
|
|
listNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value ["A",null,"B"]; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type String! not to be null at value[1].',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [ ['line' => 2, 'column' => 17] ],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow non-null lists of non-nulls to be null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesNotAllowNonNullListsOfNonNullsToBeNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]!) {
|
|
|
|
nnListNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => null]);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value null; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type [String!]! not to be null.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [ ['line' => 2, 'column' => 17] ],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows non-null lists of non-nulls to contain values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testAllowsNonNullListsOfNonNullsToContainValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]!) {
|
|
|
|
nnListNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A']]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$expected = ['data' => ['nnListNN' => '["A"]']];
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow non-null lists of non-nulls to contain null')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testDoesNotAllowNonNullListsOfNonNullsToContainNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input:[String!]!) {
|
|
|
|
nnListNN(input: $input)
|
|
|
|
}
|
|
|
|
';
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
2018-02-13 18:51:44 +03:00
|
|
|
'Variable "$input" got invalid value ["A",null,"B"]; ' .
|
2018-02-16 18:19:25 +03:00
|
|
|
'Expected non-nullable type String! not to be null at value[1].',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [ ['line' => 2, 'column' => 17] ],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow invalid types to be used as values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
|
|
|
public function testDoesNotAllowInvalidTypesToBeUsedAsValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input: TestType!) {
|
|
|
|
fieldWithObjectInput(input: $input)
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$vars = [ 'input' => [ 'list' => [ 'A', 'B' ] ] ];
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $vars);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
|
|
|
'Variable "$input" expected value of type "TestType!" which cannot ' .
|
|
|
|
'be used as an input type.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 25]],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-05-02 00:42:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not allow unknown types to be used as values')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
|
|
|
public function testDoesNotAllowUnknownTypesToBeUsedAsValues()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query q($input: UnknownType!) {
|
|
|
|
fieldWithObjectInput(input: $input)
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$vars = ['input' => 'whoknows'];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery($doc, $vars);
|
2017-07-05 13:31:35 +03:00
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'message' =>
|
|
|
|
'Variable "$input" expected value of type "UnknownType!" which ' .
|
|
|
|
'cannot be used as an input type.',
|
2017-08-07 22:00:17 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 25]],
|
|
|
|
'category' => 'graphql',
|
2017-07-05 13:31:35 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-05-02 00:42:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Describe: Execute: Uses argument default values
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('when no argument provided')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
|
|
|
public function testWhenNoArgumentProvided()
|
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('{
|
2016-05-02 00:42:05 +03:00
|
|
|
fieldWithDefaultArgumentValue
|
|
|
|
}');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
|
2018-08-22 14:40:15 +03:00
|
|
|
$result->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('when omitted variable provided')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2016-11-19 00:15:40 +03:00
|
|
|
public function testWhenOmittedVariableProvided()
|
2016-05-02 00:42:05 +03:00
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('query optionalVariable($optional: String) {
|
2016-05-02 00:42:05 +03:00
|
|
|
fieldWithDefaultArgumentValue(input: $optional)
|
|
|
|
}');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
|
2018-08-22 14:40:15 +03:00
|
|
|
$result->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('not when argument cannot be coerced')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2016-11-19 00:15:40 +03:00
|
|
|
public function testNotWhenArgumentCannotBeCoerced()
|
2016-05-02 00:42:05 +03:00
|
|
|
{
|
2018-08-22 14:40:15 +03:00
|
|
|
$result = $this->executeQuery('{
|
2016-05-02 00:42:05 +03:00
|
|
|
fieldWithDefaultArgumentValue(input: WRONG_TYPE)
|
|
|
|
}');
|
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
$expected = [
|
|
|
|
'data' => ['fieldWithDefaultArgumentValue' => null],
|
|
|
|
'errors' => [[
|
|
|
|
'message' =>
|
2018-02-15 23:29:14 +03:00
|
|
|
'Argument "input" has invalid value WRONG_TYPE.',
|
2016-11-19 00:15:40 +03:00
|
|
|
'locations' => [ [ 'line' => 2, 'column' => 50 ] ],
|
2017-08-07 22:00:17 +03:00
|
|
|
'path' => [ 'fieldWithDefaultArgumentValue' ],
|
|
|
|
'category' => 'graphql',
|
2016-11-19 00:15:40 +03:00
|
|
|
]]
|
|
|
|
];
|
|
|
|
|
2018-08-22 14:40:15 +03:00
|
|
|
$this->assertEquals($expected, $result->toArray());
|
2016-05-02 00:42:05 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
public function schema()
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
$ComplexScalarType = ComplexScalar::create();
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
$TestInputObject = new InputObjectType([
|
|
|
|
'name' => 'TestInputObject',
|
|
|
|
'fields' => [
|
|
|
|
'a' => ['type' => Type::string()],
|
|
|
|
'b' => ['type' => Type::listOf(Type::string())],
|
2015-08-17 17:01:55 +03:00
|
|
|
'c' => ['type' => Type::nonNull(Type::string())],
|
|
|
|
'd' => ['type' => $ComplexScalarType],
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
$TestNestedInputObject = new InputObjectType([
|
|
|
|
'name' => 'TestNestedInputObject',
|
|
|
|
'fields' => [
|
|
|
|
'na' => [ 'type' => Type::nonNull($TestInputObject) ],
|
|
|
|
'nb' => [ 'type' => Type::nonNull(Type::string()) ],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
$TestType = new ObjectType([
|
|
|
|
'name' => 'TestType',
|
|
|
|
'fields' => [
|
2018-08-22 14:40:15 +03:00
|
|
|
'fieldWithObjectInput' => $this->fieldWithInputArg(['type' => $TestInputObject]),
|
|
|
|
'fieldWithNullableStringInput' => $this->fieldWithInputArg(['type' => Type::string()]),
|
|
|
|
'fieldWithNonNullableStringInput' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::string())]),
|
|
|
|
'fieldWithDefaultArgumentValue' => $this->fieldWithInputArg([
|
2015-07-15 20:05:46 +03:00
|
|
|
'type' => Type::string(),
|
2018-08-22 14:40:15 +03:00
|
|
|
'defaultValue' => 'Hello World',
|
|
|
|
]),
|
|
|
|
'fieldWithNestedInputObject' => $this->fieldWithInputArg([
|
|
|
|
'type' => $TestNestedInputObject,
|
|
|
|
'defaultValue' => 'Hello World'
|
|
|
|
]),
|
|
|
|
'list' => $this->fieldWithInputArg(['type' => Type::listOf(Type::string())]),
|
|
|
|
'nnList' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::listOf(Type::string()))]),
|
|
|
|
'listNN' => $this->fieldWithInputArg(['type' => Type::listOf(Type::nonNull(Type::string()))]),
|
|
|
|
'nnListNN' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string())))]),
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
$schema = new Schema(['query' => $TestType]);
|
2015-07-15 20:05:46 +03:00
|
|
|
return $schema;
|
|
|
|
}
|
2018-08-22 14:40:15 +03:00
|
|
|
|
|
|
|
private function fieldWithInputArg($inputArg)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['input' => $inputArg],
|
|
|
|
'resolve' => function ($_, $args) {
|
|
|
|
if (isset($args['input'])) {
|
|
|
|
return json_encode($args['input']);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeQuery($query, $variableValues = null)
|
|
|
|
{
|
|
|
|
$document = Parser::parse($query);
|
|
|
|
return Executor::execute($this->schema(), $document, null, null, $variableValues);
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|