graphql-php/tests/StarWarsQueryTest.php

374 lines
9.6 KiB
PHP
Raw Normal View History

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