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;
|
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 StarWarsIntrospectionTest extends TestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Star Wars Introspection Tests
|
|
|
|
// Basic Introspection
|
2016-05-02 00:42:05 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for types')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForTypes() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionTypeQuery {
|
|
|
|
__schema {
|
|
|
|
types {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__schema' => [
|
|
|
|
'types' => [
|
2017-09-22 19:08:51 +03:00
|
|
|
['name' => 'Query'],
|
|
|
|
['name' => 'Episode'],
|
|
|
|
['name' => 'Character'],
|
2017-08-14 21:49:56 +03:00
|
|
|
['name' => 'String'],
|
2017-09-22 19:08:51 +03:00
|
|
|
['name' => 'Human'],
|
|
|
|
['name' => 'Droid'],
|
|
|
|
['name' => 'ID'],
|
2017-08-14 15:44:16 +03:00
|
|
|
['name' => 'Float'],
|
|
|
|
['name' => 'Int'],
|
|
|
|
['name' => 'Boolean'],
|
2015-07-15 20:05:46 +03:00
|
|
|
['name' => '__Schema'],
|
|
|
|
['name' => '__Type'],
|
2017-08-14 21:49:56 +03:00
|
|
|
['name' => '__TypeKind'],
|
2015-07-15 20:05:46 +03:00
|
|
|
['name' => '__Field'],
|
|
|
|
['name' => '__InputValue'],
|
|
|
|
['name' => '__EnumValue'],
|
2017-08-14 21:49:56 +03:00
|
|
|
['name' => '__Directive'],
|
2016-05-02 00:42:05 +03:00
|
|
|
['name' => '__DirectiveLocation'],
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for query type')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForQueryType() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionQueryTypeQuery {
|
|
|
|
__schema {
|
|
|
|
queryType {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__schema' => [
|
|
|
|
'queryType' => [
|
|
|
|
'name' => 'Query'
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for a specific type')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForASpecificType() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionDroidTypeQuery {
|
|
|
|
__type(name: "Droid") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Droid'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for an object kind')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingForAnObjectKind() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionDroidKindQuery {
|
|
|
|
__type(name: "Droid") {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Droid',
|
|
|
|
'kind' => 'OBJECT'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for an interface kind')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingForInterfaceKind() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionCharacterKindQuery {
|
|
|
|
__type(name: "Character") {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Character',
|
|
|
|
'kind' => 'INTERFACE'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for object fields')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingForObjectFields() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionDroidFieldsQuery {
|
|
|
|
__type(name: "Droid") {
|
|
|
|
name
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
type {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Droid',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'name' => 'id',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'NON_NULL'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'name',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'friends',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'LIST'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'appearsIn',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'LIST'
|
|
|
|
]
|
|
|
|
],
|
2016-10-22 20:46:15 +03:00
|
|
|
[
|
|
|
|
'name' => 'secretBackstory',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR'
|
|
|
|
]
|
|
|
|
],
|
2015-07-15 20:05:46 +03:00
|
|
|
[
|
|
|
|
'name' => 'primaryFunction',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for nested object fields')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForNestedObjectFields() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionDroidNestedFieldsQuery {
|
|
|
|
__type(name: "Droid") {
|
|
|
|
name
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
type {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
ofType {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Droid',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'name' => 'id',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'NON_NULL',
|
|
|
|
'ofType' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'name',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR',
|
|
|
|
'ofType' => null
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'friends',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'LIST',
|
|
|
|
'ofType' => [
|
|
|
|
'name' => 'Character',
|
|
|
|
'kind' => 'INTERFACE'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'appearsIn',
|
|
|
|
'type' => [
|
|
|
|
'name' => null,
|
|
|
|
'kind' => 'LIST',
|
|
|
|
'ofType' => [
|
|
|
|
'name' => 'Episode',
|
|
|
|
'kind' => 'ENUM'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
2016-10-22 20:46:15 +03:00
|
|
|
[
|
|
|
|
'name' => 'secretBackstory',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR',
|
|
|
|
'ofType' => null
|
|
|
|
]
|
|
|
|
],
|
2015-07-15 20:05:46 +03:00
|
|
|
[
|
|
|
|
'name' => 'primaryFunction',
|
|
|
|
'type' => [
|
|
|
|
'name' => 'String',
|
|
|
|
'kind' => 'SCALAR',
|
|
|
|
'ofType' => null
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for field args')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForFieldArgs() : void
|
2015-08-16 13:17:35 +03:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Allows querying the schema for documentation')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsQueryingTheSchemaForDocumentation() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
query IntrospectionDroidDescriptionQuery {
|
|
|
|
__type(name: "Droid") {
|
|
|
|
name
|
|
|
|
description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$expected = [
|
|
|
|
'__type' => [
|
|
|
|
'name' => 'Droid',
|
|
|
|
'description' => 'A mechanical creature in the Star Wars universe.'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->assertValidQuery($query, $expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to test a query and the expected response.
|
|
|
|
*/
|
|
|
|
private function assertValidQuery($query, $expected)
|
|
|
|
{
|
2018-05-27 15:13:32 +03:00
|
|
|
$this->assertEquals(['data' => $expected], GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|