|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheCodingMachine\GraphQLite\Mappers\Root; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use GraphQL\Type\Definition\InputType; |
| 7 | +use GraphQL\Type\Definition\NamedType; |
| 8 | +use GraphQL\Type\Definition\NonNull; |
| 9 | +use GraphQL\Type\Definition\OutputType; |
| 10 | +use GraphQL\Type\Definition\StringType; |
| 11 | +use GraphQL\Type\Definition\Type as GraphQLType; |
| 12 | +use phpDocumentor\Reflection\DocBlock; |
| 13 | +use phpDocumentor\Reflection\Type; |
| 14 | +use phpDocumentor\Reflection\Types\Array_; |
| 15 | +use phpDocumentor\Reflection\Types\Callable_; |
| 16 | +use phpDocumentor\Reflection\Types\CallableParameter; |
| 17 | +use phpDocumentor\Reflection\Types\Nullable; |
| 18 | +use phpDocumentor\Reflection\Types\Object_; |
| 19 | +use phpDocumentor\Reflection\Types\String_; |
| 20 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 21 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 22 | +use ReflectionMethod; |
| 23 | +use TheCodingMachine\GraphQLite\AbstractQueryProvider; |
| 24 | +use TheCodingMachine\GraphQLite\Fixtures\TestObject; |
| 25 | +use TheCodingMachine\GraphQLite\Fixtures\TestObject2; |
| 26 | +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; |
| 27 | + |
| 28 | +#[CoversClass(CallableTypeMapper::class)] |
| 29 | +class CallableTypeMapperTest extends AbstractQueryProvider |
| 30 | +{ |
| 31 | + public function testMapsCallableReturnTypeUsingTopRootMapper(): void |
| 32 | + { |
| 33 | + $reflection = new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'); |
| 34 | + $docBlock = new DocBlock(); |
| 35 | + |
| 36 | + $returnType = new String_(); |
| 37 | + |
| 38 | + $topRootMapper = $this->createMock(RootTypeMapperInterface::class); |
| 39 | + $topRootMapper->expects($this->once()) |
| 40 | + ->method('toGraphQLOutputType') |
| 41 | + ->with($returnType, null, $reflection, $docBlock) |
| 42 | + ->willReturn(GraphQLType::string()); |
| 43 | + |
| 44 | + $mapper = new CallableTypeMapper( |
| 45 | + $this->createMock(RootTypeMapperInterface::class), |
| 46 | + $topRootMapper, |
| 47 | + ); |
| 48 | + |
| 49 | + $result = $mapper->toGraphQLOutputType(new Callable_(returnType: $returnType), null, $reflection, $docBlock); |
| 50 | + |
| 51 | + $this->assertSame(GraphQLType::string(), $result); |
| 52 | + } |
| 53 | + |
| 54 | + public function testThrowsWhenUsingCallableWithParameters(): void |
| 55 | + { |
| 56 | + $this->expectExceptionObject(CannotMapTypeException::createForUnexpectedCallableParameters()); |
| 57 | + |
| 58 | + $mapper = new CallableTypeMapper( |
| 59 | + $this->createMock(RootTypeMapperInterface::class), |
| 60 | + $this->createMock(RootTypeMapperInterface::class) |
| 61 | + ); |
| 62 | + |
| 63 | + $type = new Callable_( |
| 64 | + parameters: [ |
| 65 | + new CallableParameter(new String_()) |
| 66 | + ] |
| 67 | + ); |
| 68 | + |
| 69 | + $mapper->toGraphQLOutputType($type, null, new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testThrowsWhenUsingCallableWithoutReturnType(): void |
| 73 | + { |
| 74 | + $this->expectExceptionObject(CannotMapTypeException::createForMissingCallableReturnType()); |
| 75 | + |
| 76 | + $mapper = new CallableTypeMapper( |
| 77 | + $this->createMock(RootTypeMapperInterface::class), |
| 78 | + $this->createMock(RootTypeMapperInterface::class) |
| 79 | + ); |
| 80 | + |
| 81 | + $mapper->toGraphQLOutputType(new Callable_(), null, new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testThrowsWhenUsingCallableAsInputType(): void |
| 85 | + { |
| 86 | + $this->expectExceptionObject(CannotMapTypeException::createForCallableAsInput()); |
| 87 | + |
| 88 | + $mapper = new CallableTypeMapper( |
| 89 | + $this->createMock(RootTypeMapperInterface::class), |
| 90 | + $this->createMock(RootTypeMapperInterface::class) |
| 91 | + ); |
| 92 | + |
| 93 | + $mapper->toGraphQLInputType(new Callable_(), null, 'arg1', new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); |
| 94 | + } |
| 95 | + |
| 96 | + #[DataProvider('skipsNonCallablesProvider')] |
| 97 | + public function testSkipsNonCallables(callable $createType): void |
| 98 | + { |
| 99 | + $type = $createType(); |
| 100 | + $reflection = new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'); |
| 101 | + $docBlock = new DocBlock(); |
| 102 | + |
| 103 | + $next = $this->createMock(RootTypeMapperInterface::class); |
| 104 | + $next->expects($this->once()) |
| 105 | + ->method('toGraphQLOutputType') |
| 106 | + ->with($type, null, $reflection, $docBlock) |
| 107 | + ->willReturn(GraphQLType::string()); |
| 108 | + $next->expects($this->once()) |
| 109 | + ->method('toGraphQLInputType') |
| 110 | + ->with($type, null, 'arg1', $reflection, $docBlock) |
| 111 | + ->willReturn(GraphQLType::int()); |
| 112 | + $next->expects($this->once()) |
| 113 | + ->method('mapNameToType') |
| 114 | + ->with('Name') |
| 115 | + ->willReturn(GraphQLType::float()); |
| 116 | + |
| 117 | + $mapper = new CallableTypeMapper($next, $this->createMock(RootTypeMapperInterface::class)); |
| 118 | + |
| 119 | + $this->assertSame(GraphQLType::string(), $mapper->toGraphQLOutputType($type, null, $reflection, $docBlock)); |
| 120 | + $this->assertSame(GraphQLType::int(), $mapper->toGraphQLInputType($type, null, 'arg1', $reflection, $docBlock)); |
| 121 | + $this->assertSame(GraphQLType::float(), $mapper->mapNameToType('Name')); |
| 122 | + } |
| 123 | + |
| 124 | + public static function skipsNonCallablesProvider(): iterable |
| 125 | + { |
| 126 | + yield [fn () => new Object_()]; |
| 127 | + yield [fn () => new Array_()]; |
| 128 | + yield [fn () => new String_()]; |
| 129 | + } |
| 130 | +} |
0 commit comments