graphql-php/tests/Executor/ExecutorSchemaTest.php

248 lines
8.0 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Executor;
2015-07-15 20:05:46 +03:00
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\CustomScalarType;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-01 18:07:06 +03:00
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-01 18:07:06 +03:00
use function sprintf;
2015-07-15 20:05:46 +03:00
2018-07-29 18:43:10 +03:00
class ExecutorSchemaTest extends TestCase
2015-07-15 20:05:46 +03:00
{
// Execute: Handles execution with a complex schema
/**
* @see it('executes using a schema')
*/
public function testExecutesUsingASchema() : void
2015-07-15 20:05:46 +03:00
{
$BlogSerializableValueType = new CustomScalarType([
'name' => 'JsonSerializableValueScalar',
'serialize' => static function ($value) {
return $value;
},
]);
2015-07-15 20:05:46 +03:00
$BlogArticle = null;
2018-09-01 18:07:06 +03:00
$BlogImage = new ObjectType([
'name' => 'Image',
2015-07-15 20:05:46 +03:00
'fields' => [
2018-09-01 18:07:06 +03:00
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
2015-07-15 20:05:46 +03:00
'height' => ['type' => Type::int()],
2018-09-01 18:07:06 +03:00
],
2015-07-15 20:05:46 +03:00
]);
$BlogAuthor = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Author',
2018-09-26 12:07:23 +03:00
'fields' => static function () use (&$BlogArticle, &$BlogImage) {
return [
2018-09-01 18:07:06 +03:00
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
'type' => $BlogImage,
2018-09-26 12:07:23 +03:00
'resolve' => static function ($obj, $args) {
return $obj['pic']($args['width'], $args['height']);
2018-09-01 18:07:06 +03:00
},
],
2018-09-01 18:07:06 +03:00
'recentArticle' => $BlogArticle,
];
2018-09-01 18:07:06 +03:00
},
2015-07-15 20:05:46 +03:00
]);
$BlogArticle = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Article',
2015-07-15 20:05:46 +03:00
'fields' => [
2018-09-01 18:07:06 +03:00
'id' => ['type' => Type::nonNull(Type::string())],
2015-07-15 20:05:46 +03:00
'isPublished' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
'author' => ['type' => $BlogAuthor],
'title' => ['type' => Type::string()],
'body' => ['type' => Type::string()],
'keywords' => ['type' => Type::listOf(Type::string())],
'meta' => ['type' => $BlogSerializableValueType],
2018-09-01 18:07:06 +03:00
],
2015-07-15 20:05:46 +03:00
]);
$BlogQuery = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
2015-07-15 20:05:46 +03:00
'fields' => [
'article' => [
2018-09-01 18:07:06 +03:00
'type' => $BlogArticle,
'args' => ['id' => ['type' => Type::id()]],
2015-07-15 20:05:46 +03:00
'resolve' => function ($_, $args) {
return $this->article($args['id']);
2018-09-01 18:07:06 +03:00
},
2015-07-15 20:05:46 +03:00
],
2018-09-01 18:07:06 +03:00
'feed' => [
'type' => Type::listOf($BlogArticle),
2015-07-15 20:05:46 +03:00
'resolve' => function () {
return [
$this->article(1),
$this->article(2),
$this->article(3),
$this->article(4),
$this->article(5),
$this->article(6),
$this->article(7),
$this->article(8),
$this->article(9),
2018-09-01 18:07:06 +03:00
$this->article(10),
2015-07-15 20:05:46 +03:00
];
2018-09-01 18:07:06 +03:00
},
],
],
2015-07-15 20:05:46 +03:00
]);
$BlogSchema = new Schema(['query' => $BlogQuery]);
2015-07-15 20:05:46 +03:00
$request = '
{
feed {
id,
title
},
article(id: "1") {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
meta
2015-07-15 20:05:46 +03:00
}
}
fragment articleFields on Article {
id,
isPublished,
title,
body,
hidden,
notdefined
}
';
$expected = [
'data' => [
2018-09-01 18:07:06 +03:00
'feed' => [
[
'id' => '1',
'title' => 'My Article 1',
],
[
'id' => '2',
'title' => 'My Article 2',
],
[
'id' => '3',
'title' => 'My Article 3',
],
[
'id' => '4',
'title' => 'My Article 4',
],
[
'id' => '5',
'title' => 'My Article 5',
],
[
'id' => '6',
'title' => 'My Article 6',
],
[
'id' => '7',
'title' => 'My Article 7',
],
[
'id' => '8',
'title' => 'My Article 8',
],
[
'id' => '9',
'title' => 'My Article 9',
],
[
'id' => '10',
'title' => 'My Article 10',
],
2015-07-15 20:05:46 +03:00
],
'article' => [
2018-09-01 18:07:06 +03:00
'id' => '1',
2015-07-15 20:05:46 +03:00
'isPublished' => true,
2018-09-01 18:07:06 +03:00
'title' => 'My Article 1',
'body' => 'This is a post',
'author' => [
'id' => '123',
'name' => 'John Smith',
'pic' => [
'url' => 'cdn://123',
'width' => 640,
'height' => 480,
2015-07-15 20:05:46 +03:00
],
'recentArticle' => [
2018-09-01 18:07:06 +03:00
'id' => '1',
2015-07-15 20:05:46 +03:00
'isPublished' => true,
2018-09-01 18:07:06 +03:00
'title' => 'My Article 1',
'body' => 'This is a post',
'keywords' => ['foo', 'bar', '1', 'true', null],
],
],
'meta' => [ 'title' => 'My Article 1 | My Blog' ],
2018-09-01 18:07:06 +03:00
],
],
2015-07-15 20:05:46 +03:00
];
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, Executor::execute($BlogSchema, Parser::parse($request))->toArray());
2015-07-15 20:05:46 +03:00
}
private function article($id)
{
$johnSmith = null;
2018-09-26 12:07:23 +03:00
$article = static function ($id) use (&$johnSmith) {
2015-07-15 20:05:46 +03:00
return [
2018-09-01 18:07:06 +03:00
'id' => $id,
2015-07-15 20:05:46 +03:00
'isPublished' => 'true',
2018-09-01 18:07:06 +03:00
'author' => $johnSmith,
'title' => 'My Article ' . $id,
'body' => 'This is a post',
'hidden' => 'This data is not exposed in the schema',
'keywords' => ['foo', 'bar', 1, true, null],
'meta' => ['title' => 'My Article 1 | My Blog'],
2015-07-15 20:05:46 +03:00
];
};
2018-09-26 12:07:23 +03:00
$getPic = static function ($uid, $width, $height) {
2015-07-15 20:05:46 +03:00
return [
2018-09-01 18:07:06 +03:00
'url' => sprintf('cdn://%s', $uid),
'width' => $width,
'height' => $height,
2015-07-15 20:05:46 +03:00
];
};
$johnSmith = [
2018-09-01 18:07:06 +03:00
'id' => 123,
'name' => 'John Smith',
2018-09-26 12:07:23 +03:00
'pic' => static function ($width, $height) use ($getPic) {
2018-09-01 18:07:06 +03:00
return $getPic(123, $width, $height);
},
2015-07-15 20:05:46 +03:00
'recentArticle' => $article(1),
];
return $article($id);
}
}