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
|
|
|
];
|
251
|
|
|
$parameters = new Parameters($data);
|
252
|
|
|
|
253
|
|
|
$this->assertTrue($parameters->hasParam('foo'));
|
254
|
|
|
$this->assertTrue($parameters->hasParam('bar'));
|
255
|
|
|
$this->assertTrue($parameters->hasParam('bax'));
|
256
|
|
|
|
257
|
|
|
$this->assertFalse($parameters->hasParam('test'));
|
258
|
|
|
$this->assertFalse($parameters->hasParam('fred'));
|
259
|
|
|
$this->assertFalse($parameters->hasParam('bob'));
|
260
|
|
|
}
|
261
|
|
|
|
262
|
|
|
/**
|
263
|
|
|
* Assert that removeParam() with remove the provided key from the collection. If the parameter name provided
|
264
|
|
|
* exists in the collection, the method should remove it and return boolean true, otherwise just return false.
|
265
|
|
|
*
|
266
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::removeParam
|
267
|
|
|
*/
|
268
|
|
|
public function testRemoveParam(): void
|
269
|
|
|
{
|
270
|
|
|
$params = new Parameters([
|
271
|
|
|
'foo' => 'bar',
|
272
|
|
|
'bar' => 'baz',
|
273
|
|
|
'abc' => 123,
|
274
|
|
|
]);
|
275
|
|
|
|
276
|
|
|
$this->assertTrue($params->removeParam('foo'));
|
277
|
|
|
$this->assertFalse($params->removeParam('bob'));
|
278
|
|
|
$this->assertTrue($params->removeParam('abc'));
|
279
|
|
|
|
280
|
|
|
$this->assertSame(['bar' => 'baz'], $params->getParams());
|
281
|
|
|
}
|
282
|
|
|
|
283
|
|
|
/**
|
284
|
|
|
* Assert that an array of parameters can be removed from the collection.
|
285
|
|
|
*
|
286
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::removeParams
|
287
|
|
|
*/
|
288
|
|
|
public function testRemoveParams(): void
|
289
|
|
|
{
|
290
|
|
|
$data = [
|
291
|
|
|
'foo' => 'bar',
|
292
|
|
|
'bar' => 'bsz',
|
293
|
|
|
'test' => 'Hello World',
|
294
|
|
|
'alex' => 'bye!',
|
295
|
|
|
];
|
296
|
|
|
|
297
|
|
|
$params = new Parameters($data);
|
298
|
|
|
|
299
|
|
|
$this->assertSame($data, $params->getParams());
|
300
|
|
|
|
301
|
|
|
$params->removeParams(['foo', 'bar']);
|
302
|
|
|
unset($data['foo'], $data['bar']);
|
303
|
|
|
|
304
|
|
|
$this->assertSame($data, $params->getParams());
|
305
|
|
|
}
|
306
|
|
|
|
307
|
|
|
/**
|
308
|
|
|
* Assert that the offsetExists() method will check if the parameter is defined
|
309
|
|
|
*
|
310
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::offsetExists
|
311
|
|
|
*/
|
312
|
|
|
public function testOffsetExists(): void
|
313
|
|
|
{
|
314
|
|
|
$params = new Parameters(['test' => 123]);
|
315
|
|
|
|
316
|
|
|
$this->assertTrue(isset($params['test']));
|
317
|
|
|
$this->assertFalse(isset($params['hello']));
|
318
|
|
|
}
|
319
|
|
|
|
320
|
|
|
/**
|
321
|
|
|
* Assert that the parameters can be set and returned using the array access interface.
|
322
|
|
|
*
|
323
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::offsetGet
|
324
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::offsetSet
|
325
|
|
|
*/
|
326
|
|
|
public function testOffsetGetAndOffsetSet(): void
|
327
|
|
|
{
|
328
|
|
|
$params = new Parameters([]);
|
329
|
|
|
|
330
|
|
|
$params['test'] = 123;
|
331
|
|
|
$params['hello'] = 'Foo';
|
332
|
|
|
|
333
|
|
|
$this->assertSame(123, $params['test']);
|
334
|
|
|
$this->assertSame('Foo', $params['hello']);
|
335
|
|
|
}
|
336
|
|
|
|
337
|
|
|
/**
|
338
|
|
|
* Assert that a parameter can be removed via the \ArrayAccess api.
|
339
|
|
|
*
|
340
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::offsetUnset
|
341
|
|
|
*/
|
342
|
|
|
public function testOffsetUnset(): void
|
343
|
|
|
{
|
344
|
|
|
$data = [
|
345
|
|
|
'foo' => 'fred',
|
346
|
|
|
'bar' => 'bob',
|
347
|
|
|
'baz' => 'alex',
|
348
|
|
|
];
|
349
|
|
|
|
350
|
|
|
$params = new Parameters($data);
|
351
|
|
|
|
352
|
|
|
unset($params['foo'], $data['foo']);
|
353
|
|
|
|
354
|
|
|
$this->assertSame($data, $params->getParams());
|
355
|
|
|
}
|
356
|
|
|
|
357
|
|
|
/**
|
358
|
|
|
* Assert that the getIterator() method will return an \ArrayIterator instance with a copy of the params collection.
|
359
|
|
|
*
|
360
|
|
|
* @covers \Arp\EventDispatcher\Event\Parameters::getIterator
|
361
|
|
|
*/
|
362
|
|
|
public function testGetIteratorWillReturnArrayIteratorWithParamsCollection(): void
|
363
|
|
|
{
|
364
|
|
|
$data = [
|
365
|
|
|
'foo' => 'fred',
|
366
|
|
|
'bar' => 'bob',
|
367
|
|
|
'baz' => 'alex',
|
368
|
|
|
];
|
369
|
|
|
|
370
|
|
|
$params = new Parameters($data);
|
371
|
|
|
|
372
|
|
|
$iterator = $params->getIterator();
|
373
|
|
|
|
374
|
|
|
$this->assertSame($data, $iterator->getArrayCopy());
|
375
|
|
|
}
|
376
|
|
|
}
|
377
|
|
|
|