1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataStructure\Container; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
6
|
|
|
|
7
|
|
|
class FlatContainerTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @dataProvider getData |
11
|
|
|
* |
12
|
|
|
* @param array $base |
13
|
|
|
* @param string $key |
14
|
|
|
* @param mixed $expected |
15
|
|
|
*/ |
16
|
|
|
public function testGet(array $base, $key, $expected) |
17
|
|
|
{ |
18
|
|
|
$cont = new FlatContainer($base); |
19
|
|
|
|
20
|
|
|
$this->assertEquals($expected, $cont->get($key)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider getData |
25
|
|
|
* |
26
|
|
|
* @param array $base |
27
|
|
|
* @param string $key |
28
|
|
|
* @param mixed $expected |
29
|
|
|
*/ |
30
|
|
|
public function testArrayGet(array $base, $key, $expected) |
31
|
|
|
{ |
32
|
|
|
$cont = new FlatContainer($base); |
33
|
|
|
|
34
|
|
|
$this->assertEquals($expected, $cont[$key]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function getData() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
[ |
44
|
|
|
['first' => 'one', 'second' => 'two'], |
45
|
|
|
'first', |
46
|
|
|
'one', |
47
|
|
|
], |
48
|
|
|
[ |
49
|
|
|
['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']], |
50
|
|
|
'second', |
51
|
|
|
['a' => 'b', 'c' => 'd'], |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']], |
55
|
|
|
'second.a', |
56
|
|
|
'b', |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']], |
60
|
|
|
'nope', |
61
|
|
|
null, |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']], |
65
|
|
|
'second.nope', |
66
|
|
|
null, |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']], |
70
|
|
|
'first.nope', |
71
|
|
|
null, |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
['indexed' => ['first', 'second']], |
75
|
|
|
'indexed.0', |
76
|
|
|
'first', |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
['top' => new Container(['a' => 'b', 'c' => 'd'])], |
80
|
|
|
'top.a', |
81
|
|
|
'b', |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
['top' => new Container(['a' => ['c' => 'd']])], |
85
|
|
|
'top.a.c', |
86
|
|
|
'd', |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider setData |
93
|
|
|
* |
94
|
|
|
* @param array $base |
95
|
|
|
* @param string $key |
96
|
|
|
* @param mixed $value |
97
|
|
|
* @param array $expected |
98
|
|
|
*/ |
99
|
|
|
public function testSet(array $base, $key, $value, array $expected) |
100
|
|
|
{ |
101
|
|
|
$cont = new FlatContainer($base); |
102
|
|
|
$cont->set($key, $value); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($expected, $cont->getAll()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @dataProvider setData |
109
|
|
|
* |
110
|
|
|
* @param array $base |
111
|
|
|
* @param string $key |
112
|
|
|
* @param mixed $value |
113
|
|
|
* @param array $expected |
114
|
|
|
*/ |
115
|
|
|
public function testArraySet(array $base, $key, $value, array $expected) |
116
|
|
|
{ |
117
|
|
|
$cont = new FlatContainer($base); |
|
|
|
|
118
|
|
|
$cont[$key] = $value; |
119
|
|
|
|
120
|
|
|
$this->assertEquals($expected, $cont->getAll()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function setData() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
[ |
130
|
|
|
[], |
131
|
|
|
'simple', |
132
|
|
|
'value', |
133
|
|
|
['simple' => 'value'], |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
[], |
137
|
|
|
'key.child', |
138
|
|
|
'value', |
139
|
|
|
['key' => ['child' => 'value']], |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
['key' => ['child' => 'one']], |
143
|
|
|
'key.child', |
144
|
|
|
'value', |
145
|
|
|
['key' => ['child' => 'value']], |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
['key' => ['child' => 'one']], |
149
|
|
|
'key.second', |
150
|
|
|
'value', |
151
|
|
|
['key' => ['child' => 'one', 'second' => 'value']], |
152
|
|
|
], |
153
|
|
|
[ |
154
|
|
|
['key' => 'node'], |
155
|
|
|
'key.second', |
156
|
|
|
'value', |
157
|
|
|
['key' => ['second' => 'value']], |
158
|
|
|
], |
159
|
|
|
[ |
160
|
|
|
['child' => new Container(['node' => 'child'])], |
161
|
|
|
'child.node', |
162
|
|
|
'value', |
163
|
|
|
['child' => new Container(['node' => 'value'])], |
164
|
|
|
], |
165
|
|
|
[ |
166
|
|
|
['child' => new Container(['node' => 'child'])], |
167
|
|
|
'child.other', |
168
|
|
|
'value', |
169
|
|
|
['child' => new Container(['node' => 'child', 'other' => 'value'])], |
170
|
|
|
], |
171
|
|
|
[ |
172
|
|
|
['child' => new Container(['node' => 'child'])], |
173
|
|
|
'child.other.more', |
174
|
|
|
'value', |
175
|
|
|
['child' => new Container(['node' => 'child', 'other' => ['more' => 'value']])], |
176
|
|
|
], |
177
|
|
|
[ |
178
|
|
|
['child' => new ImmutableContainer(['node' => 'child'])], |
179
|
|
|
'child.other.more', |
180
|
|
|
'value', |
181
|
|
|
['child' => new ImmutableContainer(['node' => 'child', 'other' => ['more' => 'value']])], |
182
|
|
|
], |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @dataProvider hasData |
188
|
|
|
* |
189
|
|
|
* @param array $base |
190
|
|
|
* @param string $key |
191
|
|
|
* @param bool $expected |
192
|
|
|
*/ |
193
|
|
|
public function testHas(array $base, $key, $expected) |
194
|
|
|
{ |
195
|
|
|
$cont = new FlatContainer($base); |
196
|
|
|
$this->assertEquals($expected, $cont->has($key)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @dataProvider hasData |
201
|
|
|
* |
202
|
|
|
* @param array $base |
203
|
|
|
* @param string $key |
204
|
|
|
* @param bool $expected |
205
|
|
|
*/ |
206
|
|
|
public function testArrayIsset(array $base, $key, $expected) |
207
|
|
|
{ |
208
|
|
|
$cont = new FlatContainer($base); |
209
|
|
|
$this->assertEquals($expected, isset($cont[$key])); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function hasData() |
216
|
|
|
{ |
217
|
|
|
return [ |
218
|
|
|
[ |
219
|
|
|
[], 'key', false, |
220
|
|
|
], |
221
|
|
|
[ |
222
|
|
|
[], 'key.nope', false, |
223
|
|
|
], |
224
|
|
|
[ |
225
|
|
|
['key' => 'value'], 'key', true, |
226
|
|
|
], |
227
|
|
|
[ |
228
|
|
|
['key' => 'value'], 'nope', false, |
229
|
|
|
], |
230
|
|
|
[ |
231
|
|
|
['key' => ['child' => 'value']], 'key', true, |
232
|
|
|
], |
233
|
|
|
[ |
234
|
|
|
['key' => ['child' => 'value']], 'key.child', true, |
235
|
|
|
], |
236
|
|
|
[ |
237
|
|
|
['key' => ['value']], 'key.value', false, |
238
|
|
|
], |
239
|
|
|
[ |
240
|
|
|
['key' => ['value']], 'key.0', true, |
241
|
|
|
], |
242
|
|
|
[ |
243
|
|
|
['key' => ['child' => 'value']], 'key.nope', false, |
244
|
|
|
], |
245
|
|
|
[ |
246
|
|
|
['key' => new Container(['child' => 'value'])], 'key.child', true, |
247
|
|
|
], |
248
|
|
|
[ |
249
|
|
|
['key' => new Container(['child' => 'value'])], 'key.nope', false, |
250
|
|
|
], |
251
|
|
|
[ |
252
|
|
|
['key' => new Container(['child' => 'value'])], 'key', true, |
253
|
|
|
], |
254
|
|
|
]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @dataProvider removeData |
259
|
|
|
* |
260
|
|
|
* @param array $base |
261
|
|
|
* @param string $key |
262
|
|
|
* @param array $expected |
263
|
|
|
*/ |
264
|
|
|
public function testRemove(array $base, $key, array $expected) |
265
|
|
|
{ |
266
|
|
|
$cont = new FlatContainer($base); |
267
|
|
|
$cont->remove($key); |
268
|
|
|
$this->assertEquals($expected, $cont->getAll()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @dataProvider removeData |
273
|
|
|
* |
274
|
|
|
* @param array $base |
275
|
|
|
* @param string $key |
276
|
|
|
* @param array $expected |
277
|
|
|
*/ |
278
|
|
|
public function testArrayUnset(array $base, $key, array $expected) |
279
|
|
|
{ |
280
|
|
|
$cont = new FlatContainer($base); |
281
|
|
|
unset($cont[$key]); |
282
|
|
|
$this->assertEquals($expected, $cont->getAll()); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
public function removeData() |
289
|
|
|
{ |
290
|
|
|
return [ |
291
|
|
|
[ |
292
|
|
|
[], 'key', [], |
293
|
|
|
], |
294
|
|
|
[ |
295
|
|
|
[], 'key.nope', [], |
296
|
|
|
], |
297
|
|
|
[ |
298
|
|
|
['key' => 'value'], 'key', [], |
299
|
|
|
], |
300
|
|
|
[ |
301
|
|
|
['key' => 'value'], 'nope', ['key' => 'value'], |
302
|
|
|
], |
303
|
|
|
[ |
304
|
|
|
['key' => ['child' => 'value']], 'key', [], |
305
|
|
|
], |
306
|
|
|
[ |
307
|
|
|
['key' => ['child' => 'value']], 'key.child', ['key' => []], |
308
|
|
|
], |
309
|
|
|
[ |
310
|
|
|
['key' => ['value']], 'key.value', ['key' => ['value']], |
311
|
|
|
], |
312
|
|
|
[ |
313
|
|
|
['key' => ['value']], 'key.0', ['key' => []], |
314
|
|
|
], |
315
|
|
|
[ |
316
|
|
|
['key' => ['child' => 'value']], 'key.nope', ['key' => ['child' => 'value']], |
317
|
|
|
], |
318
|
|
|
[ |
319
|
|
|
['key' => new Container(['child' => 'value'])], 'key', [], |
320
|
|
|
], |
321
|
|
|
[ |
322
|
|
|
['key' => new Container(['child' => 'value'])], 'key.child', ['key' => new Container([])], |
323
|
|
|
], |
324
|
|
|
[ |
325
|
|
|
['key' => new Container(['child' => 'value', 'other' => 'thing'])], 'key.child', ['key' => new Container(['other' => 'thing'])], |
|
|
|
|
326
|
|
|
], |
327
|
|
|
[ |
328
|
|
|
['key' => new ImmutableContainer(['child' => 'value'])], 'key.child', ['key' => new ImmutableContainer([])], |
|
|
|
|
329
|
|
|
], |
330
|
|
|
]; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.