1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Mouf\Picotainer\Picotainer; |
7
|
|
|
use Symfony\Component\Cache\Simple\NullCache; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType; |
12
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\FooType; |
13
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeGenerator; |
14
|
|
|
use GraphQL\Type\Definition\ObjectType; |
15
|
|
|
|
16
|
|
|
class GlobTypeMapperTest extends AbstractQueryProviderTest |
17
|
|
|
{ |
18
|
|
|
public function testGlobTypeMapper() |
19
|
|
|
{ |
20
|
|
|
$container = new Picotainer([ |
21
|
|
|
FooType::class => function() { |
22
|
|
|
return new FooType($this->getRegistry()); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
$typeGenerator = new TypeGenerator($this->getRegistry()); |
27
|
|
|
|
28
|
|
|
$mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new AnnotationReader(), new NullCache()); |
29
|
|
|
|
30
|
|
|
$this->assertTrue($mapper->canMapClassToType(TestObject::class)); |
31
|
|
|
$this->assertInstanceOf(ObjectType::class, $mapper->mapClassToType(TestObject::class)); |
32
|
|
|
$this->assertSame([TestObject::class], $mapper->getSupportedClasses()); |
33
|
|
|
|
34
|
|
|
$this->expectException(CannotMapTypeException::class); |
35
|
|
|
$mapper->mapClassToType(\stdClass::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGlobTypeMapperException() |
39
|
|
|
{ |
40
|
|
|
$container = new Picotainer([ |
41
|
|
|
TestType::class => function() { |
42
|
|
|
return new TestType($this->getRegistry()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$typeGenerator = new TypeGenerator($this->getRegistry()); |
47
|
|
|
|
48
|
|
|
$mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $container, new AnnotationReader(), new NullCache()); |
49
|
|
|
|
50
|
|
|
$this->expectException(DuplicateMappingException::class); |
51
|
|
|
$mapper->canMapClassToType(TestType::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGlobTypeMapperClassNotFoundException() |
55
|
|
|
{ |
56
|
|
|
$container = new Picotainer([ |
57
|
|
|
TestType::class => function() { |
58
|
|
|
return new TestType($this->getRegistry()); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$typeGenerator = new TypeGenerator($this->getRegistry()); |
63
|
|
|
|
64
|
|
|
$mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $container, new AnnotationReader(), new NullCache()); |
65
|
|
|
|
66
|
|
|
$this->expectException(ClassNotFoundException::class); |
67
|
|
|
$this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'"); |
68
|
|
|
$mapper->canMapClassToType(TestType::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGlobTypeMapperInputType() |
72
|
|
|
{ |
73
|
|
|
$container = new Picotainer([ |
74
|
|
|
FooType::class => function() { |
75
|
|
|
return new FooType($this->getRegistry()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$typeGenerator = new TypeGenerator($this->getRegistry()); |
80
|
|
|
|
81
|
|
|
$mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new AnnotationReader(), new NullCache()); |
82
|
|
|
|
83
|
|
|
$this->assertFalse($mapper->canMapClassToInputType(TestObject::class)); |
84
|
|
|
|
85
|
|
|
$this->expectException(CannotMapTypeException::class); |
86
|
|
|
$mapper->mapClassToInputType(TestType::class); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.