diff --git a/src/Mappers/CannotMapTypeException.php b/src/Mappers/CannotMapTypeException.php index 0affee918a..5dd92097ac 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,53 @@ 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)); + } + + 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.'); + } + + 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/Parameters/TypeHandler.php b/src/Mappers/Parameters/TypeHandler.php index da42df1291..be717fe204 100644 --- a/src/Mappers/Parameters/TypeHandler.php +++ b/src/Mappers/Parameters/TypeHandler.php @@ -28,12 +28,12 @@ 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; 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; @@ -83,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; @@ -146,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; @@ -179,7 +175,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/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/Mappers/Root/CompoundTypeMapper.php b/src/Mappers/Root/CompoundTypeMapper.php index 906430db47..e22ff0f1b7 100644 --- a/src/Mappers/Root/CompoundTypeMapper.php +++ b/src/Mappers/Root/CompoundTypeMapper.php @@ -16,9 +16,9 @@ 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; use TheCodingMachine\GraphQLite\TypeRegistry; use TheCodingMachine\GraphQLite\Types\UnionType; use Webmozart\Assert\Assert; @@ -63,9 +63,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 +77,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); @@ -107,7 +105,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/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/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/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/src/TypeMappingRuntimeException.php b/src/TypeMappingRuntimeException.php deleted file mode 100644 index 3f1f911e65..0000000000 --- a/src/TypeMappingRuntimeException.php +++ /dev/null @@ -1,125 +0,0 @@ -type = $type; - - return $e; - } - - 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.'); - if ($previous->type instanceof Array_ || $previous->type instanceof Iterable_) { - $typeStr = $previous->type instanceof Array_ ? 'array' : 'iterable'; - $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->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName(), - $typeStr, - $typeStr, - $parameter->getName() - ); - } elseif ($previous->type instanceof Mixed_) { - $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->getName(), - $declaringClass->getName(), - $parameter->getDeclaringFunction()->getName(), - $parameter->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); - $e->type = $previous->type; - - return $e; - } - - 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 . '"'); - } - - $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 edf15325ad..c93589105d 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,19 +506,19 @@ 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); } - public function testQueryProviderWithArrayParams(): void + public function testQueryProviderWithIterableParams(): void { $controller = new TestControllerWithArrayParam(); $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 iterable. For instance: @param string[] $params.'); $queryProvider->getQueries($controller); } @@ -725,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); } @@ -736,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); } @@ -747,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); } 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; } 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 3745b56b5d..0cacb886fa 100644 --- a/tests/Mappers/Root/BaseTypeMapperTest.php +++ b/tests/Mappers/Root/BaseTypeMapperTest.php @@ -11,7 +11,7 @@ use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; use TheCodingMachine\GraphQLite\GraphQLRuntimeException; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; class BaseTypeMapperTest extends AbstractQueryProviderTest { @@ -20,8 +20,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()); } @@ -29,9 +29,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()); } @@ -39,8 +38,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 +47,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/CompoundTypeMapperTest.php b/tests/Mappers/Root/CompoundTypeMapperTest.php index 101335fe49..d9ee2209cf 100644 --- a/tests/Mappers/Root/CompoundTypeMapperTest.php +++ b/tests/Mappers/Root/CompoundTypeMapperTest.php @@ -2,15 +2,15 @@ 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; class CompoundTypeMapperTest extends AbstractQueryProviderTest { @@ -23,8 +23,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 +36,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()); } diff --git a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php index 3adf80cf67..6ff3617c52 100644 --- a/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php +++ b/tests/Mappers/Root/MyCLabsEnumTypeMapperTest.php @@ -4,10 +4,9 @@ use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\Types\Object_; -use PHPUnit\Framework\TestCase; use ReflectionMethod; use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; -use TheCodingMachine\GraphQLite\TypeMappingRuntimeException; +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; class MyCLabsEnumTypeMapperTest extends AbstractQueryProviderTest { @@ -16,8 +15,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()); } } diff --git a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php index f1dfa47cd7..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 { @@ -35,8 +24,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 +33,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()); } }