From 1b2f74f115064c4c20ba47fdd70f99266e4afe89 Mon Sep 17 00:00:00 2001 From: vladar Date: Sun, 16 Aug 2015 16:17:35 +0600 Subject: [PATCH] More star wars tests --- tests/StarWarsData.php | 14 +++++ tests/StarWarsIntrospectionTest.php | 93 ++++++++++++++++++++++++++++- tests/StarWarsQueryTest.php | 21 +++++++ tests/StarWarsSchema.php | 30 ++++++++-- 4 files changed, 151 insertions(+), 7 deletions(-) diff --git a/tests/StarWarsData.php b/tests/StarWarsData.php index 653dc24..ac1b773 100644 --- a/tests/StarWarsData.php +++ b/tests/StarWarsData.php @@ -118,6 +118,20 @@ class StarWarsData return null; } + /** + * @param $episode + * @return array + */ + static function getHero($episode) + { + if ($episode === 5) { + // Luke is the hero of Episode V. + return self::luke(); + } + // Artoo is the hero otherwise. + return self::artoo(); + } + /** * Allows us to query for a character's friends. */ diff --git a/tests/StarWarsIntrospectionTest.php b/tests/StarWarsIntrospectionTest.php index e0cf799..68f034f 100644 --- a/tests/StarWarsIntrospectionTest.php +++ b/tests/StarWarsIntrospectionTest.php @@ -22,10 +22,10 @@ class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase '__schema' => [ 'types' => [ ['name' => 'Query'], + ['name' => 'Episode'], ['name' => 'Character'], ['name' => 'Human'], ['name' => 'String'], - ['name' => 'Episode'], ['name' => 'Droid'], ['name' => '__Schema'], ['name' => '__Type'], @@ -266,6 +266,97 @@ class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase $this->assertValidQuery($query, $expected); } + public function testAllowsQueryingTheSchemaForFieldArgs() + { + $query = ' + query IntrospectionQueryTypeQuery { + __schema { + queryType { + fields { + name + args { + name + description + type { + name + kind + ofType { + name + kind + } + } + defaultValue + } + } + } + } + } + '; + + $expected = array( + '__schema' => [ + 'queryType' => [ + 'fields' => [ + [ + 'name' => 'hero', + 'args' => [ + [ + 'defaultValue' => NULL, + 'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', + 'name' => 'episode', + 'type' => [ + 'kind' => 'ENUM', + 'name' => 'Episode', + 'ofType' => NULL, + ], + ], + ], + ], + [ + 'name' => 'human', + 'args' => [ + [ + 'name' => 'id', + 'description' => 'id of the human', + 'type' => [ + 'kind' => 'NON_NULL', + 'name' => NULL, + 'ofType' => [ + 'kind' => 'SCALAR', + 'name' => 'String', + ], + ], + 'defaultValue' => NULL, + ], + ], + ], + [ + 'name' => 'droid', + 'args' => [ + [ + 'name' => 'id', + 'description' => 'id of the droid', + 'type' => [ + 'kind' => 'NON_NULL', + 'name' => NULL, + 'ofType' => + [ + 'kind' => 'SCALAR', + 'name' => 'String', + ], + ], + 'defaultValue' => NULL, + ], + ], + ], + ], + ], + ], + ); + + $this->assertValidQuery($query, $expected); + } + // it('Allows querying the schema for documentation') public function testAllowsQueryingTheSchemaForDocumentation() { diff --git a/tests/StarWarsQueryTest.php b/tests/StarWarsQueryTest.php index d1c31ec..dd28c0e 100644 --- a/tests/StarWarsQueryTest.php +++ b/tests/StarWarsQueryTest.php @@ -320,6 +320,27 @@ class StarWarsQueryTest extends \PHPUnit_Framework_TestCase $this->assertValidQuery($query, $expected); } + public function testVerifyThatLukeIsHuman() + { + $query = ' + query CheckTypeOfLuke { + hero(episode: EMPIRE) { + __typename + name + } + } + '; + + $expected = [ + 'hero' => [ + '__typename' => 'Human', + 'name' => 'Luke Skywalker' + ], + ]; + + $this->assertValidQuery($query, $expected); + } + /** * Helper function to test a query and the expected response. */ diff --git a/tests/StarWarsSchema.php b/tests/StarWarsSchema.php index c794342..cae03a9 100644 --- a/tests/StarWarsSchema.php +++ b/tests/StarWarsSchema.php @@ -47,7 +47,7 @@ use GraphQL\Type\Definition\Type; * } * * type Query { - * hero: Character + * hero(episode: Episode): Character * human(id: String!): Human * droid(id: String!): Droid * } @@ -227,7 +227,7 @@ class StarWarsSchema * * This implements the following type system shorthand: * type Query { - * hero: Character + * hero(episode: Episode): Character * human(id: String!): Human * droid(id: String!): Droid * } @@ -238,13 +238,25 @@ class StarWarsSchema 'fields' => [ 'hero' => [ 'type' => $characterInterface, - 'resolve' => function () { - return StarWarsData::artoo(); + 'args' => [ + 'episode' => [ + 'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', + 'type' => $episodeEnum + ] + ], + 'resolve' => function ($root, $args) { + return StarWarsData::getHero($args['episode']); }, ], 'human' => [ 'type' => $humanType, - 'args' => ['id' => ['name' => 'id', 'type' => Type::nonNull(Type::string())]], + 'args' => [ + 'id' => [ + 'name' => 'id', + 'description' => 'id of the human', + 'type' => Type::nonNull(Type::string()) + ] + ], 'resolve' => function ($root, $args) { $humans = StarWarsData::humans(); return isset($humans[$args['id']]) ? $humans[$args['id']] : null; @@ -252,7 +264,13 @@ class StarWarsSchema ], 'droid' => [ 'type' => $droidType, - 'args' => ['id' => ['name' => 'id', 'type' => Type::nonNull(Type::string())]], + 'args' => [ + 'id' => [ + 'name' => 'id', + 'description' => 'id of the droid', + 'type' => Type::nonNull(Type::string()) + ] + ], 'resolve' => function ($root, $args) { $droids = StarWarsData::droids(); return isset($droids[$args['id']]) ? $droids[$args['id']] : null;