|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\EventDispatcher\Event; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\Event\ImmutableParameters; |
|
8
|
|
|
use Arp\EventDispatcher\Event\Parameters; |
|
9
|
|
|
use Arp\EventDispatcher\Event\ParametersInterface; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @covers \Arp\EventDispatcher\Event\ImmutableParameters |
|
15
|
|
|
* |
|
16
|
|
|
* @author Alex Patterson <[email protected]> |
|
17
|
|
|
* @package ArpTest\EventDispatcher\Event |
|
18
|
|
|
*/ |
|
19
|
|
|
final class ImmutableParametersTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ParametersInterface&MockObject |
|
23
|
|
|
*/ |
|
24
|
|
|
private $parameters; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Prepare the test case dependencies |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setUp(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->parameters = $this->createMock(ParametersInterface::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Assert that the class implements the ParametersInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testImplementsParametersInterface(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$params = new ImmutableParameters($this->parameters); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertInstanceOf(ParametersInterface::class, $params); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Assert that the class implements \ArrayAccess |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testImplementsArrayAccess(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$params = new ImmutableParameters($this->parameters); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertInstanceOf(\ArrayAccess::class, $params); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Assert that parameters can be fetched via getParams() but attempts to setParams() will be ignored |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testGetParameters(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$data = [ |
|
60
|
|
|
'hello' => 123, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
$this->parameters->expects($this->exactly(2)) |
|
64
|
|
|
->method('getParams') |
|
65
|
|
|
->willReturnOnConsecutiveCalls([], $data); |
|
66
|
|
|
|
|
67
|
|
|
$this->parameters->expects($this->never())->method('setParams'); |
|
68
|
|
|
|
|
69
|
|
|
$params = new ImmutableParameters($this->parameters); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEmpty($params->getParams()); |
|
72
|
|
|
|
|
73
|
|
|
// Ignore updates to set params with new data |
|
74
|
|
|
$params->setParams( |
|
75
|
|
|
[ |
|
76
|
|
|
'foo' => 'bar', |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals($data, $params->getParams()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Assert that calls to count() will proxy to the internal parameters collection |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testCountWillReturnParameterCount(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$parameters = new ImmutableParameters($this->parameters); |
|
89
|
|
|
|
|
90
|
|
|
$this->parameters->expects($this->once()) |
|
91
|
|
|
->method('count') |
|
92
|
|
|
->willReturn(5); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertSame(5, $parameters->count()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Assert that calls to isEmpty() will proxy to the internal parameters collection |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testIsEmptyWillProxyToInternalParametersCollection(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters([])); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertTrue($immutableParameters->isEmpty()); |
|
105
|
|
|
|
|
106
|
|
|
$immutableParameters = new ImmutableParameters( |
|
107
|
|
|
new Parameters( |
|
108
|
|
|
[ |
|
109
|
|
|
'foo' => 123, |
|
110
|
|
|
] |
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertFalse($immutableParameters->isEmpty()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Assert that calls to hasParam() will proxy to the internal parameters collection |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testHasParamWillProxyToInternalParametersCollection(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$immutableParameters = new ImmutableParameters( |
|
123
|
|
|
new Parameters( |
|
124
|
|
|
[ |
|
125
|
|
|
'foo' => 123, |
|
126
|
|
|
'bar' => true, |
|
127
|
|
|
] |
|
128
|
|
|
) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertTrue($immutableParameters->hasParam('foo')); |
|
132
|
|
|
$this->assertTrue($immutableParameters->hasParam('bar')); |
|
133
|
|
|
$this->assertFalse($immutableParameters->hasParam('baz')); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Assert that calls to getParam() will proxy to the internal parameters collection |
|
138
|
|
|
*/ |
|
139
|
|
|
public function testGetParamWillProxyToInternalParametersCollection(): void |
|
140
|
|
|
{ |
|
141
|
|
|
$immutableParameters = new ImmutableParameters( |
|
142
|
|
|
new Parameters( |
|
143
|
|
|
[ |
|
144
|
|
|
'foo' => 456, |
|
145
|
|
|
'bar' => 'Hello World!', |
|
146
|
|
|
] |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertSame(456, $immutableParameters->getParam('foo')); |
|
151
|
|
|
$this->assertSame('Hello World!', $immutableParameters->getParam('bar')); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertNull($immutableParameters->getParam('baz')); |
|
154
|
|
|
$this->assertFalse($immutableParameters->getParam('baz', false)); |
|
155
|
|
|
$this->assertSame(123, $immutableParameters->getParam('test', 123)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Assert that calls to removeParam() will NOT modify the internal parameters |
|
160
|
|
|
*/ |
|
161
|
|
|
public function testRemoveParamWillReturnFalse(): void |
|
162
|
|
|
{ |
|
163
|
|
|
$params = ['test' => 123]; |
|
164
|
|
|
|
|
165
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters($params)); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertFalse($immutableParameters->removeParam('test')); |
|
168
|
|
|
$this->assertSame($params, $immutableParameters->getParams()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Assert that calls to getKeys() will return the keys of the internal parameters collection |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testGetKeysWillProxyToInternalParametersCollection(): void |
|
175
|
|
|
{ |
|
176
|
|
|
$params = [ |
|
177
|
|
|
'foo' => 'Hello', |
|
178
|
|
|
'bar' => 'test', |
|
179
|
|
|
'baz' => 123, |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters($params)); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertSame(array_keys($params), $immutableParameters->getKeys()); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Assert that calls to getValues() will return the values of the internal parameters collection |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testGetValuesWillProxyToInternalParametersCollection(): void |
|
191
|
|
|
{ |
|
192
|
|
|
$params = [ |
|
193
|
|
|
'foo' => 'Hello', |
|
194
|
|
|
'bar' => 'test', |
|
195
|
|
|
'baz' => 123, |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters($params)); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertSame(array_values($params), $immutableParameters->getValues()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Assert that calls to getValues() will return the values of the internal parameters collection |
|
205
|
|
|
*/ |
|
206
|
|
|
public function testGetIteratorWillProxyToInternalParametersCollection(): void |
|
207
|
|
|
{ |
|
208
|
|
|
$iterator = new \ArrayIterator( |
|
209
|
|
|
[ |
|
210
|
|
|
'foo' => 'Hello', |
|
211
|
|
|
'bar' => 'test', |
|
212
|
|
|
'baz' => 123, |
|
213
|
|
|
] |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$this->parameters->expects($this->once()) |
|
217
|
|
|
->method('getIterator') |
|
218
|
|
|
->willReturn($iterator); |
|
219
|
|
|
|
|
220
|
|
|
$immutableParameters = new ImmutableParameters($this->parameters); |
|
221
|
|
|
|
|
222
|
|
|
$this->assertSame($iterator, $immutableParameters->getIterator()); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Assert that calls to offsetExists() will test the keys of the internal parameters collection |
|
227
|
|
|
*/ |
|
228
|
|
|
public function testOffsetExistsWillProxyToInternalParametersCollection(): void |
|
229
|
|
|
{ |
|
230
|
|
|
$params = [ |
|
231
|
|
|
'foo' => 'Hello', |
|
232
|
|
|
'bar' => 'test', |
|
233
|
|
|
'baz' => 123, |
|
234
|
|
|
]; |
|
235
|
|
|
|
|
236
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters($params)); |
|
237
|
|
|
|
|
238
|
|
|
$this->assertTrue(isset($immutableParameters['foo'])); |
|
239
|
|
|
$this->assertTrue(isset($immutableParameters['bar'])); |
|
240
|
|
|
$this->assertFalse(isset($immutableParameters['test'])); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Assert that calls to offsetExists() will test the keys of the internal parameters collection |
|
245
|
|
|
*/ |
|
246
|
|
|
public function testOffsetGetWillProxyToInternalParametersCollection(): void |
|
247
|
|
|
{ |
|
248
|
|
|
$params = [ |
|
249
|
|
|
'foo' => 'testing', |
|
250
|
|
|
'bar' => 999, |
|
251
|
|
|
'baz' => 3.14, |
|
252
|
|
|
]; |
|
253
|
|
|
|
|
254
|
|
|
$immutableParameters = new ImmutableParameters(new Parameters($params)); |
|
255
|
|
|
|
|
256
|
|
|
$this->assertSame($params['foo'], $immutableParameters['foo']); |
|
257
|
|
|
$this->assertSame($params['bar'], $immutableParameters['bar']); |
|
258
|
|
|
$this->assertSame($params['baz'], $immutableParameters['baz']); |
|
259
|
|
|
$this->assertNull($immutableParameters['test']); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|