diff --git a/src/Mappers/RecursiveTypeMapper.php b/src/Mappers/RecursiveTypeMapper.php index 0b3415740f..f55c341419 100644 --- a/src/Mappers/RecursiveTypeMapper.php +++ b/src/Mappers/RecursiveTypeMapper.php @@ -521,6 +521,6 @@ public function mapNameToType(string $typeName): Type&NamedType return $this->mapClassToInterfaceOrType($className, null); } - throw CannotMapTypeException::createForName($typeName); + throw TypeNotFoundException::createError($typeName); } } diff --git a/src/Mappers/TypeNotFoundException.php b/src/Mappers/TypeNotFoundException.php new file mode 100644 index 0000000000..2f5df7d0bf --- /dev/null +++ b/src/Mappers/TypeNotFoundException.php @@ -0,0 +1,23 @@ +assertSame('ClassA', $recursiveMapper->mapNameToType('ClassA')->name); $this->assertSame('ClassAInterface', $recursiveMapper->mapNameToType('ClassAInterface')->name); - $this->expectException(CannotMapTypeException::class); + $this->expectException(TypeNotFoundException::class); $recursiveMapper->mapNameToType('NotExists'); } @@ -233,6 +233,8 @@ public function testDuplicateDetection(): void /** * Tests that the RecursiveTypeMapper behaves correctly if there are no types to map. + * + * @see \GraphQL\Server\Helper::promiseToExecuteOperation() */ public function testMapNoTypes(): void { @@ -244,7 +246,7 @@ public function testMapNoTypes(): void $this->getAnnotationReader() ); - $this->expectException(CannotMapTypeException::class); + $this->expectException(TypeNotFoundException::class); $recursiveTypeMapper->mapNameToType('Foo'); } diff --git a/tests/SchemaFactoryTest.php b/tests/SchemaFactoryTest.php index 090f8d91fd..908f265f57 100644 --- a/tests/SchemaFactoryTest.php +++ b/tests/SchemaFactoryTest.php @@ -1,8 +1,12 @@ addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers'); $factory->addTypeNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration'); - $factory->setDoctrineAnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader()) + $factory->setDoctrineAnnotationReader(new AnnotationReader()) ->setAuthenticationService(new VoidAuthenticationService()) ->setAuthorizationService(new VoidAuthorizationService()) ->setNamingStrategy(new NamingStrategy()) @@ -89,8 +90,8 @@ public function testClassNameMapperInjectionWithValidMapper(): void $factory = new SchemaFactory( new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer( - new EmptyContainer() - ) + new EmptyContainer(), + ), ); $factory->setAuthenticationService(new VoidAuthenticationService()) ->setAuthorizationService(new VoidAuthorizationService()) @@ -132,8 +133,8 @@ public function testClassNameMapperInjectionWithInvalidMapper(): void $factory = new SchemaFactory( new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer( - new EmptyContainer() - ) + new EmptyContainer(), + ), ); $factory->setAuthenticationService(new VoidAuthenticationService()) ->setAuthorizationService(new VoidAuthorizationService()) @@ -141,8 +142,7 @@ public function testClassNameMapperInjectionWithInvalidMapper(): void ->addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers') ->addTypeNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration'); - $this->expectException(CannotMapTypeException::class); - $this->doTestSchema($factory->createSchema()); + $this->doTestSchemaWithError($factory->createSchema()); } public function testException(): void @@ -168,9 +168,8 @@ public function testException2(): void $factory->createSchema(); } - private function doTestSchema(Schema $schema): void + private function execTestQuery(Schema $schema): ExecutionResult { - $schema->assertValid(); $queryString = ' @@ -185,25 +184,41 @@ private function doTestSchema(Schema $schema): void } '; - $result = GraphQL::executeQuery( + return GraphQL::executeQuery( $schema, - $queryString + $queryString, ); + } + + private function doTestSchemaWithError(Schema $schema): void + { + $result = $this->execTestQuery($schema); + $resultArr = $result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS); + $this->assertArrayHasKey('errors', $resultArr); + $this->assertArrayNotHasKey('data', $resultArr); + $this->assertCount(1, $resultArr); + $this->assertSame('Unknown type "User"', $resultArr['errors'][0]['message']); + } + private function doTestSchema(Schema $schema): void + { + $result = $this->execTestQuery($schema); + $resultArr = $result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS); + $this->assertArrayHasKey('data', $resultArr); $this->assertSame([ 'contacts' => [ [ 'name' => 'Joe', - 'uppercaseName' => 'JOE' + 'uppercaseName' => 'JOE', ], [ 'name' => 'Bill', 'uppercaseName' => 'BILL', - 'email' => 'bill@example.com' - ] + 'email' => 'bill@example.com', + ], - ] - ], $result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS)['data']); + ], + ], $resultArr['data']); } public function testDuplicateQueryException(): void @@ -211,8 +226,8 @@ public function testDuplicateQueryException(): void $factory = new SchemaFactory( new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer( - new EmptyContainer() - ) + new EmptyContainer(), + ), ); $factory->setAuthenticationService(new VoidAuthenticationService()) ->setAuthorizationService(new VoidAuthorizationService()) @@ -229,7 +244,7 @@ public function testDuplicateQueryException(): void '; GraphQL::executeQuery( $schema, - $queryString + $queryString, ); } @@ -238,8 +253,8 @@ public function testDuplicateQueryInTwoControllersException(): void $factory = new SchemaFactory( new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer( - new EmptyContainer() - ) + new EmptyContainer(), + ), ); $factory->setAuthenticationService(new VoidAuthenticationService()) ->setAuthorizationService(new VoidAuthorizationService()) @@ -256,7 +271,7 @@ public function testDuplicateQueryInTwoControllersException(): void '; GraphQL::executeQuery( $schema, - $queryString + $queryString, ); } }