More star wars tests

This commit is contained in:
vladar 2015-08-16 16:17:35 +06:00
parent 04b2e409eb
commit 1b2f74f115
4 changed files with 151 additions and 7 deletions

View File

@ -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.
*/

View File

@ -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()
{

View File

@ -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.
*/

View File

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