Passed
Push — master ( ba1e45...7b307a )
by David
03:05
created

anonymous//tests/ControllerQueryProviderTest.php$1   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Importance

Changes 0
Metric Value
wmc 1
cbo 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use PHPUnit\Framework\TestCase;
7
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController;
8
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
use Youshido\GraphQL\Type\ListType\ListType;
11
use Youshido\GraphQL\Type\NonNullType;
12
use Youshido\GraphQL\Type\Object\ObjectType;
13
use Youshido\GraphQL\Type\Scalar\IntType;
14
use Youshido\GraphQL\Type\Scalar\StringType;
15
use Youshido\GraphQL\Type\TypeInterface;
16
17
class ControllerQueryProviderTest extends TestCase
18
{
19
20
    private $testObjectType;
21
    private $typeMapper;
22
    private $hydrator;
23
24
    private function getTestObjectType()
25
    {
26
        if ($this->testObjectType === null) {
27
            $this->testObjectType = new ObjectType([
28
                'name'    => 'TestObject',
29
                'fields'  => [
30
                    'test'   => new StringType(),
31
                ],
32
            ]);
33
        }
34
        return $this->testObjectType;
35
    }
36
37
    private function getTypeMapper()
38
    {
39
        if ($this->typeMapper === null) {
40
            $this->typeMapper = new class($this->getTestObjectType()) implements TypeMapperInterface {
41
                /**
42
                 * @var ObjectType
43
                 */
44
                private $testObjectType;
45
46
                public function __construct(ObjectType $testObjectType)
47
                {
48
49
                    $this->testObjectType = $testObjectType;
50
                }
51
52
                public function mapClassToType(string $className): TypeInterface
53
                {
54
                    if ($className === TestObject::class) {
55
                        return $this->testObjectType;
56
                    } else {
57
                        throw new \RuntimeException('Unexpected type');
58
                    }
59
                }
60
            };
61
        }
62
        return $this->typeMapper;
63
    }
64
65
    private function getHydrator()
66
    {
67
        if ($this->hydrator === null) {
68
            $this->hydrator = new class implements HydratorInterface
69
            {
70
                public function hydrate(array $data, TypeInterface $type)
71
                {
72
                    return new TestObject($data['test']);
73
                }
74
            };
75
        }
76
        return $this->hydrator;
77
    }
78
79
    public function testQueryProvider()
80
    {
81
        $controller = new TestController();
82
        $reader = new AnnotationReader();
83
84
        $queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator());
85
86
        $queries = $queryProvider->getQueries();
87
88
        $this->assertCount(1, $queries);
89
        $usersQuery = $queries[0];
90
        $this->assertSame('test', $usersQuery->getName());
91
92
        $this->assertCount(3, $usersQuery->getArguments());
93
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('int')->getType());
94
        $this->assertInstanceOf(IntType::class, $usersQuery->getArgument('int')->getType()->getTypeOf());
95
        $this->assertInstanceOf(StringType::class, $usersQuery->getArgument('string')->getType());
96
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType());
97
        $this->assertInstanceOf(ListType::class, $usersQuery->getArgument('list')->getType()->getTypeOf());
98
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType());
99
        $this->assertInstanceOf(ObjectType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf());
100
        $this->assertSame('TestObject', $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()->getName());
101
102
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
103
104
        $result = $usersQuery->resolve('foo', ['int'=>42, 'string'=>'foo', 'list'=>[
105
            ['test'=>42],
106
            ['test'=>12],
107
        ]], $mockResolveInfo);
108
109
        $this->assertInstanceOf(TestObject::class, $result);
110
        $this->assertSame('foo424212', $result->getTest());
111
    }
112
113
    public function testMutations()
114
    {
115
        $controller = new TestController();
116
        $reader = new AnnotationReader();
117
118
        $queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator());
119
120
        $mutations = $queryProvider->getMutations();
121
122
        $this->assertCount(1, $mutations);
123
        $mutation = $mutations[0];
124
        $this->assertSame('mutation', $mutation->getName());
125
126
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
127
128
        $result = $mutation->resolve('foo', ['testObject'=>['test'=>42]], $mockResolveInfo);
129
130
        $this->assertInstanceOf(TestObject::class, $result);
131
        $this->assertEquals('42', $result->getTest());
132
133
    }
134
}
135