Test Failed
Pull Request — master (#9)
by David
02:11
created

ControllerQueryProviderTest::testMutations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 12
nc 1
nop 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\Security\VoidAuthenticationService;
10
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
use Youshido\GraphQL\Type\InputObject\InputObjectType;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\BooleanType;
17
use Youshido\GraphQL\Type\Scalar\DateTimeType;
18
use Youshido\GraphQL\Type\Scalar\FloatType;
19
use Youshido\GraphQL\Type\Scalar\IntType;
20
use Youshido\GraphQL\Type\Scalar\StringType;
21
use Youshido\GraphQL\Type\TypeInterface;
22
use TheCodingMachine\GraphQL\Controllers\Annotations\Query;
23
24
class ControllerQueryProviderTest extends AbstractQueryProviderTest
25
{
26
    public function testQueryProvider()
27
    {
28
        $controller = new TestController();
29
        $reader = new AnnotationReader();
30
31
        $queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService());
32
33
        $queries = $queryProvider->getQueries();
34
35
        $this->assertCount(1, $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->assertSame('TestObject', $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()->getName());
52
53
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
54
55
        $result = $usersQuery->resolve('foo', ['int'=>42, 'string'=>'foo', 'list'=>[
56
                ['test'=>42],
57
                ['test'=>12],
58
            ],
59
            'boolean'=>true,
60
            'float'=>4.2,
61
            'dateTimeImmutable'=>'2017-01-01 01:01:01',
62
            'dateTime'=>'2017-01-01 01:01:01'
63
        ], $mockResolveInfo);
64
65
        $this->assertInstanceOf(TestObject::class, $result);
66
        $this->assertSame('foo424212true4.22017010101010120170101010101default', $result->getTest());
67
    }
68
69
    public function testMutations()
70
    {
71
        $controller = new TestController();
72
        $reader = new AnnotationReader();
73
74
        $queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService());
75
76
        $mutations = $queryProvider->getMutations();
77
78
        $this->assertCount(1, $mutations);
79
        $mutation = $mutations[0];
80
        $this->assertSame('mutation', $mutation->getName());
81
82
        $mockResolveInfo = $this->createMock(ResolveInfo::class);
83
84
        $result = $mutation->resolve('foo', ['testObject'=>['test'=>42]], $mockResolveInfo);
85
86
        $this->assertInstanceOf(TestObject::class, $result);
87
        $this->assertEquals('42', $result->getTest());
88
    }
89
90
    public function testErrors()
91
    {
92
        $controller = new class {
93
            /**
94
             * @Query
95
             * @return string
96
             */
97
            public function test($noTypeHint): string
0 ignored issues
show
Unused Code introduced by
The parameter $noTypeHint is not used and could be removed.

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

Loading history...
98
            {
99
                return 'foo';
100
            }
101
        };
102
        $reader = new AnnotationReader();
103
104
        $queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService());
105
106
        $this->expectException(MissingTypeHintException::class);
107
        $queryProvider->getQueries();
108
    }
109
}
110