Completed
Pull Request — master (#204)
by Ryan
11:34
created

ReservationInterface   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
c 1
b 1
f 0
lcom 0
cbo 5
dl 0
loc 15
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
14
namespace Youshido\Tests\Schema;
15
16
use Youshido\GraphQL\Execution\Processor;
17
use Youshido\GraphQL\Schema\Schema;
18
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
19
use Youshido\GraphQL\Type\ListType\ListType;
20
use Youshido\GraphQL\Type\Object\AbstractObjectType;
21
use Youshido\GraphQL\Type\Object\ObjectType;
22
use Youshido\GraphQL\Type\Scalar\IdType;
23
use Youshido\GraphQL\Type\Scalar\IntType;
24
use Youshido\GraphQL\Type\Scalar\StringType;
25
26
class UserType extends AbstractObjectType
27
{
28
    public function build($config): void
29
    {
30
        $config->addFields([
31
            'id'           => new IdType(),
32
            'fullName'     => new StringType(),
33
            'reservations' => new ListType(new ReservationInterface()),
34
        ]);
35
    }
36
}
37
38
class CourtReservation extends AbstractObjectType
39
{
40
    public function build($config): void
41
    {
42
        $config->addFields([
43
            'id'      => new IdType(),
44
            'players' => new ListType(new ObjectType([
45
                'name'   => 'Player',
46
                'fields' => [
47
                    'id'   => new IdType(),
48
                    'user' => new UserType(),
49
                ],
50
            ])),
51
        ]);
52
    }
53
54
    public function getInterfaces()
55
    {
56
        return [new ReservationInterface()];
57
    }
58
}
59
60
class ClassReservation extends AbstractObjectType
61
{
62
    public function build($config): void
63
    {
64
        $config->addFields([
65
            'id'   => new IdType(),
66
            'user' => new UserType(),
67
        ]);
68
    }
69
70
    public function getInterfaces()
71
    {
72
        return [new ReservationInterface()];
73
    }
74
}
75
76
class ReservationInterface extends AbstractInterfaceType
77
{
78
    public function resolveType($object)
79
    {
80
        return false === \mb_strpos($object['id'], 'cl') ? new CourtReservation() : new ClassReservation();
81
    }
82
83
    public function build($config): void
84
    {
85
        $config->addFields([
86
            'id' => new IdType(),
87
        ]);
88
    }
89
}
90
91
class FragmentsTest extends \PHPUnit_Framework_TestCase
92
{
93
    /**
94
     * @dataProvider queries
95
     *
96
     * @param $query
97
     * @param $expected
98
     * @param $variables
99
     */
100
    public function testVariables($query, $expected, $variables): void
101
    {
102
        $schema = new Schema([
103
            'query' => new ObjectType([
104
                'name'   => 'RootQuery',
105
                'fields' => [
106
                    'user' => [
107
                        'type'    => new UserType(),
108
                        'resolve' => static function ($args) {
109
                            return [
110
                                'id'           => 'user-id-1',
111
                                'fullName'     => 'Alex',
112
                                'reservations' => [
113
                                    [
114
                                        'id'   => 'cl-1',
115
                                        'user' => [
116
                                            'id'       => 'user-id-2',
117
                                            'fullName' => 'User class1',
118
                                        ],
119
                                    ],
120
                                    [
121
                                        'id'      => 'court-1',
122
                                        'players' => [
123
                                            [
124
                                                'id'   => 'player-id-1',
125
                                                'user' => [
126
                                                    'id'       => 'user-id-3',
127
                                                    'fullName' => 'User court1',
128
                                                ],
129
                                            ],
130
                                        ],
131
                                    ],
132
                                ],
133
                            ];
134
                        },
135
                    ],
136
                ],
137
            ]),
138
        ]);
139
140
        $processor = new Processor($schema);
141
        $processor->processPayload($query, $variables);
142
        $result = $processor->getResponseData();
143
144
        $this->assertEquals($expected, $result);
145
    }
146
147
    public function queries()
148
    {
149
        return [
150
            [
151
                'query {
152
                    user {
153
                        ...fUser
154
                        reservations {
155
                            ...fReservation
156
                        }
157
                    }
158
                }
159
                fragment fReservation on ReservationInterface {
160
                    id
161
                    ... on CourtReservation {
162
                        players {
163
                            id
164
                            user {
165
                                ...fUser
166
                            }
167
                        }
168
                    }
169
                    ... on ClassReservation {
170
                        user {
171
                            ...fUser
172
                        }
173
                    }
174
                }
175
                fragment fUser on User {
176
                    id
177
                    fullName
178
                }',
179
                [
180
                    'data' => [
181
                        'user' => [
182
                            'id'           => 'user-id-1',
183
                            'fullName'     => 'Alex',
184
                            'reservations' => [
185
                                [
186
                                    'id'   => 'cl-1',
187
                                    'user' => [
188
                                        'id'       => 'user-id-2',
189
                                        'fullName' => 'User class1',
190
                                    ],
191
                                ],
192
                                [
193
                                    'id'      => 'court-1',
194
                                    'players' => [
195
                                        [
196
                                            'id'   => 'player-id-1',
197
                                            'user' => [
198
                                                'id'       => 'user-id-3',
199
                                                'fullName' => 'User court1',
200
                                            ],
201
                                        ],
202
                                    ],
203
                                ],
204
                            ],
205
                        ],
206
                    ],
207
                ],
208
                [
209
                ],
210
            ],
211
        ];
212
    }
213
214
    public function testSimpleFragment(): void
215
    {
216
        $schema = new Schema([
217
            'query' => new ObjectType([
218
                'name'   => 'RootQuery',
219
                'fields' => [
220
                    'user' => [
221
                        'type'    => new UserType(),
222
                        'resolve' => static function ($args) {
223
                            return [
224
                                'id'       => 'user-id-1',
225
                                'fullName' => 'Alex',
226
                            ];
227
                        },
228
                        'args' => [
229
                            'id' => new IntType(),
230
                        ],
231
                    ],
232
                ],
233
            ]),
234
        ]);
235
236
        $query = '
237
        {
238
            User1: user(id: 1) {
239
                fullName
240
            }
241
            User2: user(id: 1) {
242
                ...Fields
243
            }
244
        }
245
        
246
        fragment Fields on User {
247
            fullName
248
        }';
249
250
        $processor = new Processor($schema);
251
        $processor->processPayload($query);
252
        $result = $processor->getResponseData();
253
254
        $expected = ['data' => ['User1' => ['fullName' => 'Alex'], 'User2' => ['fullName' => 'Alex']]];
255
        $this->assertEquals($expected, $result);
256
    }
257
}
258