|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author: Ivo Meißner |
| 4 | + * Date: 03.05.16 |
| 5 | + * Time: 13:14 |
| 6 | + */ |
| 7 | +namespace GraphQL\Tests\Executor; |
| 8 | + |
| 9 | +use GraphQL\Executor\Executor; |
| 10 | +use GraphQL\Language\Parser; |
| 11 | +use GraphQL\Schema; |
| 12 | +use GraphQL\Type\Definition\InterfaceType; |
| 13 | +use GraphQL\Type\Definition\ObjectType; |
| 14 | +use GraphQL\Type\Definition\Type; |
| 15 | + |
| 16 | +class LazyInterfaceTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Schema |
| 20 | + */ |
| 21 | + protected $schema; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var InterfaceType |
| 25 | + */ |
| 26 | + protected $lazyInterface; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ObjectType |
| 30 | + */ |
| 31 | + protected $testObject; |
| 32 | + |
| 33 | + /** |
| 34 | + * Setup schema |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $query = new ObjectType([ |
| 39 | + 'name' => 'query', |
| 40 | + 'fields' => function () { |
| 41 | + return [ |
| 42 | + 'lazyInterface' => [ |
| 43 | + 'type' => $this->getLazyInterfaceType(), |
| 44 | + 'resolve' => function() { |
| 45 | + return []; |
| 46 | + } |
| 47 | + ], |
| 48 | + 'testObject' => [ |
| 49 | + 'type' => $this->getTestObjectType() |
| 50 | + ] |
| 51 | + ]; |
| 52 | + } |
| 53 | + ]); |
| 54 | + |
| 55 | + $this->schema = new Schema(['query' => $query]); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the LazyInterface |
| 60 | + * |
| 61 | + * @return InterfaceType |
| 62 | + */ |
| 63 | + protected function getLazyInterfaceType() |
| 64 | + { |
| 65 | + if (!$this->lazyInterface) { |
| 66 | + $this->lazyInterface = new InterfaceType([ |
| 67 | + 'name' => 'LazyInterface', |
| 68 | + 'resolveType' => function() { |
| 69 | + return $this->getTestObjectType(); |
| 70 | + }, |
| 71 | + 'resolve' => function() { |
| 72 | + return []; |
| 73 | + } |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + return $this->lazyInterface; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the test ObjectType |
| 82 | + * @return ObjectType |
| 83 | + */ |
| 84 | + protected function getTestObjectType() |
| 85 | + { |
| 86 | + if (!$this->testObject) { |
| 87 | + $this->testObject = new ObjectType([ |
| 88 | + 'name' => 'TestObject', |
| 89 | + 'fields' => [ |
| 90 | + 'name' => [ |
| 91 | + 'type' => Type::string(), |
| 92 | + 'resolve' => function() { |
| 93 | + return 'testname'; |
| 94 | + } |
| 95 | + ] |
| 96 | + ], |
| 97 | + 'interfaces' => [$this->getLazyInterfaceType()] |
| 98 | + ]); |
| 99 | + } |
| 100 | + |
| 101 | + return $this->testObject; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Handles execution of a lazily created interface |
| 106 | + */ |
| 107 | + public function testReturnsFragmentsWithLazyCreatedInterface() |
| 108 | + { |
| 109 | + $request = ' |
| 110 | + { |
| 111 | + lazyInterface { |
| 112 | + ... on TestObject { |
| 113 | + name |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + '; |
| 118 | + |
| 119 | + $expected = [ |
| 120 | + 'data' => [ |
| 121 | + 'lazyInterface' => [ |
| 122 | + 'name' => 'testname' |
| 123 | + ] |
| 124 | + ] |
| 125 | + ]; |
| 126 | + |
| 127 | + $this->assertEquals($expected, Executor::execute($this->schema, Parser::parse($request))->toArray()); |
| 128 | + } |
| 129 | +} |
0 commit comments