Skip to content

Commit b9271b6

Browse files
committed
Add tests for numeric array access
Confirms the requested behavior of issue #8
1 parent f0c701c commit b9271b6

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tests/DataTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected function runSampleDataTests(DataInterface $data)
6161
$this->assertEquals('D3', $data->get('b.d.d3'));
6262
$this->assertEquals('D3', $data->get('b/d/d3'));
6363
$this->assertEquals(['c1', 'c2', 'c3'], $data->get('c'));
64+
$this->assertEquals('c1', $data->get('c.0'));
65+
$this->assertEquals('c2', $data->get('c/1'));
6466
$this->assertNull($data->get('foo', null), 'Foo should not exist');
6567
$this->assertNull($data->get('f.g.h.i', null));
6668
$this->assertNull($data->get('f/g/h/i', null));
@@ -93,6 +95,7 @@ public function testAppend()
9395

9496
$this->assertEquals(['A', 'B'], $data->get('a'));
9597
$this->assertEquals(['c1', 'c2', 'c3', 'c4'], $data->get('c'));
98+
$this->assertEquals('c4', $data->get('c.3'));
9699
$this->assertEquals(['C1', 'C2', 'C3', 'C4'], $data->get('b.c'));
97100
$this->assertEquals(['D3', 'D3b'], $data->get('b.d.d3'));
98101
$this->assertEquals(['D'], $data->get('b.d.d4'));
@@ -115,16 +118,19 @@ public function testSet()
115118
$this->assertNull($data->get('a', null));
116119
$this->assertNull($data->get('b/c', null));
117120
$this->assertNull($data->get('d.e', null));
121+
$this->assertNull($data->get('c.3', null));
118122

119123
$data->set('a', 'A');
120124
$data->set('b/c', 'C');
121125
$data->set('d.e', ['f' => 'F', 'g' => 'G']);
126+
$data->set('c.3', 'c4');
122127

123128
$this->assertEquals('A', $data->get('a'));
124129
$this->assertEquals(['c' => 'C'], $data->get('b'));
125130
$this->assertEquals('C', $data->get('b.c'));
126131
$this->assertEquals('F', $data->get('d/e/f'));
127132
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
133+
$this->assertEquals('c4', $data->get('c.3'));
128134

129135
$data->set('a', null);
130136
$this->assertTrue($data->has('a'), 'Data should exist with a null value');
@@ -156,6 +162,7 @@ public function testRemove()
156162
$data->remove('d');
157163
$data->remove('d.e.f');
158164
$data->remove('empty.path');
165+
$data->remove('c.2');
159166

160167
$this->assertFalse($data->has('a'));
161168
$this->assertNull($data->get('a', null));
@@ -165,6 +172,8 @@ public function testRemove()
165172
$this->assertNull($data->get('b.d.d3', null));
166173
$this->assertNull(null);
167174
$this->assertEquals('D2', $data->get('b.d.d2'));
175+
$this->assertFalse($data->has('c.2'));
176+
$this->assertNull($data->get('c.2', null));
168177

169178
$this->expectException(InvalidPathException::class);
170179
$this->expectExceptionMessage('Path cannot be an empty string');
@@ -198,13 +207,13 @@ public function testHas()
198207
$data = new Data($this->getSampleData());
199208

200209
foreach (
201-
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n'] as $existentKey
210+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n', 'c.1'] as $existentKey
202211
) {
203212
$this->assertTrue($data->has($existentKey));
204213
}
205214

206215
foreach (
207-
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1'] as $notExistentKey
216+
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1', 'c.4'] as $notExistentKey
208217
) {
209218
$this->assertFalse($data->has($notExistentKey));
210219
}
@@ -266,13 +275,13 @@ public function testOffsetExists()
266275
$data = new Data($this->getSampleData());
267276

268277
foreach (
269-
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n'] as $existentKey
278+
['a', 'i', 'b.d', 'b/d', 'f.g.h', 'f/g/h', 'h.i', 'h/i', 'b.d.d1', 'b/d/d1', 'n', 'n2/n', 'c.2'] as $existentKey
270279
) {
271280
$this->assertTrue(isset($data[$existentKey]));
272281
}
273282

274283
foreach (
275-
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1'] as $notExistentKey
284+
['p', 'b.b1', 'b/b1', 'b.c.C1', 'b/c/C1', 'h.i.I', 'h/i/I', 'b.d.d1.D1', 'b/d/d1/D1', 'c.4'] as $notExistentKey
276285
) {
277286
$this->assertFalse(isset($data[$notExistentKey]));
278287
}
@@ -296,6 +305,7 @@ public function testOffsetGet()
296305
$this->assertEquals('D3', $data['b.d.d3']);
297306
$this->assertEquals('D3', $data['b/d/d3']);
298307
$this->assertEquals(['c1', 'c2', 'c3'], $data['c']);
308+
$this->assertEquals('c3', $data['c.2']);
299309
$this->assertNull($data['foo'], 'Foo should not exist');
300310
$this->assertNull($data['f.g.h.i']);
301311
$this->assertNull($data['f/g/h/i']);
@@ -315,13 +325,16 @@ public function testOffsetSet()
315325
$data['b/c'] = 'C';
316326
$data['d.e'] = ['f' => 'F', 'g' => 'G'];
317327
$data['foo'] = null;
328+
$data['x.1'] = 'foo';
318329

319330
$this->assertEquals('A', $data['a']);
320331
$this->assertEquals(['c' => 'C'], $data['b']);
321332
$this->assertEquals('C', $data['b.c']);
322333
$this->assertEquals('F', $data['d/e/f']);
323334
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
324335
$this->assertNull($data['foo']);
336+
$this->assertEquals([1 => 'foo'], $data['x']);
337+
$this->assertEquals('foo', $data['x.1']);
325338

326339
$this->expectException(InvalidPathException::class);
327340
$this->expectExceptionMessage('Path cannot be an empty string');
@@ -338,12 +351,14 @@ public function testOffsetUnset()
338351
unset($data['d']);
339352
unset($data['d.e.f']);
340353
unset($data['empty.path']);
354+
unset($data['c.2']);
341355

342356
$this->assertNull($data['a']);
343357
$this->assertNull($data['b.c']);
344358
$this->assertNull($data['b/d/d3']);
345359
$this->assertNull(null);
346360
$this->assertEquals('D2', $data['b.d.d2']);
361+
$this->assertNull($data['c.2']);
347362

348363
$this->assertTrue($data->has('n'));
349364
$this->assertNull($data->get('n'));

0 commit comments

Comments
 (0)