-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathTypeProviderHelper.php
269 lines (243 loc) · 6.56 KB
/
TypeProviderHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace WpOrg\Requests\Tests;
use ArrayIterator;
use EmptyIterator;
use stdClass;
use WpOrg\Requests\Tests\Fixtures\ArrayAccessibleObject;
use WpOrg\Requests\Tests\Fixtures\StringableObject;
/**
* Helper class to provide an exhaustive list of types to test type safety.
*/
final class TypeProviderHelper {
/**
* Keys of all type entries representing null.
*
* @var array<string>
*/
const GROUP_NULL = ['null'];
/**
* Keys of all type entries representing a boolean.
*
* @var array<string>
*/
const GROUP_BOOL = [
'boolean false',
'boolean true',
];
/**
* Keys of all type entries representing an integer.
*
* @var array<string>
*/
const GROUP_INT = [
'integer 0',
'negative integer',
'positive integer',
];
/**
* Keys of all type entries representing a float.
*
* @var array<string>
*/
const GROUP_FLOAT = [
'float 0.0',
'negative float',
'positive float',
];
/**
* Keys of all type entries representing an integer or float.
*
* @var array<string>
*/
const GROUP_INT_FLOAT = [
'integer 0',
'negative integer',
'positive integer',
'float 0.0',
'negative float',
'positive float',
];
/**
* Keys of all type entries representing a string.
*
* @var array<string>
*/
const GROUP_STRING = [
'empty string',
'numeric string',
'textual string',
'textual string starting with numbers',
];
/**
* Keys of all type entries which are stringable.
*
* @var array<string>
*/
const GROUP_STRINGABLE = [
'empty string',
'numeric string',
'textual string',
'textual string starting with numbers',
'Stringable object',
];
/**
* Keys of all type entries representing an array.
*
* @var array<string>
*/
const GROUP_ARRAY = [
'empty array',
'array with values, no keys',
'array with values, string keys',
];
/**
* Keys of all type entries which are iterable.
*
* @var array<string>
*/
const GROUP_ITERABLE = [
'empty array',
'array with values, no keys',
'array with values, string keys',
'ArrayIterator object',
'Iterator object, no array access',
];
/**
* Keys of all type entries which have array access.
*
* @var array<string>
*/
const GROUP_ARRAY_ACCESSIBLE = [
'empty array',
'array with values, no keys',
'array with values, string keys',
'ArrayIterator object',
'ArrayAccess object',
];
/**
* Keys of all type entries representing an object.
*
* @var array<string>
*/
const GROUP_OBJECT = [
'plain object',
'Stringable object',
'ArrayIterator object',
'ArrayAccess object',
'Iterator object, no array access',
];
/**
* Keys of all type entries representing a resource.
*
* @var array<string>
*/
const GROUP_RESOURCE = [
'resource (open file handle)',
'resource (closed file handle)',
];
/**
* Keys of all type entries which are considered empty.
*
* @var array<string>
*/
const GROUP_EMPTY = [
'null',
'boolean false',
'integer 0',
'float 0.0',
'empty string',
'empty array',
];
/**
* File handle to local memory (open resource).
*
* @var resource
*/
private static $memory_handle_open;
/**
* File handle to local memory (closed resource).
*
* @var resource
*/
private static $memory_handle_closed;
/**
* Clean up after the tests.
*
* This method should be called in the `tear_down_after_class()` of any test class
* using these helper functions.
*
* @return void
*/
public static function cleanUp() {
if (isset(self::$memory_handle_open)) {
fclose(self::$memory_handle_open);
unset(self::$memory_handle_open);
}
}
/**
* Retrieve an array in data provider format with a selection of all typical PHP data types
* *except* the named types specified in the $except parameter.
*
* @param array<string> ...$except One or more arrays containing the names of the types to exclude.
* Typically, one or more of the predefined "groups" (see the constants)
* would be used here.
*
* @return array<string, array<mixed>>
*/
public static function getAllExcept(array ...$except) {
$except = array_flip(array_merge(...$except));
return array_diff_key(self::getAll(), $except);
}
/**
* Retrieve an array in data provider format with a selection of typical PHP data types.
*
* @param array<string> ...$selection One or more arrays containing the names of the types to include.
* Typically, one or more of the predefined "groups" (see the constants)
* would be used here.
*
* @return array<string, array<mixed>>
*/
public static function getSelection(array ...$selection) {
$selection = array_flip(array_merge(...$selection));
return array_intersect_key(self::getAll(), $selection);
}
/**
* Retrieve an array in data provider format with all typical PHP data types.
*
* @return array<string, array<mixed>>
*/
public static function getAll() {
if (isset(self::$memory_handle_open) === false) {
self::$memory_handle_open = fopen('php://memory', 'r+');
}
if (isset(self::$memory_handle_closed) === false) {
self::$memory_handle_closed = fopen('php://memory', 'r+');
fclose(self::$memory_handle_closed);
}
return [
'null' => [null],
'boolean false' => [false],
'boolean true' => [true],
'integer 0' => [0],
'negative integer' => [-123],
'positive integer' => [786687],
'float 0.0' => [0.0],
'negative float' => [5.600e-3],
'positive float' => [124.7],
'empty string' => [''],
'numeric string' => ['123'],
'textual string' => ['foobar'],
'textual string starting with numbers' => ['123 My Street'],
'empty array' => [[]],
'array with values, no keys' => [[1, 2, 3]],
'array with values, string keys' => [['a' => 1, 'b' => 2]],
'plain object' => [new stdClass()],
'Stringable object' => [new StringableObject('value')],
'ArrayIterator object' => [new ArrayIterator([1, 2, 3])],
'ArrayAccess object' => [new ArrayAccessibleObject()],
'Iterator object, no array access' => [new EmptyIterator()],
'resource (open file handle)' => [self::$memory_handle_open],
'resource (closed file handle)' => [self::$memory_handle_closed],
];
}
}