From 9964c88f32e441ea00ac3d8bdc55eb38b9da3311 Mon Sep 17 00:00:00 2001 From: vladar Date: Sun, 23 Oct 2016 00:46:15 +0700 Subject: [PATCH] Updated StarWars tests --- tests/StarWarsData.php | 26 +++++++++-- tests/StarWarsIntrospectionTest.php | 15 ++++++ tests/StarWarsSchema.php | 72 +++++++++++++++++------------ 3 files changed, 81 insertions(+), 32 deletions(-) diff --git a/tests/StarWarsData.php b/tests/StarWarsData.php index 05b7de4..beb61fc 100644 --- a/tests/StarWarsData.php +++ b/tests/StarWarsData.php @@ -118,6 +118,14 @@ class StarWarsData return null; } + /** + * Allows us to query for a character's friends. + */ + static function getFriends($character) + { + return array_map([__CLASS__, 'getCharacter'], $character['friends']); + } + /** * @param $episode * @return array @@ -133,10 +141,22 @@ class StarWarsData } /** - * Allows us to query for a character's friends. + * @param $id + * @return mixed|null */ - static function getFriends($character) + static function getHuman($id) { - return array_map([__CLASS__, 'getCharacter'], $character['friends']); + $humans = self::humans(); + return isset($humans[$id]) ? $humans[$id] : null; + } + + /** + * @param $id + * @return mixed|null + */ + static function getDroid($id) + { + $droids = self::droids(); + return isset($droids[$id]) ? $droids[$id] : null; } } diff --git a/tests/StarWarsIntrospectionTest.php b/tests/StarWarsIntrospectionTest.php index 53c67ed..d704913 100644 --- a/tests/StarWarsIntrospectionTest.php +++ b/tests/StarWarsIntrospectionTest.php @@ -189,6 +189,13 @@ class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase 'kind' => 'LIST' ] ], + [ + 'name' => 'secretBackstory', + 'type' => [ + 'name' => 'String', + 'kind' => 'SCALAR' + ] + ], [ 'name' => 'primaryFunction', 'type' => [ @@ -270,6 +277,14 @@ class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase ] ] ], + [ + 'name' => 'secretBackstory', + 'type' => [ + 'name' => 'String', + 'kind' => 'SCALAR', + 'ofType' => null + ] + ], [ 'name' => 'primaryFunction', 'type' => [ diff --git a/tests/StarWarsSchema.php b/tests/StarWarsSchema.php index 1448077..0b8d83a 100644 --- a/tests/StarWarsSchema.php +++ b/tests/StarWarsSchema.php @@ -103,36 +103,32 @@ class StarWarsSchema $characterInterface = new InterfaceType([ 'name' => 'Character', 'description' => 'A character in the Star Wars Trilogy', - 'fields' => [ - 'id' => [ - 'type' => Type::nonNull(Type::string()), - 'description' => 'The id of the character.', - ], - 'name' => [ - 'type' => Type::string(), - 'description' => 'The name of the character.' - ], - 'friends' => [ - 'type' => function () use (&$characterInterface) { - return Type::listOf($characterInterface); - }, - 'description' => 'The friends of the character, or an empty list if they have none.', - ], - 'appearsIn' => [ - 'type' => Type::listOf($episodeEnum), - 'description' => 'Which movies they appear in.' - ] - ], + 'fields' => function() use (&$characterInterface, $episodeEnum) { + return [ + 'id' => [ + 'type' => Type::nonNull(Type::string()), + 'description' => 'The id of the character.', + ], + 'name' => [ + 'type' => Type::string(), + 'description' => 'The name of the character.' + ], + 'friends' => [ + 'type' => Type::listOf($characterInterface), + 'description' => 'The friends of the character, or an empty list if they have none.', + ], + 'appearsIn' => [ + 'type' => Type::listOf($episodeEnum), + 'description' => 'Which movies they appear in.' + ], + 'secretBackstory' => [ + 'type' => Type::string(), + 'description' => 'All secrets about their past.', + ], + ]; + }, 'resolveType' => function ($obj) use (&$humanType, &$droidType) { - $humans = StarWarsData::humans(); - $droids = StarWarsData::droids(); - if (isset($humans[$obj['id']])) { - return $humanType; - } - if (isset($droids[$obj['id']])) { - return $droidType; - } - return null; + return StarWarsData::getHuman($obj['id']) ? $humanType : $droidType; }, ]); @@ -145,6 +141,7 @@ class StarWarsSchema * name: String * friends: [Character] * appearsIn: [Episode] + * secretBackstory: String * } */ $humanType = new ObjectType([ @@ -184,6 +181,14 @@ class StarWarsSchema 'type' => Type::string(), 'description' => 'The home planet of the human, or null if unknown.' ], + 'secretBackstory' => [ + 'type' => Type::string(), + 'description' => 'Where are they from and how they came to be who they are.', + 'resolve' => function() { + // This is to demonstrate error reporting + throw new \Exception('secretBackstory is secret.'); + }, + ], ], 'interfaces' => [$characterInterface] ]); @@ -197,6 +202,7 @@ class StarWarsSchema * name: String * friends: [Character] * appearsIn: [Episode] + * secretBackstory: String * primaryFunction: String * } */ @@ -223,6 +229,14 @@ class StarWarsSchema 'type' => Type::listOf($episodeEnum), 'description' => 'Which movies they appear in.' ], + 'secretBackstory' => [ + 'type' => Type::string(), + 'description' => 'Construction date and the name of the designer.', + 'resolve' => function() { + // This is to demonstrate error reporting + throw new \Exception('secretBackstory is secret.'); + }, + ], 'primaryFunction' => [ 'type' => Type::string(), 'description' => 'The primary function of the droid.'