Passed
Push — master ( fb5c25...257e8e )
by Max
05:13
created

ScalarValidationTest::setUp()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 38
nc 1
nop 0
dl 0
loc 58
rs 9.312
c 0
b 0
f 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()
43
    {
44
        $this->personType = new ObjectType([
45
            'name' => 'Person',
46
            'fields' => [
47
                'firstName' => [
48
                    'type' => Type::string(),
49
                    'phoneNumbers' => [
50
                        'type' => Type::listOf(Type::string()),
51
                    ],
52
                ],
53
            ],
54
        ]);
55
56
        $this->bookType = new ObjectType([
57
            'name' => 'Book',
58
            'fields' => [
59
                'title' => [
60
                    'type' => Type::string(),
61
                    'resolve' => static function ($book) {
62
                        return $book['title'];
63
                    },
64
                ],
65
                'author' => [
66
                    'type' => $this->personType,
67
                    'resolve' => static function ($book) {
68
                        return $book['author'];
69
                    },
70
                ],
71
            ],
72
        ]);
73
74
        $this->query = new ObjectType(['name' => 'Query']);
75
76
        $this->schema = new Schema([
77
            'query' => $this->query,
78
            'mutation' => new ObjectType([
79
                'name' => 'Mutation',
80
                'fields' => function () {
81
                    return [
82
                        'updateBook' => new ValidatedFieldDefinition([
83
                            'name' => 'updateBook',
84
                            'type' => $this->bookType,
85
                            'args' => [
86
                                'bookId' => [
87
                                    'type' => Type::id(),
88
                                    'errorCodes' => ['bookNotFound'],
89
                                    'validate' => function ($bookId) {
90
                                        if (isset($this->data['books'][$bookId])) {
91
                                            return 0;
92
                                        }
93
94
                                        return ['bookNotFound', 'Unknown book!'];
95
                                    },
96
                                ],
97
                            ],
98
                            'resolve' => static function ($value, $args) : bool {
0 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

98
                            'resolve' => static function ($value, /** @scrutinizer ignore-unused */ $args) : bool {

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 $value 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

98
                            'resolve' => static function (/** @scrutinizer ignore-unused */ $value, $args) : bool {

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...
99
                                return true;
100
                            },
101
                        ]),
102
                    ];
103
                },
104
            ]),
105
        ]);
106
    }
107
108
    public function testNullableScalarValidationOnNullValueSuccess()
109
    {
110
        $res = GraphQL::executeQuery(
111
            $this->schema,
112
            Utils::nowdoc('
113
				mutation UpdateBook(
114
                        $bookId:ID
115
                    ) {
116
                        updateBook (bookId: $bookId) {
117
                            valid
118
                            suberrors {
119
                                bookId {
120
                                    code
121
                                    msg
122
                                }
123
                            }
124
                            result {
125
                                title
126
                            }
127
                        }
128
                    }
129
			'),
130
            [],
131
            null,
132
            ['bookId' => null]
133
        );
134
135
        static::assertEmpty($res->errors);
136
        static::assertTrue($res->data['updateBook']['valid']);
137
    }
138
}
139