Failed Conditions
Pull Request — master (#10)
by Adrien
02:32
created

TypesTest::assertType()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.439
c 0
b 0
f 0
cc 6
eloc 26
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine;
6
7
use DateTime;
8
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
9
use Doctrine\ORM\Tools\SchemaValidator;
10
use GraphQL\Type\Definition\BooleanType;
11
use GraphQL\Type\Definition\ObjectType;
12
use GraphQL\Type\Definition\Type;
13
use GraphQL\Type\Schema;
14
use GraphQLTests\Doctrine\Blog\Model\Post;
15
use GraphQLTests\Doctrine\Blog\Model\User;
16
use GraphQLTests\Doctrine\Blog\Types\CustomType;
17
use GraphQLTests\Doctrine\Blog\Types\DateTimeType;
18
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
19
use stdClass;
20
21
class TypesTest extends \PHPUnit\Framework\TestCase
22
{
23
    use TypesTrait;
24
25
    public function testBlogMapping(): void
26
    {
27
        $validator = new SchemaValidator($this->entityManager);
28
        $errors = $validator->validateMapping();
29
30
        self::assertEmpty($errors, 'doctrine annotations should be valid');
31
    }
32
33
    public function testGraphQLSchemaFromDocumentationMustBeValid(): void
34
    {
35
        $schema = new Schema([
36
            'query' => new ObjectType([
37
                'name' => 'query',
38
                'fields' => [
39
                    'users' => [
40
                        'type' => Type::listOf($this->types->getOutput(User::class)), // Use automated ObjectType for output
41
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $args 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

41
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
Unused Code introduced by
The parameter $root 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

41
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
42
                            // call to repository...
43
                        },
44
                    ],
45
                    'posts' => [
46
                        'type' => Type::listOf($this->types->getOutput(Post::class)), // Use automated ObjectType for output
47
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $args 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

47
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
Unused Code introduced by
The parameter $root 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

47
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
48
                            // call to repository...
49
                        },
50
                    ],
51
                ],
52
            ]),
53
            'mutation' => new ObjectType([
54
                'name' => 'mutation',
55
                'fields' => [
56
                    'createUser' => [
57
                        'type' => Type::nonNull($this->types->getOutput(User::class)),
58
                        'args' => [
59
                            'input' => Type::nonNull($this->types->getInput(User::class)), // Use automated InputObjectType for input
60
                        ],
61
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $args 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

61
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
Unused Code introduced by
The parameter $root 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

61
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
62
                            // create new user and flush...
63
                        },
64
                    ],
65
                    'updateUser' => [
66
                        'type' => Type::nonNull($this->types->getOutput(User::class)),
67
                        'args' => [
68
                            'id' => Type::nonNull(Type::id()), // Use standard API when needed
69
                            'input' => $this->types->getInput(User::class),
70
                        ],
71
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $args 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

71
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
Unused Code introduced by
The parameter $root 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

71
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
72
                            // update existing user and flush...
73
                        },
74
                    ],
75
                    'createPost' => [
76
                        'type' => Type::nonNull($this->types->getOutput(Post::class)),
77
                        'args' => [
78
                            'input' => Type::nonNull($this->types->getInput(Post::class)), // Use automated InputObjectType for input
79
                        ],
80
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $args 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

80
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
Unused Code introduced by
The parameter $root 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

80
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
81
                            // create new post and flush...
82
                        },
83
                    ],
84
                    'updatePost' => [
85
                        'type' => Type::nonNull($this->types->getOutput(Post::class)),
86
                        'args' => [
87
                            'id' => Type::nonNull(Type::id()), // Use standard API when needed
88
                            'input' => $this->types->getInput(Post::class),
89
                        ],
90
                        'resolve' => function ($root, $args): void {
2 ignored issues
show
Unused Code introduced by
The parameter $root 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

90
                        'resolve' => function (/** @scrutinizer ignore-unused */ $root, $args): void {

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...
Unused Code introduced by
The parameter $args 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

90
                        'resolve' => function ($root, /** @scrutinizer ignore-unused */ $args): void {

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...
91
                            // update existing post and flush...
92
                        },
93
                    ],
94
                ],
95
            ]),
96
        ]);
97
98
        $schema->assertValid();
99
        self::assertTrue(true, 'passed validation successfully');
100
    }
101
102
    public function testCanGetUserDefinedScalarTypes(): void
103
    {
104
        $bool = $this->types->get(BooleanType::class);
105
        $status = $this->types->get(PostStatusType::class);
106
107
        self::assertInstanceOf(BooleanType::class, $bool, 'must be a instance of bool');
108
        self::assertInstanceOf(PostStatusType::class, $status, 'must be an instance of post status');
109
110
        self::assertSame($bool, $this->types->get(BooleanType::class), 'must returns the same instance of bool');
111
        self::assertSame($status, $this->types->get(PostStatusType::class), 'must returns the same instance of post status');
112
    }
113
114
    public function testCanGetUserMappedTypes(): void
115
    {
116
        $type = $this->types->get(stdClass::class);
117
118
        self::assertInstanceOf(CustomType::class, $type, 'must be a instance of CustomType');
119
        self::assertSame($type, $this->types->get('customName'));
120
    }
121
122
    public function testCanGetMappedTypesEitherByMappedPhpClassOrDirectTypeClass(): void
123
    {
124
        $viaPhp = $this->types->get(DateTime::class);
125
        $viaType = $this->types->get(DateTimeType::class);
126
        self::assertSame($viaPhp, $viaType);
127
    }
128
129
    public function testDoctrineWithoutAnnotationDriverMustThrow(): void
130
    {
131
        // Replace annotation driver with a driver chain
132
        $config = $this->entityManager->getConfiguration();
133
        $chain = new MappingDriverChain();
134
        $chain->setDefaultDriver($config->getMetadataDriverImpl());
135
        $config->setMetadataDriverImpl($chain);
136
137
        $type = $this->types->getOutput(Post::class);
138
139
        $this->expectExceptionMessage('graphql-doctrine requires Doctrine to be configured with a `Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver`.');
140
        $type->getFields();
141
    }
142
143
    public function testNonRegisteredCustomTypeMustThrow(): void
144
    {
145
        $this->expectExceptionMessage('No type registered with key `foo`. Either correct the usage, or register it in your custom types container when instantiating `GraphQL\Doctrine\Types`');
146
        $this->types->get('foo');
147
    }
148
149
    public function testHas(): void
150
    {
151
        $this->assertTrue($this->types->has(stdClass::class), 'should have custom registered key');
152
        $this->assertFalse($this->types->has('non-existing'), 'should not have non-existing things');
153
154
        $this->types->get(stdClass::class);
155
        $this->assertTrue($this->types->has('customName'), 'should have custom registered type by its name, even if custom key was different, once type is created');
156
    }
157
}
158