Fix CS in tests/Type

This commit is contained in:
Simon Podlipsky 2018-09-02 12:02:16 +02:00
parent ec54d6152b
commit b886742968
No known key found for this signature in database
GPG Key ID: 725C2BD962B42663
13 changed files with 2565 additions and 2473 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,68 +1,77 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use ArrayObject;
use GraphQL\GraphQL;
use GraphQL\Language\SourceLocation;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use function count;
use function is_array;
class EnumTypeTest extends TestCase
{
/**
* @var Schema
*/
/** @var Schema */
private $schema;
/**
* @var EnumType
*/
/** @var EnumType */
private $ComplexEnum;
/** @var mixed[] */
private $Complex1;
/** @var ArrayObject */
private $Complex2;
public function setUp()
{
$ColorType = new EnumType([
'name' => 'Color',
'name' => 'Color',
'values' => [
'RED' => ['value' => 0],
'RED' => ['value' => 0],
'GREEN' => ['value' => 1],
'BLUE' => ['value' => 2],
]
'BLUE' => ['value' => 2],
],
]);
$simpleEnum = new EnumType([
'name' => 'SimpleEnum',
'name' => 'SimpleEnum',
'values' => [
'ONE', 'TWO', 'THREE'
]
'ONE',
'TWO',
'THREE',
],
]);
$Complex1 = ['someRandomFunction' => function() {}];
$Complex1 = [
'someRandomFunction' => function () {
},
];
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
$ComplexEnum = new EnumType([
'name' => 'Complex',
'name' => 'Complex',
'values' => [
'ONE' => ['value' => $Complex1],
'TWO' => ['value' => $Complex2]
]
'TWO' => ['value' => $Complex2],
],
]);
$QueryType = new ObjectType([
'name' => 'Query',
'name' => 'Query',
'fields' => [
'colorEnum' => [
'type' => $ColorType,
'args' => [
'fromEnum' => ['type' => $ColorType],
'fromInt' => ['type' => Type::int()],
'colorEnum' => [
'type' => $ColorType,
'args' => [
'fromEnum' => ['type' => $ColorType],
'fromInt' => ['type' => Type::int()],
'fromString' => ['type' => Type::string()],
],
'resolve' => function ($value, $args) {
@ -75,28 +84,28 @@ class EnumTypeTest extends TestCase
if (isset($args['fromEnum'])) {
return $args['fromEnum'];
}
}
},
],
'simpleEnum' => [
'type' => $simpleEnum,
'args' => [
'fromName' => ['type' => Type::string()],
'fromValue' => ['type' => Type::string()]
'simpleEnum' => [
'type' => $simpleEnum,
'args' => [
'fromName' => ['type' => Type::string()],
'fromValue' => ['type' => Type::string()],
],
'resolve' => function($value, $args) {
'resolve' => function ($value, $args) {
if (isset($args['fromName'])) {
return $args['fromName'];
}
if (isset($args['fromValue'])) {
return $args['fromValue'];
}
}
},
],
'colorInt' => [
'type' => Type::int(),
'args' => [
'colorInt' => [
'type' => Type::int(),
'args' => [
'fromEnum' => ['type' => $ColorType],
'fromInt' => ['type' => Type::int()],
'fromInt' => ['type' => Type::int()],
],
'resolve' => function ($value, $args) {
if (isset($args['fromInt'])) {
@ -105,75 +114,76 @@ class EnumTypeTest extends TestCase
if (isset($args['fromEnum'])) {
return $args['fromEnum'];
}
}
},
],
'complexEnum' => [
'type' => $ComplexEnum,
'args' => [
'fromEnum' => [
'type' => $ComplexEnum,
'type' => $ComplexEnum,
'args' => [
'fromEnum' => [
'type' => $ComplexEnum,
// Note: defaultValue is provided an *internal* representation for
// Enums, rather than the string name.
'defaultValue' => $Complex1
'defaultValue' => $Complex1,
],
'provideGoodValue' => [
'type' => Type::boolean(),
],
'provideBadValue' => [
'type' => Type::boolean()
]
'provideBadValue' => [
'type' => Type::boolean(),
],
],
'resolve' => function($value, $args) use ($Complex1, $Complex2) {
if (!empty($args['provideGoodValue'])) {
'resolve' => function ($value, $args) use ($Complex1, $Complex2) {
if (! empty($args['provideGoodValue'])) {
// Note: this is one of the references of the internal values which
// ComplexEnum allows.
return $Complex2;
}
if (!empty($args['provideBadValue'])) {
if (! empty($args['provideBadValue'])) {
// Note: similar shape, but not the same *reference*
// as Complex2 above. Enum internal values require === equality.
return new \ArrayObject(['someRandomValue' => 123]);
}
return $args['fromEnum'];
}
]
]
},
],
],
]);
$MutationType = new ObjectType([
'name' => 'Mutation',
'name' => 'Mutation',
'fields' => [
'favoriteEnum' => [
'type' => $ColorType,
'args' => ['color' => ['type' => $ColorType]],
'type' => $ColorType,
'args' => ['color' => ['type' => $ColorType]],
'resolve' => function ($value, $args) {
return isset($args['color']) ? $args['color'] : null;
}
]
]
return $args['color'] ?? null;
},
],
],
]);
$SubscriptionType = new ObjectType([
'name' => 'Subscription',
'name' => 'Subscription',
'fields' => [
'subscribeToEnum' => [
'type' => $ColorType,
'args' => ['color' => ['type' => $ColorType]],
'type' => $ColorType,
'args' => ['color' => ['type' => $ColorType]],
'resolve' => function ($value, $args) {
return isset($args['color']) ? $args['color'] : null;
}
]
]
return $args['color'] ?? null;
},
],
],
]);
$this->Complex1 = $Complex1;
$this->Complex2 = $Complex2;
$this->Complex1 = $Complex1;
$this->Complex2 = $Complex2;
$this->ComplexEnum = $ComplexEnum;
$this->schema = new Schema([
'query' => $QueryType,
'mutation' => $MutationType,
'subscription' => $SubscriptionType
'query' => $QueryType,
'mutation' => $MutationType,
'subscription' => $SubscriptionType,
]);
}
@ -221,12 +231,34 @@ class EnumTypeTest extends TestCase
'{ colorEnum(fromEnum: "GREEN") }',
null,
[
'message' => "Expected type Color, found \"GREEN\"; Did you mean the enum value GREEN?",
'locations' => [new SourceLocation(1, 23)]
'message' => 'Expected type Color, found "GREEN"; Did you mean the enum value GREEN?',
'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')
*/
@ -236,8 +268,8 @@ class EnumTypeTest extends TestCase
'{ colorEnum(fromEnum: GREENISH) }',
null,
[
'message' => "Expected type Color, found GREENISH; Did you mean the enum value GREEN?",
'locations' => [new SourceLocation(1, 23)]
'message' => 'Expected type Color, found GREENISH; Did you mean the enum value GREEN?',
'locations' => [new SourceLocation(1, 23)],
]
);
}
@ -251,8 +283,8 @@ class EnumTypeTest extends TestCase
'{ colorEnum(fromEnum: green) }',
null,
[
'message' => "Expected type Color, found green; Did you mean the enum value GREEN?",
'locations' => [new SourceLocation(1, 23)]
'message' => 'Expected type Color, found green; Did you mean the enum value GREEN?',
'locations' => [new SourceLocation(1, 23)],
]
);
}
@ -266,9 +298,9 @@ class EnumTypeTest extends TestCase
'{ colorEnum(fromString: "GREEN") }',
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)],
'path' => ['colorEnum'],
'path' => ['colorEnum'],
]
);
}
@ -281,7 +313,7 @@ class EnumTypeTest extends TestCase
$this->expectFailure(
'{ colorEnum(fromEnum: 1) }',
null,
"Expected type Color, found 1."
'Expected type Color, found 1.'
);
}
@ -293,7 +325,7 @@ class EnumTypeTest extends TestCase
$this->expectFailure(
'{ colorEnum(fromInt: GREEN) }',
null,
"Expected type Int, found GREEN."
'Expected type Int, found GREEN.'
);
}
@ -381,7 +413,7 @@ class EnumTypeTest extends TestCase
$this->expectFailure(
'query test($color: Int!) { colorEnum(fromEnum: $color) }',
['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(
['data' => ['colorEnum' => 'RED', 'colorInt' => 0]],
GraphQL::executeQuery($this->schema, "{
GraphQL::executeQuery(
$this->schema,
'{
colorEnum(fromEnum: RED)
colorInt(fromEnum: RED)
}")->toArray()
}'
)->toArray()
);
}
@ -406,10 +441,13 @@ class EnumTypeTest extends TestCase
{
$this->assertEquals(
['data' => ['colorEnum' => null, 'colorInt' => null]],
GraphQL::executeQuery($this->schema, "{
GraphQL::executeQuery(
$this->schema,
'{
colorEnum
colorInt
}")->toArray()
}'
)->toArray()
);
}
@ -419,7 +457,7 @@ class EnumTypeTest extends TestCase
public function testPresentsGetValuesAPIForComplexEnums() : void
{
$ComplexEnum = $this->ComplexEnum;
$values = $ComplexEnum->getValues();
$values = $ComplexEnum->getValues();
$this->assertEquals(2, count($values));
$this->assertEquals('ONE', $values[0]->name);
@ -446,25 +484,29 @@ class EnumTypeTest extends TestCase
*/
public function testMayBeInternallyRepresentedWithComplexValues() : void
{
$result = GraphQL::executeQuery($this->schema, '{
$result = GraphQL::executeQuery(
$this->schema,
'{
first: complexEnum
second: complexEnum(fromEnum: TWO)
good: complexEnum(provideGoodValue: true)
bad: complexEnum(provideBadValue: true)
}')->toArray(true);
}'
)->toArray(true);
$expected = [
'data' => [
'first' => 'ONE',
'data' => [
'first' => 'ONE',
'second' => 'TWO',
'good' => 'TWO',
'bad' => null
'good' => 'TWO',
'bad' => null,
],
'errors' => [[
'debugMessage' =>
'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);
@ -489,35 +531,14 @@ class EnumTypeTest extends TestCase
$this->assertArraySubset(
[
'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null],
'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null],
'errors' => [[
'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)
);
}
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

View File

@ -1,11 +1,12 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
class ObjectIdStub
{
/**
* @var int
*/
/** @var int */
private $id;
/**

View File

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation;
@ -10,124 +13,84 @@ use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\EagerResolution;
use GraphQL\Type\LazyResolution;
use PHPUnit\Framework\TestCase;
use function lcfirst;
class ResolutionTest extends TestCase
{
/**
* @var ObjectType
*/
/** @var ObjectType */
private $query;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $mutation;
/**
* @var InterfaceType
*/
/** @var InterfaceType */
private $node;
/**
* @var InterfaceType
*/
/** @var InterfaceType */
private $content;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $blogStory;
/**
* @var ObjectType
*/
private $link;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $video;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $videoMetadata;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $comment;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $user;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $category;
/**
* @var UnionType
*/
/** @var UnionType */
private $mention;
/** @var ObjectType */
private $postStoryMutation;
/** @var InputObjectType */
private $postStoryMutationInput;
/** @var ObjectType */
private $postCommentMutation;
/** @var InputObjectType */
private $postCommentMutationInput;
public function setUp()
{
$this->node = new InterfaceType([
'name' => 'Node',
'name' => 'Node',
'fields' => [
'id' => Type::string()
]
'id' => Type::string(),
],
]);
$this->content = new InterfaceType([
'name' => 'Content',
'fields' => function() {
'name' => 'Content',
'fields' => function () {
return [
'title' => Type::string(),
'body' => Type::string(),
'author' => $this->user,
'comments' => Type::listOf($this->comment),
'categories' => Type::listOf($this->category)
'title' => Type::string(),
'body' => Type::string(),
'author' => $this->user,
'comments' => Type::listOf($this->comment),
'categories' => Type::listOf($this->category),
];
}
},
]);
$this->blogStory = new ObjectType([
'name' => 'BlogStory',
'name' => 'BlogStory',
'interfaces' => [
$this->node,
$this->content
$this->content,
],
'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() {
'fields' => function () {
return [
$this->node->getField('id'),
$this->content->getField('title'),
@ -135,143 +98,166 @@ class ResolutionTest extends TestCase
$this->content->getField('author'),
$this->content->getField('comments'),
$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([
'name' => 'Video',
'name' => 'Video',
'interfaces' => [
$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'),
'streamUrl' => Type::string(),
'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'),
'streamUrl' => Type::string(),
'downloadUrl' => Type::string(),
'metadata' => $this->videoMetadata = new ObjectType([
'name' => 'VideoMetadata',
'fields' => [
'lat' => Type::float(),
'lng' => Type::float()
]
])
'metadata' => $this->videoMetadata,
];
}
},
]);
$this->comment = new ObjectType([
'name' => 'Comment',
'name' => 'Comment',
'interfaces' => [
$this->node
$this->node,
],
'fields' => function() {
'fields' => function () {
return [
$this->node->getField('id'),
'author' => $this->user,
'text' => Type::string(),
'id' => $this->node->getField('id'),
'author' => $this->user,
'text' => Type::string(),
'replies' => Type::listOf($this->comment),
'parent' => $this->comment,
'content' => $this->content
'parent' => $this->comment,
'content' => $this->content,
];
}
},
]);
$this->user = new ObjectType([
'name' => 'User',
'name' => 'User',
'interfaces' => [
$this->node
$this->node,
],
'fields' => function() {
'fields' => function () {
return [
$this->node->getField('id'),
'id' => $this->node->getField('id'),
'name' => Type::string(),
];
}
},
]);
$this->category = new ObjectType([
'name' => 'Category',
'name' => 'Category',
'interfaces' => [
$this->node
$this->node,
],
'fields' => function() {
'fields' => function () {
return [
$this->node->getField('id'),
'name' => Type::string()
'id' => $this->node->getField('id'),
'name' => Type::string(),
];
}
},
]);
$this->mention = new UnionType([
'name' => 'Mention',
'name' => 'Mention',
'types' => [
$this->user,
$this->category
]
$this->category,
],
]);
$this->query = new ObjectType([
'name' => 'Query',
'name' => 'Query',
'fields' => [
'viewer' => $this->user,
'viewer' => $this->user,
'latestContent' => $this->content,
'node' => $this->node,
'mentions' => Type::listOf($this->mention)
]
'node' => $this->node,
'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([
'name' => 'Mutation',
'name' => 'Mutation',
'fields' => [
'postStory' => [
'postStory' => [
'type' => $this->postStoryMutation = new ObjectType([
'name' => 'PostStoryMutation',
'name' => 'PostStoryMutation',
'fields' => [
'story' => $this->blogStory
]
'story' => $this->blogStory,
],
]),
'args' => [
'input' => Type::nonNull($this->postStoryMutationInput = new InputObjectType([
'name' => 'PostStoryMutationInput',
'fields' => [
'title' => Type::string(),
'body' => Type::string(),
'author' => Type::id(),
'category' => Type::id()
]
])),
'clientRequestId' => Type::string()
]
'input' => Type::nonNull($this->postStoryMutationInput),
'clientRequestId' => Type::string(),
],
],
'postComment' => [
'type' => $this->postCommentMutation = new ObjectType([
'name' => 'PostCommentMutation',
'name' => 'PostCommentMutation',
'fields' => [
'comment' => $this->comment
]
'comment' => $this->comment,
],
]),
'args' => [
'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([
'name' => 'PostCommentMutationInput',
'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([
'name' => 'PostCommentMutationInput',
'fields' => [
'text' => Type::nonNull(Type::string()),
'author' => Type::nonNull(Type::id()),
'text' => Type::nonNull(Type::string()),
'author' => Type::nonNull(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:
$eagerTypeResolution = new EagerResolution([]);
$expectedTypeMap = [
'ID' => Type::id(),
'String' => Type::string(),
'Float' => Type::float(),
'Int' => Type::int(),
'Boolean' => Type::boolean()
$expectedTypeMap = [
'ID' => Type::id(),
'String' => Type::string(),
'Float' => Type::float(),
'Int' => Type::int(),
'Boolean' => Type::boolean(),
];
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
$expectedDescriptor = [
'version' => '1.0',
'typeMap' => [
'ID' => 1,
'String' => 1,
'Float' => 1,
'Int' => 1,
'version' => '1.0',
'typeMap' => [
'ID' => 1,
'String' => 1,
'Float' => 1,
'Int' => 1,
'Boolean' => 1,
],
'possibleTypeMap' => []
'possibleTypeMap' => [],
];
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
@ -321,72 +307,76 @@ class ResolutionTest extends TestCase
$this->assertSame($this->postStoryMutation, $eagerTypeResolution->resolveType('PostStoryMutation'));
$this->assertSame($this->postStoryMutationInput, $eagerTypeResolution->resolveType('PostStoryMutationInput'));
$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->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));
$expectedTypeMap = [
'Query' => $this->query,
'Mutation' => $this->mutation,
'User' => $this->user,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'Comment' => $this->comment,
'Mention' => $this->mention,
'BlogStory' => $this->blogStory,
'Category' => $this->category,
'PostStoryMutationInput' => $this->postStoryMutationInput,
'ID' => Type::id(),
'PostStoryMutation' => $this->postStoryMutation,
'Query' => $this->query,
'Mutation' => $this->mutation,
'User' => $this->user,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'Comment' => $this->comment,
'Mention' => $this->mention,
'BlogStory' => $this->blogStory,
'Category' => $this->category,
'PostStoryMutationInput' => $this->postStoryMutationInput,
'ID' => Type::id(),
'PostStoryMutation' => $this->postStoryMutation,
'PostCommentMutationInput' => $this->postCommentMutationInput,
'PostCommentMutation' => $this->postCommentMutation,
'Float' => Type::float(),
'Int' => Type::int(),
'Boolean' => Type::boolean()
'PostCommentMutation' => $this->postCommentMutation,
'Float' => Type::float(),
'Int' => Type::int(),
'Boolean' => Type::boolean(),
];
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
$expectedDescriptor = [
'version' => '1.0',
'typeMap' => [
'Query' => 1,
'Mutation' => 1,
'User' => 1,
'Node' => 1,
'String' => 1,
'Content' => 1,
'Comment' => 1,
'Mention' => 1,
'BlogStory' => 1,
'Category' => 1,
'PostStoryMutationInput' => 1,
'ID' => 1,
'PostStoryMutation' => 1,
'version' => '1.0',
'typeMap' => [
'Query' => 1,
'Mutation' => 1,
'User' => 1,
'Node' => 1,
'String' => 1,
'Content' => 1,
'Comment' => 1,
'Mention' => 1,
'BlogStory' => 1,
'Category' => 1,
'PostStoryMutationInput' => 1,
'ID' => 1,
'PostStoryMutation' => 1,
'PostCommentMutationInput' => 1,
'PostCommentMutation' => 1,
'Float' => 1,
'Int' => 1,
'Boolean' => 1
'PostCommentMutation' => 1,
'Float' => 1,
'Int' => 1,
'Boolean' => 1,
],
'possibleTypeMap' => [
'Node' => [
'User' => 1,
'Comment' => 1,
'Category' => 1,
'BlogStory' => 1
],
'Content' => [
'BlogStory' => 1
'Node' => [
'User' => 1,
'Comment' => 1,
'Category' => 1,
'BlogStory' => 1,
],
'Content' => ['BlogStory' => 1],
'Mention' => [
'User' => 1,
'Category' => 1
]
]
'User' => 1,
'Category' => 1,
],
],
];
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
@ -402,7 +392,10 @@ class ResolutionTest extends TestCase
$this->assertEquals(null, $eagerTypeResolution->resolveType('VideoMetadata'));
$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));
$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->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));
$expectedTypeMap = [
'Video' => $this->video,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'User' => $this->user,
'Comment' => $this->comment,
'Category' => $this->category,
'Video' => $this->video,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'User' => $this->user,
'Comment' => $this->comment,
'Category' => $this->category,
'VideoMetadata' => $this->videoMetadata,
'Float' => Type::float(),
'ID' => Type::id(),
'Int' => Type::int(),
'Boolean' => Type::boolean()
'Float' => Type::float(),
'ID' => Type::id(),
'Int' => Type::int(),
'Boolean' => Type::boolean(),
];
$this->assertEquals($expectedTypeMap, $eagerTypeResolution->getTypeMap());
$expectedDescriptor = [
'version' => '1.0',
'typeMap' => [
'Video' => 1,
'Node' => 1,
'String' => 1,
'Content' => 1,
'User' => 1,
'Comment' => 1,
'Category' => 1,
'version' => '1.0',
'typeMap' => [
'Video' => 1,
'Node' => 1,
'String' => 1,
'Content' => 1,
'User' => 1,
'Comment' => 1,
'Category' => 1,
'VideoMetadata' => 1,
'Float' => 1,
'ID' => 1,
'Int' => 1,
'Boolean' => 1
'Float' => 1,
'ID' => 1,
'Int' => 1,
'Boolean' => 1,
],
'possibleTypeMap' => [
'Node' => [
'Video' => 1,
'User' => 1,
'Comment' => 1,
'Category' => 1
'Node' => [
'Video' => 1,
'User' => 1,
'Comment' => 1,
'Category' => 1,
],
'Content' => [
'Video' => 1
]
]
'Content' => ['Video' => 1],
],
];
$this->assertEquals($expectedDescriptor, $eagerTypeResolution->getDescriptor());
}
@ -463,11 +457,11 @@ class ResolutionTest extends TestCase
public function testLazyResolutionFollowsEagerResolution() : void
{
// 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();
$typeLoader = function($name) {
throw new \Exception("This should be never called for empty descriptor");
$typeLoader = function ($name) {
throw new \Exception('This should be never called for empty descriptor');
};
$lazy = new LazyResolution($emptyDescriptor, $typeLoader);
@ -478,11 +472,12 @@ class ResolutionTest extends TestCase
$eager = new EagerResolution([$this->query, $this->mutation]);
$called = 0;
$called = 0;
$descriptor = $eager->getDescriptor();
$typeLoader = function($name) use (&$called) {
$typeLoader = function ($name) use (&$called) {
$called++;
$prop = lcfirst($name);
return $this->{$prop};
};
@ -507,7 +502,10 @@ class ResolutionTest extends TestCase
$this->assertSame($eager->resolveType('PostStoryMutation'), $lazy->resolveType('PostStoryMutation'));
$this->assertSame($eager->resolveType('PostStoryMutationInput'), $lazy->resolveType('PostStoryMutationInput'));
$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->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));
$called = 0;
$eager = new EagerResolution([$this->video]);
$lazy = new LazyResolution($eager->getDescriptor(), $typeLoader);
$eager = new EagerResolution([$this->video]);
$lazy = new LazyResolution($eager->getDescriptor(), $typeLoader);
$this->assertEquals($eager->resolveType('VideoMetadata'), $lazy->resolveType('VideoMetadata'));
$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));
}
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
{
$lazy = $this->createLazy();
@ -569,9 +533,39 @@ class ResolutionTest extends TestCase
$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
{
$tmp = new InterfaceType(['name' => 'a', 'fields' => []]);
$tmp = new InterfaceType(['name' => 'a', 'fields' => []]);
$lazy = $this->createLazy();
$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');
@ -580,7 +574,7 @@ class ResolutionTest extends TestCase
public function testLazyThrowsOnInvalidLoadedPossibleTypeWithInteger() : void
{
$tmp = new InterfaceType(['name' => 'b', 'fields' => []]);
$tmp = new InterfaceType(['name' => 'b', 'fields' => []]);
$lazy = $this->createLazy();
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('Lazy Type Resolution Error: Expecting GraphQL Type instance, but got integer');

View File

@ -1,11 +1,14 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
class ResolveInfoTest extends TestCase
@ -13,53 +16,56 @@ class ResolveInfoTest extends TestCase
public function testFieldSelection() : void
{
$image = new ObjectType([
'name' => 'Image',
'name' => 'Image',
'fields' => [
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()]
]
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
]);
$article = null;
$author = new ObjectType([
'name' => 'Author',
'fields' => function() use ($image, &$article) {
'name' => 'Author',
'fields' => function () use ($image, &$article) {
return [
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [ 'type' => $image, 'args' => [
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()]
]],
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'type' => $image,
'args' => [
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
],
'recentArticle' => ['type' => $article],
];
},
]);
$reply = new ObjectType([
'name' => 'Reply',
'name' => 'Reply',
'fields' => [
'author' => ['type' => $author],
'body' => ['type' => Type::string()]
]
'body' => ['type' => Type::string()],
],
]);
$article = new ObjectType([
'name' => 'Article',
'name' => 'Article',
'fields' => [
'id' => ['type' => Type::string()],
'id' => ['type' => Type::string()],
'isPublished' => ['type' => Type::boolean()],
'author' => ['type' => $author],
'title' => ['type' => Type::string()],
'body' => ['type' => Type::string()],
'image' => ['type' => $image],
'replies' => ['type' => Type::listOf($reply)]
]
'author' => ['type' => $author],
'title' => ['type' => Type::string()],
'body' => ['type' => Type::string()],
'image' => ['type' => $image],
'replies' => ['type' => Type::listOf($reply)],
],
]);
$doc = '
$doc = '
query Test {
article {
author {
@ -100,59 +106,70 @@ class ResolveInfoTest extends TestCase
}
';
$expectedDefaultSelection = [
'author' => true,
'image' => true,
'replies' => true
'author' => true,
'image' => true,
'replies' => true,
];
$expectedDeepSelection = [
'author' => [
$expectedDeepSelection = [
'author' => [
'name' => true,
'pic' => [
'url' => true,
'width' => true
]
'pic' => [
'url' => true,
'width' => true,
],
],
'image' => [
'width' => true,
'image' => [
'width' => true,
'height' => true,
'url' => true
'url' => true,
],
'replies' => [
'body' => true,
'body' => true,
'author' => [
'id' => true,
'name' => true,
'pic' => [
'url' => true,
'width' => true,
'height' => true
'id' => true,
'name' => true,
'pic' => [
'url' => true,
'width' => true,
'height' => true,
],
'recentArticle' => [
'id' => true,
'id' => true,
'title' => true,
'body' => true
]
]
]
'body' => true,
],
],
],
];
$hasCalled = false;
$hasCalled = false;
$actualDefaultSelection = null;
$actualDeepSelection = null;
$actualDeepSelection = null;
$blogQuery = new ObjectType([
'name' => 'Query',
'name' => 'Query',
'fields' => [
'article' => [
'type' => $article,
'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDefaultSelection, &$actualDeepSelection) {
$hasCalled = true;
'type' => $article,
'resolve' => function (
$value,
$args,
$context,
ResolveInfo $info
) use (
&$hasCalled,
&
$actualDefaultSelection,
&$actualDeepSelection
) {
$hasCalled = true;
$actualDefaultSelection = $info->getFieldSelection();
$actualDeepSelection = $info->getFieldSelection(5);
$actualDeepSelection = $info->getFieldSelection(5);
return null;
}
]
]
},
],
],
]);
$schema = new Schema(['query' => $blogQuery]);
@ -167,50 +184,53 @@ class ResolveInfoTest extends TestCase
public function testMergedFragmentsFieldSelection() : void
{
$image = new ObjectType([
'name' => 'Image',
'name' => 'Image',
'fields' => [
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()]
]
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
]);
$article = null;
$author = new ObjectType([
'name' => 'Author',
'fields' => function() use ($image, &$article) {
'name' => 'Author',
'fields' => function () use ($image, &$article) {
return [
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [ 'type' => $image, 'args' => [
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()]
]],
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'type' => $image,
'args' => [
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
],
'recentArticle' => ['type' => $article],
];
},
]);
$reply = new ObjectType([
'name' => 'Reply',
'name' => 'Reply',
'fields' => [
'author' => ['type' => $author],
'body' => ['type' => Type::string()]
]
'body' => ['type' => Type::string()],
],
]);
$article = new ObjectType([
'name' => 'Article',
'name' => 'Article',
'fields' => [
'id' => ['type' => Type::string()],
'id' => ['type' => Type::string()],
'isPublished' => ['type' => Type::boolean()],
'author' => ['type' => $author],
'title' => ['type' => Type::string()],
'body' => ['type' => Type::string()],
'image' => ['type' => $image],
'replies' => ['type' => Type::listOf($reply)]
]
'author' => ['type' => $author],
'title' => ['type' => Type::string()],
'body' => ['type' => Type::string()],
'image' => ['type' => $image],
'replies' => ['type' => Type::listOf($reply)],
],
]);
$doc = '
@ -264,53 +284,63 @@ class ResolveInfoTest extends TestCase
';
$expectedDeepSelection = [
'author' => [
'author' => [
'name' => true,
'pic' => [
'url' => true,
'width' => true
]
'pic' => [
'url' => true,
'width' => true,
],
],
'image' => [
'width' => true,
'image' => [
'width' => true,
'height' => true,
'url' => true
'url' => true,
],
'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' => [
'id' => true,
'name' => true,
'pic' => [
'url' => true,
'width' => true,
'height' => true
'id' => true,
'name' => true,
'pic' => [
'url' => true,
'width' => true,
'height' => true,
],
'recentArticle' => [
'id' => true,
'id' => true,
'title' => true,
'body' => true
]
]
]
'body' => true,
],
],
],
];
$hasCalled = false;
$hasCalled = false;
$actualDefaultSelection = null;
$actualDeepSelection = null;
$actualDeepSelection = null;
$blogQuery = new ObjectType([
'name' => 'Query',
'name' => 'Query',
'fields' => [
'article' => [
'type' => $article,
'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDeepSelection) {
$hasCalled = true;
'type' => $article,
'resolve' => function (
$value,
$args,
$context,
ResolveInfo $info
) use (
&$hasCalled,
&
$actualDeepSelection
) {
$hasCalled = true;
$actualDeepSelection = $info->getFieldSelection(5);
return null;
}
]
]
},
],
],
]);
$schema = new Schema(['query' => $blogQuery]);
@ -320,6 +350,4 @@ class ResolveInfoTest extends TestCase
$this->assertEquals(['data' => ['article' => null]], $result);
$this->assertEquals($expectedDeepSelection, $actualDeepSelection);
}
}

