mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 08:26:02 +03:00
297 lines
8.3 KiB
PHP
297 lines
8.3 KiB
PHP
|
<?php
|
||
|
namespace GraphQL;
|
||
|
|
||
|
|
||
|
class StarWarsIntrospectionTest extends \PHPUnit_Framework_TestCase
|
||
|
{
|
||
|
// Star Wars Introspection Tests
|
||
|
// Basic Introspection
|
||
|
// it('Allows querying the schema for types')
|
||
|
public function testAllowsQueryingTheSchemaForTypes()
|
||
|
{
|
||
|
$query = '
|
||
|
query IntrospectionTypeQuery {
|
||
|
__schema {
|
||
|
types {
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
';
|
||
|
$expected = [
|
||
|
'__schema' => [
|
||
|
'types' => [
|
||
|
['name' => 'Query'],
|
||
|
['name' => 'Character'],
|
||
|
['name' => 'Human'],
|
||
|
['name' => 'String'],
|
||
|
['name' => 'Episode'],
|
||
|
['name' => 'Droid'],
|
||
|
['name' => '__Schema'],
|
||
|
['name' => '__Type'],
|
||
|
['name' => '__TypeKind'],
|
||
|
['name' => 'Boolean'],
|
||
|
['name' => '__Field'],
|
||
|
['name' => '__InputValue'],
|
||
|
['name' => '__EnumValue'],
|
||
|
['name' => '__Directive'],
|
||
|
['name' => 'ID'],
|
||
|
['name' => 'Float'],
|
||
|
['name' => 'Int']
|
||
|
]
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for query type')
|
||
|
public function testAllowsQueryingTheSchemaForQueryType()
|
||
|
{
|
||
|
$query = '
|
||
|
query IntrospectionQueryTypeQuery {
|
||
|
__schema {
|
||
|
queryType {
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
';
|
||
|
$expected = [
|
||
|
'__schema' => [
|
||
|
'queryType' => [
|
||
|
'name' => 'Query'
|
||
|
],
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for a specific type')
|
||
|
public function testAllowsQueryingTheSchemaForASpecificType()
|
||
|
{
|
||
|
$query = '
|
||
|
query IntrospectionDroidTypeQuery {
|
||
|
__type(name: "Droid") {
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
';
|
||
|
$expected = [
|
||
|
'__type' => [
|
||
|
'name' => 'Droid'
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for an object kind')
|
||
|
public function testAllowsQueryingForAnObjectKind()
|
||
|
{
|
||
|
$query = '
|
||
|
query IntrospectionDroidKindQuery {
|
||
|
__type(name: "Droid") {
|
||
|
name
|
||
|
kind
|
||
|
}
|
||
|
}
|
||
|
';
|
||
|
$expected = [
|
||
|
'__type' => [
|
||
|
'name' => 'Droid',
|
||
|
'kind' => 'OBJECT'
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for an interface kind')
|
||
|
public function testAllowsQueryingForInterfaceKind()
|
||
|
{
|
||
|
$query = '
|
||
|
query IntrospectionCharacterKindQuery {
|
||
|
__type(name: "Character") {
|
||
|
name
|
||
|
kind
|
||
|
}
|
||
|
}
|
||
|
';
|
||
|
$expected = [
|
||
|
'__type' => [
|
||
|
'name' => 'Character',
|
||
|
'kind' => 'INTERFACE'
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for object fields')
|
||
|
public function testAllowsQueryingForObjectFields()
|
||
|
{
|
||
|
$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'
|
||
|
]
|
||
|
],
|
||
|
[
|
||
|
'name' => 'primaryFunction',
|
||
|
'type' => [
|
||
|
'name' => 'String',
|
||
|
'kind' => 'SCALAR'
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for nested object fields')
|
||
|
public function testAllowsQueryingTheSchemaForNestedObjectFields()
|
||
|
{
|
||
|
$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'
|
||
|
]
|
||
|
]
|
||
|
],
|
||
|
[
|
||
|
'name' => 'primaryFunction',
|
||
|
'type' => [
|
||
|
'name' => 'String',
|
||
|
'kind' => 'SCALAR',
|
||
|
'ofType' => null
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
];
|
||
|
$this->assertValidQuery($query, $expected);
|
||
|
}
|
||
|
|
||
|
// it('Allows querying the schema for documentation')
|
||
|
public function testAllowsQueryingTheSchemaForDocumentation()
|
||
|
{
|
||
|
$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)
|
||
|
{
|
||
|
$this->assertEquals(['data' => $expected], GraphQL::execute(StarWarsSchema::build(), $query));
|
||
|
}
|
||
|
}
|