1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Test; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\BrowserKit\Client; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method Client getClient() |
19
|
|
|
*/ |
20
|
|
|
trait GraphQLHelperTrait |
21
|
|
|
{ |
22
|
|
|
private static $endpoint; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $endpoint |
26
|
|
|
*/ |
27
|
|
|
protected static function endpoint($endpoint) |
28
|
|
|
{ |
29
|
|
|
self::$endpoint = $endpoint; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name |
34
|
|
|
* @param array $parameters (optional) |
35
|
|
|
* @param array|null $expected |
36
|
|
|
*/ |
37
|
7 |
|
protected static function query($name, array $parameters, array $expected = null) |
38
|
|
|
{ |
39
|
7 |
|
if (null === $expected) { |
40
|
|
|
$expected = $parameters; |
41
|
|
|
$parameters = []; |
42
|
|
|
} |
43
|
7 |
|
self::graphqlQuery('query', $name, $parameters, $expected); |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @param array $parameters |
49
|
|
|
* @param array $expected |
50
|
|
|
*/ |
51
|
7 |
|
protected static function mutation($name, array $parameters = [], array $expected = []) |
52
|
|
|
{ |
53
|
7 |
|
self::graphqlQuery('mutation', $name, $parameters, $expected); |
54
|
7 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $nodeType |
59
|
|
|
* @param string $databaseId |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
6 |
|
protected static function encodeID($nodeType, $databaseId) |
64
|
|
|
{ |
65
|
6 |
|
return ID::encode($nodeType, $databaseId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $type |
70
|
|
|
* @param string $name |
71
|
|
|
* @param array $parameters |
72
|
|
|
* @param array $expected |
73
|
|
|
*/ |
74
|
14 |
|
private static function graphqlQuery($type, $name, array $parameters = [], array $expected = []) |
75
|
|
|
{ |
76
|
14 |
|
$expectedStr = self::flattenExpectation($expected); |
77
|
|
|
|
78
|
14 |
|
$paramsStr = null; |
79
|
14 |
|
if ($parameters) { |
|
|
|
|
80
|
14 |
|
$paramsStr = self::flattenParameters($parameters); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$query = <<<GrahpQL |
84
|
14 |
|
$type $name{ |
85
|
14 |
|
$name$paramsStr{ |
86
|
14 |
|
$expectedStr |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
GrahpQL; |
90
|
14 |
|
$body = ['query' => $query]; |
91
|
|
|
|
92
|
14 |
|
self::getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode($body)); |
|
|
|
|
93
|
14 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $parameters |
97
|
|
|
* @param array $wrappers |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
14 |
|
private static function flattenParameters(array $parameters, $wrappers = ['(', ')']) |
102
|
|
|
{ |
103
|
14 |
|
foreach ($parameters as $key => &$value) { |
104
|
14 |
|
if (\is_string($value)) { |
105
|
11 |
|
$value = "\"$value\""; |
106
|
|
|
} |
107
|
14 |
|
if (\is_bool($value)) { |
108
|
2 |
|
$value = $value ? 'true' : 'false'; |
109
|
|
|
} |
110
|
14 |
|
if (\is_array($value)) { |
111
|
10 |
|
if (array_key_exists(0, $value)) { |
112
|
2 |
|
$value = json_encode($value); |
113
|
|
|
} else { |
114
|
8 |
|
$value = self::flattenParameters($value, ['{', '}']); |
115
|
|
|
} |
116
|
|
|
} |
117
|
14 |
|
$value = "$key: $value"; |
118
|
|
|
} |
119
|
14 |
|
unset($value); |
120
|
|
|
|
121
|
14 |
|
list($start, $end) = $wrappers; |
122
|
|
|
|
123
|
14 |
|
return "$start ".implode(', ', $parameters)." $end"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $expectation |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
14 |
|
private static function flattenExpectation(array $expectation) |
132
|
|
|
{ |
133
|
|
|
|
134
|
|
|
|
135
|
14 |
|
$expectNormalized = []; |
136
|
14 |
|
foreach ($expectation as $path => $value) { |
137
|
14 |
|
if (\is_array($value)) { |
138
|
11 |
|
if (isset($value[0], $value[1]) |
139
|
11 |
|
&& is_array($value[0]) |
140
|
11 |
|
&& is_array($value[1]) |
141
|
|
|
) { |
142
|
1 |
|
$params = self::flattenParameters($value[0]); |
143
|
1 |
|
$expectation = self::flattenExpectation($value[1]); |
144
|
|
|
|
145
|
1 |
|
$value = "$path $params {\n\t\t$expectation\n\t}"; |
146
|
|
|
} else { |
147
|
10 |
|
$value = $path." {\n\t\t".self::flattenExpectation($value)."\n\t}"; |
148
|
|
|
} |
149
|
|
|
} |
150
|
14 |
|
$expectNormalized[] = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
14 |
|
return implode("\n\t", $expectNormalized); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.