From 1f71ffc3fc61b3accd2f87c4480c22aaaaae9c36 Mon Sep 17 00:00:00 2001 From: vladar Date: Wed, 4 May 2016 18:52:02 +0600 Subject: [PATCH] Added test to catch regressions in lazy interface declarations (see #38) --- tests/Executor/LazyInterfaceTest.php | 129 +++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/Executor/LazyInterfaceTest.php diff --git a/tests/Executor/LazyInterfaceTest.php b/tests/Executor/LazyInterfaceTest.php new file mode 100644 index 000000000..81d4c4cfd --- /dev/null +++ b/tests/Executor/LazyInterfaceTest.php @@ -0,0 +1,129 @@ + 'query', + 'fields' => function () { + return [ + 'lazyInterface' => [ + 'type' => $this->getLazyInterfaceType(), + 'resolve' => function() { + return []; + } + ], + 'testObject' => [ + 'type' => $this->getTestObjectType() + ] + ]; + } + ]); + + $this->schema = new Schema(['query' => $query]); + } + + /** + * Returns the LazyInterface + * + * @return InterfaceType + */ + protected function getLazyInterfaceType() + { + if (!$this->lazyInterface) { + $this->lazyInterface = new InterfaceType([ + 'name' => 'LazyInterface', + 'resolveType' => function() { + return $this->getTestObjectType(); + }, + 'resolve' => function() { + return []; + } + ]); + } + + return $this->lazyInterface; + } + + /** + * Returns the test ObjectType + * @return ObjectType + */ + protected function getTestObjectType() + { + if (!$this->testObject) { + $this->testObject = new ObjectType([ + 'name' => 'TestObject', + 'fields' => [ + 'name' => [ + 'type' => Type::string(), + 'resolve' => function() { + return 'testname'; + } + ] + ], + 'interfaces' => [$this->getLazyInterfaceType()] + ]); + } + + return $this->testObject; + } + + /** + * Handles execution of a lazily created interface + */ + public function testReturnsFragmentsWithLazyCreatedInterface() + { + $request = ' + { + lazyInterface { + ... on TestObject { + name + } + } + } + '; + + $expected = [ + 'data' => [ + 'lazyInterface' => [ + 'name' => 'testname' + ] + ] + ]; + + $this->assertEquals($expected, Executor::execute($this->schema, Parser::parse($request))->toArray()); + } +}