View File

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\Error;
@ -8,7 +11,6 @@ use PHPUnit\Framework\TestCase;
class ScalarSerializationTest extends TestCase
{
// Type System: Scalar coercion
/**
* @see it('serializes output int')
*/
@ -42,7 +44,6 @@ class ScalarSerializationTest extends TestCase
$this->expectException(Error::class);
$this->expectExceptionMessage('Int cannot represent non-integer value: 1.1');
$intType->serialize(1.1);
}
public function testSerializesOutputIntCannotRepresentNegativeFloat() : void
@ -51,7 +52,6 @@ class ScalarSerializationTest extends TestCase
$this->expectException(Error::class);
$this->expectExceptionMessage('Int cannot represent non-integer value: -1.1');
$intType->serialize(-1.1);
}
public function testSerializesOutputIntCannotRepresentNumericString() : void
@ -60,7 +60,6 @@ class ScalarSerializationTest extends TestCase
$this->expectException(Error::class);
$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"');
}
public function testSerializesOutputIntCannotRepresentBiggerThan32Bits() : void
@ -71,7 +70,6 @@ class ScalarSerializationTest extends TestCase
$this->expectException(Error::class);
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 9876504321');
$intType->serialize(9876504321);
}
public function testSerializesOutputIntCannotRepresentLowerThan32Bits() : void
@ -104,7 +102,6 @@ class ScalarSerializationTest extends TestCase
$this->expectException(Error::class);
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: one');
$intType->serialize('one');
}
public function testSerializesOutputIntCannotRepresentEmptyString() : void
@ -196,7 +193,6 @@ class ScalarSerializationTest extends TestCase
$this->assertSame(false, $boolType->serialize(0));
$this->assertSame(true, $boolType->serialize(true));
$this->assertSame(false, $boolType->serialize(false));
// TODO: how should it behave on '0'?
}

