graphql-php/tests/Executor/LazyInterfaceTest.php

125 lines
2.9 KiB
PHP
Raw Permalink Normal View History

<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
/**
* @author: Ivo Meißner
* Date: 03.05.16
* Time: 13:14
*/
2018-09-01 18:07:06 +03:00
namespace GraphQL\Tests\Executor;
use GraphQL\Executor\Executor;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\InterfaceType;
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-07-29 18:43:10 +03:00
class LazyInterfaceTest extends TestCase
{
2018-09-01 18:07:06 +03:00
/** @var Schema */
protected $schema;
2018-09-01 18:07:06 +03:00
/** @var InterfaceType */
protected $lazyInterface;
2018-09-01 18:07:06 +03:00
/** @var ObjectType */
protected $testObject;
/**
2018-09-01 18:07:06 +03:00
* Handles execution of a lazily created interface
*/
2018-09-01 18:07:06 +03:00
public function testReturnsFragmentsWithLazyCreatedInterface() : void
{
$request = '
{
lazyInterface {
... on TestObject {
name
}
}
}
';
$expected = [
'data' => [
'lazyInterface' => ['name' => 'testname'],
],
];
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, Executor::execute($this->schema, Parser::parse($request))->toArray());
2018-09-01 18:07:06 +03:00
}
/**
* Setup schema
*/
protected function setUp()
{
$query = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'query',
'fields' => function () {
return [
'lazyInterface' => [
2018-09-01 18:07:06 +03:00
'type' => $this->getLazyInterfaceType(),
2018-09-26 12:07:23 +03:00
'resolve' => static function () {
return [];
2018-09-01 18:07:06 +03:00
},
],
];
2018-09-01 18:07:06 +03:00
},
]);
2016-09-14 15:36:10 +03:00
$this->schema = new Schema(['query' => $query, 'types' => [$this->getTestObjectType()]]);
}
/**
* Returns the LazyInterface
*
* @return InterfaceType
*/
protected function getLazyInterfaceType()
{
2018-09-01 18:07:06 +03:00
if (! $this->lazyInterface) {
$this->lazyInterface = new InterfaceType([
2018-09-01 18:07:06 +03:00
'name' => 'LazyInterface',
'fields' => [
'a' => Type::string(),
2017-08-12 22:50:34 +03:00
],
2018-09-01 18:07:06 +03:00
'resolveType' => function () {
return $this->getTestObjectType();
},
]);
}
return $this->lazyInterface;
}
/**
* Returns the test ObjectType
2018-09-26 12:07:23 +03:00
*
* @return ObjectType
*/
protected function getTestObjectType()
{
2018-09-01 18:07:06 +03:00
if (! $this->testObject) {
$this->testObject = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'TestObject',
'fields' => [
'name' => [
2018-09-01 18:07:06 +03:00
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function () {
return 'testname';
2018-09-01 18:07:06 +03:00
},
],
],
2018-09-01 18:07:06 +03:00
'interfaces' => [$this->getLazyInterfaceType()],
]);
}
return $this->testObject;
}
}