Completed
Push — master ( f44b67...3d21e8 )
by Alexandr
03:27
created

testNullableInputWithNonNull()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
rs 8.8571
cc 1
eloc 25
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\Schema\Schema;
14
use Youshido\GraphQL\Type\InputObject\InputObjectType;
15
use Youshido\GraphQL\Type\ListType\ListType;
16
use Youshido\GraphQL\Type\NonNullType;
17
use Youshido\GraphQL\Type\Object\ObjectType;
18
use Youshido\GraphQL\Type\Scalar\BooleanType;
19
use Youshido\GraphQL\Type\Scalar\StringType;
20
use Youshido\GraphQL\Type\TypeMap;
21
use Youshido\Tests\DataProvider\TestInputObjectType;
22
23
class InputObjectTypeTest extends \PHPUnit_Framework_TestCase
24
{
25
26
    public function testInternal()
27
    {
28
        $inputObjectType = new InputObjectType([
29
            'name'   => 'PostData',
30
            'fields' => [
31
                'title' => new NonNullType(new StringType()),
32
            ]
33
        ]);
34
        $this->assertEquals(TypeMap::KIND_INPUT_OBJECT, $inputObjectType->getKind());
35
        $this->assertEquals('PostData', $inputObjectType->getName());
36
37
        $this->assertFalse($inputObjectType->isValidValue('invalid value'));
38
        $this->assertTrue($inputObjectType->isValidValue(['title' => 'Super ball!']));
39
        $this->assertFalse($inputObjectType->isValidValue(['title' => null]));
40
    }
41
42
    public function testStandaloneClass()
43
    {
44
        $inputObjectType = new TestInputObjectType();
45
        $this->assertEquals('TestInputObject', $inputObjectType->getName());
46
    }
47
48
    public function testListOfInputWithNonNull()
49
    {
50
        $processor = new Processor(new Schema([
51
            'query'    => new ObjectType([
52
                'name'   => 'RootQuery',
53
                'fields' => [
54
                    'empty' => [
55
                        'type'    => new StringType(),
56
                        'resolve' => function () {
57
                            return null;
58
                        }
59
                    ]
60
                ]
61
            ]),
62
            'mutation' => new ObjectType([
63
                'name'   => 'RootMutation',
64
                'fields' => [
65
                    'createList' => [
66
                        'args'    => [
67
                            'posts' => new ListType(new InputObjectType([
68
                                'name'   => 'PostInputType',
69
                                'fields' => [
70
                                    'title' => new NonNullType(new StringType()),
71
                                ]
72
                            ]))
73
                        ],
74
                        'type'    => new BooleanType(),
75
                        '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...
76
                            return true;
77
                        }
78
                    ]
79
                ]
80
            ])
81
        ]));
82
83
        $processor->processPayload('mutation { createList(posts: [{title: null }, {}]) }');
84
        $this->assertEquals(
85
            [
86
                'data'   => ['createList' => null],
87
                'errors' => [[
88
                    'message' => 'Not valid type for argument "posts" in query "createList"',
89
                    'locations' => [
90
                        [
91
                            'line' => 1,
92
                            'column' => 23
93
                        ]
94
                    ]
95
                ]]
96
            ],
97
            $processor->getResponseData()
98
        );
99
    }
100
101
    public function testNullableInputWithNonNull(){
102
        $processor = new Processor(new Schema([
103
            'query'    => new ObjectType([
104
                'name'   => 'RootQuery',
105
                'fields' => [
106
                    'empty' => [
107
                        'type'    => new StringType(),
108
                        'resolve' => function () {
109
                            return null;
110
                        }
111
                    ]
112
                ]
113
            ]),
114
            'mutation' => new ObjectType([
115
                'name'   => 'RootMutation',
116
                'fields' => [
117
                    'createAuthor' => [
118
                        'args'    => [
119
                            'author' => new InputObjectType([
120
                                'name'   => 'AuthorInputType',
121
                                'fields' => [
122
                                    'name' => new NonNullType(new StringType()),
123
                                ]
124
                            ])
125
                        ],
126
                        'type'    => new BooleanType(),
127
                        '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...
128
                            return true;
129
                        }
130
                    ]
131
                ]
132
            ])
133
        ]));
134
        $processor->processPayload('mutation { createAuthor(author: null) }');
135
        $this->assertEquals(
136
            [
137
                'data'   => ['createAuthor' => true],
138
            ],
139
            $processor->getResponseData()
140
        );
141
    }
142
143
    public function testListInsideInputObject()
144
    {
145
        $processor = new Processor(new Schema([
146
            'query'    => new ObjectType([
147
                'name'   => 'RootQueryType',
148
                'fields' => [
149
                    'empty' => [
150
                        'type'    => new StringType(),
151
                        'resolve' => function () {
152
                        }
153
                    ],
154
                ]
155
            ]),
156
            'mutation' => new ObjectType([
157
                'name'   => 'RootMutation',
158
                'fields' => [
159
                    'createList' => [
160
                        'type'    => new StringType(),
161
                        'args'    => [
162
                            'topArgument' => new InputObjectType([
163
                                'name'   => 'topArgument',
164
                                'fields' => [
165
                                    'postObject' => new ListType(new InputObjectType([
166
                                        'name'   => 'postObject',
167
                                        'fields' => [
168
                                            'title' => new NonNullType(new StringType()),
169
                                        ]
170
                                    ]))
171
                                ]
172
                            ])
173
                        ],
174
                        'resolve' => function () {
175
                            return 'success message';
176
                        }
177
                    ]
178
                ]
179
            ])
180
        ]));
181
        $processor->processPayload('mutation { createList(topArgument: { postObject:[ { title: null } ] })}');
182
        $this->assertEquals([
183
            'data'   => ['createList' => null],
184
            'errors' => [[
185
                'message' => 'Not valid type for argument "topArgument" in query "createList"',
186
                'locations' => [
187
                    [
188
                        'line' => 1,
189
                        'column' => 23
190
                    ]
191
                ]
192
            ]],
193
        ], $processor->getResponseData());
194
        $processor->getExecutionContext()->clearErrors();
195
        $processor->processPayload('mutation { createList(topArgument:{
196
                                        postObject:[{title: "not empty"}] })}');
197
        $this->assertEquals(['data' => ['createList' => 'success message']], $processor->getResponseData());
198
    }
199
200
}
201