Updated StarWars tests

This commit is contained in:
vladar 2016-10-23 00:46:15 +07:00
parent f443c6b1ba
commit 9964c88f32
3 changed files with 81 additions and 32 deletions

View File

@ -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;
}
}

View File

@ -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' => [

View File

@ -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.'