Completed
Push — master ( 4406e7...95db8c )
by Alexandr
02:55
created

VariablesTest::queries()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 41
rs 8.8571
c 2
b 0
f 2
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Youshido\Tests\Schema;
4
5
use Youshido\GraphQL\Execution\Processor;
6
use Youshido\GraphQL\Schema\Schema;
7
use Youshido\GraphQL\Type\ListType\ListType;
8
use Youshido\GraphQL\Type\NonNullType;
9
use Youshido\GraphQL\Type\Object\ObjectType;
10
use Youshido\GraphQL\Type\Scalar\IdType;
11
use Youshido\GraphQL\Type\Scalar\StringType;
12
13
class VariablesTest extends \PHPUnit_Framework_TestCase
14
{
15
16
    public function testInvalidNullableList()
17
    {
18
        $schema = new Schema([
19
            'query' => new ObjectType([
20
                'name'   => 'RootQuery',
21
                'fields' => [
22
                    'list' => [
23
                        'type'    => new StringType(),
24
                        'args'    => [
25
                            'ids' => new ListType(new NonNullType(new IdType()))
26
                        ],
27
                        'resolve' => function () {
28
                            return 'item';
29
                        }
30
                    ],
31
                ]
32
            ])
33
        ]);
34
35
36
        $processor = new Processor($schema);
37
        $processor->processPayload(
38
            'query getList($ids: [ID!]) { list(ids: $ids) }',
39
            [
40
                'ids' => [1, 12, null]
41
            ]
42
        );
43
        $this->assertEquals(['data' => ['list' => 'item']], $processor->getResponseData());
44
45
        $processor->getExecutionContext()->clearErrors();
46
        $processor->processPayload(
47
            'query getList($ids: [ID]) { list(ids: $ids) }',
48
            [
49
                'ids' => [1, 12, null]
50
            ]
51
        );
52
        $this->assertEquals(
53
            [
54
                'data'   => ['list' => null],
55
                'errors' => [
56
                    [
57
                        'message'   => 'Invalid variable "ids" type, allowed type is "ID"',
58
                        'locations' => [
59
                            [
60
                                'line'   => 1,
61
                                'column' => 15
62
                            ]
63
                        ]
64
                    ],
65
                ]
66
            ],
67
            $processor->getResponseData());
68
    }
69
70
    /**
71
     * @dataProvider queries
72
     *
73
     * @param $query
74
     * @param $expected
75
     * @param $variables
76
     */
77
    public function testVariables($query, $expected, $variables)
78
    {
79
        $schema = new Schema([
80
            'query' => new ObjectType([
81
                'name'   => 'RootQuery',
82
                'fields' => [
83
                    'stringQuery' => [
84
                        'type'    => new StringType(),
85
                        'args'    => [
86
                            'sortOrder' => new StringType(),
87
                        ],
88
                        'resolve' => function ($args) {
89
                            return sprintf('Result with %s order', empty($args['sortOrder']) ? 'default' : $args['sortOrder']);
90
                        },
91
                    ],
92
                ]
93
            ])
94
        ]);
95
96
        $processor = new Processor($schema);
97
        $processor->processPayload($query, $variables);
98
        $result = $processor->getResponseData();
99
100
        $this->assertEquals($expected, $result);
101
    }
102
103
    public function queries()
104
    {
105
        return [
106
            [
107
                'query ListQuery($sort:String) {
108
                  stringQuery(sortOrder: $sort)
109
                }',
110
                [
111
                    'data' => [
112
                        'stringQuery' => 'Result with default order'
113
                    ],
114
                ],
115
                [
116
                    'sort' => null
117
                ]
118
            ],
119
            [
120
                'query queryWithVariable($abc:String) {
121
                  stringQuery {
122
                    ...someFragment
123
                  }
124
                }',
125
                [
126
                    'errors' => [
127
                        [
128
                            'message'   => 'Fragment "someFragment" not defined in query',
129
                            'locations' => [
130
                                [
131
                                    'line'   => 3,
132
                                    'column' => 24
133
                                ]
134
                            ]
135
                        ]
136
                    ],
137
                ],
138
                [
139
                    'abc' => null
140
                ]
141
            ],
142
        ];
143
    }
144
145
}
146