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; return null;
} }
/**
* Allows us to query for a character's friends.
*/
static function getFriends($character)
{
return array_map([__CLASS__, 'getCharacter'], $character['friends']);
}
/** /**
* @param $episode * @param $episode
* @return array * @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' 'kind' => 'LIST'
] ]
], ],
[
'name' => 'secretBackstory',
'type' => [
'name' => 'String',
'kind' => 'SCALAR'
]
],
[ [
'name' => 'primaryFunction', 'name' => 'primaryFunction',
'type' => [ 'type' => [
@ -270,6 +277,14 @@ class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase
] ]
] ]
], ],
[
'name' => 'secretBackstory',
'type' => [
'name' => 'String',
'kind' => 'SCALAR',
'ofType' => null
]
],
[ [
'name' => 'primaryFunction', 'name' => 'primaryFunction',
'type' => [ 'type' => [

View File

@ -103,36 +103,32 @@ class StarWarsSchema
$characterInterface = new InterfaceType([ $characterInterface = new InterfaceType([
'name' => 'Character', 'name' => 'Character',
'description' => 'A character in the Star Wars Trilogy', 'description' => 'A character in the Star Wars Trilogy',
'fields' => [ 'fields' => function() use (&$characterInterface, $episodeEnum) {
'id' => [ return [
'type' => Type::nonNull(Type::string()), 'id' => [
'description' => 'The id of the character.', 'type' => Type::nonNull(Type::string()),
], 'description' => 'The id of the character.',
'name' => [ ],
'type' => Type::string(), 'name' => [
'description' => 'The name of the character.' 'type' => Type::string(),
], 'description' => 'The name of the character.'
'friends' => [ ],
'type' => function () use (&$characterInterface) { 'friends' => [
return Type::listOf($characterInterface); 'type' => Type::listOf($characterInterface),
}, 'description' => 'The friends of the character, or an empty list if they have none.',
'description' => 'The friends of the character, or an empty list if they have none.', ],
], 'appearsIn' => [
'appearsIn' => [ 'type' => Type::listOf($episodeEnum),
'type' => Type::listOf($episodeEnum), 'description' => 'Which movies they appear in.'
'description' => 'Which movies they appear in.' ],
] 'secretBackstory' => [
], 'type' => Type::string(),
'description' => 'All secrets about their past.',
],
];
},
'resolveType' => function ($obj) use (&$humanType, &$droidType) { 'resolveType' => function ($obj) use (&$humanType, &$droidType) {
$humans = StarWarsData::humans(); return StarWarsData::getHuman($obj['id']) ? $humanType : $droidType;
$droids = StarWarsData::droids();
if (isset($humans[$obj['id']])) {
return $humanType;
}
if (isset($droids[$obj['id']])) {
return $droidType;
}
return null;
}, },
]); ]);
@ -145,6 +141,7 @@ class StarWarsSchema
* name: String * name: String
* friends: [Character] * friends: [Character]
* appearsIn: [Episode] * appearsIn: [Episode]
* secretBackstory: String
* } * }
*/ */
$humanType = new ObjectType([ $humanType = new ObjectType([
@ -184,6 +181,14 @@ class StarWarsSchema
'type' => Type::string(), 'type' => Type::string(),
'description' => 'The home planet of the human, or null if unknown.' '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] 'interfaces' => [$characterInterface]
]); ]);
@ -197,6 +202,7 @@ class StarWarsSchema
* name: String * name: String
* friends: [Character] * friends: [Character]
* appearsIn: [Episode] * appearsIn: [Episode]
* secretBackstory: String
* primaryFunction: String * primaryFunction: String
* } * }
*/ */
@ -223,6 +229,14 @@ class StarWarsSchema
'type' => Type::listOf($episodeEnum), 'type' => Type::listOf($episodeEnum),
'description' => 'Which movies they appear in.' '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' => [ 'primaryFunction' => [
'type' => Type::string(), 'type' => Type::string(),
'description' => 'The primary function of the droid.' 'description' => 'The primary function of the droid.'