1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of graphql-youshido project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 11/28/15 2:02 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\Tests; |
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Schema; |
12
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
13
|
|
|
use Youshido\GraphQL\Processor; |
14
|
|
|
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator; |
15
|
|
|
use Youshido\Tests\DataProvider\UserType; |
16
|
|
|
|
17
|
|
|
class ProcessorTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $query |
22
|
|
|
* @param $response |
23
|
|
|
* |
24
|
|
|
* @dataProvider predefinedSchemaProvider |
25
|
|
|
*/ |
26
|
|
|
public function testPredefinedQueries($query, $response) |
27
|
|
|
{ |
28
|
|
|
$schema = new Schema([ |
29
|
|
|
'query' => new ObjectType([ |
30
|
|
|
'name' => 'TestSchema', |
31
|
|
|
'description' => 'Root of TestSchema' |
32
|
|
|
]) |
33
|
|
|
]); |
34
|
|
|
$schema->addQuery('latest', |
35
|
|
|
new ObjectType( |
36
|
|
|
[ |
37
|
|
|
'name' => 'latest', |
38
|
|
|
'fields' => [ |
39
|
|
|
'id' => ['type' => 'int'], |
40
|
|
|
'name' => ['type' => 'string'] |
41
|
|
|
], |
42
|
|
|
'resolve' => function () { |
43
|
|
|
return [ |
44
|
|
|
'id' => 1, |
45
|
|
|
'name' => 'Alex' |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
])); |
49
|
|
|
|
50
|
|
|
$validator = new ResolveValidator(); |
51
|
|
|
$processor = new Processor($validator); |
52
|
|
|
|
53
|
|
|
$processor->setSchema($schema); |
54
|
|
|
|
55
|
|
|
$processor->processQuery($query); |
56
|
|
|
|
57
|
|
|
$this->assertEquals($processor->getResponseData(), $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function predefinedSchemaProvider() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
[ |
65
|
|
|
'{ |
66
|
|
|
__schema { |
67
|
|
|
queryType { |
68
|
|
|
kind, |
69
|
|
|
name, |
70
|
|
|
description, |
71
|
|
|
interfaces { |
72
|
|
|
name |
73
|
|
|
}, |
74
|
|
|
possibleTypes { |
75
|
|
|
name |
76
|
|
|
}, |
77
|
|
|
inputFields { |
78
|
|
|
name |
79
|
|
|
}, |
80
|
|
|
ofType{ |
81
|
|
|
name |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
}', |
86
|
|
|
['data' => [ |
87
|
|
|
'__schema' => [ |
88
|
|
|
'queryType' => [ |
89
|
|
|
'kind' => 'OBJECT', |
90
|
|
|
'name' => 'TestSchema', |
91
|
|
|
'description' => 'Root of TestSchema', |
92
|
|
|
'interfaces' => [], |
93
|
|
|
'possibleTypes' => [], |
94
|
|
|
'inputFields' => [], |
95
|
|
|
'ofType' => [] |
96
|
|
|
] |
97
|
|
|
] |
98
|
|
|
]] |
99
|
|
|
] |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @dataProvider schemaProvider |
106
|
|
|
*/ |
107
|
|
|
public function testProcessor($query, $response) |
108
|
|
|
{ |
109
|
|
|
$schema = new Schema(); |
110
|
|
|
$schema->addQuery('latest', |
111
|
|
|
new ObjectType( |
112
|
|
|
[ |
113
|
|
|
'name' => 'latest', |
114
|
|
|
'fields' => [ |
115
|
|
|
'id' => ['type' => 'int'], |
116
|
|
|
'name' => ['type' => 'string'] |
117
|
|
|
], |
118
|
|
|
'resolve' => function () { |
119
|
|
|
return [ |
120
|
|
|
'id' => 1, |
121
|
|
|
'name' => 'Alex' |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
])); |
125
|
|
|
|
126
|
|
|
$schema->addQuery('user', new UserType()); |
127
|
|
|
|
128
|
|
|
$validator = new ResolveValidator(); |
129
|
|
|
$processor = new Processor($validator); |
130
|
|
|
|
131
|
|
|
$processor->setSchema($schema); |
132
|
|
|
$processor->processQuery($query); |
133
|
|
|
|
134
|
|
|
$this->assertEquals( |
135
|
|
|
$processor->getResponseData(), |
136
|
|
|
$response |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function schemaProvider() |
141
|
|
|
{ |
142
|
|
|
return [ |
143
|
|
|
[ |
144
|
|
|
'{ latest { name } }', |
145
|
|
|
[ |
146
|
|
|
'data' => ['latest' => null] |
147
|
|
|
] |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
'{ user(id:1) { id, name } }', |
151
|
|
|
[ |
152
|
|
|
'data' => ['user' => ['id' => 1, 'name' => 'John']] |
153
|
|
|
] |
154
|
|
|
] |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|