Merge pull request #347 from simPod/fix-cs-test

Fix CS in tests
This commit is contained in:
Vladimir Razuvaev 2018-09-02 21:30:04 +07:00 committed by GitHub
commit bfff27ef34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 415 additions and 422 deletions

View File

@ -1,8 +1,41 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests; namespace GraphQL\Tests;
use function array_map;
class StarWarsData class StarWarsData
{ {
/**
* Helper function to get a character by ID.
*/
public static function getCharacter($id)
{
$humans = self::humans();
$droids = self::droids();
if (isset($humans[$id])) {
return $humans[$id];
}
if (isset($droids[$id])) {
return $droids[$id];
}
return null;
}
public static function humans()
{
return [
'1000' => self::luke(),
'1001' => self::vader(),
'1002' => self::han(),
'1003' => self::leia(),
'1004' => self::tarkin(),
];
}
private static function luke() private static function luke()
{ {
return [ return [
@ -56,14 +89,11 @@ class StarWarsData
]; ];
} }
static function humans() public static function droids()
{ {
return [ return [
'1000' => self::luke(), '2000' => self::threepio(),
'1001' => self::vader(), '2001' => self::artoo(),
'1002' => self::han(),
'1003' => self::leia(),
'1004' => self::tarkin(),
]; ];
} }
@ -82,7 +112,7 @@ class StarWarsData
* We export artoo directly because the schema returns him * We export artoo directly because the schema returns him
* from a root field, and hence needs to reference him. * from a root field, and hence needs to reference him.
*/ */
static function artoo() private static function artoo()
{ {
return [ return [
@ -94,48 +124,25 @@ class StarWarsData
]; ];
} }
static function droids()
{
return [
'2000' => self::threepio(),
'2001' => self::artoo(),
];
}
/**
* Helper function to get a character by ID.
*/
static function getCharacter($id)
{
$humans = self::humans();
$droids = self::droids();
if (isset($humans[$id])) {
return $humans[$id];
}
if (isset($droids[$id])) {
return $droids[$id];
}
return null;
}
/** /**
* Allows us to query for a character's friends. * Allows us to query for a character's friends.
*/ */
static function getFriends($character) public static function getFriends($character)
{ {
return array_map([__CLASS__, 'getCharacter'], $character['friends']); return array_map([__CLASS__, 'getCharacter'], $character['friends']);
} }
/** /**
* @param $episode * @param int $episode
* @return array * @return mixed[]
*/ */
static function getHero($episode) public static function getHero($episode)
{ {
if ($episode === 5) { if ($episode === 5) {
// Luke is the hero of Episode V. // Luke is the hero of Episode V.
return self::luke(); return self::luke();
} }
// Artoo is the hero otherwise. // Artoo is the hero otherwise.
return self::artoo(); return self::artoo();
} }
@ -144,19 +151,21 @@ class StarWarsData
* @param $id * @param $id
* @return mixed|null * @return mixed|null
*/ */
static function getHuman($id) public static function getHuman($id)
{ {
$humans = self::humans(); $humans = self::humans();
return isset($humans[$id]) ? $humans[$id] : null;
return $humans[$id] ?? null;
} }
/** /**
* @param $id * @param $id
* @return mixed|null * @return mixed|null
*/ */
static function getDroid($id) public static function getDroid($id)
{ {
$droids = self::droids(); $droids = self::droids();
return isset($droids[$id]) ? $droids[$id] : null;
return $droids[$id] ?? null;
} }
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests; namespace GraphQL\Tests;
use GraphQL\GraphQL; use GraphQL\GraphQL;
@ -44,12 +47,20 @@ class StarWarsIntrospectionTest extends TestCase
['name' => '__EnumValue'], ['name' => '__EnumValue'],
['name' => '__Directive'], ['name' => '__Directive'],
['name' => '__DirectiveLocation'], ['name' => '__DirectiveLocation'],
] ],
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
/**
* Helper function to test a query and the expected response.
*/
private function assertValidQuery($query, $expected) : void
{
$this->assertEquals(['data' => $expected], GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray());
}
/** /**
* @see it('Allows querying the schema for query type') * @see it('Allows querying the schema for query type')
*/ */
@ -66,10 +77,8 @@ class StarWarsIntrospectionTest extends TestCase
'; ';
$expected = [ $expected = [
'__schema' => [ '__schema' => [
'queryType' => [ 'queryType' => ['name' => 'Query'],
'name' => 'Query'
], ],
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -87,9 +96,7 @@ class StarWarsIntrospectionTest extends TestCase
} }
'; ';
$expected = [ $expected = [
'__type' => [ '__type' => ['name' => 'Droid'],
'name' => 'Droid'
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -110,8 +117,8 @@ class StarWarsIntrospectionTest extends TestCase
$expected = [ $expected = [
'__type' => [ '__type' => [
'name' => 'Droid', 'name' => 'Droid',
'kind' => 'OBJECT' 'kind' => 'OBJECT',
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -132,8 +139,8 @@ class StarWarsIntrospectionTest extends TestCase
$expected = [ $expected = [
'__type' => [ '__type' => [
'name' => 'Character', 'name' => 'Character',
'kind' => 'INTERFACE' 'kind' => 'INTERFACE',
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -165,46 +172,46 @@ class StarWarsIntrospectionTest extends TestCase
'name' => 'id', 'name' => 'id',
'type' => [ 'type' => [
'name' => null, 'name' => null,
'kind' => 'NON_NULL' 'kind' => 'NON_NULL',
] ],
], ],
[ [
'name' => 'name', 'name' => 'name',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR' 'kind' => 'SCALAR',
] ],
], ],
[ [
'name' => 'friends', 'name' => 'friends',
'type' => [ 'type' => [
'name' => null, 'name' => null,
'kind' => 'LIST' 'kind' => 'LIST',
] ],
], ],
[ [
'name' => 'appearsIn', 'name' => 'appearsIn',
'type' => [ 'type' => [
'name' => null, 'name' => null,
'kind' => 'LIST' 'kind' => 'LIST',
] ],
], ],
[ [
'name' => 'secretBackstory', 'name' => 'secretBackstory',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR' 'kind' => 'SCALAR',
] ],
], ],
[ [
'name' => 'primaryFunction', 'name' => 'primaryFunction',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR' 'kind' => 'SCALAR',
] ],
] ],
] ],
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -243,17 +250,17 @@ class StarWarsIntrospectionTest extends TestCase
'kind' => 'NON_NULL', 'kind' => 'NON_NULL',
'ofType' => [ 'ofType' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR' 'kind' => 'SCALAR',
] ],
] ],
], ],
[ [
'name' => 'name', 'name' => 'name',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR', 'kind' => 'SCALAR',
'ofType' => null 'ofType' => null,
] ],
], ],
[ [
'name' => 'friends', 'name' => 'friends',
@ -262,9 +269,9 @@ class StarWarsIntrospectionTest extends TestCase
'kind' => 'LIST', 'kind' => 'LIST',
'ofType' => [ 'ofType' => [
'name' => 'Character', 'name' => 'Character',
'kind' => 'INTERFACE' 'kind' => 'INTERFACE',
] ],
] ],
], ],
[ [
'name' => 'appearsIn', 'name' => 'appearsIn',
@ -273,28 +280,28 @@ class StarWarsIntrospectionTest extends TestCase
'kind' => 'LIST', 'kind' => 'LIST',
'ofType' => [ 'ofType' => [
'name' => 'Episode', 'name' => 'Episode',
'kind' => 'ENUM' 'kind' => 'ENUM',
] ],
] ],
], ],
[ [
'name' => 'secretBackstory', 'name' => 'secretBackstory',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR', 'kind' => 'SCALAR',
'ofType' => null 'ofType' => null,
] ],
], ],
[ [
'name' => 'primaryFunction', 'name' => 'primaryFunction',
'type' => [ 'type' => [
'name' => 'String', 'name' => 'String',
'kind' => 'SCALAR', 'kind' => 'SCALAR',
'ofType' => null 'ofType' => null,
] ],
] ],
] ],
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -329,7 +336,7 @@ class StarWarsIntrospectionTest extends TestCase
} }
'; ';
$expected = array( $expected = [
'__schema' => [ '__schema' => [
'queryType' => [ 'queryType' => [
'fields' => [ 'fields' => [
@ -337,13 +344,13 @@ class StarWarsIntrospectionTest extends TestCase
'name' => 'hero', 'name' => 'hero',
'args' => [ 'args' => [
[ [
'defaultValue' => NULL, 'defaultValue' => null,
'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', 'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.',
'name' => 'episode', 'name' => 'episode',
'type' => [ 'type' => [
'kind' => 'ENUM', 'kind' => 'ENUM',
'name' => 'Episode', 'name' => 'Episode',
'ofType' => NULL, 'ofType' => null,
], ],
], ],
], ],
@ -356,13 +363,13 @@ class StarWarsIntrospectionTest extends TestCase
'description' => 'id of the human', 'description' => 'id of the human',
'type' => [ 'type' => [
'kind' => 'NON_NULL', 'kind' => 'NON_NULL',
'name' => NULL, 'name' => null,
'ofType' => [ 'ofType' => [
'kind' => 'SCALAR', 'kind' => 'SCALAR',
'name' => 'String', 'name' => 'String',
], ],
], ],
'defaultValue' => NULL, 'defaultValue' => null,
], ],
], ],
], ],
@ -374,21 +381,21 @@ class StarWarsIntrospectionTest extends TestCase
'description' => 'id of the droid', 'description' => 'id of the droid',
'type' => [ 'type' => [
'kind' => 'NON_NULL', 'kind' => 'NON_NULL',
'name' => NULL, 'name' => null,
'ofType' => 'ofType' =>
[ [
'kind' => 'SCALAR', 'kind' => 'SCALAR',
'name' => 'String', 'name' => 'String',
], ],
], ],
'defaultValue' => NULL, 'defaultValue' => null,
], ],
], ],
], ],
], ],
], ],
], ],
); ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -409,17 +416,9 @@ class StarWarsIntrospectionTest extends TestCase
$expected = [ $expected = [
'__type' => [ '__type' => [
'name' => 'Droid', 'name' => 'Droid',
'description' => 'A mechanical creature in the Star Wars universe.' 'description' => 'A mechanical creature in the Star Wars universe.',
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
/**
* Helper function to test a query and the expected response.
*/
private function assertValidQuery($query, $expected)
{
$this->assertEquals(['data' => $expected], GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray());
}
} }

View File

@ -1,15 +1,14 @@
<?php <?php
namespace GraphQL\Tests;
declare(strict_types=1);
namespace GraphQL\Tests;
use GraphQL\GraphQL; use GraphQL\GraphQL;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class StarWarsQueryTest extends TestCase class StarWarsQueryTest extends TestCase
{ {
// Star Wars Query Tests
// Basic Queries
/** /**
* @see it('Correctly identifies R2-D2 as the hero of the Star Wars Saga') * @see it('Correctly identifies R2-D2 as the hero of the Star Wars Saga')
*/ */
@ -23,13 +22,24 @@ class StarWarsQueryTest extends TestCase
} }
'; ';
$expected = [ $expected = [
'hero' => [ 'hero' => ['name' => 'R2-D2'],
'name' => 'R2-D2'
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
/**
* Helper function to test a query and the expected response.
*/
private function assertValidQuery($query, $expected) : void
{
$this->assertEquals(
['data' => $expected],
GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray()
);
}
// Describe: Nested Queries
/** /**
* @see it('Allows us to query for the ID and friends of R2-D2') * @see it('Allows us to query for the ID and friends of R2-D2')
*/ */
@ -51,22 +61,16 @@ class StarWarsQueryTest extends TestCase
'id' => '2001', 'id' => '2001',
'name' => 'R2-D2', 'name' => 'R2-D2',
'friends' => [ 'friends' => [
[ ['name' => 'Luke Skywalker'],
'name' => 'Luke Skywalker', ['name' => 'Han Solo'],
['name' => 'Leia Organa'],
], ],
[
'name' => 'Han Solo',
], ],
[
'name' => 'Leia Organa',
],
]
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
// Describe: Nested Queries // Describe: Using IDs and query parameters to refetch objects
/** /**
* @see it('Allows us to query for the friends of friends of R2-D2') * @see it('Allows us to query for the friends of friends of R2-D2')
@ -93,42 +97,40 @@ class StarWarsQueryTest extends TestCase
'friends' => [ 'friends' => [
[ [
'name' => 'Luke Skywalker', 'name' => 'Luke Skywalker',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI',], 'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends' => [ 'friends' => [
['name' => 'Han Solo',], ['name' => 'Han Solo'],
['name' => 'Leia Organa',], ['name' => 'Leia Organa'],
['name' => 'C-3PO',], ['name' => 'C-3PO'],
['name' => 'R2-D2',], ['name' => 'R2-D2'],
], ],
], ],
[ [
'name' => 'Han Solo', 'name' => 'Han Solo',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'], 'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends' => [ 'friends' => [
['name' => 'Luke Skywalker',], ['name' => 'Luke Skywalker'],
['name' => 'Leia Organa'], ['name' => 'Leia Organa'],
['name' => 'R2-D2',], ['name' => 'R2-D2'],
] ],
], ],
[ [
'name' => 'Leia Organa', 'name' => 'Leia Organa',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'], 'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends' => 'friends' =>
[ [
['name' => 'Luke Skywalker',], ['name' => 'Luke Skywalker'],
['name' => 'Han Solo',], ['name' => 'Han Solo'],
['name' => 'C-3PO',], ['name' => 'C-3PO'],
['name' => 'R2-D2',], ['name' => 'R2-D2'],
],
], ],
], ],
], ],
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
// Describe: Using IDs and query parameters to refetch objects
/** /**
* @see it('Using IDs and query parameters to refetch objects') * @see it('Using IDs and query parameters to refetch objects')
*/ */
@ -142,9 +144,7 @@ class StarWarsQueryTest extends TestCase
} }
'; ';
$expected = [ $expected = [
'human' => [ 'human' => ['name' => 'Luke Skywalker'],
'name' => 'Luke Skywalker'
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
@ -162,18 +162,27 @@ class StarWarsQueryTest extends TestCase
} }
} }
'; ';
$params = [ $params = ['someId' => '1000'];
'someId' => '1000'
];
$expected = [ $expected = [
'human' => [ 'human' => ['name' => 'Luke Skywalker'],
'name' => 'Luke Skywalker'
]
]; ];
$this->assertValidQueryWithParams($query, $params, $expected); $this->assertValidQueryWithParams($query, $params, $expected);
} }
/**
* Helper function to test a query with params and the expected response.
*/
private function assertValidQueryWithParams($query, $params, $expected)
{
$this->assertEquals(
['data' => $expected],
GraphQL::executeQuery(StarWarsSchema::build(), $query, null, null, $params)->toArray()
);
}
// Using aliases to change the key in the response
/** /**
* @see it('Allows us to create a generic query, then use it to fetch Han Solo using his ID') * @see it('Allows us to create a generic query, then use it to fetch Han Solo using his ID')
*/ */
@ -186,13 +195,9 @@ class StarWarsQueryTest extends TestCase
} }
} }
'; ';
$params = [ $params = ['someId' => '1002'];
'someId' => '1002'
];
$expected = [ $expected = [
'human' => [ 'human' => ['name' => 'Han Solo'],
'name' => 'Han Solo'
]
]; ];
$this->assertValidQueryWithParams($query, $params, $expected); $this->assertValidQueryWithParams($query, $params, $expected);
} }
@ -209,21 +214,17 @@ class StarWarsQueryTest extends TestCase
} }
} }
'; ';
$params = [ $params = ['id' => 'not a valid id'];
'id' => 'not a valid id' $expected = ['human' => null];
];
$expected = [
'human' => null
];
$this->assertValidQueryWithParams($query, $params, $expected); $this->assertValidQueryWithParams($query, $params, $expected);
} }
// Using aliases to change the key in the response // Uses fragments to express more complex queries
/** /**
* @see it('Allows us to query for Luke, changing his key with an alias') * @see it('Allows us to query for Luke, changing his key with an alias')
*/ */
function testLukeKeyAlias() public function testLukeKeyAlias() : void
{ {
$query = ' $query = '
query FetchLukeAliased { query FetchLukeAliased {
@ -233,9 +234,7 @@ class StarWarsQueryTest extends TestCase
} }
'; ';
$expected = [ $expected = [
'luke' => [ 'luke' => ['name' => 'Luke Skywalker'],
'name' => 'Luke Skywalker'
],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -243,7 +242,7 @@ class StarWarsQueryTest extends TestCase
/** /**
* @see it('Allows us to query for both Luke and Leia, using two root fields and an alias') * @see it('Allows us to query for both Luke and Leia, using two root fields and an alias')
*/ */
function testTwoRootKeysAsAnAlias() public function testTwoRootKeysAsAnAlias() : void
{ {
$query = ' $query = '
query FetchLukeAndLeiaAliased { query FetchLukeAndLeiaAliased {
@ -256,22 +255,16 @@ class StarWarsQueryTest extends TestCase
} }
'; ';
$expected = [ $expected = [
'luke' => [ 'luke' => ['name' => 'Luke Skywalker'],
'name' => 'Luke Skywalker' 'leia' => ['name' => 'Leia Organa'],
],
'leia' => [
'name' => 'Leia Organa'
]
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
// Uses fragments to express more complex queries
/** /**
* @see it('Allows us to query using duplicated content') * @see it('Allows us to query using duplicated content')
*/ */
function testQueryUsingDuplicatedContent() public function testQueryUsingDuplicatedContent() : void
{ {
$query = ' $query = '
query DuplicateFields { query DuplicateFields {
@ -288,12 +281,12 @@ class StarWarsQueryTest extends TestCase
$expected = [ $expected = [
'luke' => [ 'luke' => [
'name' => 'Luke Skywalker', 'name' => 'Luke Skywalker',
'homePlanet' => 'Tatooine' 'homePlanet' => 'Tatooine',
], ],
'leia' => [ 'leia' => [
'name' => 'Leia Organa', 'name' => 'Leia Organa',
'homePlanet' => 'Alderaan' 'homePlanet' => 'Alderaan',
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -301,7 +294,7 @@ class StarWarsQueryTest extends TestCase
/** /**
* @see it('Allows us to use a fragment to avoid duplicating content') * @see it('Allows us to use a fragment to avoid duplicating content')
*/ */
function testUsingFragment() public function testUsingFragment() : void
{ {
$query = ' $query = '
query UseFragment { query UseFragment {
@ -322,12 +315,12 @@ class StarWarsQueryTest extends TestCase
$expected = [ $expected = [
'luke' => [ 'luke' => [
'name' => 'Luke Skywalker', 'name' => 'Luke Skywalker',
'homePlanet' => 'Tatooine' 'homePlanet' => 'Tatooine',
], ],
'leia' => [ 'leia' => [
'name' => 'Leia Organa', 'name' => 'Leia Organa',
'homePlanet' => 'Alderaan' 'homePlanet' => 'Alderaan',
] ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
@ -348,7 +341,7 @@ class StarWarsQueryTest extends TestCase
$expected = [ $expected = [
'hero' => [ 'hero' => [
'__typename' => 'Droid', '__typename' => 'Droid',
'name' => 'R2-D2' 'name' => 'R2-D2',
], ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
@ -371,32 +364,10 @@ class StarWarsQueryTest extends TestCase
$expected = [ $expected = [
'hero' => [ 'hero' => [
'__typename' => 'Human', '__typename' => 'Human',
'name' => 'Luke Skywalker' 'name' => 'Luke Skywalker',
], ],
]; ];
$this->assertValidQuery($query, $expected); $this->assertValidQuery($query, $expected);
} }
/**
* Helper function to test a query and the expected response.
*/
private function assertValidQuery($query, $expected)
{
$this->assertEquals(
['data' => $expected],
GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray()
);
}
/**
* Helper function to test a query with params and the expected response.
*/
private function assertValidQueryWithParams($query, $params, $expected)
{
$this->assertEquals(
['data' => $expected],
GraphQL::executeQuery(StarWarsSchema::build(), $query, null, null, $params)->toArray()
);
}
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests; namespace GraphQL\Tests;
/** /**
@ -11,13 +14,16 @@ namespace GraphQL\Tests;
* NOTE: This may contain spoilers for the original Star * NOTE: This may contain spoilers for the original Star
* Wars trilogy. * Wars trilogy.
*/ */
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use function array_intersect_key;
use function array_map;
/** /**
* Using our shorthand to describe type systems, the type system for our * Using our shorthand to describe type systems, the type system for our
@ -56,10 +62,9 @@ use GraphQL\Type\Definition\Type;
* *
* We begin by setting up our schema. * We begin by setting up our schema.
*/ */
class StarWarsSchema class StarWarsSchema
{ {
public static function build() public static function build() : Schema
{ {
/** /**
* The original trilogy consists of three movies. * The original trilogy consists of three movies.
@ -73,17 +78,17 @@ class StarWarsSchema
'values' => [ 'values' => [
'NEWHOPE' => [ 'NEWHOPE' => [
'value' => 4, 'value' => 4,
'description' => 'Released in 1977.' 'description' => 'Released in 1977.',
], ],
'EMPIRE' => [ 'EMPIRE' => [
'value' => 5, 'value' => 5,
'description' => 'Released in 1980.' 'description' => 'Released in 1980.',
], ],
'JEDI' => [ 'JEDI' => [
'value' => 6, 'value' => 6,
'description' => 'Released in 1983.' 'description' => 'Released in 1983.',
],
], ],
]
]); ]);
$humanType = null; $humanType = null;
@ -103,7 +108,7 @@ 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' => function() use (&$characterInterface, $episodeEnum) { 'fields' => function () use (&$characterInterface, $episodeEnum) {
return [ return [
'id' => [ 'id' => [
'type' => Type::nonNull(Type::string()), 'type' => Type::nonNull(Type::string()),
@ -111,7 +116,7 @@ class StarWarsSchema
], ],
'name' => [ 'name' => [
'type' => Type::string(), 'type' => Type::string(),
'description' => 'The name of the character.' 'description' => 'The name of the character.',
], ],
'friends' => [ 'friends' => [
'type' => Type::listOf($characterInterface), 'type' => Type::listOf($characterInterface),
@ -119,7 +124,7 @@ class StarWarsSchema
], ],
'appearsIn' => [ 'appearsIn' => [
'type' => Type::listOf($episodeEnum), 'type' => Type::listOf($episodeEnum),
'description' => 'Which movies they appear in.' 'description' => 'Which movies they appear in.',
], ],
'secretBackstory' => [ 'secretBackstory' => [
'type' => Type::string(), 'type' => Type::string(),
@ -164,7 +169,7 @@ class StarWarsSchema
$fieldSelection['id'] = true; $fieldSelection['id'] = true;
$friends = array_map( $friends = array_map(
function($friend) use ($fieldSelection) { function ($friend) use ($fieldSelection) {
return array_intersect_key($friend, $fieldSelection); return array_intersect_key($friend, $fieldSelection);
}, },
StarWarsData::getFriends($human) StarWarsData::getFriends($human)
@ -175,22 +180,22 @@ class StarWarsSchema
], ],
'appearsIn' => [ 'appearsIn' => [
'type' => Type::listOf($episodeEnum), 'type' => Type::listOf($episodeEnum),
'description' => 'Which movies they appear in.' 'description' => 'Which movies they appear in.',
], ],
'homePlanet' => [ 'homePlanet' => [
'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' => [ 'secretBackstory' => [
'type' => Type::string(), 'type' => Type::string(),
'description' => 'Where are they from and how they came to be who they are.', 'description' => 'Where are they from and how they came to be who they are.',
'resolve' => function() { 'resolve' => function () {
// This is to demonstrate error reporting // This is to demonstrate error reporting
throw new \Exception('secretBackstory is secret.'); throw new \Exception('secretBackstory is secret.');
}, },
], ],
], ],
'interfaces' => [$characterInterface] 'interfaces' => [$characterInterface],
]); ]);
/** /**
@ -216,7 +221,7 @@ class StarWarsSchema
], ],
'name' => [ 'name' => [
'type' => Type::string(), 'type' => Type::string(),
'description' => 'The name of the droid.' 'description' => 'The name of the droid.',
], ],
'friends' => [ 'friends' => [
'type' => Type::listOf($characterInterface), 'type' => Type::listOf($characterInterface),
@ -227,22 +232,22 @@ class StarWarsSchema
], ],
'appearsIn' => [ 'appearsIn' => [
'type' => Type::listOf($episodeEnum), 'type' => Type::listOf($episodeEnum),
'description' => 'Which movies they appear in.' 'description' => 'Which movies they appear in.',
], ],
'secretBackstory' => [ 'secretBackstory' => [
'type' => Type::string(), 'type' => Type::string(),
'description' => 'Construction date and the name of the designer.', 'description' => 'Construction date and the name of the designer.',
'resolve' => function() { 'resolve' => function () {
// This is to demonstrate error reporting // This is to demonstrate error reporting
throw new \Exception('secretBackstory is secret.'); 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.',
]
], ],
'interfaces' => [$characterInterface] ],
'interfaces' => [$characterInterface],
]); ]);
/** /**
@ -267,11 +272,11 @@ class StarWarsSchema
'args' => [ 'args' => [
'episode' => [ 'episode' => [
'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', 'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.',
'type' => $episodeEnum 'type' => $episodeEnum,
] ],
], ],
'resolve' => function ($root, $args) { 'resolve' => function ($root, $args) {
return StarWarsData::getHero(isset($args['episode']) ? $args['episode'] : null); return StarWarsData::getHero($args['episode'] ?? null);
}, },
], ],
'human' => [ 'human' => [
@ -280,13 +285,14 @@ class StarWarsSchema
'id' => [ 'id' => [
'name' => 'id', 'name' => 'id',
'description' => 'id of the human', 'description' => 'id of the human',
'type' => Type::nonNull(Type::string()) 'type' => Type::nonNull(Type::string()),
] ],
], ],
'resolve' => function ($root, $args) { 'resolve' => function ($root, $args) {
$humans = StarWarsData::humans(); $humans = StarWarsData::humans();
return isset($humans[$args['id']]) ? $humans[$args['id']] : null;
} return $humans[$args['id']] ?? null;
},
], ],
'droid' => [ 'droid' => [
'type' => $droidType, 'type' => $droidType,
@ -294,15 +300,16 @@ class StarWarsSchema
'id' => [ 'id' => [
'name' => 'id', 'name' => 'id',
'description' => 'id of the droid', 'description' => 'id of the droid',
'type' => Type::nonNull(Type::string()) 'type' => Type::nonNull(Type::string()),
] ],
], ],
'resolve' => function ($root, $args) { 'resolve' => function ($root, $args) {
$droids = StarWarsData::droids(); $droids = StarWarsData::droids();
return isset($droids[$args['id']]) ? $droids[$args['id']] : null;
} return $droids[$args['id']] ?? null;
] },
] ],
],
]); ]);
return new Schema(['query' => $queryType]); return new Schema(['query' => $queryType]);

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests; namespace GraphQL\Tests;
use GraphQL\Language\Parser; use GraphQL\Language\Parser;
@ -37,6 +40,16 @@ class StarWarsValidationTest extends TestCase
$this->assertEquals(true, empty($errors)); $this->assertEquals(true, empty($errors));
} }
/**
* Helper function to test a query and the expected response.
*/
private function validationErrors($query)
{
$ast = Parser::parse($query);
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
}
/** /**
* @see it('Notes that non-existent fields are invalid') * @see it('Notes that non-existent fields are invalid')
*/ */
@ -142,13 +155,4 @@ class StarWarsValidationTest extends TestCase
$errors = $this->validationErrors($query); $errors = $this->validationErrors($query);
$this->assertEquals(true, empty($errors)); $this->assertEquals(true, empty($errors));
} }
/**
* Helper function to test a query and the expected response.
*/
private function validationErrors($query)
{
$ast = Parser::parse($query);
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
}
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests; namespace GraphQL\Tests;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;