Completed
Push — master ( 8c19ea...8f4b0a )
by Portey
03:08
created

NonNullableTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testNullableResolving() 0 36 1
B queries() 0 46 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
11
use Youshido\GraphQL\Execution\Processor;
12
use Youshido\GraphQL\Schema\Schema;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\IntType;
17
18
class NonNullableTest extends \PHPUnit_Framework_TestCase
19
{
20
21
    /**
22
     * @dataProvider queries
23
     *
24
     * @param $query
25
     * @param $expected
26
     */
27
    public function testNullableResolving($query, $expected)
28
    {
29
        $schema = new Schema([
30
            'query' => new ObjectType([
31
                'name'   => 'RootQuery',
32
                'fields' => [
33
                    'nonNullScalar' => [
34
                        'type'    => new NonNullType(new IntType()),
35
                        'resolve' => function () {
36
                            return null;
37
                        },
38
                    ],
39
40
                    'nonNullList' => [
41
                        'type'    => new NonNullType(new ListType(new IntType())),
42
                        'resolve' => function () {
43
                            return null;
44
                        }
45
                    ],
46
47
                    'nonNullListOfNpnNull' => [
48
                        'type'    => new NonNullType(new ListType(new NonNullType(new IntType()))),
49
                        'resolve' => function () {
50
                            return [1, null];
51
                        }
52
                    ]
53
                ]
54
            ])
55
        ]);
56
57
        $processor = new Processor($schema);
58
        $processor->processPayload($query);
59
        $result = $processor->getResponseData();
60
61
        $this->assertEquals($expected, $result);
62
    }
63
64
    public function queries()
65
    {
66
        return [
67
            [
68
                '{ nonNullScalar  }',
69
                [
70
                    'data' => [
71
                        'nonNullScalar' => null
72
                    ],
73
                    'errors' => [
74
                        [
75
                            'message' => 'Cannot return null for non-nullable field "nonNullScalar"'
76
                        ]
77
                    ]
78
                ]
79
            ],
80
81
            [
82
                '{ nonNullList  }',
83
                [
84
                    'data' => [
85
                        'nonNullList' => null
86
                    ],
87
                    'errors' => [
88
                        [
89
                            'message' => 'Cannot return null for non-nullable field "nonNullList"'
90
                        ]
91
                    ]
92
                ]
93
            ],
94
95
            [
96
                '{ nonNullListOfNpnNull  }',
97
                [
98
                    'data' => [
99
                        'nonNullListOfNpnNull' => [1, null]
100
                    ],
101
                    'errors' => [
102
                        [
103
                            'message' => 'Cannot return null for non-nullable field "nonNullListOfNpnNull"'
104
                        ]
105
                    ]
106
                ]
107
            ]
108
        ];
109
    }
110
111
}