graphql-php/tests/StarWarsQueryTest.php

402 lines
9.7 KiB
PHP
Raw Permalink Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests;
2015-07-15 20:05:46 +03:00
2016-04-09 10:36:53 +03:00
use GraphQL\GraphQL;
2015-07-15 20:05:46 +03:00
class StarWarsQueryTest extends \PHPUnit_Framework_TestCase
{
// Star Wars Query Tests
// Basic Queries
/**
* @it Correctly identifies R2-D2 as the hero of the Star Wars Saga
*/
2015-07-15 20:05:46 +03:00
public function testCorrectlyIdentifiesR2D2AsTheHeroOfTheStarWarsSaga()
{
$query = '
query HeroNameQuery {
hero {
name
}
}
';
$expected = [
'hero' => [
'name' => 'R2-D2'
]
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Allows us to query for the ID and friends of R2-D2
*/
2015-07-15 20:05:46 +03:00
public function testAllowsUsToQueryForTheIDAndFriendsOfR2D2()
{
$query = '
query HeroNameAndFriendsQuery {
hero {
id
name
friends {
name
}
}
}
';
$expected = [
'hero' => [
'id' => '2001',
'name' => 'R2-D2',
'friends' => [
[
'name' => 'Luke Skywalker',
],
[
'name' => 'Han Solo',
],
[
'name' => 'Leia Organa',
],
]
]
];
$this->assertValidQuery($query, $expected);
}
// Describe: Nested Queries
/**
* @it Allows us to query for the friends of friends of R2-D2
*/
2015-07-15 20:05:46 +03:00
public function testAllowsUsToQueryForTheFriendsOfFriendsOfR2D2()
{
$query = '
query NestedQuery {
hero {
name
friends {
name
appearsIn
friends {
name
}
}
}
}
';
$expected = [
'hero' => [
'name' => 'R2-D2',
'friends' => [
[
'name' => 'Luke Skywalker',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI',],
'friends' => [
['name' => 'Han Solo',],
['name' => 'Leia Organa',],
['name' => 'C-3PO',],
['name' => 'R2-D2',],
],
],
[
'name' => 'Han Solo',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends' => [
['name' => 'Luke Skywalker',],
['name' => 'Leia Organa'],
['name' => 'R2-D2',],
]
],
[
'name' => 'Leia Organa',
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends' =>
[
['name' => 'Luke Skywalker',],
['name' => 'Han Solo',],
['name' => 'C-3PO',],
['name' => 'R2-D2',],
],
],
],
]
];
$this->assertValidQuery($query, $expected);
}
// Describe: Using IDs and query parameters to refetch objects
/**
* @it Using IDs and query parameters to refetch objects
*/
2015-07-15 20:05:46 +03:00
public function testAllowsUsToQueryForLukeSkywalkerDirectlyUsingHisID()
{
$query = '
query FetchLukeQuery {
human(id: "1000") {
name
}
}
';
$expected = [
'human' => [
'name' => 'Luke Skywalker'
]
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID
*/
2015-07-15 20:05:46 +03:00
public function testGenericQueryToGetLukeSkywalkerById()
{
$query = '
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
';
$params = [
'someId' => '1000'
];
$expected = [
'human' => [
'name' => 'Luke Skywalker'
]
];
$this->assertValidQueryWithParams($query, $params, $expected);
}
/**
* @it Allows us to create a generic query, then use it to fetch Han Solo using his ID
*/
2015-07-15 20:05:46 +03:00
public function testGenericQueryToGetHanSoloById()
{
$query = '
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
';
$params = [
'someId' => '1002'
];
$expected = [
'human' => [
'name' => 'Han Solo'
]
];
$this->assertValidQueryWithParams($query, $params, $expected);
}
/**
* @it Allows us to create a generic query, then pass an invalid ID to get null back
*/
2015-07-15 20:05:46 +03:00
public function testGenericQueryWithInvalidId()
{
$query = '
query humanQuery($id: String!) {
human(id: $id) {
name
}
}
';
$params = [
'id' => 'not a valid id'
];
$expected = [
'human' => null
];
$this->assertValidQueryWithParams($query, $params, $expected);
}
// Using aliases to change the key in the response
/**
* @it Allows us to query for Luke, changing his key with an alias
*/
2015-07-15 20:05:46 +03:00
function testLukeKeyAlias()
{
$query = '
query FetchLukeAliased {
luke: human(id: "1000") {
name
}
}
';
$expected = [
'luke' => [
'name' => 'Luke Skywalker'
],
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Allows us to query for both Luke and Leia, using two root fields and an alias
*/
2015-07-15 20:05:46 +03:00
function testTwoRootKeysAsAnAlias()
{
$query = '
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
}
leia: human(id: "1003") {
name
}
}
';
$expected = [
'luke' => [
'name' => 'Luke Skywalker'
],
'leia' => [
'name' => 'Leia Organa'
]
];
$this->assertValidQuery($query, $expected);
}
// Uses fragments to express more complex queries
/**
* @it Allows us to query using duplicated content
*/
2015-07-15 20:05:46 +03:00
function testQueryUsingDuplicatedContent()
{
$query = '
query DuplicateFields {
luke: human(id: "1000") {
name
homePlanet
}
leia: human(id: "1003") {
name
homePlanet
}
}
';
$expected = [
'luke' => [
'name' => 'Luke Skywalker',
'homePlanet' => 'Tatooine'
],
'leia' => [
'name' => 'Leia Organa',
'homePlanet' => 'Alderaan'
]
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Allows us to use a fragment to avoid duplicating content
*/
2015-07-15 20:05:46 +03:00
function testUsingFragment()
{
$query = '
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
fragment HumanFragment on Human {
name
homePlanet
}
';
$expected = [
'luke' => [
'name' => 'Luke Skywalker',
'homePlanet' => 'Tatooine'
],
'leia' => [
'name' => 'Leia Organa',
'homePlanet' => 'Alderaan'
]
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Using __typename to find the type of an object
*/
2015-07-15 20:05:46 +03:00
public function testVerifyThatR2D2IsADroid()
{
$query = '
query CheckTypeOfR2 {
hero {
__typename
name
}
}
';
$expected = [
'hero' => [
'__typename' => 'Droid',
'name' => 'R2-D2'
],
];
$this->assertValidQuery($query, $expected);
}
/**
* @it Allows us to verify that Luke is a human
*/
2015-08-16 13:17:35 +03:00
public function testVerifyThatLukeIsHuman()
{
$query = '
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
';
$expected = [
'hero' => [
'__typename' => 'Human',
'name' => 'Luke Skywalker'
],
];
$this->assertValidQuery($query, $expected);
}
2015-07-15 20:05:46 +03:00
/**
* 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()
);
2015-07-15 20:05:46 +03:00
}
/**
* 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()
);
2015-07-15 20:05:46 +03:00
}
}