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