Completed
Push — master ( 8f4b0a...3474c6 )
by Alexandr
02:52
created

NonNullableTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 10
dl 0
loc 129
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testNullableResolving() 0 49 1
A queries() 0 68 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
                ]
83
            ])
84
        ]);
85
86
        $processor = new Processor($schema);
87
        $processor->processPayload($query);
88
        $result = $processor->getResponseData();
89
90
        $this->assertEquals($expected, $result);
91
    }
92
93
    public function queries()
94
    {
95
        return [
96
            [
97
                '{ nonNullScalar  }',
98
                [
99
                    'data'   => [
100
                        'nonNullScalar' => null
101
                    ],
102
                    'errors' => [
103
                        [
104
                            'message' => 'Cannot return null for non-nullable field "nonNullScalar"'
105
                        ]
106
                    ]
107
                ]
108
            ],
109
110
            [
111
                '{ nonNullList  }',
112
                [
113
                    'data'   => [
114
                        'nonNullList' => null
115
                    ],
116
                    'errors' => [
117
                        [
118
                            'message' => 'Cannot return null for non-nullable field "nonNullList"'
119
                        ]
120
                    ]
121
                ]
122
            ],
123
124
            [
125
                '{ nonNullListOfNpnNull  }',
126
                [
127
                    'data'   => [
128
                        'nonNullListOfNpnNull' => [1, null]
129
                    ],
130
                    'errors' => [
131
                        [
132
                            'message' => 'Cannot return null for non-nullable field "nonNullListOfNpnNull"'
133
                        ]
134
                    ]
135
                ]
136
            ],
137
138
            [
139
                '{ user {id, name}  }',
140
                [
141
                    'data' => [
142
                        'user' => [
143
                            'id'   => '6cfb044c-9c0a-4ddd-9ef8-a0b940818db3',
144
                            'name' => 'Alex'
145
                        ]
146
                    ]
147
                ]
148
            ],
149
            [
150
                '{ user { __typename }  }',
151
                [
152
                    'data' => [
153
                        'user' => [
154
                            '__typename' => 'User'
155
                        ]
156
                    ]
157
                ]
158
            ]
159
        ];
160
    }
161
162
}