-
Notifications
You must be signed in to change notification settings - Fork 495
/
ConstructorTest.php
364 lines (326 loc) · 8.87 KB
/
ConstructorTest.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
namespace WpOrg\Requests\Tests\Cookie;
use WpOrg\Requests\Cookie;
use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Tests\TestCase;
use WpOrg\Requests\Tests\TypeProviderHelper;
use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
/**
* @covers \WpOrg\Requests\Cookie::__construct
*/
final class ConstructorTest extends TestCase {
/**
* Tests receiving an exception when the constructor received an invalid input type as `$name`.
*
* @dataProvider dataInvalidStringInput
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testInvalidName($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #1 ($name) must be of type string');
new Cookie($input, 'value');
}
/**
* Tests receiving an exception when the constructor received an invalid input type as `$value`.
*
* @dataProvider dataInvalidStringInput
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testInvalidValue($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #2 ($value) must be of type string');
new Cookie('name', $input);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataInvalidStringInput() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
/**
* Tests receiving an exception when the constructor received an invalid input type as `$name`.
*
* @dataProvider dataInvalidAttributes
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testInvalidAttributes($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #3 ($attributes) must be of type array|ArrayAccess&Traversable');
new Cookie('name', 'value', $input);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataInvalidAttributes() {
$except = array_intersect(TypeProviderHelper::GROUP_ITERABLE, TypeProviderHelper::GROUP_ARRAY_ACCESSIBLE);
return TypeProviderHelper::getAllExcept($except);
}
/**
* Tests receiving an exception when the constructor received an invalid input type as `$flags`.
*
* @dataProvider dataInvalidFlags
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testInvalidFlags($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #4 ($flags) must be of type array');
new Cookie('name', 'value', [], $input);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataInvalidFlags() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_ARRAY);
}
/**
* Tests receiving an exception when the constructor received an invalid input type as `$reference_time`.
*
* @dataProvider dataInvalidReferenceTime
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testInvalidReferenceTime($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #5 ($reference_time) must be of type integer|null');
new Cookie('name', 'value', [], [], $input);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataInvalidReferenceTime() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_NULL, TypeProviderHelper::GROUP_INT);
}
/**
* Tests the constructor handles valid data correctly when called with only the required arguments.
*
* @dataProvider dataMinimalArguments
*
* @param string $name The name of the cookie.
* @param string $value The value for the cookie.
*
* @return void
*/
public function testMinimalArguments($name, $value) {
$cookie = new Cookie($name, $value);
$this->assertSame($name, $cookie->name, 'Name was not set correctly');
$this->assertSame($value, $cookie->value, 'Value was not set correctly');
}
/**
* Data Provider.
*
* @return array
*/
public static function dataMinimalArguments() {
return [
'empty name and value' => [
'name' => '',
'value' => '',
],
'non-empty name and value' => [
'name' => 'cookie-name',
'value' => 'cookie-value',
],
];
}
/**
* Tests the constructor handles merging of flags correctly.
*
* @dataProvider dataFlagMerging
*
* @param array $flags Passed flags.
* @param array $expected Flags as expected to be set by the function.
*
* @return void
*/
public function testFlagMerging($flags, $expected) {
/*
* Set creation and last-access values last moment (if not set in the test case)
* to prevent the test from failing on time difference between when the data provider
* is called and when the actual test is run.
*/
if (!isset($expected['last-access'])) {
$expected = $this->arrayUnshiftAssoc($expected, 'last-access', time());
}
if (!isset($expected['creation'])) {
$expected = $this->arrayUnshiftAssoc($expected, 'creation', time());
}
$cookie = new Cookie('name', 'value', [], $flags);
$this->assertSame($expected, $cookie->flags);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataFlagMerging() {
return [
'empty array' => [
'flags' => [],
'expected' => [
'persistent' => false,
'host-only' => true,
],
],
'all supported flags passed' => [
'flags' => [
'creation' => 543547575,
'last-access' => 543548555,
'persistent' => true,
'host-only' => false,
],
'expected' => [
'creation' => 543547575,
'last-access' => 543548555,
'persistent' => true,
'host-only' => false,
],
],
'unsupported keys are allowed through' => [
'flags' => [
'creation' => 12345,
'last-access' => 12345,
'invalid' => 'invalid',
],
'expected' => [
'creation' => 12345,
'last-access' => 12345,
'persistent' => false,
'host-only' => true,
'invalid' => 'invalid',
],
],
];
}
/**
* Tests the constructor handles valid reference time setting correctly.
*
* @dataProvider dataSetReferenceTime
*
* @param mixed $time The passed reference time.
* @param int|null $expected Reference time as expected to be set by the function.
*
* @return void
*/
public function testSetReferenceTime($time, $expected = null) {
/*
* Set expected time last moment (if not set in the test case) to prevent the test
* from failing on time difference between when the data provider
* is called and when the actual test is run.
*/
if (!isset($expected)) {
$expected = time();
}
$cookie = new Cookie('name', 'value', [], [], $time);
$this->assertSame($expected, $cookie->reference_time);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataSetReferenceTime() {
return [
'null' => [
'time' => null,
],
'valid time' => [
'time' => 2178381267,
'expected' => 2178381267,
],
];
}
/**
* Tests the constructor normalizes passed attributes.
*
* @dataProvider dataAttributesAreNormalized
*
* @param array $attributes The value to use for the attributes argument.
* @param array $expected The attributes as they are expected to be set.
*
* @return void
*/
public function testAttributesAreNormalized($attributes, $expected) {
$cookie = new Cookie('name', 'value', $attributes);
$this->assertSame($expected, $cookie->attributes);
}
/**
* Data Provider.
*
* @return array
*/
public static function dataAttributesAreNormalized() {
return [
'empty array' => [
'attributes' => [],
'expected' => [],
],
'attributes array' => [
'attributes' => [
'domain' => '',
'expires' => '2022-04-07',
'max-age' => 2874934,
],
'expected' => [
'expires' => 1649289600,
'max-age' => 2874934,
],
],
];
}
/**
* Tests the constructor normalizes passed attributes when the attributes are passed as a Dictionary object.
*
* @return void
*/
public function testAttributesAreNormalizedInDictionary() {
$attributes = new CaseInsensitiveDictionary(
[
'domain' => '',
'expires' => '2022-04-07',
'max-age' => 2874934,
]
);
$expected = [
'expires' => 1649289600,
'max-age' => 2874934,
];
$cookie = new Cookie('name', 'value', $attributes);
$this->assertSame($expected, $cookie->attributes->getAll());
}
/**
* Test helper function to prepend values onto an array and prevent issues with array order in assertions.
*
* @param array $base_array The array to adjust.
* @param string|int $key Key for the array item to add.
* @param mixed $value Value for the array item to add.
*
* @return array
*/
private function arrayUnshiftAssoc($base_array, $key, $value ) {
$base_array = array_reverse($base_array, true);
$base_array[$key] = $value;
return array_reverse($base_array, true);
}
}