graphql-php/tests/Language/PrinterTest.php

165 lines
3.7 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\Document;
use GraphQL\Language\AST\EnumValue;
use GraphQL\Language\AST\Field;
use GraphQL\Language\AST\Name;
use GraphQL\Language\AST\OperationDefinition;
use GraphQL\Language\AST\SelectionSet;
use GraphQL\Language\AST\StringValue;
use GraphQL\Language\AST\Variable;
use GraphQL\Language\AST\VariableDefinition;
2016-04-09 10:36:53 +03:00
use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
2015-07-15 20:05:46 +03:00
class PrinterTest extends \PHPUnit_Framework_TestCase
{
2016-04-24 14:01:04 +03:00
/**
* @it does not alter ast
*/
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);
$this->assertEquals($astCopy, $ast);
}
2016-04-24 14:01:04 +03:00
/**
* @it prints minimal ast
*/
2015-07-15 20:05:46 +03:00
public function testPrintsMinimalAst()
{
$ast = new Field(['name' => new Name(['value' => 'foo'])]);
$this->assertEquals('foo', Printer::doPrint($ast));
}
2016-04-24 14:01:04 +03:00
/**
* @it produces helpful error messages
*/
2015-07-15 20:05:46 +03:00
public function testProducesHelpfulErrorMessages()
{
$badAst1 = new \ArrayObject(array('random' => 'Data'));
try {
Printer::doPrint($badAst1);
$this->fail('Expected exception not thrown');
} catch (\Exception $e) {
$this->assertEquals('Invalid AST Node: {"random":"Data"}', $e->getMessage());
}
}
2016-04-24 14:01:04 +03:00
/**
* @it correctly prints non-query operations without name
*/
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));
$queryAstWithArtifacts = Parser::parse(
'query ($foo: TestType) @testDirective { id, name }'
);
$expected = 'query ($foo: TestType) @testDirective {
id
name
}
';
$this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts));
$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
}
2016-04-24 14:01:04 +03:00
/**
* @it prints kitchen sink
*/
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);
$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"})
}
{
2016-04-24 14:01:04 +03:00
unnamed(truthy: true, falsey: false)
2015-07-15 20:05:46 +03:00
query
}
EOT;
$this->assertEquals($expected, $printed);
}
}