Completed
Push — master ( 786636...6c7957 )
by Alexandr
03:01
created

InputObjectTypeTest::testInputObjectDefaultValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/15/16 3:28 PM
7
*/
8
9
namespace Youshido\Tests\Library\Type;
10
11
12
use Youshido\GraphQL\Execution\Processor;
13
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject;
14
use Youshido\GraphQL\Schema\Schema;
15
use Youshido\GraphQL\Type\InputObject\InputObjectType;
16
use Youshido\GraphQL\Type\ListType\ListType;
17
use Youshido\GraphQL\Type\NonNullType;
18
use Youshido\GraphQL\Type\Object\ObjectType;
19
use Youshido\GraphQL\Type\Scalar\BooleanType;
20
use Youshido\GraphQL\Type\Scalar\IntType;
21
use Youshido\GraphQL\Type\Scalar\StringType;
22
use Youshido\GraphQL\Type\TypeMap;
23
use Youshido\Tests\DataProvider\TestInputObjectType;
24
25
class InputObjectTypeTest extends \PHPUnit_Framework_TestCase
26
{
27
28
    public function testInternal()
29
    {
30
        $inputObjectType = new InputObjectType([
31
            'name'   => 'PostData',
32
            'fields' => [
33
                'title' => new NonNullType(new StringType()),
34
            ]
35
        ]);
36
        $this->assertEquals(TypeMap::KIND_INPUT_OBJECT, $inputObjectType->getKind());
37
        $this->assertEquals('PostData', $inputObjectType->getName());
38
39
        $this->assertFalse($inputObjectType->isValidValue('invalid value'));
40
        $this->assertTrue($inputObjectType->isValidValue(['title' => 'Super ball!']));
41
        $this->assertFalse($inputObjectType->isValidValue(['title' => null]));
42
    }
43
44
    public function testStandaloneClass()
45
    {
46
        $inputObjectType = new TestInputObjectType();
47
        $this->assertEquals('TestInputObject', $inputObjectType->getName());
48
    }
49
50
    public function testListOfInputWithNonNull()
51
    {
52
        $processor = new Processor(new Schema([
53
            'query'    => new ObjectType([
54
                'name'   => 'RootQuery',
55
                'fields' => [
56
                    'empty' => [
57
                        'type'    => new StringType(),
58
                        'resolve' => function () {
59
                            return null;
60
                        }
61
                    ]
62
                ]
63
            ]),
64
            'mutation' => new ObjectType([
65
                'name'   => 'RootMutation',
66
                'fields' => [
67
                    'createList' => [
68
                        'args'    => [
69
                            'posts' => new ListType(new InputObjectType([
70
                                'name'   => 'PostInputType',
71
                                'fields' => [
72
                                    'title' => new NonNullType(new StringType()),
73
                                ]
74
                            ]))
75
                        ],
76
                        'type'    => new BooleanType(),
77
                        'resolve' => function ($object, $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from 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 $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
                            return true;
79
                        }
80
                    ]
81
                ]
82
            ])
83
        ]));
84
85
        $processor->processPayload('mutation { createList(posts: [{title: null }, {}]) }');
86
        $this->assertEquals(
87
            [
88
                'data'   => ['createList' => null],
89
                'errors' => [[
90
                                 'message'   => 'Not valid type for argument "posts" in query "createList"',
91
                                 'locations' => [
92
                                     [
93
                                         'line'   => 1,
94
                                         'column' => 23
95
                                     ]
96
                                 ]
97
                             ]]
98
            ],
99
            $processor->getResponseData()
100
        );
101
    }
102
103
    public function testNullableInputWithNonNull()
104
    {
105
        $processor = new Processor(new Schema([
106
            'query'    => new ObjectType([
107
                'name'   => 'RootQuery',
108
                'fields' => [
109
                    'empty' => [
110
                        'type'    => new StringType(),
111
                        'resolve' => function () {
112
                            return null;
113
                        }
114
                    ]
115
                ]
116
            ]),
117
            'mutation' => new ObjectType([
118
                'name'   => 'RootMutation',
119
                'fields' => [
120
                    'createAuthor' => [
121
                        'args'    => [
122
                            'author' => new InputObjectType([
123
                                'name'   => 'AuthorInputType',
124
                                'fields' => [
125
                                    'name' => new NonNullType(new StringType()),
126
                                ]
127
                            ])
128
                        ],
129
                        'type'    => new BooleanType(),
130
                        'resolve' => function ($object, $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from 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 $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
                            return true;
132
                        }
133
                    ]
134
                ]
135
            ])
136
        ]));
137
        $processor->processPayload('mutation { createAuthor(author: null) }');
138
        $this->assertEquals(
139
            [
140
                'data' => ['createAuthor' => true],
141
            ],
142
            $processor->getResponseData()
143
        );
144
    }
145
146
    public function testListInsideInputObject()
147
    {
148
        $processor = new Processor(new Schema([
149
            'query'    => new ObjectType([
150
                'name'   => 'RootQueryType',
151
                'fields' => [
152
                    'empty' => [
153
                        'type'    => new StringType(),
154
                        'resolve' => function () {
155
                        }
156
                    ],
157
                ]
158
            ]),
159
            'mutation' => new ObjectType([
160
                'name'   => 'RootMutation',
161
                'fields' => [
162
                    'createList' => [
163
                        'type'    => new StringType(),
164
                        'args'    => [
165
                            'topArgument' => new InputObjectType([
166
                                'name'   => 'topArgument',
167
                                'fields' => [
168
                                    'postObject' => new ListType(new InputObjectType([
169
                                        'name'   => 'postObject',
170
                                        'fields' => [
171
                                            'title' => new NonNullType(new StringType()),
172
                                        ]
173
                                    ]))
174
                                ]
175
                            ])
176
                        ],
177
                        'resolve' => function () {
178
                            return 'success message';
179
                        }
180
                    ]
181
                ]
182
            ])
183
        ]));
184
        $processor->processPayload('mutation { createList(topArgument: { postObject:[ { title: null } ] })}');
185
        $this->assertEquals([
186
            'data'   => ['createList' => null],
187
            'errors' => [[
188
                             'message'   => 'Not valid type for argument "topArgument" in query "createList"',
189
                             'locations' => [
190
                                 [
191
                                     'line'   => 1,
192
                                     'column' => 23
193
                                 ]
194
                             ]
195
                         ]],
196
        ], $processor->getResponseData());
197
        $processor->getExecutionContext()->clearErrors();
198
        $processor->processPayload('mutation { createList(topArgument:{
199
                                        postObject:[{title: "not empty"}] })}');
