Passed
Pull Request — master (#24)
by David
03:06
created

php$0 ➔ testExposedFieldDoesNotExists()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

107
            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...
108
            {
109
                return 'foo';
110
            }
111
        };
112
113
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
114
115
        $this->expectException(MissingTypeHintException::class);
116
        $queryProvider->getQueries();
117
    }
118
119
    public function testQueryProviderWithFixedReturnType()
120
    {
121
        $controller = new TestController();
122
123
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
124
125
        $queries = $queryProvider->getQueries();
126
127
        $this->assertCount(3, $queries);
128
        $fixedQuery = $queries[1];
129
130
        $this->assertInstanceOf(TestType::class, $fixedQuery->getType());
131
    }
132
133
    public function testNameFromAnnotation()
134
    {
135
        $controller = new TestController();
136
137
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
138
139
        $queries = $queryProvider->getQueries();
140
141
        $query = $queries[2];
142
143
        $this->assertSame('nameFromAnnotation', $query->getName());
144
    }
145
146
    public function testExposedField()
147
    {
148
        $controller = new TestType($this->getRegistry());
149
150
        $queryProvider = new ControllerQueryProvider($controller, $this->getRegistry());
151
152
        $fields = $queryProvider->getFields();
153
154
        $this->assertCount(3, $fields);
155
156
        $this->assertSame('customField', $fields[0]->getName());
157
        $this->assertSame('test', $fields[1]->getName());
158
    }
159
160
    public function testMissingTypeAnnotation()
161
    {
162
        $queryProvider = new ControllerQueryProvider(new TestTypeMissingAnnotation(), $this->getRegistry());
163
164
        $this->expectException(MissingAnnotationException::class);
165
        $queryProvider->getFields();
166
    }
167
168
    public function testExposedFieldDoesNotExists()
169
    {
170
        $queryProvider = new ControllerQueryProvider(new TestTypeMissingField(), $this->getRegistry());
171
172
        $this->expectException(FieldNotFoundException::class);
173
        $this->expectExceptionMessage("There is an issue with a @ExposedField annotation in class \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField\": Could not find a getter or a isser for field \"notExists\". Looked for: \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::getNotExists()\", \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::isNotExists()");
174
        $queryProvider->getFields();
175
    }
176
}
177