Passed
Pull Request — master (#5)
by Max
05:22 queued 23s
created

ScalarValidationTest::setUp()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 62

Duplication

Lines 7
Ratio 11.29 %

Importance

Changes 0
Metric Value
dl 7
loc 62
rs 8.829
c 0
b 0
f 0
cc 2
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ScalarValidationTest 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 ObjectType */
37
    protected $query;
38
39
    /** @var Schema */
40
    protected $schema;
41
42
    protected function setUp() : void
43
    {
44
        $this->personType = new ObjectType([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GraphQL\Type\Defini...tion\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...
45
            'name' => 'Person',
46
            'fields' => [
47
                'firstName' => [
48
                    'type' => Type::string(),
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->query = new ObjectType(['name' => 'Query']);
72
73
        $this->schema = new Schema([
74
            'query' => $this->query,
75
            'mutation' => new ObjectType([
76
                'name' => 'Mutation',
77
                'fields' => function () {
78
                    return [
79
                        'updateBook' => new ValidatedFieldDefinition([
80
                            'name' => 'updateBook',
81
                            'type' => $this->bookType,
82
                            'args' => [
83
                                'bookId' => [
84
                                    'type' => Type::id(),
85
                                    'errorCodes' => ['bookNotFound'],
86 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...
87
                                        if (isset($this->data['books'][$bookId])) {
88
                                            return 0;
89
                                        }
90
91
                                        return ['bookNotFound', 'Unknown book!'];
92
                                    },
93
                                ],
94
                            ],
95
                            'resolve' => static function ($value) : bool {
96
                                return !!$value;
97
                            },
98
                        ]),
99
                    ];
100
                },
101
            ]),
102
        ]);
103
    }
104
105 View Code Duplication
    public function testNullableScalarValidationOnNullValueSuccess() : 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...
106
    {
107
        $res = GraphQL::executeQuery(
108
            $this->schema,
109
            Utils::nowdoc('
110
                mutation UpdateBook(
111
                    $bookId:ID
112
                ) {
113
                    updateBook (bookId: $bookId) {
114
                        valid
115
                        suberrors {
116
                            bookId {
117
                                code
118
                                msg
119
                            }
120
                        }
121
                        result {
122
                            title
123
                        }
124
                    }
125
                }
126
            '),
127
            [],
128
            null,
129
            ['bookId' => null]
130
        );
131
132
        static::assertEmpty($res->errors);
133
        static::assertTrue($res->data['updateBook']['valid']);
134
    }
135
}
136