Passed
Pull Request — master (#5)
by Max
04:52 queued 10s
created

NonNullScalarValidationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 148
Duplicated Lines 44.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 66
loc 148
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 7 62 2
A testNonNullScalarValidationSuccess() 29 29 1
A testNonNullScalarValidationFail() 30 30 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Type\ValidatedFieldDefinition;
6
7
use GraphQL\GraphQL;
8
use GraphQL\Tests\Utils;
9
use GraphQL\Type\Definition\ObjectType;
10
use GraphQL\Type\Definition\Type;
11
use GraphQL\Type\Definition\ValidatedFieldDefinition;
12
use GraphQL\Type\Schema;
13
use PHPUnit\Framework\TestCase;
14
15
final class NonNullScalarValidationTest extends TestCase
16
{
17
    /** @var Type */
18
    protected $bookType;
19
20
    /** @var Type */
21
    protected $personType;
22
23
    /** @var mixed[] */
24
    protected $data = [
25
        'people' => [
26
            1 => ['firstName' => 'Wilson'],
27
        ],
28
        'books' => [
29
            1 => [
30
                'title' => 'Where the Red Fern Grows',
31
                'author' => 1,
32
            ],
33
        ],
34
    ];
35
36
    /** @var Schema */
37
    protected $schema;
38
39
    protected function setUp(): void
40
    {
41
        $this->personType = new ObjectType([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GraphQL\Type\Defini...on\Type::string())))))) of type object<GraphQL\Type\Definition\ObjectType> is incompatible with the declared type object<GraphQL\Type\Definition\Type> of property $personType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
            'name' => 'Person',
43
            'fields' => [
44
                'firstName' => [
45
                    'type' => Type::string(),
46
                    'phoneNumbers' => [
47
                        'type' => Type::listOf(Type::string()),
48
                    ],
49
                ],
50
            ],
51
        ]);
52
53
        $this->bookType = new ObjectType([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GraphQL\Type\Defini... $book['author']; })))) of type object<GraphQL\Type\Definition\ObjectType> is incompatible with the declared type object<GraphQL\Type\Definition\Type> of property $bookType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
            'name' => 'Book',
55
            'fields' => [
56
                'title' => [
57
                    'type' => Type::string(),
58
                    'resolve' => static function ($book) {
59
                        return $book['title'];
60
                    },
61
                ],
62
                'author' => [
63
                    'type' => $this->personType,
64
                    'resolve' => static function ($book) {
65
                        return $book['author'];
66
                    },
67
                ],
68
            ],
69
        ]);
70
71
        $this->schema = new Schema([
72
            'mutation' => new ObjectType([
73
                'name' => 'Mutation',
74
                'fields' => function () {
75
                    return [
76
                        'updateBook' => new ValidatedFieldDefinition([
77
                            'name' => 'updateBook',
78
                            'type' => $this->bookType,
79
                            'args' => [
80
                                'bookId' => [
81
                                    'type' => Type::nonNull(Type::id()),
82
                                    'errorCodes' => ['bookNotFound'],
83 View Code Duplication
                                    'validate' => function ($bookId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                                        if (isset($this->data['books'][$bookId])) {
85
                                            return 0;
86
                                        }
87
88
                                        return ['bookNotFound', 'Unknown book!'];
89
                                    },
90
                                ],
91
                            ],
92
                            'resolve' => function ($value, $args) : array {
93
                                return $this->data['books'][$args['bookId']];
94
                            },
95
                        ]),
96
                    ];
97
                },
98
            ]),
99
        ]);
100
    }
101
102 View Code Duplication
    public function testNonNullScalarValidationSuccess(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $res = GraphQL::executeQuery(
105
            $this->schema,
106
            Utils::nowdoc('
107
                mutation UpdateBook(
108
                    $bookId:ID!
109
                ) {
110
                    updateBook (bookId: $bookId) {
111
                        valid
112
                        suberrors {
113
                            bookId {
114
                                code
115
                                msg
116
                            }
117
                        }
118
                        result {
119
                            title
120
                        }
121
                    }
122
                }
123
            '),
124
            [],
125
            null,
126
            ['bookId' => 1]
127
        );
128
129
        static::assertTrue($res->data['updateBook']['valid']);
130
    }
131
132 View Code Duplication
    public function testNonNullScalarValidationFail() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $res = GraphQL::executeQuery(
135
            $this->schema,
136
            Utils::nowdoc('
137
                mutation UpdateBook(
138
                        $bookId:ID!
139
                    ) {
140
                        updateBook (bookId: $bookId) {
141
                            valid
142
                            suberrors {
143
                                bookId {
144
                                    code
145
                                    msg
146
                                }
147
                            }
148
                            result {
149
                                title
150
                            }
151
                        }
152
                    }
153
            '),
154
            [],
155
            null,
156
            ['bookId' => 37]
157
        );
158
159
        static::assertEmpty($res->errors);
160
        static::assertFalse($res->data['updateBook']['valid']);
161
    }
162
}
163