ClassReservation::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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