2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2018-09-01 21:21:08 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Language;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2017-07-05 13:31:35 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2018-09-01 21:21:08 +03:00
|
|
|
use GraphQL\Error\SyntaxError;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\ArgumentNode;
|
|
|
|
use GraphQL\Language\AST\FieldNode;
|
|
|
|
use GraphQL\Language\AST\NameNode;
|
2016-10-16 22:57:24 +03:00
|
|
|
use GraphQL\Language\AST\Node;
|
2016-11-19 13:31:47 +03:00
|
|
|
use GraphQL\Language\AST\NodeKind;
|
2017-07-21 18:29:59 +03:00
|
|
|
use GraphQL\Language\AST\NodeList;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\SelectionSetNode;
|
|
|
|
use GraphQL\Language\AST\StringValueNode;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\Source;
|
|
|
|
use GraphQL\Language\SourceLocation;
|
2017-07-10 15:50:26 +03:00
|
|
|
use GraphQL\Utils\Utils;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-09-01 21:21:08 +03:00
|
|
|
use function file_get_contents;
|
|
|
|
use function sprintf;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ParserTest extends TestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAssertsThatASourceToParseIsNotNull() : void
|
2017-07-05 13:31:35 +03:00
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage('GraphQL query body is expected to be string, but got NULL');
|
2017-12-21 09:52:43 +03:00
|
|
|
Parser::parse(null);
|
|
|
|
}
|
2017-07-05 13:31:35 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAssertsThatASourceToParseIsNotArray() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage('GraphQL query body is expected to be string, but got array');
|
2017-12-21 09:52:43 +03:00
|
|
|
Parser::parse(['a' => 'b']);
|
|
|
|
}
|
2017-07-05 13:31:35 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAssertsThatASourceToParseIsNotObject() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage('GraphQL query body is expected to be string, but got stdClass');
|
2017-12-21 09:52:43 +03:00
|
|
|
Parser::parse(new \stdClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parseProvidesUsefulErrors()
|
|
|
|
{
|
|
|
|
return [
|
2018-09-01 21:21:08 +03:00
|
|
|
[
|
|
|
|
'{',
|
|
|
|
'Syntax Error: Expected Name, found <EOF>',
|
|
|
|
"Syntax Error: Expected Name, found <EOF>\n\nGraphQL request (1:2)\n1: {\n ^\n",
|
|
|
|
[1],
|
|
|
|
[new SourceLocation(
|
|
|
|
1,
|
|
|
|
2
|
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'{ ...MissingOn }
|
2017-12-21 09:52:43 +03:00
|
|
|
fragment MissingOn Type
|
2018-09-01 21:21:08 +03:00
|
|
|
', 'Syntax Error: Expected "on", found Name "Type"',
|
|
|
|
"Syntax Error: Expected \"on\", found Name \"Type\"\n\nGraphQL request (2:20)\n1: { ...MissingOn }\n2: fragment MissingOn Type\n ^\n3: \n",
|
|
|
|
],
|
|
|
|
['{ field: {} }', 'Syntax Error: Expected Name, found {', "Syntax Error: Expected Name, found {\n\nGraphQL request (1:10)\n1: { field: {} }\n ^\n"],
|
|
|
|
['notanoperation Foo { field }', 'Syntax Error: Unexpected Name "notanoperation"', "Syntax Error: Unexpected Name \"notanoperation\"\n\nGraphQL request (1:1)\n1: notanoperation Foo { field }\n ^\n"],
|
|
|
|
['...', 'Syntax Error: Unexpected ...', "Syntax Error: Unexpected ...\n\nGraphQL request (1:1)\n1: ...\n ^\n"],
|
2017-12-21 09:52:43 +03:00
|
|
|
];
|
2017-07-05 13:31:35 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2017-12-21 09:52:43 +03:00
|
|
|
* @dataProvider parseProvidesUsefulErrors
|
2018-09-01 21:21:08 +03:00
|
|
|
* @see it('parse provides useful errors')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-09-01 21:21:08 +03:00
|
|
|
public function testParseProvidesUsefulErrors(
|
|
|
|
$str,
|
|
|
|
$expectedMessage,
|
|
|
|
$stringRepresentation,
|
|
|
|
$expectedPositions = null,
|
|
|
|
$expectedLocations = null
|
|
|
|
) : void {
|
2017-12-21 09:52:43 +03:00
|
|
|
try {
|
|
|
|
Parser::parse($str);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (SyntaxError $e) {
|
|
|
|
$this->assertEquals($expectedMessage, $e->getMessage());
|
2018-02-12 14:23:39 +03:00
|
|
|
$this->assertEquals($stringRepresentation, (string) $e);
|
2015-08-16 23:53:11 +03:00
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
if ($expectedPositions) {
|
|
|
|
$this->assertEquals($expectedPositions, $e->getPositions());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
if ($expectedLocations) {
|
|
|
|
$this->assertEquals($expectedLocations, $e->getLocations());
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parse provides useful error when using source')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParseProvidesUsefulErrorWhenUsingSource() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-02-12 14:23:39 +03:00
|
|
|
try {
|
|
|
|
Parser::parse(new Source('query', 'MyQuery.graphql'));
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (SyntaxError $error) {
|
|
|
|
$this->assertEquals(
|
|
|
|
"Syntax Error: Expected {, found <EOF>\n\nMyQuery.graphql (1:6)\n1: query\n ^\n",
|
|
|
|
(string) $error
|
|
|
|
);
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses variable inline values')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesVariableInlineValues() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-07 19:59:48 +03:00
|
|
|
$this->expectNotToPerformAssertions();
|
2015-07-15 20:05:46 +03:00
|
|
|
// Following line should not throw:
|
|
|
|
Parser::parse('{ field(complex: { a: { b: [ $var ] } }) }');
|
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses constant default values')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesConstantDefaultValues() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-02-12 14:23:39 +03:00
|
|
|
$this->expectSyntaxError(
|
|
|
|
'query Foo($x: Complex = { a: { b: [ $var ] } }) { field }',
|
|
|
|
'Unexpected $',
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->loc(1, 37)
|
2018-02-12 14:23:39 +03:00
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
private function expectSyntaxError($text, $message, $location)
|
|
|
|
{
|
|
|
|
$this->expectException(SyntaxError::class);
|
|
|
|
$this->expectExceptionMessage($message);
|
|
|
|
try {
|
|
|
|
Parser::parse($text);
|
|
|
|
} catch (SyntaxError $error) {
|
|
|
|
$this->assertEquals([$location], $error->getLocations());
|
|
|
|
throw $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loc($line, $column)
|
|
|
|
{
|
|
|
|
return new SourceLocation($line, $column);
|
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not accept fragments spread of "on"')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDoesNotAcceptFragmentsNamedOn() : void
|
2015-08-16 23:53:11 +03:00
|
|
|
{
|
2018-02-12 14:23:39 +03:00
|
|
|
$this->expectSyntaxError(
|
|
|
|
'fragment on on on { on }',
|
|
|
|
'Unexpected Name "on"',
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->loc(1, 10)
|
2018-02-12 14:23:39 +03:00
|
|
|
);
|
2015-08-16 23:53:11 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not accept fragments spread of "on"')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDoesNotAcceptFragmentSpreadOfOn() : void
|
2015-08-16 23:53:11 +03:00
|
|
|
{
|
2018-02-12 14:23:39 +03:00
|
|
|
$this->expectSyntaxError(
|
|
|
|
'{ ...on }',
|
|
|
|
'Expected Name, found }',
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->loc(1, 9)
|
2018-02-12 14:23:39 +03:00
|
|
|
);
|
2015-08-16 23:53:11 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses multi-byte characters')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesMultiByteCharacters() : void
|
2016-04-23 21:24:41 +03:00
|
|
|
{
|
|
|
|
// Note: \u0A0A could be naively interpretted as two line-feed chars.
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
$char = Utils::chr(0x0A0A);
|
2016-04-23 21:24:41 +03:00
|
|
|
$query = <<<HEREDOC
|
|
|
|
# This comment has a $char multi-byte character.
|
|
|
|
{ field(arg: "Has a $char multi-byte character.") }
|
|
|
|
HEREDOC;
|
|
|
|
|
|
|
|
$result = Parser::parse($query, ['noLocation' => true]);
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
$expected = new SelectionSetNode([
|
2017-07-21 18:29:59 +03:00
|
|
|
'selections' => new NodeList([
|
2016-11-19 02:12:18 +03:00
|
|
|
new FieldNode([
|
2018-09-01 21:21:08 +03:00
|
|
|
'name' => new NameNode(['value' => 'field']),
|
|
|
|
'arguments' => new NodeList([
|
2016-11-19 02:12:18 +03:00
|
|
|
new ArgumentNode([
|
2018-09-01 21:21:08 +03:00
|
|
|
'name' => new NameNode(['value' => 'arg']),
|
|
|
|
'value' => new StringValueNode(
|
|
|
|
['value' => sprintf('Has a %s multi-byte character.', $char)]
|
|
|
|
),
|
|
|
|
]),
|
2017-07-21 18:29:59 +03:00
|
|
|
]),
|
2018-09-01 21:21:08 +03:00
|
|
|
'directives' => new NodeList([]),
|
|
|
|
]),
|
|
|
|
]),
|
2016-04-23 21:24:41 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result->definitions[0]->selectionSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses kitchen sink')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesKitchenSink() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Following should not throw:
|
|
|
|
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
|
2018-09-01 21:21:08 +03:00
|
|
|
$result = Parser::parse($kitchenSink);
|
2015-08-16 23:53:11 +03:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
|
|
|
* allows non-keywords anywhere a Name is allowed
|
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsNonKeywordsAnywhereANameIsAllowed() : void
|
2015-08-16 23:53:11 +03:00
|
|
|
{
|
|
|
|
$nonKeywords = [
|
|
|
|
'on',
|
|
|
|
'fragment',
|
|
|
|
'query',
|
|
|
|
'mutation',
|
2016-04-23 21:24:41 +03:00
|
|
|
'subscription',
|
2015-08-16 23:53:11 +03:00
|
|
|
'true',
|
2018-09-01 21:21:08 +03:00
|
|
|
'false',
|
2015-08-16 23:53:11 +03:00
|
|
|
];
|
|
|
|
foreach ($nonKeywords as $keyword) {
|
|
|
|
$fragmentName = $keyword;
|
|
|
|
if ($keyword === 'on') {
|
|
|
|
$fragmentName = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expected not to throw:
|
2018-09-01 21:21:08 +03:00
|
|
|
$result = Parser::parse(<<<GRAPHQL
|
|
|
|
query $keyword {
|
|
|
|
... $fragmentName
|
|
|
|
... on $keyword { field }
|
|
|
|
}
|
|
|
|
fragment $fragmentName on Type {
|
|
|
|
$keyword($keyword: \$$keyword) @$keyword($keyword: $keyword)
|
2015-08-16 23:53:11 +03:00
|
|
|
}
|
|
|
|
fragment $fragmentName on Type {
|
2018-09-01 21:21:08 +03:00
|
|
|
$keyword($keyword: \$$keyword) @$keyword($keyword: $keyword)
|
2015-08-16 23:53:11 +03:00
|
|
|
}
|
2018-09-01 21:21:08 +03:00
|
|
|
GRAPHQL
|
|
|
|
);
|
2015-08-16 23:53:11 +03:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-23 21:24:41 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses anonymous mutation operations')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsessAnonymousMutationOperations() : void
|
2016-04-23 21:24:41 +03:00
|
|
|
{
|
2018-08-07 19:59:48 +03:00
|
|
|
$this->expectNotToPerformAssertions();
|
2016-04-23 21:24:41 +03:00
|
|
|
// Should not throw:
|
|
|
|
Parser::parse('
|
|
|
|
mutation {
|
|
|
|
mutationField
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses anonymous subscription operations')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesAnonymousSubscriptionOperations() : void
|
2016-04-23 21:24:41 +03:00
|
|
|
{
|
2018-08-07 19:59:48 +03:00
|
|
|
$this->expectNotToPerformAssertions();
|
2016-04-23 21:24:41 +03:00
|
|
|
// Should not throw:
|
|
|
|
Parser::parse('
|
|
|
|
subscription {
|
|
|
|
subscriptionField
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses named mutation operations')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesNamedMutationOperations() : void
|
2016-04-23 21:24:41 +03:00
|
|
|
{
|
2018-08-07 19:59:48 +03:00
|
|
|
$this->expectNotToPerformAssertions();
|
2016-04-23 21:24:41 +03:00
|
|
|
// Should not throw:
|
|
|
|
Parser::parse('
|
|
|
|
mutation Foo {
|
|
|
|
mutationField
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses named subscription operations')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesNamedSubscriptionOperations() : void
|
2016-04-23 21:24:41 +03:00
|
|
|
{
|
2018-08-07 19:59:48 +03:00
|
|
|
$this->expectNotToPerformAssertions();
|
2016-04-23 21:24:41 +03:00
|
|
|
Parser::parse('
|
|
|
|
subscription Foo {
|
|
|
|
subscriptionField
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('creates ast')
|
2016-04-23 21:24:41 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParseCreatesAst() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{
|
|
|
|
node(id: 4) {
|
|
|
|
id,
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
$result = Parser::parse($source);
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
$loc = function ($start, $end) use ($source) {
|
2016-10-16 22:57:24 +03:00
|
|
|
return [
|
|
|
|
'start' => $start,
|
2018-09-01 21:21:08 +03:00
|
|
|
'end' => $end,
|
2016-10-16 22:57:24 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::DOCUMENT,
|
|
|
|
'loc' => $loc(0, 41),
|
2016-10-16 22:57:24 +03:00
|
|
|
'definitions' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::OPERATION_DEFINITION,
|
|
|
|
'loc' => $loc(0, 40),
|
|
|
|
'operation' => 'query',
|
|
|
|
'name' => null,
|
2018-02-08 17:30:30 +03:00
|
|
|
'variableDefinitions' => [],
|
2018-09-01 21:21:08 +03:00
|
|
|
'directives' => [],
|
|
|
|
'selectionSet' => [
|
|
|
|
'kind' => NodeKind::SELECTION_SET,
|
|
|
|
'loc' => $loc(0, 40),
|
2016-10-16 22:57:24 +03:00
|
|
|
'selections' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::FIELD,
|
|
|
|
'loc' => $loc(4, 38),
|
|
|
|
'alias' => null,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(4, 8),
|
|
|
|
'value' => 'node',
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'arguments' => [
|
2016-10-16 22:57:24 +03:00
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::ARGUMENT,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(9, 11),
|
|
|
|
'value' => 'id',
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
|
|
|
'value' => [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::INT,
|
|
|
|
'loc' => $loc(13, 14),
|
|
|
|
'value' => '4',
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'loc' => $loc(9, 14, $source),
|
|
|
|
],
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'directives' => [],
|
2016-10-16 22:57:24 +03:00
|
|
|
'selectionSet' => [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::SELECTION_SET,
|
|
|
|
'loc' => $loc(16, 38),
|
2016-10-16 22:57:24 +03:00
|
|
|
'selections' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::FIELD,
|
|
|
|
'loc' => $loc(22, 24),
|
|
|
|
'alias' => null,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(22, 24),
|
|
|
|
'value' => 'id',
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'arguments' => [],
|
|
|
|
'directives' => [],
|
|
|
|
'selectionSet' => null,
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::FIELD,
|
|
|
|
'loc' => $loc(30, 34),
|
|
|
|
'alias' => null,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(30, 34),
|
|
|
|
'value' => 'name',
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'arguments' => [],
|
|
|
|
'directives' => [],
|
|
|
|
'selectionSet' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2016-10-16 22:57:24 +03:00
|
|
|
];
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals($expected, self::nodeToArray($result));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public static function nodeToArray(Node $node) : array
|
|
|
|
{
|
|
|
|
return TestUtils::nodeToArray($node);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 17:30:30 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('creates ast from nameless query without variables')
|
2018-02-08 17:30:30 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParseCreatesAstFromNamelessQueryWithoutVariables() : void
|
2018-02-08 17:30:30 +03:00
|
|
|
{
|
|
|
|
$source = new Source('query {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
$result = Parser::parse($source);
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
$loc = function ($start, $end) use ($source) {
|
2018-02-08 17:30:30 +03:00
|
|
|
return [
|
|
|
|
'start' => $start,
|
2018-09-01 21:21:08 +03:00
|
|
|
'end' => $end,
|
2018-02-08 17:30:30 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::DOCUMENT,
|
|
|
|
'loc' => $loc(0, 30),
|
2018-02-08 17:30:30 +03:00
|
|
|
'definitions' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::OPERATION_DEFINITION,
|
|
|
|
'loc' => $loc(0, 29),
|
|
|
|
'operation' => 'query',
|
|
|
|
'name' => null,
|
2018-02-08 17:30:30 +03:00
|
|
|
'variableDefinitions' => [],
|
2018-09-01 21:21:08 +03:00
|
|
|
'directives' => [],
|
|
|
|
'selectionSet' => [
|
|
|
|
'kind' => NodeKind::SELECTION_SET,
|
|
|
|
'loc' => $loc(6, 29),
|
2018-02-08 17:30:30 +03:00
|
|
|
'selections' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::FIELD,
|
|
|
|
'loc' => $loc(10, 27),
|
|
|
|
'alias' => null,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(10, 14),
|
|
|
|
'value' => 'node',
|
2018-02-08 17:30:30 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'arguments' => [],
|
|
|
|
'directives' => [],
|
2018-02-08 17:30:30 +03:00
|
|
|
'selectionSet' => [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::SELECTION_SET,
|
|
|
|
'loc' => $loc(15, 27),
|
2018-02-08 17:30:30 +03:00
|
|
|
'selections' => [
|
|
|
|
[
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::FIELD,
|
|
|
|
'loc' => $loc(21, 23),
|
|
|
|
'alias' => null,
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => $loc(21, 23),
|
|
|
|
'value' => 'id',
|
2018-02-08 17:30:30 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
'arguments' => [],
|
|
|
|
'directives' => [],
|
|
|
|
'selectionSet' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2018-02-08 17:30:30 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->nodeToArray($result));
|
|
|
|
}
|
|
|
|
|
2016-10-16 22:57:24 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows parsing without source location information')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsParsingWithoutSourceLocationInformation() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{ id }');
|
|
|
|
$result = Parser::parse($source, ['noLocation' => true]);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $result->loc);
|
|
|
|
}
|
|
|
|
|
2018-02-15 15:37:45 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Experimental: allows parsing fragment defined variables')
|
2018-02-15 15:37:45 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testExperimentalAllowsParsingFragmentDefinedVariables() : void
|
2018-02-15 15:37:45 +03:00
|
|
|
{
|
|
|
|
$source = new Source('fragment a($v: Boolean = false) on t { f(v: $v) }');
|
|
|
|
// not throw
|
|
|
|
Parser::parse($source, ['experimentalFragmentVariables' => true]);
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(SyntaxError::class);
|
2018-02-15 15:37:45 +03:00
|
|
|
Parser::parse($source);
|
|
|
|
}
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
// Describe: parseValue
|
|
|
|
|
2016-10-16 22:57:24 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains location information that only stringifys start/end')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsLocationInformationThatOnlyStringifysStartEnd() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{ id }');
|
|
|
|
$result = Parser::parse($source);
|
|
|
|
$this->assertEquals(['start' => 0, 'end' => '6'], TestUtils::locationToArray($result->loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains references to source')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsReferencesToSource() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{ id }');
|
|
|
|
$result = Parser::parse($source);
|
|
|
|
$this->assertEquals($source, $result->loc->source);
|
|
|
|
}
|
|
|
|
|
2018-09-01 21:21:08 +03:00
|
|
|
// Describe: parseType
|
|
|
|
|
2016-10-16 22:57:24 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains references to start and end tokens')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsReferencesToStartAndEndTokens() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{ id }');
|
|
|
|
$result = Parser::parse($source);
|
|
|
|
$this->assertEquals('<SOF>', $result->loc->startToken->kind);
|
|
|
|
$this->assertEquals('<EOF>', $result->loc->endToken->kind);
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:59:28 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses null value')
|
2016-11-18 19:59:28 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesNullValues() : void
|
2016-11-18 19:59:28 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::NULL,
|
|
|
|
'loc' => ['start' => 0, 'end' => 4],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseValue('null'))
|
|
|
|
);
|
2016-11-18 19:59:28 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 22:57:24 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses list values')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesListValues() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::LST,
|
|
|
|
'loc' => ['start' => 0, 'end' => 11],
|
|
|
|
'values' => [
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::INT,
|
|
|
|
'loc' => ['start' => 1, 'end' => 4],
|
|
|
|
'value' => '123',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::STRING,
|
|
|
|
'loc' => ['start' => 5, 'end' => 10],
|
|
|
|
'value' => 'abc',
|
|
|
|
'block' => false,
|
|
|
|
],
|
2016-10-16 22:57:24 +03:00
|
|
|
],
|
2018-09-01 21:21:08 +03:00
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseValue('[123 "abc"]'))
|
|
|
|
);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses well known types')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesWellKnownTypes() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::NAMED_TYPE,
|
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
|
|
|
'value' => 'String',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseType('String'))
|
|
|
|
);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses custom types')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesCustomTypes() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::NAMED_TYPE,
|
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
|
|
|
'value' => 'MyType',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseType('MyType'))
|
|
|
|
);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses list types')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesListTypes() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::LIST_TYPE,
|
|
|
|
'loc' => ['start' => 0, 'end' => 8],
|
|
|
|
'type' => [
|
|
|
|
'kind' => NodeKind::NAMED_TYPE,
|
|
|
|
'loc' => ['start' => 1, 'end' => 7],
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => ['start' => 1, 'end' => 7],
|
|
|
|
'value' => 'MyType',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseType('[MyType]'))
|
|
|
|
);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('parses non-null types')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesNonNullTypes() : void
|
2016-10-16 22:57:24 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2016-11-19 13:31:47 +03:00
|
|
|
'kind' => NodeKind::NON_NULL_TYPE,
|
2018-09-01 21:21:08 +03:00
|
|
|
'loc' => ['start' => 0, 'end' => 7],
|
2016-10-16 22:57:24 +03:00
|
|
|
'type' => [
|
2016-11-19 13:31:47 +03:00
|
|
|
'kind' => NodeKind::NAMED_TYPE,
|
2018-09-01 21:21:08 +03:00
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
2016-10-16 22:57:24 +03:00
|
|
|
'name' => [
|
2018-09-01 21:21:08 +03:00
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => ['start' => 0, 'end' => 6],
|
|
|
|
'value' => 'MyType',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseType('MyType!'))
|
|
|
|
);
|
2016-10-16 22:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-01 21:21:08 +03:00
|
|
|
* @see it('parses nested types')
|
2016-10-16 22:57:24 +03:00
|
|
|
*/
|
2018-09-01 21:21:08 +03:00
|
|
|
public function testParsesNestedTypes() : void
|
2018-02-12 14:23:39 +03:00
|
|
|
{
|
2018-09-01 21:21:08 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'kind' => NodeKind::LIST_TYPE,
|
|
|
|
'loc' => ['start' => 0, 'end' => 9],
|
|
|
|
'type' => [
|
|
|
|
'kind' => NodeKind::NON_NULL_TYPE,
|
|
|
|
'loc' => ['start' => 1, 'end' => 8],
|
|
|
|
'type' => [
|
|
|
|
'kind' => NodeKind::NAMED_TYPE,
|
|
|
|
'loc' => ['start' => 1, 'end' => 7],
|
|
|
|
'name' => [
|
|
|
|
'kind' => NodeKind::NAME,
|
|
|
|
'loc' => ['start' => 1, 'end' => 7],
|
|
|
|
'value' => 'MyType',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$this->nodeToArray(Parser::parseType('[MyType!]'))
|
|
|
|
);
|
2018-02-12 14:23:39 +03:00
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|