Completed
Pull Request — master (#19)
by David
03:53
created

anonymous//tests/ControllerQueryProviderTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 9
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 TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
10
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
11
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
use Youshido\GraphQL\Type\InputObject\InputObjectType;
14
use Youshido\GraphQL\Type\ListType\ListType;
15
use Youshido\GraphQL\Type\NonNullType;
16
use Youshido\GraphQL\Type\Object\ObjectType;
17
use Youshido\GraphQL\Type\Scalar\BooleanType;
18
use Youshido\GraphQL\Type\Scalar\DateTimeType;
19
use Youshido\GraphQL\Type\Scalar\FloatType;
20
use Youshido\GraphQL\Type\Scalar\IntType;
21
use Youshido\GraphQL\Type\Scalar\StringType;
22
use Youshido\GraphQL\Type\TypeInterface;
23
use TheCodingMachine\GraphQL\Controllers\Annotations\Query;
24
25
class ControllerQueryProviderTest extends AbstractQueryProviderTest
26
{
27
    public function testQueryProvider()
28
    {
29
        $controller = new TestController();
30
31
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
32
33
        $queries = $queryProvider->getQueries();
34
35
        $this->assertCount(2, $queries);
36
        $usersQuery = $queries[0];
37
        $this->assertSame('test', $usersQuery->getName());
38
39
        $this->assertCount(8, $usersQuery->getArguments());
40
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('int')->getType());
41
        $this->assertInstanceOf(IntType::class, $usersQuery->getArgument('int')->getType()->getTypeOf());
42
        $this->assertInstanceOf(StringType::class, $usersQuery->getArgument('string')->getType());
43
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType());
44
        $this->assertInstanceOf(ListType::class, $usersQuery->getArgument('list')->getType()->getTypeOf());
45
        $this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType());
46
        $this->assertInstanceOf(InputObjectType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf());
47
        $this->assertInstanceOf(BooleanType::class, $usersQuery->getArgument('boolean')->getType());
48
        $this->assertInstanceOf(FloatType::class, $usersQuery->getArgument('float')->getType());
49
        $this->assertInstanceOf(DateTimeType::class, $usersQuery->getArgument('dateTimeImmutable')->getType());
50
        $this->assertInstanceOf(DateTimeType::class, $usersQuery->getArgument('dateTime')->getType());
51
        $this->assertInstanceOf(StringType::class, $usersQuery->getArgument('withDefault')->getType());
52
        $this->assertSame('TestObject', $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()->getName());
53
54
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
55
56
        $context = ['int' => 42, 'string' => 'foo', 'list' => [
57
            ['test' => 42],
58
            ['test' => 12],
59
        ],
60
            'boolean' => true,
61
            'float' => 4.2,
62
            'dateTimeImmutable' => '2017-01-01 01:01:01',
63
            'dateTime' => '2017-01-01 01:01:01'
64
        ];
65
66
        $result = $usersQuery->resolve('foo', $context, $mockResolveInfo);
67
68
        $this->assertInstanceOf(TestObject::class, $result);
69
        $this->assertSame('foo424212true4.22017010101010120170101010101default', $result->getTest());
70
71
        unset($context['string']); // Testing null default value
72
        $result = $usersQuery->resolve('foo', $context, $mockResolveInfo);
73
74
        $this->assertSame('424212true4.22017010101010120170101010101default', $result->getTest());
75
    }
76
77
    public function testMutations()
78
    {
79
        $controller = new TestController();
80
81
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
82
83
        $mutations = $queryProvider->getMutations();
84
85
        $this->assertCount(1, $mutations);
86
        $mutation = $mutations[0];
87
        $this->assertSame('mutation', $mutation->getName());
88
89
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
90
91
        $result = $mutation->resolve('foo', ['testObject' => ['test' => 42]], $mockResolveInfo);
92
93
        $this->assertInstanceOf(TestObject::class, $result);
94
        $this->assertEquals('42', $result->getTest());
95
    }
96
97
    public function testErrors()
98
    {
99
        $controller = new class
100
        {
101
            /**
102
             * @Query
103
             * @return string
104
             */
105
            public function test($noTypeHint): string
0 ignored issues
show
Unused Code introduced by
The parameter $noTypeHint is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
            public function test(/** @scrutinizer ignore-unused */ $noTypeHint): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
            {
107
                return 'foo';
108
            }
109
        };
110
111
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
112
113
        $this->expectException(MissingTypeHintException::class);
114
        $queryProvider->getQueries();
115
    }
116
117
    public function testQueryProviderWithFixedReturnType()
118
    {
119
        $controller = new TestController();
120
121
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
122
123
        $queries = $queryProvider->getQueries();
124
125
        $this->assertCount(2, $queries);
126
        $fixedQuery = $queries[1];
127
128
        $this->assertInstanceOf(TestType::class, $fixedQuery->getType());
129
    }
130
}