NonNullableTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 10
dl 0
loc 189
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testNullableResolving() 0 67 1
B queries() 0 110 1
1
<?php
2
/**
3
 * Date: 03.11.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\Tests\Schema;
9
10
use Youshido\GraphQL\Execution\Processor;
11
use Youshido\GraphQL\Schema\Schema;
12
use Youshido\GraphQL\Type\ListType\ListType;
13
use Youshido\GraphQL\Type\NonNullType;
14
use Youshido\GraphQL\Type\Object\ObjectType;
15
use Youshido\GraphQL\Type\Scalar\IdType;
16
use Youshido\GraphQL\Type\Scalar\IntType;
17
use Youshido\GraphQL\Type\Scalar\StringType;
18
19
class uid
20
{
21
    private $uid;
22
23
    public function __construct($uid)
24
    {
25
        $this->uid = $uid;
26
    }
27
28
    public function __toString()
29
    {
30
        return $this->uid;
31
    }
32
}
33
34
class NonNullableTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
35
{
36
37
    /**
38
     * @dataProvider queries
39
     *
40
     * @param $query
41
     * @param $expected
42
     */
43
    public function testNullableResolving($query, $expected)
44
    {
45
        $schema = new Schema([
46
            'query' => new ObjectType([
47
                'name'   => 'RootQuery',
48
                'fields' => [
49
                    'nonNullScalar'        => [
50
                        'type'    => new NonNullType(new IntType()),
51
                        'resolve' => function () {
52
                            return null;
53
                        },
54
                    ],
55
                    'nonNullList'          => [
56
                        'type'    => new NonNullType(new ListType(new IntType())),
57
                        'resolve' => function () {
58
                            return null;
59
                        }
60
                    ],
61
                    'user'                 => [
62
                        'type'    => new NonNullType(new ObjectType([
63
                            'name'   => 'User',
64
                            'fields' => [
65
                                'id'   => new NonNullType(new IdType()),
66
                                'name' => new StringType(),
67
                            ]
68
                        ])),
69
                        'resolve' => function () {
70
                            return [
71
                                'id'   => new uid('6cfb044c-9c0a-4ddd-9ef8-a0b940818db3'),
72
                                'name' => 'Alex'
73
                            ];
74
                        }
75
                    ],
76
                    'nonNullListOfNpnNull' => [
77
                        'type'    => new NonNullType(new ListType(new NonNullType(new IntType()))),
78
                        'resolve' => function () {
79
                            return [1, null];
80
                        }
81
                    ],
82
                    'nonNullArgument'     => [
83
                        'args'    => [
84
                            'ids' => new NonNullType(new ListType(new IntType()))
85
                        ],
86
                        'type'    => new IntType(),
87
                        'resolve' => function () {
88
                            return 1;
89
                        }
90
                    ],
91
                    'nonNullArgument2'     => [
92
                        'args'    => [
93
                            'ids' => new NonNullType(new ListType(new NonNullType(new IntType())))
94
                        ],
95
                        'type'    => new IntType(),
96
                        'resolve' => function () {
97
                            return 1;
98
                        }
99
                    ],
100
                ]
101
            ])
102
        ]);
103
104
        $processor = new Processor($schema);
105
        $processor->processPayload($query);
106
        $result = $processor->getResponseData();
107
108
        $this->assertEquals($expected, $result);
109
    }
110
111
    public function queries()
112
    {
113
        return [
114
            [
115
                '{ test:nonNullArgument2(ids: [1, 2]) }',
116
                [
117
                    'data' => [
118
                        'test' => 1
119
                    ]
120
                ],
121
            ],
122
            [
123
                '{ test:nonNullArgument2(ids: [1, null]) }',
124
                [
125
                    'data' => [
126
                        'test' => null
127
                    ],
128
                    'errors' => [
129
                        [
130
                            'message' => 'Not valid type for argument "ids" in query "nonNullArgument2": Field must not be NULL',
131
                            'locations' => [['line' => 1, 'column' => 25]]
132
                        ]
133
                    ]
134
                ],
135
            ],
136
            [
137
                '{ test:nonNullArgument(ids: [1, null]) }',
138
                [
139
                    'data' => [
140
                        'test' => 1
141
                    ]
142
                ]
143
            ],
144
            [
145
                '{ test:nonNullArgument }',
146
                [
147
                    'data' => [
148
                        'test' => null
149
                    ],
150
                    'errors' => [
151
                        [
152
                            'message' => 'Require "ids" arguments to query "nonNullArgument"'
153
                        ]
154
                    ]
155
                ]
156
            ],
157
            [
158
                '{ nonNullScalar  }',
159
                [
160
                    'data'   => [
161
                        'nonNullScalar' => null
162
                    ],
163
                    'errors' => [
164
                        [
165
                            'message' => 'Cannot return null for non-nullable field "nonNullScalar"'
166
                        ]
167
                    ]
168
                ]
169
            ],
170
171
            [
172
                '{ nonNullList  }',
173
                [
174
                    'data'   => [
175
                        'nonNullList' => null
176
                    ],
177
                    'errors' => [
178
                        [
179
                            'message' => 'Cannot return null for non-nullable field "nonNullList"'
180
                        ]
181
                    ]
182
                ]
183
            ],
184
            [
185
                '{ nonNullListOfNpnNull  }',
186
                [
187
                    'data'   => [
188
                        'nonNullListOfNpnNull' => null,
189
                    ],
190
                    'errors' => [
191
                        [
192
                            'message' => 'Not valid resolved type for field "nonNullListOfNpnNull": Field must not be NULL'
193
                        ]
194
                    ]
195
                ]
196
            ],
197
198
            [
199
                '{ user {id, name}  }',
200
                [
201
                    'data' => [
202
                        'user' => [
203
                            'id'   => '6cfb044c-9c0a-4ddd-9ef8-a0b940818db3',
204
                            'name' => 'Alex'
205
                        ]
206
                    ]
207
                ]
208
            ],
209
            [
210
                '{ user { __typename }  }',
211
                [
212
                    'data' => [
213
                        'user' => [
214
                            '__typename' => 'User'
215
                        ]
216
                    ]
217
                ]
218
            ]
219
        ];
220
    }
221
222
}
223