View File

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation;
@ -12,14 +15,19 @@ use PHPUnit\Framework\TestCase;
class SchemaTest extends TestCase
{
/** @var InterfaceType */
private $interfaceType;
/** @var ObjectType */
private $implementingType;
/** @var InputObjectType */
private $directiveInputType;
/** @var InputObjectType */
private $wrappedDirectiveInputType;
/** @var Directive */
private $directive;
/** @var Schema */
@ -28,20 +36,25 @@ class SchemaTest extends TestCase
public function setUp()
{
$this->interfaceType = new InterfaceType([
'name' => 'Interface',
'name' => 'Interface',
'fields' => ['fieldName' => ['type' => Type::string()]],
]);
$this->implementingType = new ObjectType([
'name' => 'Object',
'name' => 'Object',
'interfaces' => [$this->interfaceType],
'fields' => ['fieldName' => ['type' => Type::string(), 'resolve' => function () {
'fields' => [
'fieldName' => [
'type' => Type::string(),
'resolve' => function () {
return '';
}]],
},
],
],
]);
$this->directiveInputType = new InputObjectType([
'name' => 'DirInput',
'name' => 'DirInput',
'fields' => [
'field' => [
'type' => Type::string(),
@ -50,7 +63,7 @@ class SchemaTest extends TestCase
]);
$this->wrappedDirectiveInputType = new InputObjectType([
'name' => 'WrappedDirInput',
'name' => 'WrappedDirInput',
'fields' => [
'field' => [
'type' => Type::string(),
@ -59,10 +72,10 @@ class SchemaTest extends TestCase
]);
$this->directive = new Directive([
'name' => 'dir',
'name' => 'dir',
'locations' => ['OBJECT'],
'args' => [
'arg' => [
'args' => [
'arg' => [
'type' => $this->directiveInputType,
],
'argList' => [
@ -72,11 +85,11 @@ class SchemaTest extends TestCase
]);
$this->schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'getObject' => [
'type' => $this->interfaceType,
'type' => $this->interfaceType,
'resolve' => function () {
return [];
},

View File

@ -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);
}
}

View 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);
}
}

View 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);
}
}

View File

@ -1,6 +1,8 @@
<?php
namespace GraphQL\Tests\Type;
declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\InputObjectType;
@ -9,52 +11,35 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use function lcfirst;
class TypeLoaderTest extends TestCase
{
/**
* @var ObjectType
*/
/** @var ObjectType */
private $query;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $mutation;
/**
* @var InterfaceType
*/
/** @var InterfaceType */
private $node;
/**
* @var InterfaceType
*/
/** @var InterfaceType */
private $content;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $blogStory;
/**
* @var ObjectType
*/
/** @var ObjectType */
private $postStoryMutation;
/**
* @var InputObjectType
*/
/** @var InputObjectType */
private $postStoryMutationInput;
/**
* @var callable
*/
/** @var callable */
private $typeLoader;
/**
* @var array
*/
/** @var string[] */
private $calls;
public function setUp()
@ -62,36 +47,41 @@ class TypeLoaderTest extends TestCase
$this->calls = [];
$this->node = new InterfaceType([
'name' => 'Node',
'fields' => function() {
'name' => 'Node',
'fields' => function () {
$this->calls[] = 'Node.fields';
return [
'id' => Type::string()
'id' => Type::string(),
];
},
'resolveType' => function() {}
'resolveType' => function () {
},
]);
$this->content = new InterfaceType([
'name' => 'Content',
'fields' => function() {
'name' => 'Content',
'fields' => function () {
$this->calls[] = 'Content.fields';
return [
'title' => Type::string(),
'body' => Type::string(),
'body' => Type::string(),
];
},
'resolveType' => function() {}
'resolveType' => function () {
},
]);
$this->blogStory = new ObjectType([
'name' => 'BlogStory',
'name' => 'BlogStory',
'interfaces' => [
$this->node,
$this->content
$this->content,
],
'fields' => function() {
'fields' => function () {
$this->calls[] = 'BlogStory.fields';
return [
$this->node->getField('id'),
$this->content->getField('title'),
@ -101,53 +91,56 @@ class TypeLoaderTest extends TestCase
]);
$this->query = new ObjectType([
'name' => 'Query',
'fields' => function() {
'name' => 'Query',
'fields' => function () {
$this->calls[] = 'Query.fields';
return [
'latestContent' => $this->content,
'node' => $this->node,
'node' => $this->node,
];
}
},
]);
$this->mutation = new ObjectType([
'name' => 'Mutation',
'fields' => function() {
'name' => 'Mutation',
'fields' => function () {
$this->calls[] = 'Mutation.fields';
return [
'postStory' => [
'type' => $this->postStoryMutation,
'args' => [
'input' => Type::nonNull($this->postStoryMutationInput),
'clientRequestId' => Type::string()
]
]
'input' => Type::nonNull($this->postStoryMutationInput),
'clientRequestId' => Type::string(),
],
],
];
}
},
]);
$this->postStoryMutation = new ObjectType([
'name' => 'PostStoryMutation',
'name' => 'PostStoryMutation',
'fields' => [
'story' => $this->blogStory
]
'story' => $this->blogStory,
],
]);
$this->postStoryMutationInput = new InputObjectType([
'name' => 'PostStoryMutationInput',
'name' => 'PostStoryMutationInput',
'fields' => [
'title' => Type::string(),
'body' => Type::string(),
'author' => Type::id(),
'category' => Type::id()
]
'title' => Type::string(),
'body' => Type::string(),
'author' => Type::id(),
'category' => Type::id(),
],
]);
$this->typeLoader = function($name) {
$this->typeLoader = function ($name) {
$this->calls[] = $name;
$prop = lcfirst($name);
return isset($this->{$prop}) ? $this->{$prop} : null;
$prop = lcfirst($name);
return $this->{$prop} ?? null;
};
}
@ -155,11 +148,12 @@ class TypeLoaderTest extends TestCase
{
$this->expectNotToPerformAssertions();
new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => ['a' => Type::string()]
'query' => new ObjectType([
'name' => 'Query',
'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: []');
new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => ['a' => Type::string()]
'query' => new ObjectType([
'name' => 'Query',
'fields' => ['a' => Type::string()],
]),
'typeLoader' => []
'typeLoader' => [],
]);
}
public function testWorksWithoutTypeLoader() : void
{
$schema = new Schema([
'query' => $this->query,
'query' => $this->query,
'mutation' => $this->mutation,
'types' => [$this->blogStory]
'types' => [$this->blogStory],
]);
$expected = [
@ -203,12 +197,12 @@ class TypeLoaderTest extends TestCase
$this->assertSame($this->postStoryMutationInput, $schema->getType('PostStoryMutationInput'));
$expectedTypeMap = [
'Query' => $this->query,
'Mutation' => $this->mutation,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'BlogStory' => $this->blogStory,
'Query' => $this->query,
'Mutation' => $this->mutation,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'BlogStory' => $this->blogStory,
'PostStoryMutationInput' => $this->postStoryMutationInput,
];
@ -218,9 +212,9 @@ class TypeLoaderTest extends TestCase
public function testWorksWithTypeLoader() : void
{
$schema = new Schema([
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader,
]);
$this->assertEquals([], $this->calls);
@ -244,8 +238,8 @@ class TypeLoaderTest extends TestCase
public function testOnlyCallsLoaderOnce() : void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => $this->typeLoader
'query' => $this->query,
'typeLoader' => $this->typeLoader,
]);
$schema->getType('Node');
@ -258,8 +252,9 @@ class TypeLoaderTest extends TestCase
public function testFailsOnNonExistentType() : void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => function() {}
'query' => $this->query,
'typeLoader' => function () {
},
]);
$this->expectException(InvariantViolation::class);
@ -271,10 +266,10 @@ class TypeLoaderTest extends TestCase
public function testFailsOnNonType() : void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => function() {
'query' => $this->query,
'typeLoader' => function () {
return new \stdClass();
}
},
]);
$this->expectException(InvariantViolation::class);
@ -286,10 +281,10 @@ class TypeLoaderTest extends TestCase
public function testFailsOnInvalidLoad() : void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => function() {
'query' => $this->query,
'typeLoader' => function () {
return $this->content;
}
},
]);
$this->expectException(InvariantViolation::class);
@ -301,10 +296,10 @@ class TypeLoaderTest extends TestCase
public function testPassesThroughAnExceptionInLoader() : void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => function() {
throw new \Exception("This is the exception we are looking for");
}
'query' => $this->query,
'typeLoader' => function () {
throw new \Exception('This is the exception we are looking for');
},
]);
$this->expectException(\Throwable::class);
@ -316,14 +311,14 @@ class TypeLoaderTest extends TestCase
public function testReturnsIdenticalResults() : void
{
$withoutLoader = new Schema([
'query' => $this->query,
'mutation' => $this->mutation
'query' => $this->query,
'mutation' => $this->mutation,
]);
$withLoader = new Schema([
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader,
]);
$this->assertSame($withoutLoader->getQueryType(), $withLoader->getQueryType());
@ -335,9 +330,9 @@ class TypeLoaderTest extends TestCase
public function testSkipsLoaderForInternalTypes() : void
{
$schema = new Schema([
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader,
]);
$type = $schema->getType('ID');

File diff suppressed because it is too large Load Diff