1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\EventDispatcher\Event; |
6
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\Event\Parameters; |
8
|
|
|
use Arp\EventDispatcher\Event\ParametersInterface; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters |
13
|
|
|
*/ |
14
|
|
|
final class ParametersTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Assert that the class implements the ParametersInterface |
18
|
|
|
*/ |
19
|
|
|
public function testImplementsParametersInterface(): void |
20
|
|
|
{ |
21
|
|
|
$params = new Parameters([]); |
22
|
|
|
|
23
|
|
|
$this->assertInstanceOf(ParametersInterface::class, $params); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Assert that the class implements \ArrayAccess |
28
|
|
|
*/ |
29
|
|
|
public function testImplementsArrayAccess(): void |
30
|
|
|
{ |
31
|
|
|
$params = new Parameters([]); |
32
|
|
|
|
33
|
|
|
$this->assertInstanceOf(\ArrayAccess::class, $params); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Assert that parameters can be set and fetched vis getParams() and setParams() |
38
|
|
|
* |
39
|
|
|
* @param array<mixed> $data The test case data |
40
|
|
|
* |
41
|
|
|
* @dataProvider getGetSetParametersData |
42
|
|
|
*/ |
43
|
|
|
public function testGetSetParameters(array $data): void |
44
|
|
|
{ |
45
|
|
|
$params = new Parameters(); |
46
|
|
|
|
47
|
|
|
$this->assertEmpty($params->getParams()); |
48
|
|
|
|
49
|
|
|
$params->setParams($data); |
50
|
|
|
|
51
|
|
|
$this->assertEquals($data, $params->getParams()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array<mixed> |
56
|
|
|
*/ |
57
|
|
|
public function getGetSetParametersData(): array |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
// Simple |
61
|
|
|
[ |
62
|
|
|
[ |
63
|
|
|
'foo' => 'bar', |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
|
67
|
|
|
// Extra data types |
68
|
|
|
[ |
69
|
|
|
[ |
70
|
|
|
'test' => 'Red', |
71
|
|
|
'blue' => true, |
72
|
|
|
'Hello' => 455.667, |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
|
76
|
|
|
// Complex data types. |
77
|
|
|
[ |
78
|
|
|
[ |
79
|
|
|
'callable' => static function () { |
80
|
|
|
return 'test'; |
81
|
|
|
}, |
82
|
|
|
'data' => [ |
83
|
|
|
'foo' => 'bar', |
84
|
|
|
'789' => 123.456, |
85
|
|
|
], |
86
|
|
|
'object' => new \stdClass(), |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Assert that parameter values can be set and get by name |
94
|
|
|
*/ |
95
|
|
|
public function testGetAndSetParam(): void |
96
|
|
|
{ |
97
|
|
|
$params = new Parameters([]); |
98
|
|
|
|
99
|
|
|
$params->setParam('foo', 123); |
100
|
|
|
|
101
|
|
|
$this->assertSame(123, $params->getParam('foo')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Assert that the default value is returned for calls to getParam() when the request parameter value cannot be |
106
|
|
|
* found within the collection |
107
|
|
|
*/ |
108
|
|
|
public function testGetParamWillReturnDefaultValueForNonExistingParam(): void |
109
|
|
|
{ |
110
|
|
|
$params = new Parameters([]); |
111
|
|
|
|
112
|
|
|
$this->assertNull($params->getParam('bob')); |
113
|
|
|
|
114
|
|
|
$params->setParam('foo', 123); |
115
|
|
|
$params->setParam('bar', 'Hello'); |
116
|
|
|
|
117
|
|
|
$defaultValue = 'default value'; |
118
|
|
|
|
119
|
|
|
$this->assertSame($defaultValue, $params->getParam('test', $defaultValue)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Assert that isEmpty() will return true when the params collection is empty |
124
|
|
|
*/ |
125
|
|
|
public function testIsEmptyReturnsTrueWhenEmpty(): void |
126
|
|
|
{ |
127
|
|
|
$params = new Parameters([]); |
128
|
|
|
|
129
|
|
|
$this->assertTrue( |
130
|
|
|
$params->isEmpty(), |
131
|
|
|
'Method isEmpty() returned false when the params collection was empty' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Assert that getParams() will return true when the params collection is not empty |
137
|
|
|
*/ |
138
|
|
|
public function testIsEmptyReturnsFalseWhenNotEmpty(): void |
139
|
|
|
{ |
140
|
|
|
$params = new Parameters([ |
141
|
|
|
'foo' => 'bar', |
142
|
|
|
'bar' => 'baz', |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
$this->assertFalse( |
146
|
|
|
$params->isEmpty(), |
147
|
|
|
'Method isEmpty() returned true when the params collection was not empty' |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Assert that the count method returns the correct count of the number of params within the collection. |
153
|
|
|
* |
154
|
|
|
* @param array<mixed> $data The data test set |
155
|
|
|
* |
156
|
|
|
* @dataProvider getCountData |
157
|
|
|
*/ |
158
|
|
|
public function testCount(array $data): void |
159
|
|
|
{ |
160
|
|
|
$params = new Parameters($data); |
161
|
|
|
|
162
|
|
|
$this->assertSame(count($data), $params->count()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array<mixed> |
167
|
|
|
*/ |
168
|
|
|
public function getCountData(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
[ |
172
|
|
|
[], |
173
|
|
|
], |
174
|
|
|
|
175
|
|
|
[ |
176
|
|
|
['foo' => 'bar'], |
177
|
|
|
], |
178
|
|
|
|
179
|
|
|
[ |
180
|
|
|
[ |
181
|
|
|
'foo' => 'bar', |
182
|
|
|
'baz' => 'test', |
183
|
|
|
], |
184
|
|
|
], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Assert that getValues() will return a numerically indexed array of the parameter values. |
190
|
|
|
* |
191
|
|
|
* @param array<mixed> $params The parameters that should be set. |
192
|
|
|
* |
193
|
|
|
* @dataProvider getGetValuesWillReturnTheParametersValuesData |
194
|
|
|
*/ |
195
|
|
|
public function testGetValuesWillReturnTheParametersValues(array $params): void |
196
|
|
|
{ |
197
|
|
|
$parameters = new Parameters($params); |
198
|
|
|
|
199
|
|
|
$this->assertSame(array_values($params), $parameters->getValues()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return array<mixed> |
204
|
|
|
*/ |
205
|
|
|
public function getGetValuesWillReturnTheParametersValuesData(): array |
206
|
|
|
{ |
207
|
|
|
return [ |
208
|
|
|
[ |
209
|
|
|
[ |
210
|
|
|
'foo' => 'bar', |
211
|
|
|
'baz' => 'boo', |
212
|
|
|
'hello' => 'test', |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Assert that a parameter has been with calls to hasParam() |
220
|
|
|
*/ |
221
|
|
|
public function testHasParamWillReturnBool(): void |
222
|
|
|
{ |
223
|
|
|
$data = [ |
224
|
|
|
'foo' => 123, |
225
|
|
|
'bar' => 'baz', |
226
|
|
|
'bax' => 45, |
227
|
|
|
'null_test' => null, |
228
|
|
|
]; |
229
|
|
|
$parameters = new Parameters($data); |
230
|
|
|
|
231
|
|
|
$this->assertTrue($parameters->hasParam('foo')); |
232
|
|
|
$this->assertTrue($parameters->hasParam('bar')); |
233
|
|
|
$this->assertTrue($parameters->hasParam('bax')); |
234
|
|
|
$this->assertTrue($parameters->hasParam('null_test')); |
235
|
|
|
|
236
|
|
|
$this->assertFalse($parameters->hasParam('test')); |
237
|
|
|
$this->assertFalse($parameters->hasParam('fred')); |
238
|
|
|
$this->assertFalse($parameters->hasParam('bob')); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Assert that removeParam() with remove the provided key from the collection. If the parameter name provided |
243
|
|
|
* exists in the collection, the method should remove it and return boolean true, otherwise just return false |
244
|
|
|
*/ |
245
|
|
|
public function testRemoveParam(): void |
246
|
|
|
{ |
247
|
|
|
$params = new Parameters([ |
248
|
|
|
'foo' => 'bar', |
249
|
|
|
'bar' => 'baz', |
250
|
|
|
'abc' => 123, |
251
|
|
|
]); |
252
|
|
|
|
253
|
|
|
$this->assertTrue($params->removeParam('foo')); |
254
|
|
|
$this->assertFalse($params->removeParam('bob')); |
255
|
|
|
$this->assertTrue($params->removeParam('abc')); |
256
|
|
|
|
257
|
|
|
$this->assertSame(['bar' => 'baz'], $params->getParams()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Assert that an array of parameters can be removed from the collection |
262
|
|
|
*/ |
263
|
|
|
public function testRemoveParams(): void |
264
|
|
|
{ |
265
|
|
|
$data = [ |
266
|
|
|
'foo' => 'bar', |
267
|
|
|
'bar' => 'bsz', |
268
|
|
|
'test' => 'Hello World', |
269
|
|
|
'alex' => 'bye!', |
270
|
|
|
]; |
271
|
|
|
|
272
|
|
|
$params = new Parameters($data); |
273
|
|
|
|
274
|
|
|
$this->assertSame($data, $params->getParams()); |
275
|
|
|
|
276
|
|
|
$params->removeParams(['foo', 'bar']); |
277
|
|
|
unset($data['foo'], $data['bar']); |
278
|
|
|
|
279
|
|
|
$this->assertSame($data, $params->getParams()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Assert that the offsetExists() method will check if the parameter is defined |
284
|
|
|
*/ |
285
|
|
|
public function testOffsetExists(): void |
286
|
|
|
{ |
287
|
|
|
$params = new Parameters(['test' => 123]); |
288
|
|
|
|
289
|
|
|
$this->assertTrue(isset($params['test'])); |
290
|
|
|
$this->assertFalse(isset($params['hello'])); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Assert that the parameters can be set and returned using the array access interface |
295
|
|
|
*/ |
296
|
|
|
public function testOffsetGetAndOffsetSet(): void |
297
|
|
|
{ |
298
|
|
|
$params = new Parameters([]); |
299
|
|
|
|
300
|
|
|
$params['test'] = 123; |
301
|
|
|
$params['hello'] = 'Foo'; |
302
|
|
|
|
303
|
|
|
$this->assertSame(123, $params['test']); |
304
|
|
|
$this->assertSame('Foo', $params['hello']); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Assert that a parameter can be removed via the \ArrayAccess api |
309
|
|
|
*/ |
310
|
|
|
public function testOffsetUnset(): void |
311
|
|
|
{ |
312
|
|
|
$data = [ |
313
|
|
|
'foo' => 'fred', |
314
|
|
|
'bar' => 'bob', |
315
|
|
|
'baz' => 'alex', |
316
|
|
|
]; |
317
|
|
|
|
318
|
|
|
$params = new Parameters($data); |
319
|
|
|
|
320
|
|
|
unset($params['foo'], $data['foo']); |
321
|
|
|
|
322
|
|
|
$this->assertSame($data, $params->getParams()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Assert that the getIterator() method will return an \ArrayIterator instance with a copy of the params collection |
327
|
|
|
*/ |
328
|
|
|
public function testGetIteratorWillReturnArrayIteratorWithParamsCollection(): void |
329
|
|
|
{ |
330
|
|
|
$data = [ |
331
|
|
|
'foo' => 'fred', |
332
|
|
|
'bar' => 'bob', |
333
|
|
|
'baz' => 'alex', |
334
|
|
|
]; |
335
|
|
|
|
336
|
|
|
$params = new Parameters($data); |
337
|
|
|
|
338
|
|
|
$iterator = $params->getIterator(); |
339
|
|
|
|
340
|
|
|
$this->assertSame($data, $iterator->getArrayCopy()); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|