Completed
Pull Request — master (#204)
by Ryan
11:34
created

NonNullableTest::queries()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 110
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 110
rs 8.2857
cc 1
eloc 43
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
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/**
14
 * Date: 03.11.16.
15
 */
16
17
namespace Youshido\Tests\Schema;
18
19
use Youshido\GraphQL\Execution\Processor;
20
use Youshido\GraphQL\Schema\Schema;
21
use Youshido\GraphQL\Type\ListType\ListType;
22
use Youshido\GraphQL\Type\NonNullType;
23
use Youshido\GraphQL\Type\Object\ObjectType;
24
use Youshido\GraphQL\Type\Scalar\IdType;
25
use Youshido\GraphQL\Type\Scalar\IntType;
26
use Youshido\GraphQL\Type\Scalar\StringType;
27
28
class uid
29
{
30
    private $uid;
31
32
    public function __construct($uid)
33
    {
34
        $this->uid = $uid;
35
    }
36
37
    public function __toString()
38
    {
39
        return $this->uid;
40
    }
41
}
42
43
class NonNullableTest extends \PHPUnit_Framework_TestCase
44
{
45
    /**
46
     * @dataProvider queries
47
     *
48
     * @param $query
49
     * @param $expected
50
     */
51
    public function testNullableResolving($query, $expected): void
52
    {
53
        $schema = new Schema([
54
            'query' => new ObjectType([
55
                'name'   => 'RootQuery',
56
                'fields' => [
57
                    'nonNullScalar' => [
58
                        'type'    => new NonNullType(new IntType()),
59
                        'resolve' => static function () {
60
                        },
61
                    ],
62
                    'nonNullList' => [
63
                        'type'    => new NonNullType(new ListType(new IntType())),
64
                        'resolve' => static function () {
65
                        },
66
                    ],
67
                    'user' => [
68
                        'type' => new NonNullType(new ObjectType([
69
                            'name'   => 'User',
70
                            'fields' => [
71
                                'id'   => new NonNullType(new IdType()),
72
                                'name' => new StringType(),
73
                            ],
74
                        ])),
75
                        'resolve' => static function () {
76
                            return [
77
                                'id'   => new uid('6cfb044c-9c0a-4ddd-9ef8-a0b940818db3'),
78
                                'name' => 'Alex',
79
                            ];
80
                        },
81
                    ],
82
                    'nonNullListOfNpnNull' => [
83
                        'type'    => new NonNullType(new ListType(new NonNullType(new IntType()))),
84
                        'resolve' => static function () {
85
                            return [1, null];
86
                        },
87
                    ],
88
                    'nonNullArgument' => [
89
                        'args' => [
90
                            'ids' => new NonNullType(new ListType(new IntType())),
91
                        ],
92
                        'type'    => new IntType(),
93
                        'resolve' => static function () {
94
                            return 1;
95
                        },
96
                    ],
97
                    'nonNullArgument2' => [
98
                        'args' => [
99
                            'ids' => new NonNullType(new ListType(new NonNullType(new IntType()))),
100
                        ],
101
                        'type'    => new IntType(),
102
                        'resolve' => static function () {
103
                            return 1;
104
                        },
105
                    ],
106
                ],
107
            ]),
108
        ]);
109
110
        $processor = new Processor($schema);
111
        $processor->processPayload($query);
112
        $result = $processor->getResponseData();
113
114
        $this->assertEquals($expected, $result);
115
    }
116
117
    public function queries()
118
    {
119
        return [
120
            [
121
                '{ test:nonNullArgument2(ids: [1, 2]) }',
122
                [
123
                    'data' => [
124
                        'test' => 1,
125
                    ],
126
                ],
127
            ],
128
            [
129
                '{ test:nonNullArgument2(ids: [1, null]) }',
130
                [
131
                    'data' => [
132
                        'test' => null,
133
                    ],
134
                    'errors' => [
135
                        [
136
                            'message'   => 'Not valid type for argument "ids" in query "nonNullArgument2": Field must not be NULL',
137
                            'locations' => [['line' => 1, 'column' => 25]],
138
                        ],
139
                    ],
140
                ],
141
            ],
142
            [
143
                '{ test:nonNullArgument(ids: [1, null]) }',
144
                [
145
                    'data' => [
146
                        'test' => 1,
147
                    ],
148
                ],
149
            ],
150
            [
151
                '{ test:nonNullArgument }',
152
                [
153
                    'data' => [
154
                        'test' => null,
155
                    ],
156
                    'errors' => [
157
                        [
158
                            'message' => 'Require "ids" arguments to query "nonNullArgument"',
159
                        ],
160
                    ],
161
                ],
162
            ],
163
            [
164
                '{ nonNullScalar  }',
165
                [
166
                    'data' => [
167
                        'nonNullScalar' => null,
168
                    ],
169
                    'errors' => [
170
                        [
171
                            'message' => 'Cannot return null for non-nullable field "nonNullScalar"',
172
                        ],
173
                    ],
174
                ],
175
            ],
176
177
            [
178
                '{ nonNullList  }',
179
                [
180
                    'data' => [
181
                        'nonNullList' => null,
182
                    ],
183
                    'errors' => [
184
                        [
185
                            'message' => 'Cannot return null for non-nullable field "nonNullList"',
186
                        ],
187
                    ],
188
                ],
189
            ],
190
            [
191
                '{ nonNullListOfNpnNull  }',
192
                [
193
                    'data' => [
194
                        'nonNullListOfNpnNull' => null,
195
                    ],
196
                    'errors' => [
197
                        [
198
                            'message' => 'Not valid resolved type for field "nonNullListOfNpnNull": Field must not be NULL',
199
                        ],
200
                    ],
201
                ],
202
            ],
203
204
            [
205
                '{ user {id, name}  }',
206
                [
207
                    'data' => [
208
                        'user' => [
209
                            'id'   => '6cfb044c-9c0a-4ddd-9ef8-a0b940818db3',
210
                            'name' => 'Alex',
211
                        ],
212
                    ],
213
                ],
214
            ],
215
            [
216
                '{ user { __typename }  }',
217
                [
218
                    'data' => [
219
                        'user' => [
220
                            '__typename' => 'User',
221
                        ],
222
                    ],
223
                ],
224
            ],
225
        ];
226
    }
227
}
228