200
        $this->assertEquals(['data' => ['createList' => 'success message']], $processor->getResponseData());
201
    }
202
203
    public function testInputObjectDefaultValue()
204
    {
205
        $processor = new Processor(new Schema([
206
            'query'    => new ObjectType([
207
                'name'   => 'RootQuery',
208
                'fields' => [
209
                    'cities' => [
210
                        'type'    => new ListType(new StringType()),
211
                        'args'    => [
212
                            'paging' => [
213
                                'type'         => new InputObjectType([
214
                                    'name'   => 'paging',
215
                                    'fields' => [
216
                                        'limit'  => new IntType(),
217
                                        'offset' => new IntType(),
218
                                    ]
219
                                ]),
220
                                'defaultValue' => [
221
                                    'limit'  => 10,
222
                                    'offset' => 0,
223
                                ],
224
                            ],
225
                        ],
226
                        'resolve' => function ($source, $args) {
227
                            return [
228
                                'limit is ' . $args['paging']['limit'],
229
                                'offset is ' . $args['paging']['offset'],
230
                            ];
231
                        }
232
                    ],
233
234
                ]
235
            ]),
236
        ]));
237
        $processor->processPayload('{ cities }');
238
        $response = $processor->getResponseData();
239
        $this->assertEquals(
240
            [
241
                'data' => ['cities' => [
242
                    'limit is 10',
243
                    'offset is 0'
244
                ]],
245
            ],
246
            $response
247
        );
248
    }
249
250
251
}
252