1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Resolver; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\ListOfType; |
15
|
|
|
use GraphQL\Type\Definition\NonNull; |
16
|
|
|
use GraphQL\Type\Definition\ObjectType; |
17
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
18
|
|
|
|
19
|
|
|
class TypeResolverTest extends AbstractResolverTest |
20
|
|
|
{ |
21
|
|
|
protected function createResolver() |
22
|
|
|
{ |
23
|
|
|
return new TypeResolver(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function getResolverSolutionsMapping() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'Toto' => ['solutionFunc' => [$this, 'createObjectType'], 'solutionFuncArgs' => [['name' => 'Toto']]], |
30
|
|
|
'Tata' => ['solutionFunc' => [$this, 'createObjectType'], 'solutionFuncArgs' => [['name' => 'Tata']]], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function createObjectType(array $config) |
35
|
|
|
{ |
36
|
|
|
return new ObjectType($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @expectedException \Overblog\GraphQLBundle\Resolver\UnsupportedResolverException |
41
|
|
|
* @expectedExceptionMessage Resolver "not-supported" must be "GraphQL\Type\Definition\Type" "stdClass" given. |
42
|
|
|
*/ |
43
|
|
|
public function testAddNotSupportedSolution() |
44
|
|
|
{ |
45
|
|
|
$this->resolver->addSolution('not-supported', function () { |
46
|
|
|
return new \stdClass(); |
47
|
|
|
}); |
48
|
|
|
$this->resolver->getSolution('not-supported'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testResolveKnownType() |
52
|
|
|
{ |
53
|
|
|
$type = $this->resolver->resolve('Toto'); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(ObjectType::class, $type); |
56
|
|
|
$this->assertEquals('Toto', $type->name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @expectedException \Overblog\GraphQLBundle\Resolver\UnresolvableException |
61
|
|
|
*/ |
62
|
|
|
public function testResolveUnknownType() |
63
|
|
|
{ |
64
|
|
|
$this->resolver->resolve('Fake'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException \Overblog\GraphQLBundle\Resolver\UnresolvableException |
69
|
|
|
*/ |
70
|
|
|
public function testWrongListOfWrappingType() |
71
|
|
|
{ |
72
|
|
|
$this->resolver->resolve('[Tata'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testResolveWithListOfWrapper() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
78
|
|
|
$type = $this->resolver->resolve('[Tata]'); |
79
|
|
|
|
80
|
|
|
$this->assertInstanceOf(ListOfType::class, $type); |
81
|
|
|
$this->assertEquals('Tata', $type->getWrappedType()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testResolveWithNonNullWrapper() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
87
|
|
|
$type = $this->resolver->resolve('Toto!'); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf(NonNull::class, $type); |
90
|
|
|
$this->assertEquals('Toto', $type->getWrappedType()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function testResolveWithNonNullListOfWrapper() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
96
|
|
|
$type = $this->resolver->resolve('[Toto]!'); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf(NonNull::class, $type); |
99
|
|
|
$this->assertInstanceOf(ListOfType::class, $type->getWrappedType()); |
100
|
|
|
$this->assertEquals('Toto', $type->getWrappedType()->getWrappedType()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function testResolveWitListOfNonNullWrapper() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
106
|
|
|
$type = $this->resolver->resolve('[Toto!]'); |
107
|
|
|
|
108
|
|
|
$this->assertInstanceOf(ListOfType::class, $type); |
109
|
|
|
$this->assertInstanceOf(NonNull::class, $type->getWrappedType()); |
110
|
|
|
$this->assertEquals('Toto', $type->getWrappedType()->getWrappedType()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testResolveWitNonNullListOfNonNullWrapper() |
114
|
|
|
{ |
115
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
116
|
|
|
$type = $this->resolver->resolve('[Toto!]!'); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf(NonNull::class, $type); |
119
|
|
|
$this->assertInstanceOf(ListOfType::class, $type->getWrappedType()); |
120
|
|
|
$this->assertInstanceOf(NonNull::class, $type->getWrappedType()->getWrappedType()); |
121
|
|
|
$this->assertEquals('Toto', $type->getWrappedType()->getWrappedType()->getWrappedType()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
public function testResolveWitListOfListOfWrapper() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
/** @var \GraphQL\Type\Definition\WrappingType $type */ |
127
|
|
|
$type = $this->resolver->resolve('[[Toto]]'); |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf(ListOfType::class, $type); |
130
|
|
|
$this->assertInstanceOf(ListOfType::class, $type->getWrappedType()); |
131
|
|
|
$this->assertEquals('Toto', $type->getWrappedType()->getWrappedType()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.