mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
Fix CS in tests/Type
This commit is contained in:
parent
ec54d6152b
commit
b886742968
File diff suppressed because it is too large
Load Diff
@ -1,68 +1,77 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
|
use ArrayObject;
|
||||||
use GraphQL\GraphQL;
|
use GraphQL\GraphQL;
|
||||||
use GraphQL\Language\SourceLocation;
|
use GraphQL\Language\SourceLocation;
|
||||||
use GraphQL\Type\Schema;
|
|
||||||
use GraphQL\Type\Definition\EnumType;
|
use GraphQL\Type\Definition\EnumType;
|
||||||
use GraphQL\Type\Definition\ObjectType;
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
use GraphQL\Type\Introspection;
|
use GraphQL\Type\Introspection;
|
||||||
|
use GraphQL\Type\Schema;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use function count;
|
||||||
|
use function is_array;
|
||||||
|
|
||||||
class EnumTypeTest extends TestCase
|
class EnumTypeTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/** @var Schema */
|
||||||
* @var Schema
|
|
||||||
*/
|
|
||||||
private $schema;
|
private $schema;
|
||||||
|
|
||||||
/**
|
/** @var EnumType */
|
||||||
* @var EnumType
|
|
||||||
*/
|
|
||||||
private $ComplexEnum;
|
private $ComplexEnum;
|
||||||
|
|
||||||
|
/** @var mixed[] */
|
||||||
private $Complex1;
|
private $Complex1;
|
||||||
|
|
||||||
|
/** @var ArrayObject */
|
||||||
private $Complex2;
|
private $Complex2;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$ColorType = new EnumType([
|
$ColorType = new EnumType([
|
||||||
'name' => 'Color',
|
'name' => 'Color',
|
||||||
'values' => [
|
'values' => [
|
||||||
'RED' => ['value' => 0],
|
'RED' => ['value' => 0],
|
||||||
'GREEN' => ['value' => 1],
|
'GREEN' => ['value' => 1],
|
||||||
'BLUE' => ['value' => 2],
|
'BLUE' => ['value' => 2],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$simpleEnum = new EnumType([
|
$simpleEnum = new EnumType([
|
||||||
'name' => 'SimpleEnum',
|
'name' => 'SimpleEnum',
|
||||||
'values' => [
|
'values' => [
|
||||||
'ONE', 'TWO', 'THREE'
|
'ONE',
|
||||||
]
|
'TWO',
|
||||||
|
'THREE',
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$Complex1 = ['someRandomFunction' => function() {}];
|
$Complex1 = [
|
||||||
|
'someRandomFunction' => function () {
|
||||||
|
},
|
||||||
|
];
|
||||||
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
|
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
|
||||||
|
|
||||||
$ComplexEnum = new EnumType([
|
$ComplexEnum = new EnumType([
|
||||||
'name' => 'Complex',
|
'name' => 'Complex',
|
||||||
'values' => [
|
'values' => [
|
||||||
'ONE' => ['value' => $Complex1],
|
'ONE' => ['value' => $Complex1],
|
||||||
'TWO' => ['value' => $Complex2]
|
'TWO' => ['value' => $Complex2],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$QueryType = new ObjectType([
|
$QueryType = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'colorEnum' => [
|
'colorEnum' => [
|
||||||
'type' => $ColorType,
|
'type' => $ColorType,
|
||||||
'args' => [
|
'args' => [
|
||||||
'fromEnum' => ['type' => $ColorType],
|
'fromEnum' => ['type' => $ColorType],
|
||||||
'fromInt' => ['type' => Type::int()],
|
'fromInt' => ['type' => Type::int()],
|
||||||
'fromString' => ['type' => Type::string()],
|
'fromString' => ['type' => Type::string()],
|
||||||
],
|
],
|
||||||
'resolve' => function ($value, $args) {
|
'resolve' => function ($value, $args) {
|
||||||
@ -75,28 +84,28 @@ class EnumTypeTest extends TestCase
|
|||||||
if (isset($args['fromEnum'])) {
|
if (isset($args['fromEnum'])) {
|
||||||
return $args['fromEnum'];
|
return $args['fromEnum'];
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
'simpleEnum' => [
|
'simpleEnum' => [
|
||||||
'type' => $simpleEnum,
|
'type' => $simpleEnum,
|
||||||
'args' => [
|
'args' => [
|
||||||
'fromName' => ['type' => Type::string()],
|
'fromName' => ['type' => Type::string()],
|
||||||
'fromValue' => ['type' => Type::string()]
|
'fromValue' => ['type' => Type::string()],
|
||||||
],
|
],
|
||||||
'resolve' => function($value, $args) {
|
'resolve' => function ($value, $args) {
|
||||||
if (isset($args['fromName'])) {
|
if (isset($args['fromName'])) {
|
||||||
return $args['fromName'];
|
return $args['fromName'];
|
||||||
}
|
}
|
||||||
if (isset($args['fromValue'])) {
|
if (isset($args['fromValue'])) {
|
||||||
return $args['fromValue'];
|
return $args['fromValue'];
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
'colorInt' => [
|
'colorInt' => [
|
||||||
'type' => Type::int(),
|
'type' => Type::int(),
|
||||||
'args' => [
|
'args' => [
|
||||||
'fromEnum' => ['type' => $ColorType],
|
'fromEnum' => ['type' => $ColorType],
|
||||||
'fromInt' => ['type' => Type::int()],
|
'fromInt' => ['type' => Type::int()],
|
||||||
],
|
],
|
||||||
'resolve' => function ($value, $args) {
|
'resolve' => function ($value, $args) {
|
||||||
if (isset($args['fromInt'])) {
|
if (isset($args['fromInt'])) {
|
||||||
@ -105,75 +114,76 @@ class EnumTypeTest extends TestCase
|
|||||||
if (isset($args['fromEnum'])) {
|
if (isset($args['fromEnum'])) {
|
||||||
return $args['fromEnum'];
|
return $args['fromEnum'];
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
'complexEnum' => [
|
'complexEnum' => [
|
||||||
'type' => $ComplexEnum,
|
'type' => $ComplexEnum,
|
||||||
'args' => [
|
'args' => [
|
||||||
'fromEnum' => [
|
'fromEnum' => [
|
||||||
'type' => $ComplexEnum,
|
'type' => $ComplexEnum,
|
||||||
// Note: defaultValue is provided an *internal* representation for
|
// Note: defaultValue is provided an *internal* representation for
|
||||||
// Enums, rather than the string name.
|
// Enums, rather than the string name.
|
||||||
'defaultValue' => $Complex1
|
'defaultValue' => $Complex1,
|
||||||
],
|
],
|
||||||
'provideGoodValue' => [
|
'provideGoodValue' => [
|
||||||
'type' => Type::boolean(),
|
'type' => Type::boolean(),
|
||||||
],
|
],
|
||||||
'provideBadValue' => [
|
'provideBadValue' => [
|
||||||
'type' => Type::boolean()
|
'type' => Type::boolean(),
|
||||||
]
|
],
|
||||||
],
|
],
|
||||||
'resolve' => function($value, $args) use ($Complex1, $Complex2) {
|
'resolve' => function ($value, $args) use ($Complex1, $Complex2) {
|
||||||
if (!empty($args['provideGoodValue'])) {
|
if (! empty($args['provideGoodValue'])) {
|
||||||
// Note: this is one of the references of the internal values which
|
// Note: this is one of the references of the internal values which
|
||||||
// ComplexEnum allows.
|
// ComplexEnum allows.
|
||||||
return $Complex2;
|
return $Complex2;
|
||||||
}
|
}
|
||||||
if (!empty($args['provideBadValue'])) {
|
if (! empty($args['provideBadValue'])) {
|
||||||
// Note: similar shape, but not the same *reference*
|
// Note: similar shape, but not the same *reference*
|
||||||
// as Complex2 above. Enum internal values require === equality.
|
// as Complex2 above. Enum internal values require === equality.
|
||||||
return new \ArrayObject(['someRandomValue' => 123]);
|
return new \ArrayObject(['someRandomValue' => 123]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $args['fromEnum'];
|
return $args['fromEnum'];
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$MutationType = new ObjectType([
|
$MutationType = new ObjectType([
|
||||||
'name' => 'Mutation',
|
'name' => 'Mutation',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'favoriteEnum' => [
|
'favoriteEnum' => [
|
||||||
'type' => $ColorType,
|
'type' => $ColorType,
|
||||||
'args' => ['color' => ['type' => $ColorType]],
|
'args' => ['color' => ['type' => $ColorType]],
|
||||||
'resolve' => function ($value, $args) {
|
'resolve' => function ($value, $args) {
|
||||||
return isset($args['color']) ? $args['color'] : null;
|
return $args['color'] ?? null;
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$SubscriptionType = new ObjectType([
|
$SubscriptionType = new ObjectType([
|
||||||
'name' => 'Subscription',
|
'name' => 'Subscription',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'subscribeToEnum' => [
|
'subscribeToEnum' => [
|
||||||
'type' => $ColorType,
|
'type' => $ColorType,
|
||||||
'args' => ['color' => ['type' => $ColorType]],
|
'args' => ['color' => ['type' => $ColorType]],
|
||||||
'resolve' => function ($value, $args) {
|
'resolve' => function ($value, $args) {
|
||||||
return isset($args['color']) ? $args['color'] : null;
|
return $args['color'] ?? null;
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->Complex1 = $Complex1;
|
$this->Complex1 = $Complex1;
|
||||||
$this->Complex2 = $Complex2;
|
$this->Complex2 = $Complex2;
|
||||||
$this->ComplexEnum = $ComplexEnum;
|
$this->ComplexEnum = $ComplexEnum;
|
||||||
|
|
||||||
$this->schema = new Schema([
|
$this->schema = new Schema([
|
||||||
'query' => $QueryType,
|
'query' => $QueryType,
|
||||||
'mutation' => $MutationType,
|
'mutation' => $MutationType,
|
||||||
'subscription' => $SubscriptionType
|
'subscription' => $SubscriptionType,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,12 +231,34 @@ class EnumTypeTest extends TestCase
|
|||||||
'{ colorEnum(fromEnum: "GREEN") }',
|
'{ colorEnum(fromEnum: "GREEN") }',
|
||||||
null,
|
null,
|
||||||
[
|
[
|
||||||
'message' => "Expected type Color, found \"GREEN\"; Did you mean the enum value GREEN?",
|
'message' => 'Expected type Color, found "GREEN"; Did you mean the enum value GREEN?',
|
||||||
'locations' => [new SourceLocation(1, 23)]
|
'locations' => [new SourceLocation(1, 23)],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function expectFailure($query, $vars, $err)
|
||||||
|
{
|
||||||
|
$result = GraphQL::executeQuery($this->schema, $query, null, null, $vars);
|
||||||
|
$this->assertEquals(1, count($result->errors));
|
||||||
|
|
||||||
|
if (is_array($err)) {
|
||||||
|
$this->assertEquals(
|
||||||
|
$err['message'],
|
||||||
|
$result->errors[0]->getMessage()
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
$err['locations'],
|
||||||
|
$result->errors[0]->getLocations()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$this->assertEquals(
|
||||||
|
$err,
|
||||||
|
$result->errors[0]->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see it('does not accept valuesNotInTheEnum')
|
* @see it('does not accept valuesNotInTheEnum')
|
||||||
*/
|
*/
|
||||||
@ -236,8 +268,8 @@ class EnumTypeTest extends TestCase
|
|||||||
'{ colorEnum(fromEnum: GREENISH) }',
|
'{ colorEnum(fromEnum: GREENISH) }',
|
||||||
null,
|
null,
|
||||||
[
|
[
|
||||||
'message' => "Expected type Color, found GREENISH; Did you mean the enum value GREEN?",
|
'message' => 'Expected type Color, found GREENISH; Did you mean the enum value GREEN?',
|
||||||
'locations' => [new SourceLocation(1, 23)]
|
'locations' => [new SourceLocation(1, 23)],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -251,8 +283,8 @@ class EnumTypeTest extends TestCase
|
|||||||
'{ colorEnum(fromEnum: green) }',
|
'{ colorEnum(fromEnum: green) }',
|
||||||
null,
|
null,
|
||||||
[
|
[
|
||||||
'message' => "Expected type Color, found green; Did you mean the enum value GREEN?",
|
'message' => 'Expected type Color, found green; Did you mean the enum value GREEN?',
|
||||||
'locations' => [new SourceLocation(1, 23)]
|
'locations' => [new SourceLocation(1, 23)],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -266,9 +298,9 @@ class EnumTypeTest extends TestCase
|
|||||||
'{ colorEnum(fromString: "GREEN") }',
|
'{ colorEnum(fromString: "GREEN") }',
|
||||||
null,
|
null,
|
||||||
[
|
[
|
||||||
'message' => 'Expected a value of type "Color" but received: GREEN',
|
'message' => 'Expected a value of type "Color" but received: GREEN',
|
||||||
'locations' => [new SourceLocation(1, 3)],
|
'locations' => [new SourceLocation(1, 3)],
|
||||||
'path' => ['colorEnum'],
|
'path' => ['colorEnum'],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -281,7 +313,7 @@ class EnumTypeTest extends TestCase
|
|||||||
$this->expectFailure(
|
$this->expectFailure(
|
||||||
'{ colorEnum(fromEnum: 1) }',
|
'{ colorEnum(fromEnum: 1) }',
|
||||||
null,
|
null,
|
||||||
"Expected type Color, found 1."
|
'Expected type Color, found 1.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +325,7 @@ class EnumTypeTest extends TestCase
|
|||||||
$this->expectFailure(
|
$this->expectFailure(
|
||||||
'{ colorEnum(fromInt: GREEN) }',
|
'{ colorEnum(fromInt: GREEN) }',
|
||||||
null,
|
null,
|
||||||
"Expected type Int, found GREEN."
|
'Expected type Int, found GREEN.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,7 +413,7 @@ class EnumTypeTest extends TestCase
|
|||||||
$this->expectFailure(
|
$this->expectFailure(
|
||||||
'query test($color: Int!) { colorEnum(fromEnum: $color) }',
|
'query test($color: Int!) { colorEnum(fromEnum: $color) }',
|
||||||
['color' => 2],
|
['color' => 2],
|
||||||
'Variable "$color" of type "Int!" used in position ' . 'expecting type "Color".'
|
'Variable "$color" of type "Int!" used in position expecting type "Color".'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,10 +424,13 @@ class EnumTypeTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
['data' => ['colorEnum' => 'RED', 'colorInt' => 0]],
|
['data' => ['colorEnum' => 'RED', 'colorInt' => 0]],
|
||||||
GraphQL::executeQuery($this->schema, "{
|
GraphQL::executeQuery(
|
||||||
|
$this->schema,
|
||||||
|
'{
|
||||||
colorEnum(fromEnum: RED)
|
colorEnum(fromEnum: RED)
|
||||||
colorInt(fromEnum: RED)
|
colorInt(fromEnum: RED)
|
||||||
}")->toArray()
|
}'
|
||||||
|
)->toArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,10 +441,13 @@ class EnumTypeTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
['data' => ['colorEnum' => null, 'colorInt' => null]],
|
['data' => ['colorEnum' => null, 'colorInt' => null]],
|
||||||
GraphQL::executeQuery($this->schema, "{
|
GraphQL::executeQuery(
|
||||||
|
$this->schema,
|
||||||
|
'{
|
||||||
colorEnum
|
colorEnum
|
||||||
colorInt
|
colorInt
|
||||||
}")->toArray()
|
}'
|
||||||
|
)->toArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,7 +457,7 @@ class EnumTypeTest extends TestCase
|
|||||||
public function testPresentsGetValuesAPIForComplexEnums() : void
|
public function testPresentsGetValuesAPIForComplexEnums() : void
|
||||||
{
|
{
|
||||||
$ComplexEnum = $this->ComplexEnum;
|
$ComplexEnum = $this->ComplexEnum;
|
||||||
$values = $ComplexEnum->getValues();
|
$values = $ComplexEnum->getValues();
|
||||||
|
|
||||||
$this->assertEquals(2, count($values));
|
$this->assertEquals(2, count($values));
|
||||||
$this->assertEquals('ONE', $values[0]->name);
|
$this->assertEquals('ONE', $values[0]->name);
|
||||||
@ -446,25 +484,29 @@ class EnumTypeTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testMayBeInternallyRepresentedWithComplexValues() : void
|
public function testMayBeInternallyRepresentedWithComplexValues() : void
|
||||||
{
|
{
|
||||||
$result = GraphQL::executeQuery($this->schema, '{
|
$result = GraphQL::executeQuery(
|
||||||
|
$this->schema,
|
||||||
|
'{
|
||||||
first: complexEnum
|
first: complexEnum
|
||||||
second: complexEnum(fromEnum: TWO)
|
second: complexEnum(fromEnum: TWO)
|
||||||
good: complexEnum(provideGoodValue: true)
|
good: complexEnum(provideGoodValue: true)
|
||||||
bad: complexEnum(provideBadValue: true)
|
bad: complexEnum(provideBadValue: true)
|
||||||
}')->toArray(true);
|
}'
|
||||||
|
)->toArray(true);
|
||||||
|
|
||||||
$expected = [
|
$expected = [
|
||||||
'data' => [
|
'data' => [
|
||||||
'first' => 'ONE',
|
'first' => 'ONE',
|
||||||
'second' => 'TWO',
|
'second' => 'TWO',
|
||||||
'good' => 'TWO',
|
'good' => 'TWO',
|
||||||
'bad' => null
|
'bad' => null,
|
||||||
],
|
],
|
||||||
'errors' => [[
|
'errors' => [[
|
||||||
'debugMessage' =>
|
'debugMessage' =>
|
||||||
'Expected a value of type "Complex" but received: instance of ArrayObject',
|
'Expected a value of type "Complex" but received: instance of ArrayObject',
|
||||||
'locations' => [['line' => 5, 'column' => 9]]
|
'locations' => [['line' => 5, 'column' => 9]],
|
||||||
]]
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->assertArraySubset($expected, $result);
|
$this->assertArraySubset($expected, $result);
|
||||||
@ -489,35 +531,14 @@ class EnumTypeTest extends TestCase
|
|||||||
|
|
||||||
$this->assertArraySubset(
|
$this->assertArraySubset(
|
||||||
[
|
[
|
||||||
'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null],
|
'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null],
|
||||||
'errors' => [[
|
'errors' => [[
|
||||||
'debugMessage' => 'Expected a value of type "SimpleEnum" but received: WRONG',
|
'debugMessage' => 'Expected a value of type "SimpleEnum" but received: WRONG',
|
||||||
'locations' => [['line' => 4, 'column' => 13]]
|
'locations' => [['line' => 4, 'column' => 13]],
|
||||||
]]
|
],
|
||||||
|
],
|
||||||
],
|
],
|
||||||
GraphQL::executeQuery($this->schema, $q)->toArray(true)
|
GraphQL::executeQuery($this->schema, $q)->toArray(true)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function expectFailure($query, $vars, $err)
|
|
||||||
{
|
|
||||||
$result = GraphQL::executeQuery($this->schema, $query, null, null, $vars);
|
|
||||||
$this->assertEquals(1, count($result->errors));
|
|
||||||
|
|
||||||
if (is_array($err)) {
|
|
||||||
$this->assertEquals(
|
|
||||||
$err['message'],
|
|
||||||
$result->errors[0]->getMessage()
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
$err['locations'],
|
|
||||||
$result->errors[0]->getLocations()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->assertEquals(
|
|
||||||
$err,
|
|
||||||
$result->errors[0]->getMessage()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
class ObjectIdStub
|
class ObjectIdStub
|
||||||
{
|
{
|
||||||
/**
|
/** @var int */
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\InvariantViolation;
|
||||||
@ -10,124 +13,84 @@ use GraphQL\Type\Definition\UnionType;
|
|||||||
use GraphQL\Type\EagerResolution;
|
use GraphQL\Type\EagerResolution;
|
||||||
use GraphQL\Type\LazyResolution;
|
use GraphQL\Type\LazyResolution;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use function lcfirst;
|
||||||
|
|
||||||
class ResolutionTest extends TestCase
|
class ResolutionTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $query;
|
private $query;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $mutation;
|
private $mutation;
|
||||||
|
|
||||||
/**
|
/** @var InterfaceType */
|
||||||
* @var InterfaceType
|
|
||||||
*/
|
|
||||||
private $node;
|
private $node;
|
||||||
|
|
||||||
/**
|
/** @var InterfaceType */
|
||||||
* @var InterfaceType
|
|
||||||
*/
|
|
||||||
private $content;
|
private $content;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $blogStory;
|
private $blogStory;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $link;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $video;
|
private $video;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $videoMetadata;
|
private $videoMetadata;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $comment;
|
private $comment;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $category;
|
private $category;
|
||||||
|
|
||||||
/**
|
/** @var UnionType */
|
||||||
* @var UnionType
|
|
||||||
*/
|
|
||||||
private $mention;
|
private $mention;
|
||||||
|
|
||||||
|
/** @var ObjectType */
|
||||||
private $postStoryMutation;
|
private $postStoryMutation;
|
||||||
|
|
||||||
|
/** @var InputObjectType */
|
||||||
private $postStoryMutationInput;
|
private $postStoryMutationInput;
|
||||||
|
|
||||||
|
/** @var ObjectType */
|
||||||
private $postCommentMutation;
|
private $postCommentMutation;
|
||||||
|
|
||||||
|
/** @var InputObjectType */
|
||||||
private $postCommentMutationInput;
|
private $postCommentMutationInput;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->node = new InterfaceType([
|
$this->node = new InterfaceType([
|
||||||
'name' => 'Node',
|
'name' => 'Node',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'id' => Type::string()
|
'id' => Type::string(),
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->content = new InterfaceType([
|
$this->content = new InterfaceType([
|
||||||
'name' => 'Content',
|
'name' => 'Content',
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
return [
|
||||||
'title' => Type::string(),
|
'title' => Type::string(),
|
||||||
'body' => Type::string(),
|
'body' => Type::string(),
|
||||||
'author' => $this->user,
|
'author' => $this->user,
|
||||||
'comments' => Type::listOf($this->comment),
|
'comments' => Type::listOf($this->comment),
|
||||||
'categories' => Type::listOf($this->category)
|
'categories' => Type::listOf($this->category),
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->blogStory = new ObjectType([
|
$this->blogStory = new ObjectType([
|
||||||
'name' => 'BlogStory',
|
'name' => 'BlogStory',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node,
|
$this->node,
|
||||||
$this->content
|
$this->content,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
|
||||||
$this->node->getField('id'),
|
|
||||||
$this->content->getField('title'),
|
|
||||||
$this->content->getField('body'),
|
|
||||||
$this->content->getField('author'),
|
|
||||||
$this->content->getField('comments'),
|
|
||||||
$this->content->getField('categories')
|
|
||||||
];
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->link = new ObjectType([
|
|
||||||
'name' => 'Link',
|
|
||||||
'interfaces' => [
|
|
||||||
$this->node,
|
|
||||||
$this->content
|
|
||||||
],
|
|
||||||
'fields' => function() {
|
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
$this->node->getField('id'),
|
||||||
$this->content->getField('title'),
|
$this->content->getField('title'),
|
||||||
@ -135,143 +98,166 @@ class ResolutionTest extends TestCase
|
|||||||
$this->content->getField('author'),
|
$this->content->getField('author'),
|
||||||
$this->content->getField('comments'),
|
$this->content->getField('comments'),
|
||||||
$this->content->getField('categories'),
|
$this->content->getField('categories'),
|
||||||
'url' => Type::string()
|
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
new ObjectType([
|
||||||
|
'name' => 'Link',
|
||||||
|
'interfaces' => [
|
||||||
|
$this->node,
|
||||||
|
$this->content,
|
||||||
|
],
|
||||||
|
'fields' => function () {
|
||||||
|
return [
|
||||||
|
'id' => $this->node->getField('id'),
|
||||||
|
'title' => $this->content->getField('title'),
|
||||||
|
'body' => $this->content->getField('body'),
|
||||||
|
'author' => $this->content->getField('author'),
|
||||||
|
'comments' => $this->content->getField('comments'),
|
||||||
|
'categories' => $this->content->getField('categories'),
|
||||||
|
'url' => Type::string(),
|
||||||
|
];
|
||||||
|
},
|
||||||
|
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->videoMetadata = new ObjectType([
|
||||||
|
'name' => 'VideoMetadata',
|
||||||
|
'fields' => [
|
||||||
|
'lat' => Type::float(),
|
||||||
|
'lng' => Type::float(),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
$this->video = new ObjectType([
|
$this->video = new ObjectType([
|
||||||
'name' => 'Video',
|
'name' => 'Video',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node,
|
$this->node,
|
||||||
$this->content
|
$this->content,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
'id' => $this->node->getField('id'),
|
||||||
$this->content->getField('title'),
|
'title' => $this->content->getField('title'),
|
||||||
$this->content->getField('body'),
|
'body' => $this->content->getField('body'),
|
||||||
$this->content->getField('author'),
|
'author' => $this->content->getField('author'),
|
||||||
$this->content->getField('comments'),
|
'comments' => $this->content->getField('comments'),
|
||||||
$this->content->getField('categories'),
|
'categories' => $this->content->getField('categories'),
|
||||||
'streamUrl' => Type::string(),
|
'streamUrl' => Type::string(),
|
||||||
'downloadUrl' => Type::string(),
|
'downloadUrl' => Type::string(),
|
||||||
'metadata' => $this->videoMetadata = new ObjectType([
|
'metadata' => $this->videoMetadata,
|
||||||
'name' => 'VideoMetadata',
|
|
||||||
'fields' => [
|
|
||||||
'lat' => Type::float(),
|
|
||||||
'lng' => Type::float()
|
|
||||||
]
|
|
||||||
])
|
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->comment = new ObjectType([
|
$this->comment = new ObjectType([
|
||||||
'name' => 'Comment',
|
'name' => 'Comment',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node
|
$this->node,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
'id' => $this->node->getField('id'),
|
||||||
'author' => $this->user,
|
'author' => $this->user,
|
||||||
'text' => Type::string(),
|
'text' => Type::string(),
|
||||||
'replies' => Type::listOf($this->comment),
|
'replies' => Type::listOf($this->comment),
|
||||||
'parent' => $this->comment,
|
'parent' => $this->comment,
|
||||||
'content' => $this->content
|
'content' => $this->content,
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->user = new ObjectType([
|
$this->user = new ObjectType([
|
||||||
'name' => 'User',
|
'name' => 'User',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node
|
$this->node,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
'id' => $this->node->getField('id'),
|
||||||
'name' => Type::string(),
|
'name' => Type::string(),
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->category = new ObjectType([
|
$this->category = new ObjectType([
|
||||||
'name' => 'Category',
|
'name' => 'Category',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node
|
$this->node,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
'id' => $this->node->getField('id'),
|
||||||
'name' => Type::string()
|
'name' => Type::string(),
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->mention = new UnionType([
|
$this->mention = new UnionType([
|
||||||
'name' => 'Mention',
|
'name' => 'Mention',
|
||||||
'types' => [
|
'types' => [
|
||||||
$this->user,
|
$this->user,
|
||||||
$this->category
|
$this->category,
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->query = new ObjectType([
|
$this->query = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'viewer' => $this->user,
|
'viewer' => $this->user,
|
||||||
'latestContent' => $this->content,
|
'latestContent' => $this->content,
|
||||||
'node' => $this->node,
|
'node' => $this->node,
|
||||||
'mentions' => Type::listOf($this->mention)
|
'mentions' => Type::listOf($this->mention),
|
||||||
]
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->postStoryMutationInput = new InputObjectType([
|
||||||
|
'name' => 'PostStoryMutationInput',
|
||||||
|
'fields' => [
|
||||||
|
'title' => Type::string(),
|
||||||
|
'body' => Type::string(),
|
||||||
|
'author' => Type::id(),
|
||||||
|
'category' => Type::id(),
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->mutation = new ObjectType([
|
$this->mutation = new ObjectType([
|
||||||
'name' => 'Mutation',
|
'name' => 'Mutation',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'postStory' => [
|
'postStory' => [
|
||||||
'type' => $this->postStoryMutation = new ObjectType([
|
'type' => $this->postStoryMutation = new ObjectType([
|
||||||
'name' => 'PostStoryMutation',
|
'name' => 'PostStoryMutation',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'story' => $this->blogStory
|
'story' => $this->blogStory,
|
||||||
]
|
],
|
||||||
]),
|
]),
|
||||||
'args' => [
|
'args' => [
|
||||||
'input' => Type::nonNull($this->postStoryMutationInput = new InputObjectType([
|
'input' => Type::nonNull($this->postStoryMutationInput),
|
||||||
'name' => 'PostStoryMutationInput',
|
'clientRequestId' => Type::string(),
|
||||||
'fields' => [
|
],
|
||||||
'title' => Type::string(),
|
|
||||||
'body' => Type::string(),
|
|
||||||
'author' => Type::id(),
|
|
||||||
'category' => Type::id()
|
|
||||||
]
|
|
||||||
])),
|
|
||||||
'clientRequestId' => Type::string()
|
|
||||||
]
|
|
||||||
],
|
],
|
||||||
'postComment' => [
|
'postComment' => [
|
||||||
'type' => $this->postCommentMutation = new ObjectType([
|
'type' => $this->postCommentMutation = new ObjectType([
|
||||||
'name' => 'PostCommentMutation',
|
'name' => 'PostCommentMutation',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'comment' => $this->comment
|
'comment' => $this->comment,
|
||||||
]
|
],
|
||||||
]),
|
]),
|
||||||
'args' => [
|
'args' => [
|
||||||
'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([
|
'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([
|
||||||
'name' => 'PostCommentMutationInput',
|
'name' => 'PostCommentMutationInput',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'text' => Type::nonNull(Type::string()),
|
'text' => Type::nonNull(Type::string()),
|
||||||
'author' => Type::nonNull(Type::id()),
|
'author' => Type::nonNull(Type::id()),
|
||||||
'content' => Type::id(),
|
'content' => Type::id(),
|
||||||
'parent' => Type::id()
|
'parent' => Type::id(),
|
||||||
]
|
],
|
||||||
])),
|
])),
|
||||||
'clientRequestId' => Type::string()
|
'clientRequestId' => Type::string(),
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,25 +265,25 @@ class ResolutionTest extends TestCase
|
|||||||
{
|
{
|
||||||
// Has internal types by default:
|
// Has internal types by default:
|
||||||
$eagerTypeResolution = new EagerResolution([]);
|
$eagerTypeResolution = new EagerResolution([]);
|
||||||
$expectedTypeMap = [
|
$expectedTypeMap = [
|
||||||
'ID' => Type::id(),
|
'ID' => Type::id(),
|
||||||
'String' => Type::string(),
|
'String' => Type::string(),
|
||||||
'Float' => Type::float(),
|
'Float' => Type::float(),
|
||||||
'Int' => Type::int(),
|
'Int' => Type::int(),
|
||||||
'Boolean' => Type::boolean()
|
'Boolean' => Type::boolean(),
|
||||||
];
|
];
|
||||||
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
||||||
|
|
||||||
$expectedDescriptor = [
|
$expectedDescriptor = [
|
||||||
'version' => '1.0',
|
'version' => '1.0',
|
||||||
'typeMap' => [
|
'typeMap' => [
|
||||||
'ID' => 1,
|
'ID' => 1,
|
||||||
'String' => 1,
|
'String' => 1,
|
||||||
'Float' => 1,
|
'Float' => 1,
|
||||||
'Int' => 1,
|
'Int' => 1,
|
||||||
'Boolean' => 1,
|
'Boolean' => 1,
|
||||||
],
|
],
|
||||||
'possibleTypeMap' => []
|
'possibleTypeMap' => [],
|
||||||
];
|
];
|
||||||
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
||||||
|
|
||||||
@ -321,72 +307,76 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertSame($this->postStoryMutation, $eagerTypeResolution->resolveType('PostStoryMutation'));
|
$this->assertSame($this->postStoryMutation, $eagerTypeResolution->resolveType('PostStoryMutation'));
|
||||||
$this->assertSame($this->postStoryMutationInput, $eagerTypeResolution->resolveType('PostStoryMutationInput'));
|
$this->assertSame($this->postStoryMutationInput, $eagerTypeResolution->resolveType('PostStoryMutationInput'));
|
||||||
$this->assertSame($this->postCommentMutation, $eagerTypeResolution->resolveType('PostCommentMutation'));
|
$this->assertSame($this->postCommentMutation, $eagerTypeResolution->resolveType('PostCommentMutation'));
|
||||||
$this->assertSame($this->postCommentMutationInput, $eagerTypeResolution->resolveType('PostCommentMutationInput'));
|
$this->assertSame(
|
||||||
|
$this->postCommentMutationInput,
|
||||||
|
$eagerTypeResolution->resolveType('PostCommentMutationInput')
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals([$this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
$this->assertEquals([$this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
||||||
$this->assertEquals([$this->user, $this->comment, $this->category, $this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->node));
|
$this->assertEquals(
|
||||||
|
[$this->user, $this->comment, $this->category, $this->blogStory],
|
||||||
|
$eagerTypeResolution->resolvePossibleTypes($this->node)
|
||||||
|
);
|
||||||
$this->assertEquals([$this->user, $this->category], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
$this->assertEquals([$this->user, $this->category], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
||||||
|
|
||||||
$expectedTypeMap = [
|
$expectedTypeMap = [
|
||||||
'Query' => $this->query,
|
'Query' => $this->query,
|
||||||
'Mutation' => $this->mutation,
|
'Mutation' => $this->mutation,
|
||||||
'User' => $this->user,
|
'User' => $this->user,
|
||||||
'Node' => $this->node,
|
'Node' => $this->node,
|
||||||
'String' => Type::string(),
|
'String' => Type::string(),
|
||||||
'Content' => $this->content,
|
'Content' => $this->content,
|
||||||
'Comment' => $this->comment,
|
'Comment' => $this->comment,
|
||||||
'Mention' => $this->mention,
|
'Mention' => $this->mention,
|
||||||
'BlogStory' => $this->blogStory,
|
'BlogStory' => $this->blogStory,
|
||||||
'Category' => $this->category,
|
'Category' => $this->category,
|
||||||
'PostStoryMutationInput' => $this->postStoryMutationInput,
|
'PostStoryMutationInput' => $this->postStoryMutationInput,
|
||||||
'ID' => Type::id(),
|
'ID' => Type::id(),
|
||||||
'PostStoryMutation' => $this->postStoryMutation,
|
'PostStoryMutation' => $this->postStoryMutation,
|
||||||
'PostCommentMutationInput' => $this->postCommentMutationInput,
|
'PostCommentMutationInput' => $this->postCommentMutationInput,
|
||||||
'PostCommentMutation' => $this->postCommentMutation,
|
'PostCommentMutation' => $this->postCommentMutation,
|
||||||
'Float' => Type::float(),
|
'Float' => Type::float(),
|
||||||
'Int' => Type::int(),
|
'Int' => Type::int(),
|
||||||
'Boolean' => Type::boolean()
|
'Boolean' => Type::boolean(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
||||||
|
|
||||||
$expectedDescriptor = [
|
$expectedDescriptor = [
|
||||||
'version' => '1.0',
|
'version' => '1.0',
|
||||||
'typeMap' => [
|
'typeMap' => [
|
||||||
'Query' => 1,
|
'Query' => 1,
|
||||||
'Mutation' => 1,
|
'Mutation' => 1,
|
||||||
'User' => 1,
|
'User' => 1,
|
||||||
'Node' => 1,
|
'Node' => 1,
|
||||||
'String' => 1,
|
'String' => 1,
|
||||||
'Content' => 1,
|
'Content' => 1,
|
||||||
'Comment' => 1,
|
'Comment' => 1,
|
||||||
'Mention' => 1,
|
'Mention' => 1,
|
||||||
'BlogStory' => 1,
|
'BlogStory' => 1,
|
||||||
'Category' => 1,
|
'Category' => 1,
|
||||||
'PostStoryMutationInput' => 1,
|
'PostStoryMutationInput' => 1,
|
||||||
'ID' => 1,
|
'ID' => 1,
|
||||||
'PostStoryMutation' => 1,
|
'PostStoryMutation' => 1,
|
||||||
'PostCommentMutationInput' => 1,
|
'PostCommentMutationInput' => 1,
|
||||||
'PostCommentMutation' => 1,
|
'PostCommentMutation' => 1,
|
||||||
'Float' => 1,
|
'Float' => 1,
|
||||||
'Int' => 1,
|
'Int' => 1,
|
||||||
'Boolean' => 1
|
'Boolean' => 1,
|
||||||
],
|
],
|
||||||
'possibleTypeMap' => [
|
'possibleTypeMap' => [
|
||||||
'Node' => [
|
'Node' => [
|
||||||
'User' => 1,
|
'User' => 1,
|
||||||
'Comment' => 1,
|
'Comment' => 1,
|
||||||
'Category' => 1,
|
'Category' => 1,
|
||||||
'BlogStory' => 1
|
'BlogStory' => 1,
|
||||||
],
|
|
||||||
'Content' => [
|
|
||||||
'BlogStory' => 1
|
|
||||||
],
|
],
|
||||||
|
'Content' => ['BlogStory' => 1],
|
||||||
'Mention' => [
|
'Mention' => [
|
||||||
'User' => 1,
|
'User' => 1,
|
||||||
'Category' => 1
|
'Category' => 1,
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
||||||
@ -402,7 +392,10 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertEquals(null, $eagerTypeResolution->resolveType('VideoMetadata'));
|
$this->assertEquals(null, $eagerTypeResolution->resolveType('VideoMetadata'));
|
||||||
|
|
||||||
$this->assertEquals([$this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
$this->assertEquals([$this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
||||||
$this->assertEquals([$this->user, $this->comment, $this->category, $this->blogStory], $eagerTypeResolution->resolvePossibleTypes($this->node));
|
$this->assertEquals(
|
||||||
|
[$this->user, $this->comment, $this->category, $this->blogStory],
|
||||||
|
$eagerTypeResolution->resolvePossibleTypes($this->node)
|
||||||
|
);
|
||||||
$this->assertEquals([$this->user, $this->category], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
$this->assertEquals([$this->user, $this->category], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
||||||
|
|
||||||
$eagerTypeResolution = new EagerResolution([null, $this->video, null]);
|
$eagerTypeResolution = new EagerResolution([null, $this->video, null]);
|
||||||
@ -410,52 +403,53 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertEquals($this->video, $eagerTypeResolution->resolveType('Video'));
|
$this->assertEquals($this->video, $eagerTypeResolution->resolveType('Video'));
|
||||||
|
|
||||||
$this->assertEquals([$this->video], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
$this->assertEquals([$this->video], $eagerTypeResolution->resolvePossibleTypes($this->content));
|
||||||
$this->assertEquals([$this->video, $this->user, $this->comment, $this->category], $eagerTypeResolution->resolvePossibleTypes($this->node));
|
$this->assertEquals(
|
||||||
|
[$this->video, $this->user, $this->comment, $this->category],
|
||||||
|
$eagerTypeResolution->resolvePossibleTypes($this->node)
|
||||||
|
);
|
||||||
$this->assertEquals([], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
$this->assertEquals([], $eagerTypeResolution->resolvePossibleTypes($this->mention));
|
||||||
|
|
||||||
$expectedTypeMap = [
|
$expectedTypeMap = [
|
||||||
'Video' => $this->video,
|
'Video' => $this->video,
|
||||||
'Node' => $this->node,
|
'Node' => $this->node,
|
||||||
'String' => Type::string(),
|
'String' => Type::string(),
|
||||||
'Content' => $this->content,
|
'Content' => $this->content,
|
||||||
'User' => $this->user,
|
'User' => $this->user,
|
||||||
'Comment' => $this->comment,
|
'Comment' => $this->comment,
|
||||||
'Category' => $this->category,
|
'Category' => $this->category,
|
||||||
'VideoMetadata' => $this->videoMetadata,
|
'VideoMetadata' => $this->videoMetadata,
|
||||||
'Float' => Type::float(),
|
'Float' => Type::float(),
|
||||||
'ID' => Type::id(),
|
'ID' => Type::id(),
|
||||||
'Int' => Type::int(),
|
'Int' => Type::int(),
|
||||||
'Boolean' => Type::boolean()
|
'Boolean' => Type::boolean(),
|
||||||
];
|
];
|
||||||
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
|
||||||
|
|
||||||
$expectedDescriptor = [
|
$expectedDescriptor = [
|
||||||
'version' => '1.0',
|
'version' => '1.0',
|
||||||
'typeMap' => [
|
'typeMap' => [
|
||||||
'Video' => 1,
|
'Video' => 1,
|
||||||
'Node' => 1,
|
'Node' => 1,
|
||||||
'String' => 1,
|
'String' => 1,
|
||||||
'Content' => 1,
|
'Content' => 1,
|
||||||
'User' => 1,
|
'User' => 1,
|
||||||
'Comment' => 1,
|
'Comment' => 1,
|
||||||
'Category' => 1,
|
'Category' => 1,
|
||||||
'VideoMetadata' => 1,
|
'VideoMetadata' => 1,
|
||||||
'Float' => 1,
|
'Float' => 1,
|
||||||
'ID' => 1,
|
'ID' => 1,
|
||||||
'Int' => 1,
|
'Int' => 1,
|
||||||
'Boolean' => 1
|
'Boolean' => 1,
|
||||||
],
|
],
|
||||||
'possibleTypeMap' => [
|
'possibleTypeMap' => [
|
||||||
'Node' => [
|
'Node' => [
|
||||||
'Video' => 1,
|
'Video' => 1,
|
||||||
'User' => 1,
|
'User' => 1,
|
||||||
'Comment' => 1,
|
'Comment' => 1,
|
||||||
'Category' => 1
|
'Category' => 1,
|
||||||
],
|
],
|
||||||
'Content' => [
|
'Content' => ['Video' => 1],
|
||||||
'Video' => 1
|
],
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
|
||||||
}
|
}
|
||||||
@ -463,11 +457,11 @@ class ResolutionTest extends TestCase
|
|||||||
public function testLazyResolutionFollowsEagerResolution() : void
|
public function testLazyResolutionFollowsEagerResolution() : void
|
||||||
{
|
{
|
||||||
// Lazy resolution should work the same way as eager resolution works, except that it should load types on demand
|
// Lazy resolution should work the same way as eager resolution works, except that it should load types on demand
|
||||||
$eager = new EagerResolution([]);
|
$eager = new EagerResolution([]);
|
||||||
$emptyDescriptor = $eager->getDescriptor();
|
$emptyDescriptor = $eager->getDescriptor();
|
||||||
|
|
||||||
$typeLoader = function($name) {
|
$typeLoader = function ($name) {
|
||||||
throw new \Exception("This should be never called for empty descriptor");
|
throw new \Exception('This should be never called for empty descriptor');
|
||||||
};
|
};
|
||||||
|
|
||||||
$lazy = new LazyResolution($emptyDescriptor, $typeLoader);
|
$lazy = new LazyResolution($emptyDescriptor, $typeLoader);
|
||||||
@ -478,11 +472,12 @@ class ResolutionTest extends TestCase
|
|||||||
|
|
||||||
$eager = new EagerResolution([$this->query, $this->mutation]);
|
$eager = new EagerResolution([$this->query, $this->mutation]);
|
||||||
|
|
||||||
$called = 0;
|
$called = 0;
|
||||||
$descriptor = $eager->getDescriptor();
|
$descriptor = $eager->getDescriptor();
|
||||||
$typeLoader = function($name) use (&$called) {
|
$typeLoader = function ($name) use (&$called) {
|
||||||
$called++;
|
$called++;
|
||||||
$prop = lcfirst($name);
|
$prop = lcfirst($name);
|
||||||
|
|
||||||
return $this->{$prop};
|
return $this->{$prop};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -507,7 +502,10 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertSame($eager->resolveType('PostStoryMutation'), $lazy->resolveType('PostStoryMutation'));
|
$this->assertSame($eager->resolveType('PostStoryMutation'), $lazy->resolveType('PostStoryMutation'));
|
||||||
$this->assertSame($eager->resolveType('PostStoryMutationInput'), $lazy->resolveType('PostStoryMutationInput'));
|
$this->assertSame($eager->resolveType('PostStoryMutationInput'), $lazy->resolveType('PostStoryMutationInput'));
|
||||||
$this->assertSame($eager->resolveType('PostCommentMutation'), $lazy->resolveType('PostCommentMutation'));
|
$this->assertSame($eager->resolveType('PostCommentMutation'), $lazy->resolveType('PostCommentMutation'));
|
||||||
$this->assertSame($eager->resolveType('PostCommentMutationInput'), $lazy->resolveType('PostCommentMutationInput'));
|
$this->assertSame(
|
||||||
|
$eager->resolveType('PostCommentMutationInput'),
|
||||||
|
$lazy->resolveType('PostCommentMutationInput')
|
||||||
|
);
|
||||||
$this->assertSame(13, $called);
|
$this->assertSame(13, $called);
|
||||||
|
|
||||||
$this->assertEquals($eager->resolvePossibleTypes($this->content), $lazy->resolvePossibleTypes($this->content));
|
$this->assertEquals($eager->resolvePossibleTypes($this->content), $lazy->resolvePossibleTypes($this->content));
|
||||||
@ -515,8 +513,8 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertEquals($eager->resolvePossibleTypes($this->mention), $lazy->resolvePossibleTypes($this->mention));
|
$this->assertEquals($eager->resolvePossibleTypes($this->mention), $lazy->resolvePossibleTypes($this->mention));
|
||||||
|
|
||||||
$called = 0;
|
$called = 0;
|
||||||
$eager = new EagerResolution([$this->video]);
|
$eager = new EagerResolution([$this->video]);
|
||||||
$lazy = new LazyResolution($eager->getDescriptor(), $typeLoader);
|
$lazy = new LazyResolution($eager->getDescriptor(), $typeLoader);
|
||||||
|
|
||||||
$this->assertEquals($eager->resolveType('VideoMetadata'), $lazy->resolveType('VideoMetadata'));
|
$this->assertEquals($eager->resolveType('VideoMetadata'), $lazy->resolveType('VideoMetadata'));
|
||||||
$this->assertEquals($eager->resolveType('Video'), $lazy->resolveType('Video'));
|
$this->assertEquals($eager->resolveType('Video'), $lazy->resolveType('Video'));
|
||||||
@ -527,40 +525,6 @@ class ResolutionTest extends TestCase
|
|||||||
$this->assertEquals($eager->resolvePossibleTypes($this->mention), $lazy->resolvePossibleTypes($this->mention));
|
$this->assertEquals($eager->resolvePossibleTypes($this->mention), $lazy->resolvePossibleTypes($this->mention));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createLazy(){
|
|
||||||
|
|
||||||
$descriptor = [
|
|
||||||
'version' => '1.0',
|
|
||||||
'typeMap' => [
|
|
||||||
'null' => 1,
|
|
||||||
'int' => 1
|
|
||||||
],
|
|
||||||
'possibleTypeMap' => [
|
|
||||||
'a' => [
|
|
||||||
'null' => 1,
|
|
||||||
],
|
|
||||||
'b' => [
|
|
||||||
'int' => 1
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
$invalidTypeLoader = function($name) {
|
|
||||||
switch ($name) {
|
|
||||||
case 'null':
|
|
||||||
return null;
|
|
||||||
case 'int':
|
|
||||||
return 7;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$lazy = new LazyResolution($descriptor, $invalidTypeLoader);
|
|
||||||
$value = $lazy->resolveType('null');
|
|
||||||
$this->assertEquals(null, $value);
|
|
||||||
|
|
||||||
return $lazy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLazyThrowsOnInvalidLoadedType() : void
|
public function testLazyThrowsOnInvalidLoadedType() : void
|
||||||
{
|
{
|
||||||
$lazy = $this->createLazy();
|
$lazy = $this->createLazy();
|
||||||
@ -569,9 +533,39 @@ class ResolutionTest extends TestCase
|
|||||||
$lazy->resolveType('int');
|
$lazy->resolveType('int');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createLazy()
|
||||||
|
{
|
||||||
|
$descriptor = [
|
||||||
|
'version' => '1.0',
|
||||||
|
'typeMap' => [
|
||||||
|
'null' => 1,
|
||||||
|
'int' => 1,
|
||||||
|
],
|
||||||
|
'possibleTypeMap' => [
|
||||||
|
'a' => ['null' => 1],
|
||||||
|
'b' => ['int' => 1],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$invalidTypeLoader = function ($name) {
|
||||||
|
switch ($name) {
|
||||||
|
case 'null':
|
||||||
|
return null;
|
||||||
|
case 'int':
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$lazy = new LazyResolution($descriptor, $invalidTypeLoader);
|
||||||
|
$value = $lazy->resolveType('null');
|
||||||
|
$this->assertEquals(null, $value);
|
||||||
|
|
||||||
|
return $lazy;
|
||||||
|
}
|
||||||
|
|
||||||
public function testLazyThrowsOnInvalidLoadedPossibleType() : void
|
public function testLazyThrowsOnInvalidLoadedPossibleType() : void
|
||||||
{
|
{
|
||||||
$tmp = new InterfaceType(['name' => 'a', 'fields' => []]);
|
$tmp = new InterfaceType(['name' => 'a', 'fields' => []]);
|
||||||
$lazy = $this->createLazy();
|
$lazy = $this->createLazy();
|
||||||
$this->expectException(InvariantViolation::class);
|
$this->expectException(InvariantViolation::class);
|
||||||
$this->expectExceptionMessage('Lazy Type Resolution Error: Implementation null of interface a is expected to be instance of ObjectType, but got NULL');
|
$this->expectExceptionMessage('Lazy Type Resolution Error: Implementation null of interface a is expected to be instance of ObjectType, but got NULL');
|
||||||
@ -580,7 +574,7 @@ class ResolutionTest extends TestCase
|
|||||||
|
|
||||||
public function testLazyThrowsOnInvalidLoadedPossibleTypeWithInteger() : void
|
public function testLazyThrowsOnInvalidLoadedPossibleTypeWithInteger() : void
|
||||||
{
|
{
|
||||||
$tmp = new InterfaceType(['name' => 'b', 'fields' => []]);
|
$tmp = new InterfaceType(['name' => 'b', 'fields' => []]);
|
||||||
$lazy = $this->createLazy();
|
$lazy = $this->createLazy();
|
||||||
$this->expectException(InvariantViolation::class);
|
$this->expectException(InvariantViolation::class);
|
||||||
$this->expectExceptionMessage('Lazy Type Resolution Error: Expecting GraphQL Type instance, but got integer');
|
$this->expectExceptionMessage('Lazy Type Resolution Error: Expecting GraphQL Type instance, but got integer');
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\GraphQL;
|
use GraphQL\GraphQL;
|
||||||
use GraphQL\Type\Schema;
|
|
||||||
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 PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class ResolveInfoTest extends TestCase
|
class ResolveInfoTest extends TestCase
|
||||||
@ -13,53 +16,56 @@ class ResolveInfoTest extends TestCase
|
|||||||
public function testFieldSelection() : void
|
public function testFieldSelection() : void
|
||||||
{
|
{
|
||||||
$image = new ObjectType([
|
$image = new ObjectType([
|
||||||
'name' => 'Image',
|
'name' => 'Image',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'url' => ['type' => Type::string()],
|
'url' => ['type' => Type::string()],
|
||||||
'width' => ['type' => Type::int()],
|
'width' => ['type' => Type::int()],
|
||||||
'height' => ['type' => Type::int()]
|
'height' => ['type' => Type::int()],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$article = null;
|
$article = null;
|
||||||
|
|
||||||
$author = new ObjectType([
|
$author = new ObjectType([
|
||||||
'name' => 'Author',
|
'name' => 'Author',
|
||||||
'fields' => function() use ($image, &$article) {
|
'fields' => function () use ($image, &$article) {
|
||||||
return [
|
return [
|
||||||
'id' => ['type' => Type::string()],
|
'id' => ['type' => Type::string()],
|
||||||
'name' => ['type' => Type::string()],
|
'name' => ['type' => Type::string()],
|
||||||
'pic' => [ 'type' => $image, 'args' => [
|
'pic' => [
|
||||||
'width' => ['type' => Type::int()],
|
'type' => $image,
|
||||||
'height' => ['type' => Type::int()]
|
'args' => [
|
||||||
]],
|
'width' => ['type' => Type::int()],
|
||||||
|
'height' => ['type' => Type::int()],
|
||||||
|
],
|
||||||
|
],
|
||||||
'recentArticle' => ['type' => $article],
|
'recentArticle' => ['type' => $article],
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$reply = new ObjectType([
|
$reply = new ObjectType([
|
||||||
'name' => 'Reply',
|
'name' => 'Reply',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'author' => ['type' => $author],
|
'author' => ['type' => $author],
|
||||||
'body' => ['type' => Type::string()]
|
'body' => ['type' => Type::string()],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$article = new ObjectType([
|
$article = new ObjectType([
|
||||||
'name' => 'Article',
|
'name' => 'Article',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'id' => ['type' => Type::string()],
|
'id' => ['type' => Type::string()],
|
||||||
'isPublished' => ['type' => Type::boolean()],
|
'isPublished' => ['type' => Type::boolean()],
|
||||||
'author' => ['type' => $author],
|
'author' => ['type' => $author],
|
||||||
'title' => ['type' => Type::string()],
|
'title' => ['type' => Type::string()],
|
||||||
'body' => ['type' => Type::string()],
|
'body' => ['type' => Type::string()],
|
||||||
'image' => ['type' => $image],
|
'image' => ['type' => $image],
|
||||||
'replies' => ['type' => Type::listOf($reply)]
|
'replies' => ['type' => Type::listOf($reply)],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$doc = '
|
$doc = '
|
||||||
query Test {
|
query Test {
|
||||||
article {
|
article {
|
||||||
author {
|
author {
|
||||||
@ -100,59 +106,70 @@ class ResolveInfoTest extends TestCase
|
|||||||
}
|
}
|
||||||
';
|
';
|
||||||
$expectedDefaultSelection = [
|
$expectedDefaultSelection = [
|
||||||
'author' => true,
|
'author' => true,
|
||||||
'image' => true,
|
'image' => true,
|
||||||
'replies' => true
|
'replies' => true,
|
||||||
];
|
];
|
||||||
$expectedDeepSelection = [
|
$expectedDeepSelection = [
|
||||||
'author' => [
|
'author' => [
|
||||||
'name' => true,
|
'name' => true,
|
||||||
'pic' => [
|
'pic' => [
|
||||||
'url' => true,
|
'url' => true,
|
||||||
'width' => true
|
'width' => true,
|
||||||
]
|
],
|
||||||
],
|
],
|
||||||
'image' => [
|
'image' => [
|
||||||
'width' => true,
|
'width' => true,
|
||||||
'height' => true,
|
'height' => true,
|
||||||
'url' => true
|
'url' => true,
|
||||||
],
|
],
|
||||||
'replies' => [
|
'replies' => [
|
||||||
'body' => true,
|
'body' => true,
|
||||||
'author' => [
|
'author' => [
|
||||||
'id' => true,
|
'id' => true,
|
||||||
'name' => true,
|
'name' => true,
|
||||||
'pic' => [
|
'pic' => [
|
||||||
'url' => true,
|
'url' => true,
|
||||||
'width' => true,
|
'width' => true,
|
||||||
'height' => true
|
'height' => true,
|
||||||
],
|
],
|
||||||
'recentArticle' => [
|
'recentArticle' => [
|
||||||
'id' => true,
|
'id' => true,
|
||||||
'title' => true,
|
'title' => true,
|
||||||
'body' => true
|
'body' => true,
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$hasCalled = false;
|
$hasCalled = false;
|
||||||
$actualDefaultSelection = null;
|
$actualDefaultSelection = null;
|
||||||
$actualDeepSelection = null;
|
$actualDeepSelection = null;
|
||||||
|
|
||||||
$blogQuery = new ObjectType([
|
$blogQuery = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'article' => [
|
'article' => [
|
||||||
'type' => $article,
|
'type' => $article,
|
||||||
'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDefaultSelection, &$actualDeepSelection) {
|
'resolve' => function (
|
||||||
$hasCalled = true;
|
$value,
|
||||||
|
$args,
|
||||||
|
$context,
|
||||||
|
ResolveInfo $info
|
||||||
|
) use (
|
||||||
|
&$hasCalled,
|
||||||
|
&
|
||||||
|
$actualDefaultSelection,
|
||||||
|
&$actualDeepSelection
|
||||||
|
) {
|
||||||
|
$hasCalled = true;
|
||||||
$actualDefaultSelection = $info->getFieldSelection();
|
$actualDefaultSelection = $info->getFieldSelection();
|
||||||
$actualDeepSelection = $info->getFieldSelection(5);
|
$actualDeepSelection = $info->getFieldSelection(5);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$schema = new Schema(['query' => $blogQuery]);
|
$schema = new Schema(['query' => $blogQuery]);
|
||||||
@ -167,50 +184,53 @@ class ResolveInfoTest extends TestCase
|
|||||||
public function testMergedFragmentsFieldSelection() : void
|
public function testMergedFragmentsFieldSelection() : void
|
||||||
{
|
{
|
||||||
$image = new ObjectType([
|
$image = new ObjectType([
|
||||||
'name' => 'Image',
|
'name' => 'Image',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'url' => ['type' => Type::string()],
|
'url' => ['type' => Type::string()],
|
||||||
'width' => ['type' => Type::int()],
|
'width' => ['type' => Type::int()],
|
||||||
'height' => ['type' => Type::int()]
|
'height' => ['type' => Type::int()],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$article = null;
|
$article = null;
|
||||||
|
|
||||||
$author = new ObjectType([
|
$author = new ObjectType([
|
||||||
'name' => 'Author',
|
'name' => 'Author',
|
||||||
'fields' => function() use ($image, &$article) {
|
'fields' => function () use ($image, &$article) {
|
||||||
return [
|
return [
|
||||||
'id' => ['type' => Type::string()],
|
'id' => ['type' => Type::string()],
|
||||||
'name' => ['type' => Type::string()],
|
'name' => ['type' => Type::string()],
|
||||||
'pic' => [ 'type' => $image, 'args' => [
|
'pic' => [
|
||||||
'width' => ['type' => Type::int()],
|
'type' => $image,
|
||||||
'height' => ['type' => Type::int()]
|
'args' => [
|
||||||
]],
|
'width' => ['type' => Type::int()],
|
||||||
|
'height' => ['type' => Type::int()],
|
||||||
|
],
|
||||||
|
],
|
||||||
'recentArticle' => ['type' => $article],
|
'recentArticle' => ['type' => $article],
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$reply = new ObjectType([
|
$reply = new ObjectType([
|
||||||
'name' => 'Reply',
|
'name' => 'Reply',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'author' => ['type' => $author],
|
'author' => ['type' => $author],
|
||||||
'body' => ['type' => Type::string()]
|
'body' => ['type' => Type::string()],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$article = new ObjectType([
|
$article = new ObjectType([
|
||||||
'name' => 'Article',
|
'name' => 'Article',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'id' => ['type' => Type::string()],
|
'id' => ['type' => Type::string()],
|
||||||
'isPublished' => ['type' => Type::boolean()],
|
'isPublished' => ['type' => Type::boolean()],
|
||||||
'author' => ['type' => $author],
|
'author' => ['type' => $author],
|
||||||
'title' => ['type' => Type::string()],
|
'title' => ['type' => Type::string()],
|
||||||
'body' => ['type' => Type::string()],
|
'body' => ['type' => Type::string()],
|
||||||
'image' => ['type' => $image],
|
'image' => ['type' => $image],
|
||||||
'replies' => ['type' => Type::listOf($reply)]
|
'replies' => ['type' => Type::listOf($reply)],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$doc = '
|
$doc = '
|
||||||
@ -264,53 +284,63 @@ class ResolveInfoTest extends TestCase
|
|||||||
';
|
';
|
||||||
|
|
||||||
$expectedDeepSelection = [
|
$expectedDeepSelection = [
|
||||||
'author' => [
|
'author' => [
|
||||||
'name' => true,
|
'name' => true,
|
||||||
'pic' => [
|
'pic' => [
|
||||||
'url' => true,
|
'url' => true,
|
||||||
'width' => true
|
'width' => true,
|
||||||
]
|
],
|
||||||
],
|
],
|
||||||
'image' => [
|
'image' => [
|
||||||
'width' => true,
|
'width' => true,
|
||||||
'height' => true,
|
'height' => true,
|
||||||
'url' => true
|
'url' => true,
|
||||||
],
|
],
|
||||||
'replies' => [
|
'replies' => [
|
||||||
'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98
|
'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98
|
||||||
'author' => [
|
'author' => [
|
||||||
'id' => true,
|
'id' => true,
|
||||||
'name' => true,
|
'name' => true,
|
||||||
'pic' => [
|
'pic' => [
|
||||||
'url' => true,
|
'url' => true,
|
||||||
'width' => true,
|
'width' => true,
|
||||||
'height' => true
|
'height' => true,
|
||||||
],
|
],
|
||||||
'recentArticle' => [
|
'recentArticle' => [
|
||||||
'id' => true,
|
'id' => true,
|
||||||
'title' => true,
|
'title' => true,
|
||||||
'body' => true
|
'body' => true,
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$hasCalled = false;
|
$hasCalled = false;
|
||||||
$actualDefaultSelection = null;
|
$actualDefaultSelection = null;
|
||||||
$actualDeepSelection = null;
|
$actualDeepSelection = null;
|
||||||
|
|
||||||
$blogQuery = new ObjectType([
|
$blogQuery = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'article' => [
|
'article' => [
|
||||||
'type' => $article,
|
'type' => $article,
|
||||||
'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDeepSelection) {
|
'resolve' => function (
|
||||||
$hasCalled = true;
|
$value,
|
||||||
|
$args,
|
||||||
|
$context,
|
||||||
|
ResolveInfo $info
|
||||||
|
) use (
|
||||||
|
&$hasCalled,
|
||||||
|
&
|
||||||
|
$actualDeepSelection
|
||||||
|
) {
|
||||||
|
$hasCalled = true;
|
||||||
$actualDeepSelection = $info->getFieldSelection(5);
|
$actualDeepSelection = $info->getFieldSelection(5);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$schema = new Schema(['query' => $blogQuery]);
|
$schema = new Schema(['query' => $blogQuery]);
|
||||||
@ -320,6 +350,4 @@ class ResolveInfoTest extends TestCase
|
|||||||
$this->assertEquals(['data' => ['article' => null]], $result);
|
$this->assertEquals(['data' => ['article' => null]], $result);
|
||||||
$this->assertEquals($expectedDeepSelection, $actualDeepSelection);
|
$this->assertEquals($expectedDeepSelection, $actualDeepSelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\Error\Error;
|
use GraphQL\Error\Error;
|
||||||
@ -8,7 +11,6 @@ use PHPUnit\Framework\TestCase;
|
|||||||
class ScalarSerializationTest extends TestCase
|
class ScalarSerializationTest extends TestCase
|
||||||
{
|
{
|
||||||
// Type System: Scalar coercion
|
// Type System: Scalar coercion
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see it('serializes output int')
|
* @see it('serializes output int')
|
||||||
*/
|
*/
|
||||||
@ -42,7 +44,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->expectException(Error::class);
|
$this->expectException(Error::class);
|
||||||
$this->expectExceptionMessage('Int cannot represent non-integer value: 1.1');
|
$this->expectExceptionMessage('Int cannot represent non-integer value: 1.1');
|
||||||
$intType->serialize(1.1);
|
$intType->serialize(1.1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerializesOutputIntCannotRepresentNegativeFloat() : void
|
public function testSerializesOutputIntCannotRepresentNegativeFloat() : void
|
||||||
@ -51,7 +52,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->expectException(Error::class);
|
$this->expectException(Error::class);
|
||||||
$this->expectExceptionMessage('Int cannot represent non-integer value: -1.1');
|
$this->expectExceptionMessage('Int cannot represent non-integer value: -1.1');
|
||||||
$intType->serialize(-1.1);
|
$intType->serialize(-1.1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerializesOutputIntCannotRepresentNumericString() : void
|
public function testSerializesOutputIntCannotRepresentNumericString() : void
|
||||||
@ -60,7 +60,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->expectException(Error::class);
|
$this->expectException(Error::class);
|
||||||
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: Int cannot represent non-integer value: "-1.1"');
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: Int cannot represent non-integer value: "-1.1"');
|
||||||
$intType->serialize('Int cannot represent non-integer value: "-1.1"');
|
$intType->serialize('Int cannot represent non-integer value: "-1.1"');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerializesOutputIntCannotRepresentBiggerThan32Bits() : void
|
public function testSerializesOutputIntCannotRepresentBiggerThan32Bits() : void
|
||||||
@ -71,7 +70,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->expectException(Error::class);
|
$this->expectException(Error::class);
|
||||||
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 9876504321');
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 9876504321');
|
||||||
$intType->serialize(9876504321);
|
$intType->serialize(9876504321);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerializesOutputIntCannotRepresentLowerThan32Bits() : void
|
public function testSerializesOutputIntCannotRepresentLowerThan32Bits() : void
|
||||||
@ -104,7 +102,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->expectException(Error::class);
|
$this->expectException(Error::class);
|
||||||
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: one');
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: one');
|
||||||
$intType->serialize('one');
|
$intType->serialize('one');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerializesOutputIntCannotRepresentEmptyString() : void
|
public function testSerializesOutputIntCannotRepresentEmptyString() : void
|
||||||
@ -196,7 +193,6 @@ class ScalarSerializationTest extends TestCase
|
|||||||
$this->assertSame(false, $boolType->serialize(0));
|
$this->assertSame(false, $boolType->serialize(0));
|
||||||
$this->assertSame(true, $boolType->serialize(true));
|
$this->assertSame(true, $boolType->serialize(true));
|
||||||
$this->assertSame(false, $boolType->serialize(false));
|
$this->assertSame(false, $boolType->serialize(false));
|
||||||
|
|
||||||
// TODO: how should it behave on '0'?
|
// TODO: how should it behave on '0'?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\InvariantViolation;
|
||||||
@ -12,14 +15,19 @@ use PHPUnit\Framework\TestCase;
|
|||||||
|
|
||||||
class SchemaTest extends TestCase
|
class SchemaTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/** @var InterfaceType */
|
||||||
private $interfaceType;
|
private $interfaceType;
|
||||||
|
|
||||||
|
/** @var ObjectType */
|
||||||
private $implementingType;
|
private $implementingType;
|
||||||
|
|
||||||
|
/** @var InputObjectType */
|
||||||
private $directiveInputType;
|
private $directiveInputType;
|
||||||
|
|
||||||
|
/** @var InputObjectType */
|
||||||
private $wrappedDirectiveInputType;
|
private $wrappedDirectiveInputType;
|
||||||
|
|
||||||
|
/** @var Directive */
|
||||||
private $directive;
|
private $directive;
|
||||||
|
|
||||||
/** @var Schema */
|
/** @var Schema */
|
||||||
@ -28,20 +36,25 @@ class SchemaTest extends TestCase
|
|||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->interfaceType = new InterfaceType([
|
$this->interfaceType = new InterfaceType([
|
||||||
'name' => 'Interface',
|
'name' => 'Interface',
|
||||||
'fields' => ['fieldName' => ['type' => Type::string()]],
|
'fields' => ['fieldName' => ['type' => Type::string()]],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->implementingType = new ObjectType([
|
$this->implementingType = new ObjectType([
|
||||||
'name' => 'Object',
|
'name' => 'Object',
|
||||||
'interfaces' => [$this->interfaceType],
|
'interfaces' => [$this->interfaceType],
|
||||||
'fields' => ['fieldName' => ['type' => Type::string(), 'resolve' => function () {
|
'fields' => [
|
||||||
|
'fieldName' => [
|
||||||
|
'type' => Type::string(),
|
||||||
|
'resolve' => function () {
|
||||||
return '';
|
return '';
|
||||||
}]],
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->directiveInputType = new InputObjectType([
|
$this->directiveInputType = new InputObjectType([
|
||||||
'name' => 'DirInput',
|
'name' => 'DirInput',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'field' => [
|
'field' => [
|
||||||
'type' => Type::string(),
|
'type' => Type::string(),
|
||||||
@ -50,7 +63,7 @@ class SchemaTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->wrappedDirectiveInputType = new InputObjectType([
|
$this->wrappedDirectiveInputType = new InputObjectType([
|
||||||
'name' => 'WrappedDirInput',
|
'name' => 'WrappedDirInput',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'field' => [
|
'field' => [
|
||||||
'type' => Type::string(),
|
'type' => Type::string(),
|
||||||
@ -59,10 +72,10 @@ class SchemaTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->directive = new Directive([
|
$this->directive = new Directive([
|
||||||
'name' => 'dir',
|
'name' => 'dir',
|
||||||
'locations' => ['OBJECT'],
|
'locations' => ['OBJECT'],
|
||||||
'args' => [
|
'args' => [
|
||||||
'arg' => [
|
'arg' => [
|
||||||
'type' => $this->directiveInputType,
|
'type' => $this->directiveInputType,
|
||||||
],
|
],
|
||||||
'argList' => [
|
'argList' => [
|
||||||
@ -72,11 +85,11 @@ class SchemaTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->schema = new Schema([
|
$this->schema = new Schema([
|
||||||
'query' => new ObjectType([
|
'query' => new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'getObject' => [
|
'getObject' => [
|
||||||
'type' => $this->interfaceType,
|
'type' => $this->interfaceType,
|
||||||
'resolve' => function () {
|
'resolve' => function () {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace GraphQL\Tests\Type;
|
|
||||||
|
|
||||||
use GraphQL\Type\Definition\ObjectType;
|
|
||||||
use GraphQL\Type\Definition\Type;
|
|
||||||
|
|
||||||
class MyCustomType extends ObjectType
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$config = [
|
|
||||||
'fields' => [
|
|
||||||
'a' => Type::string()
|
|
||||||
]
|
|
||||||
];
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: named OtherCustom vs OtherCustomType intentionally
|
|
||||||
class OtherCustom extends ObjectType
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$config = [
|
|
||||||
'fields' => [
|
|
||||||
'b' => Type::string()
|
|
||||||
]
|
|
||||||
];
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
}
|
|
21
tests/Type/TestClasses/MyCustomType.php
Normal file
21
tests/Type/TestClasses/MyCustomType.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace GraphQL\Tests\Type\TestClasses;
|
||||||
|
|
||||||
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
|
use GraphQL\Type\Definition\Type;
|
||||||
|
|
||||||
|
class MyCustomType extends ObjectType
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'fields' => [
|
||||||
|
'a' => Type::string(),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
}
|
24
tests/Type/TestClasses/OtherCustom.php
Normal file
24
tests/Type/TestClasses/OtherCustom.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace GraphQL\Tests\Type\TestClasses;
|
||||||
|
|
||||||
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
|
use GraphQL\Type\Definition\Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: named OtherCustom vs OtherCustomType intentionally
|
||||||
|
*/
|
||||||
|
class OtherCustom extends ObjectType
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'fields' => [
|
||||||
|
'b' => Type::string(),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Tests\Type;
|
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\InvariantViolation;
|
||||||
use GraphQL\Type\Definition\InputObjectType;
|
use GraphQL\Type\Definition\InputObjectType;
|
||||||
@ -9,52 +11,35 @@ use GraphQL\Type\Definition\ObjectType;
|
|||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
use GraphQL\Type\Schema;
|
use GraphQL\Type\Schema;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use function lcfirst;
|
||||||
|
|
||||||
class TypeLoaderTest extends TestCase
|
class TypeLoaderTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $query;
|
private $query;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $mutation;
|
private $mutation;
|
||||||
|
|
||||||
/**
|
/** @var InterfaceType */
|
||||||
* @var InterfaceType
|
|
||||||
*/
|
|
||||||
private $node;
|
private $node;
|
||||||
|
|
||||||
/**
|
/** @var InterfaceType */
|
||||||
* @var InterfaceType
|
|
||||||
*/
|
|
||||||
private $content;
|
private $content;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $blogStory;
|
private $blogStory;
|
||||||
|
|
||||||
/**
|
/** @var ObjectType */
|
||||||
* @var ObjectType
|
|
||||||
*/
|
|
||||||
private $postStoryMutation;
|
private $postStoryMutation;
|
||||||
|
|
||||||
/**
|
/** @var InputObjectType */
|
||||||
* @var InputObjectType
|
|
||||||
*/
|
|
||||||
private $postStoryMutationInput;
|
private $postStoryMutationInput;
|
||||||
|
|
||||||
/**
|
/** @var callable */
|
||||||
* @var callable
|
|
||||||
*/
|
|
||||||
private $typeLoader;
|
private $typeLoader;
|
||||||
|
|
||||||
/**
|
/** @var string[] */
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $calls;
|
private $calls;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
@ -62,36 +47,41 @@ class TypeLoaderTest extends TestCase
|
|||||||
$this->calls = [];
|
$this->calls = [];
|
||||||
|
|
||||||
$this->node = new InterfaceType([
|
$this->node = new InterfaceType([
|
||||||
'name' => 'Node',
|
'name' => 'Node',
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
$this->calls[] = 'Node.fields';
|
$this->calls[] = 'Node.fields';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => Type::string()
|
'id' => Type::string(),
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
'resolveType' => function() {}
|
'resolveType' => function () {
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->content = new InterfaceType([
|
$this->content = new InterfaceType([
|
||||||
'name' => 'Content',
|
'name' => 'Content',
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
$this->calls[] = 'Content.fields';
|
$this->calls[] = 'Content.fields';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'title' => Type::string(),
|
'title' => Type::string(),
|
||||||
'body' => Type::string(),
|
'body' => Type::string(),
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
'resolveType' => function() {}
|
'resolveType' => function () {
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->blogStory = new ObjectType([
|
$this->blogStory = new ObjectType([
|
||||||
'name' => 'BlogStory',
|
'name' => 'BlogStory',
|
||||||
'interfaces' => [
|
'interfaces' => [
|
||||||
$this->node,
|
$this->node,
|
||||||
$this->content
|
$this->content,
|
||||||
],
|
],
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
$this->calls[] = 'BlogStory.fields';
|
$this->calls[] = 'BlogStory.fields';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
$this->node->getField('id'),
|
$this->node->getField('id'),
|
||||||
$this->content->getField('title'),
|
$this->content->getField('title'),
|
||||||
@ -101,53 +91,56 @@ class TypeLoaderTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->query = new ObjectType([
|
$this->query = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
$this->calls[] = 'Query.fields';
|
$this->calls[] = 'Query.fields';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'latestContent' => $this->content,
|
'latestContent' => $this->content,
|
||||||
'node' => $this->node,
|
'node' => $this->node,
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->mutation = new ObjectType([
|
$this->mutation = new ObjectType([
|
||||||
'name' => 'Mutation',
|
'name' => 'Mutation',
|
||||||
'fields' => function() {
|
'fields' => function () {
|
||||||
$this->calls[] = 'Mutation.fields';
|
$this->calls[] = 'Mutation.fields';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'postStory' => [
|
'postStory' => [
|
||||||
'type' => $this->postStoryMutation,
|
'type' => $this->postStoryMutation,
|
||||||
'args' => [
|
'args' => [
|
||||||
'input' => Type::nonNull($this->postStoryMutationInput),
|
'input' => Type::nonNull($this->postStoryMutationInput),
|
||||||
'clientRequestId' => Type::string()
|
'clientRequestId' => Type::string(),
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->postStoryMutation = new ObjectType([
|
$this->postStoryMutation = new ObjectType([
|
||||||
'name' => 'PostStoryMutation',
|
'name' => 'PostStoryMutation',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'story' => $this->blogStory
|
'story' => $this->blogStory,
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->postStoryMutationInput = new InputObjectType([
|
$this->postStoryMutationInput = new InputObjectType([
|
||||||
'name' => 'PostStoryMutationInput',
|
'name' => 'PostStoryMutationInput',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'title' => Type::string(),
|
'title' => Type::string(),
|
||||||
'body' => Type::string(),
|
'body' => Type::string(),
|
||||||
'author' => Type::id(),
|
'author' => Type::id(),
|
||||||
'category' => Type::id()
|
'category' => Type::id(),
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->typeLoader = function($name) {
|
$this->typeLoader = function ($name) {
|
||||||
$this->calls[] = $name;
|
$this->calls[] = $name;
|
||||||
$prop = lcfirst($name);
|
$prop = lcfirst($name);
|
||||||
return isset($this->{$prop}) ? $this->{$prop} : null;
|
|
||||||
|
return $this->{$prop} ?? null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,11 +148,12 @@ class TypeLoaderTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->expectNotToPerformAssertions();
|
$this->expectNotToPerformAssertions();
|
||||||
new Schema([
|
new Schema([
|
||||||
'query' => new ObjectType([
|
'query' => new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => ['a' => Type::string()]
|
'fields' => ['a' => Type::string()],
|
||||||
]),
|
]),
|
||||||
'typeLoader' => function() {}
|
'typeLoader' => function () {
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,20 +163,20 @@ class TypeLoaderTest extends TestCase
|
|||||||
$this->expectExceptionMessage('Schema type loader must be callable if provided but got: []');
|
$this->expectExceptionMessage('Schema type loader must be callable if provided but got: []');
|
||||||
|
|
||||||
new Schema([
|
new Schema([
|
||||||
'query' => new ObjectType([
|
'query' => new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Query',
|
||||||
'fields' => ['a' => Type::string()]
|
'fields' => ['a' => Type::string()],
|
||||||
]),
|
]),
|
||||||
'typeLoader' => []
|
'typeLoader' => [],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWorksWithoutTypeLoader() : void
|
public function testWorksWithoutTypeLoader() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'mutation' => $this->mutation,
|
'mutation' => $this->mutation,
|
||||||
'types' => [$this->blogStory]
|
'types' => [$this->blogStory],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$expected = [
|
$expected = [
|
||||||
@ -203,12 +197,12 @@ class TypeLoaderTest extends TestCase
|
|||||||
$this->assertSame($this->postStoryMutationInput, $schema->getType('PostStoryMutationInput'));
|
$this->assertSame($this->postStoryMutationInput, $schema->getType('PostStoryMutationInput'));
|
||||||
|
|
||||||
$expectedTypeMap = [
|
$expectedTypeMap = [
|
||||||
'Query' => $this->query,
|
'Query' => $this->query,
|
||||||
'Mutation' => $this->mutation,
|
'Mutation' => $this->mutation,
|
||||||
'Node' => $this->node,
|
'Node' => $this->node,
|
||||||
'String' => Type::string(),
|
'String' => Type::string(),
|
||||||
'Content' => $this->content,
|
'Content' => $this->content,
|
||||||
'BlogStory' => $this->blogStory,
|
'BlogStory' => $this->blogStory,
|
||||||
'PostStoryMutationInput' => $this->postStoryMutationInput,
|
'PostStoryMutationInput' => $this->postStoryMutationInput,
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -218,9 +212,9 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testWorksWithTypeLoader() : void
|
public function testWorksWithTypeLoader() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'mutation' => $this->mutation,
|
'mutation' => $this->mutation,
|
||||||
'typeLoader' => $this->typeLoader
|
'typeLoader' => $this->typeLoader,
|
||||||
]);
|
]);
|
||||||
$this->assertEquals([], $this->calls);
|
$this->assertEquals([], $this->calls);
|
||||||
|
|
||||||
@ -244,8 +238,8 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testOnlyCallsLoaderOnce() : void
|
public function testOnlyCallsLoaderOnce() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'typeLoader' => $this->typeLoader
|
'typeLoader' => $this->typeLoader,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$schema->getType('Node');
|
$schema->getType('Node');
|
||||||
@ -258,8 +252,9 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testFailsOnNonExistentType() : void
|
public function testFailsOnNonExistentType() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'typeLoader' => function() {}
|
'typeLoader' => function () {
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->expectException(InvariantViolation::class);
|
$this->expectException(InvariantViolation::class);
|
||||||
@ -271,10 +266,10 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testFailsOnNonType() : void
|
public function testFailsOnNonType() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'typeLoader' => function() {
|
'typeLoader' => function () {
|
||||||
return new \stdClass();
|
return new \stdClass();
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->expectException(InvariantViolation::class);
|
$this->expectException(InvariantViolation::class);
|
||||||
@ -286,10 +281,10 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testFailsOnInvalidLoad() : void
|
public function testFailsOnInvalidLoad() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'typeLoader' => function() {
|
'typeLoader' => function () {
|
||||||
return $this->content;
|
return $this->content;
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->expectException(InvariantViolation::class);
|
$this->expectException(InvariantViolation::class);
|
||||||
@ -301,10 +296,10 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testPassesThroughAnExceptionInLoader() : void
|
public function testPassesThroughAnExceptionInLoader() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'typeLoader' => function() {
|
'typeLoader' => function () {
|
||||||
throw new \Exception("This is the exception we are looking for");
|
throw new \Exception('This is the exception we are looking for');
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->expectException(\Throwable::class);
|
$this->expectException(\Throwable::class);
|
||||||
@ -316,14 +311,14 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testReturnsIdenticalResults() : void
|
public function testReturnsIdenticalResults() : void
|
||||||
{
|
{
|
||||||
$withoutLoader = new Schema([
|
$withoutLoader = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'mutation' => $this->mutation
|
'mutation' => $this->mutation,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$withLoader = new Schema([
|
$withLoader = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'mutation' => $this->mutation,
|
'mutation' => $this->mutation,
|
||||||
'typeLoader' => $this->typeLoader
|
'typeLoader' => $this->typeLoader,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertSame($withoutLoader->getQueryType(), $withLoader->getQueryType());
|
$this->assertSame($withoutLoader->getQueryType(), $withLoader->getQueryType());
|
||||||
@ -335,9 +330,9 @@ class TypeLoaderTest extends TestCase
|
|||||||
public function testSkipsLoaderForInternalTypes() : void
|
public function testSkipsLoaderForInternalTypes() : void
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = new Schema([
|
||||||
'query' => $this->query,
|
'query' => $this->query,
|
||||||
'mutation' => $this->mutation,
|
'mutation' => $this->mutation,
|
||||||
'typeLoader' => $this->typeLoader
|
'typeLoader' => $this->typeLoader,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$type = $schema->getType('ID');
|
$type = $schema->getType('ID');
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user