From 2043a5778c711170b07bddec4cea2ba1cc927658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 14:25:20 +0100 Subject: [PATCH 1/8] Migrating away from TypeMappingRuntimeException TypeMappingRuntimeException code is overly complex and most of the analysis done in the wrapping can be now done when the exception is created. This commit migrates code away from TypeMappingRuntimeException into a CannotMapTypeException. Slowly, we will get completely rid of the TypeMappingRuntimeException. --- src/Mappers/CannotMapTypeException.php | 35 ++++++++++ src/Mappers/Parameters/TypeHandler.php | 3 +- src/TypeMappingRuntimeException.php | 93 ++++++++------------------ tests/FieldsBuilderTest.php | 17 ++--- 4 files changed, 73 insertions(+), 75 deletions(-) diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index 0affee918a..f84a5651c0 100644 --- a/src/Mappers/CannotMapTypeException.php +++ b/src/Mappers/CannotMapTypeException.php @@ -11,8 +11,14 @@ use GraphQL\Type\Definition\NamedType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; +use phpDocumentor\Reflection\Type as PhpDocumentorType; +use phpDocumentor\Reflection\Types\Array_; +use phpDocumentor\Reflection\Types\Iterable_; +use phpDocumentor\Reflection\Types\Mixed_; +use ReflectionMethod; use TheCodingMachine\GraphQLite\Annotations\ExtendType; use function array_map; +use function assert; use function implode; use function sprintf; @@ -109,4 +115,33 @@ public static function extendTypeWithBadTargetedClass(string $className, ExtendT { return new self('For ' . self::extendTypeToString($extendType) . ' annotation declared in class "' . $className . '", the pointed at GraphQL type cannot be extended. You can only target types extending the MutableObjectType (like types created with the @Type annotation).'); } + + /** + * @param Array_|Iterable_|Mixed_ $type + */ + public static function createForMissingPhpDoc(PhpDocumentorType $type, ReflectionMethod $refMethod, ?string $argumentName = null): self + { + $typeStr = ''; + if ($type instanceof Array_) { + $typeStr = 'array'; + } elseif ($type instanceof Iterable_) { + $typeStr = 'iterable'; + } elseif ($type instanceof Mixed_) { + $typeStr = 'mixed'; + } + assert($typeStr !== ''); + if ($argumentName === null) { + if ($typeStr === 'mixed') { + return new self('a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint.'); + } + + return new self(sprintf('please provide an additional @return in the PHPDoc block to further specify the return type of %s. For instance: @return string[]', $typeStr)); + } + + if ($typeStr === 'mixed') { + return new self(sprintf('a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint. For instance: "string $%s".', $argumentName)); + } + + return new self(sprintf('please provide an additional @param in the PHPDoc block to further specify the type of the %s. For instance: @param string[] $%s.', $typeStr, $argumentName)); + } } diff --git a/src/Mappers/Parameters/TypeHandler.php b/src/Mappers/Parameters/TypeHandler.php index da42df1291..00dd91d796 100644 --- a/src/Mappers/Parameters/TypeHandler.php +++ b/src/Mappers/Parameters/TypeHandler.php @@ -28,6 +28,7 @@ use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations; use TheCodingMachine\GraphQLite\Annotations\UseInputType; use TheCodingMachine\GraphQLite\InvalidDocBlockRuntimeException; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface; use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperInterface; use TheCodingMachine\GraphQLite\Parameters\DefaultValueParameter; @@ -179,7 +180,7 @@ private function mapType(Type $type, ?Type $docBlockType, bool $isNullable, bool if ($innerType instanceof Array_ || $innerType instanceof Iterable_ || $innerType instanceof Mixed_) { // We need to use the docBlockType if ($docBlockType === null) { - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForMissingPhpDoc($innerType, $refMethod, $argumentName); } if ($mapToInputType === true) { Assert::notNull($argumentName); diff --git a/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php index 3f1f911e65..b834b21b81 100644 --- a/src/TypeMappingRuntimeException.php +++ b/src/TypeMappingRuntimeException.php @@ -5,10 +5,7 @@ namespace TheCodingMachine\GraphQLite; use phpDocumentor\Reflection\Type; -use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Compound; -use phpDocumentor\Reflection\Types\Iterable_; -use phpDocumentor\Reflection\Types\Mixed_; use phpDocumentor\Reflection\Types\Object_; use ReflectionMethod; use ReflectionParameter; @@ -33,49 +30,29 @@ public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, { $declaringClass = $parameter->getDeclaringClass(); Assert::notNull($declaringClass, 'Parameter passed must be a parameter of a method, not a parameter of a function.'); - if ($previous->type instanceof Array_ || $previous->type instanceof Iterable_) { - $typeStr = $previous->type instanceof Array_ ? 'array' : 'iterable'; + + if ($previous->type instanceof Compound) { $message = sprintf( - 'Parameter $%s in %s::%s is type-hinted to %s. Please provide an additional @param in the PHPDoc block to further specify the type of the %s. For instance: @param string[] $%s.', + 'Parameter $%s in %s::%s is type-hinted to "' . $previous->type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.', $parameter->getName(), $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName(), - $typeStr, - $typeStr, - $parameter->getName() + $parameter->getDeclaringFunction()->getName() ); - } elseif ($previous->type instanceof Mixed_) { + } elseif (! ($previous->type instanceof Object_)) { + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); + } else { + $fqcn = (string) $previous->type->getFqsen(); + + if ($fqcn !== '\\DateTime') { + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); + } + $message = sprintf( - 'Parameter $%s in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint. For instance: "string $%s".', + 'Parameter $%s in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', $parameter->getName(), $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName(), - $parameter->getName() + $parameter->getDeclaringFunction()->getName() ); - } else { - if ($previous->type instanceof Compound) { - $message = sprintf( - 'Parameter $%s in %s::%s is type-hinted to "' . $previous->type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.', - $parameter->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName() - ); - } elseif (! ($previous->type instanceof Object_)) { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } else { - $fqcn = (string) $previous->type->getFqsen(); - - if ($fqcn !== '\\DateTime') { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); - } - - $message = sprintf( - 'Parameter $%s in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', - $parameter->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName() - ); - } } $e = new self($message, 0, $previous); @@ -86,37 +63,21 @@ public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, public static function wrapWithReturnInfo(TypeMappingRuntimeException $previous, ReflectionMethod $method): TypeMappingRuntimeException { - if ($previous->type instanceof Array_ || $previous->type instanceof Iterable_) { - $typeStr = $previous->type instanceof Array_ ? 'array' : 'iterable'; - $message = sprintf( - 'Return type in %s::%s is type-hinted to %s. Please provide an additional @return in the PHPDoc block to further specify the type of the array. For instance: @return string[]', - $method->getDeclaringClass()->getName(), - $method->getName(), - $typeStr - ); - } elseif ($previous->type instanceof Mixed_) { - $message = sprintf( - 'Return type in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint.', - $method->getDeclaringClass()->getName(), - $method->getName() - ); - } else { - if (! ($previous->type instanceof Object_)) { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } - - $fqcn = (string) $previous->type->getFqsen(); - if ($fqcn !== '\\DateTime') { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); - } + if (! ($previous->type instanceof Object_)) { + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); + } - $message = sprintf( - 'Return type in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', - $method->getDeclaringClass()->getName(), - $method->getName() - ); + $fqcn = (string) $previous->type->getFqsen(); + if ($fqcn !== '\\DateTime') { + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); } + $message = sprintf( + 'Return type in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', + $method->getDeclaringClass()->getName(), + $method->getName() + ); + $e = new self($message, 0, $previous); $e->type = $previous->type; diff --git a/tests/FieldsBuilderTest.php b/tests/FieldsBuilderTest.php index edf15325ad..b74ef34928 100644 --- a/tests/FieldsBuilderTest.php +++ b/tests/FieldsBuilderTest.php @@ -170,7 +170,7 @@ public function test($noTypeHint): string $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); + $this->expectException(CannotMapTypeException::class); $queryProvider->getQueries($controller); } @@ -365,8 +365,8 @@ public function testSourceFieldHasMissingReturnType(): void { $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Return type in TheCodingMachine\\GraphQLite\\Fixtures\\TestObjectMissingReturnType::getTest is missing a type-hint (or type-hinted to \"mixed\"). Please provide a better type-hint."); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For return type of TheCodingMachine\GraphQLite\Fixtures\TestObjectMissingReturnType::getTest, a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint.'); $queryProvider->getFields(new TestTypeMissingReturnType(), true); } @@ -443,7 +443,8 @@ public function testQueryProviderWithIterable(): void public function testNoReturnTypeError(): void { $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For return type of TheCodingMachine\GraphQLite\Fixtures\TestControllerNoReturnType::test, a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint.'); $queryProvider->getQueries(new TestControllerNoReturnType()); } @@ -505,8 +506,8 @@ public function testQueryProviderWithArrayReturnType(): void $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Return type in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayReturnType::test is type-hinted to array. Please provide an additional @return in the PHPDoc block to further specify the type of the array. For instance: @return string[]'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For return type of TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayReturnType::test, please provide an additional @return in the PHPDoc block to further specify the return type of array. For instance: @return string[]'); $queryProvider->getQueries($controller); } @@ -516,8 +517,8 @@ public function testQueryProviderWithArrayParams(): void $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Parameter $params in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayParam::test is type-hinted to array. Please provide an additional @param in the PHPDoc block to further specify the type of the array. For instance: @param string[] $params.'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For parameter $params, in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayParam::test, please provide an additional @param in the PHPDoc block to further specify the type of the array. For instance: @param string[] $params.'); $queryProvider->getQueries($controller); } From 671b07e517710583eba44af6f807c030f7a55b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 14:37:12 +0100 Subject: [PATCH 2/8] Migrating DateTime exception away from TypeMappingRuntimeException --- src/Mappers/CannotMapTypeException.php | 5 ++++ src/Mappers/Root/BaseTypeMapper.php | 4 +-- src/TypeMappingRuntimeException.php | 33 +++-------------------- tests/FieldsBuilderTest.php | 8 +++--- tests/Mappers/Root/BaseTypeMapperTest.php | 6 ++--- 5 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index f84a5651c0..d73c32904c 100644 --- a/src/Mappers/CannotMapTypeException.php +++ b/src/Mappers/CannotMapTypeException.php @@ -144,4 +144,9 @@ public static function createForMissingPhpDoc(PhpDocumentorType $type, Reflectio return new self(sprintf('please provide an additional @param in the PHPDoc block to further specify the type of the %s. For instance: @param string[] $%s.', $typeStr, $argumentName)); } + + public static function createForDateTime(): self + { + return new self('type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); + } } diff --git a/src/Mappers/Root/BaseTypeMapper.php b/src/Mappers/Root/BaseTypeMapper.php index 1c2e7a51b0..bf9960e91d 100644 --- a/src/Mappers/Root/BaseTypeMapper.php +++ b/src/Mappers/Root/BaseTypeMapper.php @@ -24,9 +24,9 @@ use phpDocumentor\Reflection\Types\String_; use Psr\Http\Message\UploadedFileInterface; use ReflectionMethod; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface; use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; use TheCodingMachine\GraphQLite\Types\DateTimeType; use TheCodingMachine\GraphQLite\Types\ID; use function ltrim; @@ -144,7 +144,7 @@ private function mapBaseType(Type $type) case '\\' . UploadedFileInterface::class: return self::getUploadType(); case '\\DateTime': - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForDateTime(); case '\\' . ID::class: return GraphQLType::id(); default: diff --git a/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php index b834b21b81..74d7e1a206 100644 --- a/src/TypeMappingRuntimeException.php +++ b/src/TypeMappingRuntimeException.php @@ -31,6 +31,7 @@ public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, $declaringClass = $parameter->getDeclaringClass(); Assert::notNull($declaringClass, 'Parameter passed must be a parameter of a method, not a parameter of a function.'); + $message = ''; if ($previous->type instanceof Compound) { $message = sprintf( 'Parameter $%s in %s::%s is type-hinted to "' . $previous->type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.', @@ -40,19 +41,6 @@ public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, ); } elseif (! ($previous->type instanceof Object_)) { throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } else { - $fqcn = (string) $previous->type->getFqsen(); - - if ($fqcn !== '\\DateTime') { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); - } - - $message = sprintf( - 'Parameter $%s in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', - $parameter->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName() - ); } $e = new self($message, 0, $previous); @@ -63,24 +51,9 @@ public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, public static function wrapWithReturnInfo(TypeMappingRuntimeException $previous, ReflectionMethod $method): TypeMappingRuntimeException { - if (! ($previous->type instanceof Object_)) { + //if (! ($previous->type instanceof Object_)) { throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } - $fqcn = (string) $previous->type->getFqsen(); - if ($fqcn !== '\\DateTime') { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got a '" . $fqcn . '"'); - } - - $message = sprintf( - 'Return type in %s::%s is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.', - $method->getDeclaringClass()->getName(), - $method->getName() - ); - - $e = new self($message, 0, $previous); - $e->type = $previous->type; - - return $e; + //} } } diff --git a/tests/FieldsBuilderTest.php b/tests/FieldsBuilderTest.php index b74ef34928..67e37b101e 100644 --- a/tests/FieldsBuilderTest.php +++ b/tests/FieldsBuilderTest.php @@ -726,8 +726,8 @@ public function testQueryProviderWithParamDateTime(): void $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Parameter $dateTime in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithParamDateTime::test is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For parameter $dateTime, in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithParamDateTime::test, type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); $queries = $queryProvider->getQueries($controller); } @@ -737,8 +737,8 @@ public function testQueryProviderWithReturnDateTime(): void $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Return type in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithReturnDateTime::test is type-hinted to "DateTime". Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For return type of TheCodingMachine\GraphQLite\Fixtures\TestControllerWithReturnDateTime::test, type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); $queries = $queryProvider->getQueries($controller); } diff --git a/tests/Mappers/Root/BaseTypeMapperTest.php b/tests/Mappers/Root/BaseTypeMapperTest.php index 3745b56b5d..aca24d8621 100644 --- a/tests/Mappers/Root/BaseTypeMapperTest.php +++ b/tests/Mappers/Root/BaseTypeMapperTest.php @@ -11,6 +11,7 @@ use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\GraphQLRuntimeException; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; class BaseTypeMapperTest extends AbstractQueryProviderTest @@ -29,9 +30,8 @@ public function testToGraphQLOutputTypeException(): void { $baseTypeMapper = new BaseTypeMapper(new FinalRootTypeMapper($this->getTypeMapper()), $this->getTypeMapper(), $this->getRootTypeMapper()); - $this->expectException(GraphQLRuntimeException::class); - //$this->expectExceptionMessage('Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); - $this->expectExceptionMessage('Don\'t know how to handle type \DateTime'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage("type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead."); $baseTypeMapper->toGraphQLInputType(new Object_(new Fqsen('\\DateTime')), null, 'foo', new ReflectionMethod(BaseTypeMapper::class, '__construct'), new DocBlock()); } From 7373839f17d234333c3851b4145a0a550af2c9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 14:51:10 +0100 Subject: [PATCH 3/8] Migrating phpdoc null exception from typeMappingRuntimeException --- src/Mappers/CannotMapTypeException.php | 5 +++++ src/Mappers/Root/NullableTypeMapperAdapter.php | 6 +++--- tests/Mappers/Root/NullableTypeMapperAdapterTest.php | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index d73c32904c..cc53eb67c2 100644 --- a/src/Mappers/CannotMapTypeException.php +++ b/src/Mappers/CannotMapTypeException.php @@ -149,4 +149,9 @@ public static function createForDateTime(): self { return new self('type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); } + + public static function createForNull(): self + { + return new self('type-hinting against null only in the PHPDoc is not allowed.'); + } } diff --git a/src/Mappers/Root/NullableTypeMapperAdapter.php b/src/Mappers/Root/NullableTypeMapperAdapter.php index 0a5b251365..5aebc5651e 100644 --- a/src/Mappers/Root/NullableTypeMapperAdapter.php +++ b/src/Mappers/Root/NullableTypeMapperAdapter.php @@ -15,7 +15,7 @@ use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Nullable; use ReflectionMethod; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputObjectType; use function array_filter; use function array_map; @@ -50,7 +50,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection if ($isNullable) { $nonNullableType = $this->getNonNullable($type); if ($nonNullableType === null) { - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForNull(); } $type = $nonNullableType; } @@ -77,7 +77,7 @@ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argu if ($isNullable) { $nonNullableType = $this->getNonNullable($type); if ($nonNullableType === null) { - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForNull(); } $type = $nonNullableType; } diff --git a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php index f1dfa47cd7..2a4910d5f6 100644 --- a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php +++ b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php @@ -35,8 +35,8 @@ public function testOnlyNull(): void { $compoundTypeMapper = $this->getRootTypeMapper(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Don\'t know how to handle type null'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('type-hinting against null only in the PHPDoc is not allowed.'); $compoundTypeMapper->toGraphQLOutputType($this->resolveType('null'), null, new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock()); } @@ -44,8 +44,8 @@ public function testOnlyNull2(): void { $compoundTypeMapper = $this->getRootTypeMapper(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Don\'t know how to handle type null'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('type-hinting against null only in the PHPDoc is not allowed.'); $compoundTypeMapper->toGraphQLInputType($this->resolveType('null'), null, 'foo', new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock()); } } From 5c729a1e6cd643a3a84959d5872184a8148fa840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 16:36:58 +0100 Subject: [PATCH 4/8] Migrating union in params exception out of TypeMappingRuntimeException --- src/Mappers/CannotMapTypeException.php | 5 +++++ src/Mappers/Root/CompoundTypeMapper.php | 2 +- src/TypeMappingRuntimeException.php | 25 ++----------------------- tests/FieldsBuilderTest.php | 4 ++-- 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index cc53eb67c2..d1268d61cf 100644 --- a/src/Mappers/CannotMapTypeException.php +++ b/src/Mappers/CannotMapTypeException.php @@ -154,4 +154,9 @@ public static function createForNull(): self { return new self('type-hinting against null only in the PHPDoc is not allowed.'); } + + public static function createForInputUnionType(PhpDocumentorType $type): self + { + return new self('parameter is type-hinted to "' . $type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.'); + } } diff --git a/src/Mappers/Root/CompoundTypeMapper.php b/src/Mappers/Root/CompoundTypeMapper.php index 906430db47..e1a679c4ee 100644 --- a/src/Mappers/Root/CompoundTypeMapper.php +++ b/src/Mappers/Root/CompoundTypeMapper.php @@ -107,7 +107,7 @@ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argu // At this point, the |null has been removed and the |iterable has been removed too. // So there should only be compound input types, which is forbidden by the GraphQL spec. // Let's kill this right away - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForInputUnionType($type); } private function isWrappedListOfType(GraphQLType $type): bool diff --git a/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php index 74d7e1a206..d59b0722c6 100644 --- a/src/TypeMappingRuntimeException.php +++ b/src/TypeMappingRuntimeException.php @@ -28,32 +28,11 @@ public static function createFromType(Type $type): self public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, ReflectionParameter $parameter): TypeMappingRuntimeException { - $declaringClass = $parameter->getDeclaringClass(); - Assert::notNull($declaringClass, 'Parameter passed must be a parameter of a method, not a parameter of a function.'); - - $message = ''; - if ($previous->type instanceof Compound) { - $message = sprintf( - 'Parameter $%s in %s::%s is type-hinted to "' . $previous->type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.', - $parameter->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName() - ); - } elseif (! ($previous->type instanceof Object_)) { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } - - $e = new self($message, 0, $previous); - $e->type = $previous->type; - - return $e; + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); } public static function wrapWithReturnInfo(TypeMappingRuntimeException $previous, ReflectionMethod $method): TypeMappingRuntimeException { - //if (! ($previous->type instanceof Object_)) { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - - //} + throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); } } diff --git a/tests/FieldsBuilderTest.php b/tests/FieldsBuilderTest.php index 67e37b101e..d250c62160 100644 --- a/tests/FieldsBuilderTest.php +++ b/tests/FieldsBuilderTest.php @@ -748,8 +748,8 @@ public function testQueryProviderWithUnionInputParam(): void $queryProvider = $this->buildFieldsBuilder(); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage('Parameter $testObject in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithUnionInputParam::test is type-hinted to "\TheCodingMachine\GraphQLite\Fixtures\TestObject|\TheCodingMachine\GraphQLite\Fixtures\TestObject2". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.'); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage('For parameter $testObject, in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithUnionInputParam::test, parameter is type-hinted to "\TheCodingMachine\GraphQLite\Fixtures\TestObject|\TheCodingMachine\GraphQLite\Fixtures\TestObject2". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.'); $queries = $queryProvider->getQueries($controller); } From d4aaec6684e553c15025a87ebeb639b1b36b36d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 17:00:17 +0100 Subject: [PATCH 5/8] Migrating exceptions from compound type away from TypeMappingRuntimeException --- src/Mappers/Root/CompoundTypeMapper.php | 7 +++---- tests/Mappers/Root/CompoundTypeMapperTest.php | 9 +++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Mappers/Root/CompoundTypeMapper.php b/src/Mappers/Root/CompoundTypeMapper.php index e1a679c4ee..bad53a9c55 100644 --- a/src/Mappers/Root/CompoundTypeMapper.php +++ b/src/Mappers/Root/CompoundTypeMapper.php @@ -16,6 +16,7 @@ use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Iterable_; use ReflectionMethod; +use RuntimeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface; use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; @@ -63,9 +64,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection } $filteredDocBlockTypes = iterator_to_array($type); - if (empty($filteredDocBlockTypes)) { - throw TypeMappingRuntimeException::createFromType($type); - } + Assert::notEmpty($filteredDocBlockTypes); $unionTypes = []; $lastException = null; @@ -79,7 +78,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection } if ($mustBeIterable && empty($unionTypes)) { - throw TypeMappingRuntimeException::createFromType(new Iterable_()); + throw new RuntimeException('Iterable compound type cannot be alone in the compound.'); } $return = $this->getTypeFromUnion($unionTypes); diff --git a/tests/Mappers/Root/CompoundTypeMapperTest.php b/tests/Mappers/Root/CompoundTypeMapperTest.php index 101335fe49..9f116fd4c0 100644 --- a/tests/Mappers/Root/CompoundTypeMapperTest.php +++ b/tests/Mappers/Root/CompoundTypeMapperTest.php @@ -2,12 +2,14 @@ namespace TheCodingMachine\GraphQLite\Mappers\Root; +use InvalidArgumentException; use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Iterable_; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; use ReflectionMethod; +use RuntimeException; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; @@ -23,8 +25,7 @@ public function testException1() $this->getTypeMapper() ); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type"); + $this->expectException(InvalidArgumentException::class); $compoundTypeMapper->toGraphQLOutputType(new Compound([]), null, new ReflectionMethod(__CLASS__, 'testException1'), new DocBlock()); } @@ -37,8 +38,8 @@ public function testException2() $this->getTypeMapper() ); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type iterable"); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Iterable compound type cannot be alone in the compound.'); $compoundTypeMapper->toGraphQLOutputType(new Compound([new Iterable_()]), null, new ReflectionMethod(__CLASS__, 'testException1'), new DocBlock()); } From 84a901ded074b8a57ab154bcc7de16decaec9623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 17:10:21 +0100 Subject: [PATCH 6/8] Removing last calls to TypeMapperRuntimeException in main source --- src/Mappers/CannotMapTypeException.php | 5 +++++ src/Mappers/Root/CompoundTypeMapper.php | 1 - src/Mappers/Root/FinalRootTypeMapper.php | 6 +++--- src/TypeMappingRuntimeException.php | 4 ---- tests/Mappers/Root/BaseTypeMapperTest.php | 12 ++++++------ tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php | 5 +++-- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index d1268d61cf..5dd92097ac 100644 --- a/src/Mappers/CannotMapTypeException.php +++ b/src/Mappers/CannotMapTypeException.php @@ -159,4 +159,9 @@ public static function createForInputUnionType(PhpDocumentorType $type): self { return new self('parameter is type-hinted to "' . $type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.'); } + + public static function createForPhpDocType(PhpDocumentorType $type): self + { + return new self("don't know how to handle type " . (string) $type); + } } diff --git a/src/Mappers/Root/CompoundTypeMapper.php b/src/Mappers/Root/CompoundTypeMapper.php index bad53a9c55..e22ff0f1b7 100644 --- a/src/Mappers/Root/CompoundTypeMapper.php +++ b/src/Mappers/Root/CompoundTypeMapper.php @@ -19,7 +19,6 @@ use RuntimeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; use TheCodingMachine\GraphQLite\TypeRegistry; use TheCodingMachine\GraphQLite\Types\UnionType; use Webmozart\Assert\Assert; diff --git a/src/Mappers/Root/FinalRootTypeMapper.php b/src/Mappers/Root/FinalRootTypeMapper.php index aadc08fede..6a5ca701a2 100644 --- a/src/Mappers/Root/FinalRootTypeMapper.php +++ b/src/Mappers/Root/FinalRootTypeMapper.php @@ -11,8 +11,8 @@ use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\Type; use ReflectionMethod; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; /** * The final root type mapper of the RootTypeMapperInterface chain. @@ -37,7 +37,7 @@ public function __construct(RecursiveTypeMapperInterface $recursiveTypeMapper) */ public function toGraphQLOutputType(Type $type, ?OutputType $subType, ReflectionMethod $refMethod, DocBlock $docBlockObj): OutputType { - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForPhpDocType($type); } /** @@ -47,7 +47,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection */ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argumentName, ReflectionMethod $refMethod, DocBlock $docBlockObj): InputType { - throw TypeMappingRuntimeException::createFromType($type); + throw CannotMapTypeException::createForPhpDocType($type); } /** diff --git a/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php index d59b0722c6..186be3c3a8 100644 --- a/src/TypeMappingRuntimeException.php +++ b/src/TypeMappingRuntimeException.php @@ -5,13 +5,9 @@ namespace TheCodingMachine\GraphQLite; use phpDocumentor\Reflection\Type; -use phpDocumentor\Reflection\Types\Compound; -use phpDocumentor\Reflection\Types\Object_; use ReflectionMethod; use ReflectionParameter; -use Webmozart\Assert\Assert; use function get_class; -use function sprintf; class TypeMappingRuntimeException extends GraphQLRuntimeException { diff --git a/tests/Mappers/Root/BaseTypeMapperTest.php b/tests/Mappers/Root/BaseTypeMapperTest.php index aca24d8621..eba1642b74 100644 --- a/tests/Mappers/Root/BaseTypeMapperTest.php +++ b/tests/Mappers/Root/BaseTypeMapperTest.php @@ -21,8 +21,8 @@ public function testNullableToGraphQLInputType(): void { $baseTypeMapper = new BaseTypeMapper(new FinalRootTypeMapper($this->getTypeMapper()), $this->getTypeMapper(), $this->getRootTypeMapper()); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type ?\Exception"); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage("don't know how to handle type ?\Exception"); $baseTypeMapper->toGraphQLInputType(new Nullable(new Object_(new Fqsen('\\Exception'))), null, 'foo', new ReflectionMethod(BaseTypeMapper::class, '__construct'), new DocBlock()); } @@ -39,8 +39,8 @@ public function testUnmappableOutputArray(): void { $baseTypeMapper = new BaseTypeMapper(new FinalRootTypeMapper($this->getTypeMapper()), $this->getTypeMapper(), $this->getRootTypeMapper()); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type resource"); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage("don't know how to handle type resource"); $mappedType = $baseTypeMapper->toGraphQLOutputType(new Array_(new Resource_()), null, new ReflectionMethod(BaseTypeMapper::class, '__construct'), new DocBlock()); } @@ -48,8 +48,8 @@ public function testUnmappableInputArray(): void { $baseTypeMapper = new BaseTypeMapper(new FinalRootTypeMapper($this->getTypeMapper()), $this->getTypeMapper(), $this->getRootTypeMapper()); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type resource"); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage("don't know how to handle type resource"); $mappedType = $baseTypeMapper->toGraphQLInputType(new Array_(new Resource_()), null, 'foo', new ReflectionMethod(BaseTypeMapper::class, '__construct'), new DocBlock()); } } diff --git a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php index 3adf80cf67..308c3051b3 100644 --- a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php +++ b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; class MyCLabsEnumTypeMapperTest extends AbstractQueryProviderTest @@ -16,8 +17,8 @@ public function testObjectTypeHint(): void { $mapper = new MyCLabsEnumTypeMapper(new FinalRootTypeMapper($this->getTypeMapper())); - $this->expectException(TypeMappingRuntimeException::class); - $this->expectExceptionMessage("Don't know how to handle type object"); + $this->expectException(CannotMapTypeException::class); + $this->expectExceptionMessage("don't know how to handle type object"); $mapper->toGraphQLOutputType(new Object_(), null, new ReflectionMethod(__CLASS__, 'testObjectTypeHint'), new DocBlock()); } } From fbaf86eec7bc6659b47f29209f1e152b1b17eb50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 17:16:58 +0100 Subject: [PATCH 7/8] Final removal of any reference to TypeMappingRuntimeException --- src/Mappers/Parameters/TypeHandler.php | 5 --- src/Mappers/Root/IteratorTypeMapper.php | 5 ++- src/TypeMappingRuntimeException.php | 34 ------------------- tests/Mappers/CompositeTypeMapperTest.php | 8 ++--- tests/Mappers/Root/BaseTypeMapperTest.php | 1 - tests/Mappers/Root/CompoundTypeMapperTest.php | 2 -- .../Root/MyCLabsEnumTypeMapperTest.php | 2 -- .../Root/NullableTypeMapperAdapterTest.php | 11 ------ 8 files changed, 4 insertions(+), 64 deletions(-) delete mode 100644 src/TypeMappingRuntimeException.php diff --git a/src/Mappers/Parameters/TypeHandler.php b/src/Mappers/Parameters/TypeHandler.php index 00dd91d796..be717fe204 100644 --- a/src/Mappers/Parameters/TypeHandler.php +++ b/src/Mappers/Parameters/TypeHandler.php @@ -34,7 +34,6 @@ use TheCodingMachine\GraphQLite\Parameters\DefaultValueParameter; use TheCodingMachine\GraphQLite\Parameters\InputTypeParameter; use TheCodingMachine\GraphQLite\Parameters\ParameterInterface; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; use TheCodingMachine\GraphQLite\Types\ArgumentResolver; use TheCodingMachine\GraphQLite\Types\TypeResolver; use Webmozart\Assert\Assert; @@ -84,8 +83,6 @@ public function mapReturnType(ReflectionMethod $refMethod, DocBlock $docBlockObj try { $type = $this->mapType($phpdocType, $docBlockReturnType, $returnType ? $returnType->allowsNull() : false, false, $refMethod, $docBlockObj); assert($type instanceof GraphQLType && $type instanceof OutputType); - } catch (TypeMappingRuntimeException $e) { - throw TypeMappingRuntimeException::wrapWithReturnInfo($e, $refMethod); } catch (CannotMapTypeExceptionInterface $e) { $e->addReturnInfo($refMethod); throw $e; @@ -147,8 +144,6 @@ public function mapParameter(ReflectionParameter $parameter, DocBlock $docBlock, $declaringFunction = $parameter->getDeclaringFunction(); Assert::isInstanceOf($declaringFunction, ReflectionMethod::class, 'Parameter of a function passed. Only parameters of methods are supported.'); $type = $this->mapType($phpdocType, $paramTagType, $allowsNull || $parameter->isDefaultValueAvailable(), true, $declaringFunction, $docBlock, $parameter->getName()); - } catch (TypeMappingRuntimeException $e) { - throw TypeMappingRuntimeException::wrapWithParamInfo($e, $parameter); } catch (CannotMapTypeExceptionInterface $e) { $e->addParamInfo($parameter); throw $e; diff --git a/src/Mappers/Root/IteratorTypeMapper.php b/src/Mappers/Root/IteratorTypeMapper.php index 0c5f04aeb7..9e0d298ccf 100644 --- a/src/Mappers/Root/IteratorTypeMapper.php +++ b/src/Mappers/Root/IteratorTypeMapper.php @@ -22,7 +22,6 @@ use ReflectionMethod; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; use Webmozart\Assert\Assert; use function assert; use function count; @@ -165,7 +164,7 @@ private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $ } $unionTypes[] = $topToGraphQLType($iteratorType, $subGraphQlType); - } catch (TypeMappingRuntimeException | CannotMapTypeExceptionInterface $e) { + } catch (CannotMapTypeExceptionInterface $e) { // We have several types. It is ok not to be able to match one. $lastException = $e; @@ -183,7 +182,7 @@ private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $ // We have an issue, let's try without the subType try { $result = $topToGraphQLType($iteratorType, null); - } catch (TypeMappingRuntimeException | CannotMapTypeExceptionInterface $otherException) { + } catch (CannotMapTypeExceptionInterface $otherException) { // Still an issue? Let's rethrow the previous exception. throw $lastException; } diff --git a/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php deleted file mode 100644 index 186be3c3a8..0000000000 --- a/src/TypeMappingRuntimeException.php +++ /dev/null @@ -1,34 +0,0 @@ -type = $type; - - return $e; - } - - public static function wrapWithParamInfo(TypeMappingRuntimeException $previous, ReflectionParameter $parameter): TypeMappingRuntimeException - { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } - - public static function wrapWithReturnInfo(TypeMappingRuntimeException $previous, ReflectionMethod $method): TypeMappingRuntimeException - { - throw new GraphQLRuntimeException("Unexpected type in TypeMappingException. Got '" . get_class($previous->type) . '"'); - } -} diff --git a/tests/Mappers/CompositeTypeMapperTest.php b/tests/Mappers/CompositeTypeMapperTest.php index 2ba57902e8..553e0fc4e2 100644 --- a/tests/Mappers/CompositeTypeMapperTest.php +++ b/tests/Mappers/CompositeTypeMapperTest.php @@ -2,21 +2,17 @@ namespace TheCodingMachine\GraphQLite\Mappers; -use GraphQL\Type\Definition\InputType; use GraphQL\Type\Definition\NamedType; use GraphQL\Type\Definition\OutputType; use GraphQL\Type\Definition\Type; -use PHPUnit\Framework\TestCase; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\Fixtures\Mocks\MockResolvableInputObjectType; use TheCodingMachine\GraphQLite\Fixtures\TestObject; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ObjectType; use TheCodingMachine\GraphQLite\Types\MutableInterface; use TheCodingMachine\GraphQLite\Types\MutableObjectType; use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputInterface; -use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputObjectType; class CompositeTypeMapperTest extends AbstractQueryProviderTest { @@ -38,7 +34,7 @@ public function mapClassToType(string $className, ?OutputType $subType): \TheCod ], ]); } else { - throw TypeMappingRuntimeException::createFromType(TestObject::class); + throw CannotMapTypeException::createForType(TestObject::class); } } @@ -52,7 +48,7 @@ public function mapClassToInputType(string $className): ResolvableMutableInputIn ], ]); } else { - throw TypeMappingRuntimeException::createFromType(TestObject::class); + throw CannotMapTypeException::createForType(TestObject::class); } } diff --git a/tests/Mappers/Root/BaseTypeMapperTest.php b/tests/Mappers/Root/BaseTypeMapperTest.php index eba1642b74..0cacb886fa 100644 --- a/tests/Mappers/Root/BaseTypeMapperTest.php +++ b/tests/Mappers/Root/BaseTypeMapperTest.php @@ -12,7 +12,6 @@ use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\GraphQLRuntimeException; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; class BaseTypeMapperTest extends AbstractQueryProviderTest { diff --git a/tests/Mappers/Root/CompoundTypeMapperTest.php b/tests/Mappers/Root/CompoundTypeMapperTest.php index 9f116fd4c0..d9ee2209cf 100644 --- a/tests/Mappers/Root/CompoundTypeMapperTest.php +++ b/tests/Mappers/Root/CompoundTypeMapperTest.php @@ -7,12 +7,10 @@ use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Iterable_; use phpDocumentor\Reflection\Types\String_; -use PHPUnit\Framework\TestCase; use ReflectionMethod; use RuntimeException; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; class CompoundTypeMapperTest extends AbstractQueryProviderTest { diff --git a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php index 308c3051b3..6ff3617c52 100644 --- a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php +++ b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php @@ -4,11 +4,9 @@ use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\Types\Object_; -use PHPUnit\Framework\TestCase; use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; class MyCLabsEnumTypeMapperTest extends AbstractQueryProviderTest { diff --git a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php index 2a4910d5f6..fe7f45f060 100644 --- a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php +++ b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php @@ -4,22 +4,11 @@ use GraphQL\Type\Definition\NonNull; use phpDocumentor\Reflection\DocBlock; -use phpDocumentor\Reflection\Fqsen; -use phpDocumentor\Reflection\TypeResolver as PhpDocumentorTypeResolver; -use phpDocumentor\Reflection\Types\Boolean; -use phpDocumentor\Reflection\Types\Compound; -use phpDocumentor\Reflection\Types\Iterable_; -use phpDocumentor\Reflection\Types\Null_; -use phpDocumentor\Reflection\Types\Object_; -use phpDocumentor\Reflection\Types\String_; -use PHPUnit\Framework\TestCase; use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\Fixtures\TestObject; use TheCodingMachine\GraphQLite\Fixtures\TestObject2; use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; -use phpDocumentor\Reflection\Type; class NullableTypeMapperAdapterTest extends AbstractQueryProviderTest { From 65d349669458b16d3546277117ce37f53bad3e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 20 Dec 2019 17:29:55 +0100 Subject: [PATCH 8/8] Covering uncovered line --- tests/FieldsBuilderTest.php | 4 ++-- tests/Fixtures/TestControllerWithArrayParam.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/FieldsBuilderTest.php b/tests/FieldsBuilderTest.php index d250c62160..c93589105d 100644 --- a/tests/FieldsBuilderTest.php +++ b/tests/FieldsBuilderTest.php @@ -511,14 +511,14 @@ public function testQueryProviderWithArrayReturnType(): void $queryProvider->getQueries($controller); } - public function testQueryProviderWithArrayParams(): void + public function testQueryProviderWithIterableParams(): void { $controller = new TestControllerWithArrayParam(); $queryProvider = $this->buildFieldsBuilder(); $this->expectException(CannotMapTypeException::class); - $this->expectExceptionMessage('For parameter $params, in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayParam::test, please provide an additional @param in the PHPDoc block to further specify the type of the array. For instance: @param string[] $params.'); + $this->expectExceptionMessage('For parameter $params, in TheCodingMachine\GraphQLite\Fixtures\TestControllerWithArrayParam::test, please provide an additional @param in the PHPDoc block to further specify the type of the iterable. For instance: @param string[] $params.'); $queryProvider->getQueries($controller); } diff --git a/tests/Fixtures/TestControllerWithArrayParam.php b/tests/Fixtures/TestControllerWithArrayParam.php index 3b9b86da9b..eed0990612 100644 --- a/tests/Fixtures/TestControllerWithArrayParam.php +++ b/tests/Fixtures/TestControllerWithArrayParam.php @@ -11,7 +11,7 @@ class TestControllerWithArrayParam /** * @Query() */ - public function test(array $params): array + public function test(iterable $params): array { return $params; }