graphql-php/tests/Language/PrinterTest.php

267 lines
6.3 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Language;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Language\AST\VariableDefinitionNode;
2016-04-09 10:36:53 +03:00
use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
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 PrinterTest extends TestCase
2015-07-15 20:05:46 +03:00
{
2016-04-24 14:01:04 +03:00
/**
* @see it('does not alter ast')
2016-04-24 14:01:04 +03:00
*/
2015-07-15 20:05:46 +03:00
public function testDoesntAlterAST()
{
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
$ast = Parser::parse($kitchenSink);
$astCopy = $ast->cloneDeep();
$this->assertEquals($astCopy, $ast);
Printer::doPrint($ast);
2015-07-15 20:05:46 +03:00
$this->assertEquals($astCopy, $ast);
}
2016-04-24 14:01:04 +03:00
/**
* @see it('prints minimal ast')
2016-04-24 14:01:04 +03:00
*/
2015-07-15 20:05:46 +03:00
public function testPrintsMinimalAst()
{
$ast = new FieldNode(['name' => new NameNode(['value' => 'foo'])]);
$this->assertEquals('foo', Printer::doPrint($ast));
2015-07-15 20:05:46 +03:00
}
2016-04-24 14:01:04 +03:00
/**
* @see it('produces helpful error messages')
2016-04-24 14:01:04 +03:00
*/
2015-07-15 20:05:46 +03:00
public function testProducesHelpfulErrorMessages()
{
$badAst1 = new \ArrayObject(['random' => 'Data']);
2018-07-29 18:43:10 +03:00
$this->expectException(\Throwable::class);
$this->expectExceptionMessage('Invalid AST Node: {"random":"Data"}');
Printer::doPrint($badAst1);
2015-07-15 20:05:46 +03:00
}
2016-04-24 14:01:04 +03:00
/**
* @see it('correctly prints non-query operations without name')
2016-04-24 14:01:04 +03:00
*/
public function testCorrectlyPrintsOpsWithoutName()
2015-07-15 20:05:46 +03:00
{
2016-04-24 14:01:04 +03:00
$queryAstShorthanded = Parser::parse('query { id, name }');
2015-07-15 20:05:46 +03:00
2016-04-24 14:01:04 +03:00
$expected = '{
id
name
}
';
$this->assertEquals($expected, Printer::doPrint($queryAstShorthanded));
2015-07-15 20:05:46 +03:00
2016-04-24 14:01:04 +03:00
$mutationAst = Parser::parse('mutation { id, name }');
$expected = 'mutation {
id
name
}
';
$this->assertEquals($expected, Printer::doPrint($mutationAst));
2016-04-24 14:01:04 +03:00
$queryAstWithArtifacts = Parser::parse(
'query ($foo: TestType) @testDirective { id, name }'
);
$expected = 'query ($foo: TestType) @testDirective {
id
name
}
';
$this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts));
2016-04-24 14:01:04 +03:00
$mutationAstWithArtifacts = Parser::parse(
'mutation ($foo: TestType) @testDirective { id, name }'
);
$expected = 'mutation ($foo: TestType) @testDirective {
id
name
}
';
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('correctly prints single-line with leading space')
*/
public function testCorrectlyPrintsSingleLineBlockStringsWithLeadingSpace()
{
$mutationAstWithArtifacts = Parser::parse(
'{ field(arg: """ space-led value""") }'
);
$expected = '{
field(arg: """ space-led value""")
}
';
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
}
/**
* @see it('correctly prints string with a first line indentation')
*/
public function testCorrectlyPrintsBlockStringsWithAFirstLineIndentation()
{
$mutationAstWithArtifacts = Parser::parse(
'{
field(arg: """
first
line
indentation
""")
}'
);
$expected = '{
field(arg: """
first
line
indentation
""")
}
';
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
}
2018-08-22 08:28:13 +03:00
/**
* @see it('correctly prints single-line with leading space and quotation')
2018-08-22 08:28:13 +03:00
*/
public function testCorrectlyPrintsSingleLineWithLeadingSpaceAndQuotation()
{
$mutationAstWithArtifacts = Parser::parse('
{
field(arg: """ space-led value "quoted string"
""")
}
');
$expected = <<<END
{
field(arg: """ space-led value "quoted string"
""")
}
END;
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
}
/**
* @see it('Experimental: correctly prints fragment defined variables')
*/
public function testExperimentalCorrectlyPrintsFragmentDefinedVariables()
{
$fragmentWithVariable = Parser::parse('
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
id
}
',
['experimentalFragmentVariables' => true]
);
$this->assertEquals(
Printer::doPrint($fragmentWithVariable),
'fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
id
}
'
);
}
/**
* @see it('correctly prints single-line with leading space and quotation')
*/
public function testCorrectlyPrintsSingleLineStringsWithLeadingSpaceAndQuotation()
{
$mutationAstWithArtifacts = Parser::parse(
'{
field(arg: """ space-led value "quoted string"
""")
}'
);
$expected = '{
field(arg: """ space-led value "quoted string"
""")
}
';
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
}
2016-04-24 14:01:04 +03:00
/**
* @see it('prints kitchen sink')
2016-04-24 14:01:04 +03:00
*/
2015-07-15 20:05:46 +03:00
public function testPrintsKitchenSink()
{
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
$ast = Parser::parse($kitchenSink);
$printed = Printer::doPrint($ast);
2015-07-15 20:05:46 +03:00
$expected = <<<'EOT'
query queryName($foo: ComplexType, $site: Site = MOBILE) {
whoever123is: node(id: [123, 456]) {
2016-04-24 14:01:04 +03:00
id
2015-07-15 20:05:46 +03:00
... on User @defer {
field2 {
2016-04-24 14:01:04 +03:00
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
2016-04-24 14:01:04 +03:00
id
2015-07-15 20:05:46 +03:00
...frag
}
}
}
2016-04-24 14:01:04 +03:00
... @skip(unless: $foo) {
id
}
... {
id
}
2015-07-15 20:05:46 +03:00
}
}
mutation likeStory {
like(story: 123) @defer {
story {
id
}
}
}
2016-04-24 14:01:04 +03:00
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}
2015-07-15 20:05:46 +03:00
fragment frag on Friend {
foo(size: $size, bar: $b, obj: {key: "value", block: """
block string uses \"""
"""})
2015-07-15 20:05:46 +03:00
}
{
2016-11-18 19:59:28 +03:00
unnamed(truthy: true, falsey: false, nullish: null)
2015-07-15 20:05:46 +03:00
query
}
EOT;
$this->assertEquals($expected, $printed);
}
}