1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Novactive Collection. |
4
|
|
|
* |
5
|
|
|
* @author Luke Visinoni <[email protected], [email protected]> |
6
|
|
|
* @author Sébastien Morel <[email protected], [email protected]> |
7
|
|
|
* @copyright 2017 Novactive |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Novactive\Tests; |
12
|
|
|
|
13
|
|
|
use Novactive\Collection\Factory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ArrayAccessCollectionTest. |
17
|
|
|
*/ |
18
|
|
|
class ArrayAccessCollectionTest extends UnitTestCase |
19
|
|
|
{ |
20
|
|
|
public function testArrayAccessUnsetRemovesItemByKeyAndReturnsNull() |
21
|
|
|
{ |
22
|
|
|
$exp = $this->fixtures['assoc']; |
23
|
|
|
$coll = Factory::create($exp); |
24
|
|
|
$this->assertTrue($coll->containsKey('2nd')); |
25
|
|
|
$removed = $coll->offsetUnset('2nd'); |
26
|
|
|
$this->assertFalse($coll->containsKey('2nd')); |
27
|
|
|
$this->assertNull($removed, 'The ArrayAccess interface expects offsetUnset to have no return value.'); |
28
|
|
|
$this->assertTrue($coll->containsKey('3rd')); |
29
|
|
|
unset($coll['3rd']); |
30
|
|
|
$this->assertFalse($coll->containsKey('3rd')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testArrayAccessOffsetExistsAllowsIssetToWorkWithSquareBrackets() |
34
|
|
|
{ |
35
|
|
|
// associative |
36
|
|
|
$exp = $this->fixtures['assoc']; |
37
|
|
|
$coll = Factory::create($exp); |
38
|
|
|
$this->assertTrue($coll->containsKey('2nd')); |
39
|
|
|
$this->assertTrue($coll->offsetExists('2nd'), 'Collection::offsetExists should return true if index exists.'); |
40
|
|
|
$this->assertTrue( |
41
|
|
|
isset($coll['2nd']), |
42
|
|
|
'Collection::offsetExists should allow for the use of isset() on a collection using square brackets.' |
43
|
|
|
); |
44
|
|
|
$this->assertFalse($coll->containsKey('4th')); |
45
|
|
|
$this->assertFalse( |
46
|
|
|
$coll->offsetExists('4th'), |
47
|
|
|
'Collection::offsetExists should return false if index does not exist.' |
48
|
|
|
); |
49
|
|
|
$this->assertFalse( |
50
|
|
|
isset($coll['4th']), |
51
|
|
|
'Collection::offsetExists should allow for the use of isset() on a '. |
52
|
|
|
'collection using square brackets for an index that does not exist.' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
// numeric |
56
|
|
|
$exp = $this->fixtures['array']; |
57
|
|
|
$coll = Factory::create($exp); |
58
|
|
|
$this->assertTrue($coll->containsKey(1)); |
59
|
|
|
$this->assertTrue( |
60
|
|
|
$coll->offsetExists(1), |
61
|
|
|
'Collection::offsetExists should return true if numeric offset exists.' |
62
|
|
|
); |
63
|
|
|
$this->assertTrue( |
64
|
|
|
isset($coll[1]), |
65
|
|
|
'Collection::offsetExists should allow for the use of isset() on a collection using '. |
66
|
|
|
'square brackets and numeric offset.' |
67
|
|
|
); |
68
|
|
|
$this->assertFalse($coll->containsKey(5)); |
69
|
|
|
$this->assertFalse( |
70
|
|
|
$coll->offsetExists(5), |
71
|
|
|
'Collection::offsetExists should return false if numeric offset does not exist.' |
72
|
|
|
); |
73
|
|
|
$this->assertFalse( |
74
|
|
|
isset($coll[5]), |
75
|
|
|
'Collection::offsetExists should allow for the use of isset() on a collection using square brackets '. |
76
|
|
|
'for a numeric offset that does not exist.' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testArrayAccessOffsetSetAllowsUseOfSquareBracketsForSetting() |
81
|
|
|
{ |
82
|
|
|
// associative |
83
|
|
|
$exp = $this->fixtures['assoc']; |
84
|
|
|
$coll = Factory::create($exp); |
85
|
|
|
$this->assertFalse($coll->containsKey('foo')); |
86
|
|
|
$coll['foo'] = 'bar'; |
87
|
|
|
$this->assertTrue($coll->containsKey('foo')); |
88
|
|
|
$this->assertEquals('bar', $coll->get('foo')); |
89
|
|
|
|
90
|
|
|
$this->assertFalse($coll->containsKey('boo')); |
91
|
|
|
$this->assertNull($coll->offsetSet('boo', 'far'), 'ArrayAccess offsetSet MUST NOT have a return value.'); |
92
|
|
|
$this->assertTrue($coll->containsKey('boo')); |
93
|
|
|
$this->assertEquals('far', $coll->get('boo')); |
94
|
|
|
|
95
|
|
|
// numeric |
96
|
|
|
$exp = $this->fixtures['array']; |
97
|
|
|
$coll = Factory::create($exp); |
98
|
|
|
$this->assertFalse($coll->containsKey(5)); |
99
|
|
|
$coll[5] = 'bar'; |
100
|
|
|
$this->assertTrue($coll->containsKey(5)); |
101
|
|
|
$this->assertEquals('bar', $coll->get(5)); |
102
|
|
|
|
103
|
|
|
$this->assertFalse($coll->containsKey('boo')); |
104
|
|
|
$this->assertNull($coll->offsetSet(6, 'far'), 'ArrayAccess offsetSet MUST NOT have a return value.'); |
105
|
|
|
$this->assertTrue($coll->containsKey(6)); |
106
|
|
|
$this->assertEquals('far', $coll->get(6)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testArrayAccessOffsetSetAllowsUseOfSquareBracketsForSettingWithoutIndex() |
110
|
|
|
{ |
111
|
|
|
// associative |
112
|
|
|
$assoc = $this->fixtures['assoc']; |
113
|
|
|
$aColl = Factory::create($assoc); |
114
|
|
|
$this->assertFalse($aColl->containsKey(0)); |
115
|
|
|
$aColl[] = 'test'; |
116
|
|
|
$this->assertTrue($aColl->containsKey(0)); |
117
|
|
|
|
118
|
|
|
// numeric |
119
|
|
|
$arr = $this->fixtures['array']; |
120
|
|
|
$arrColl = Factory::create($arr); |
121
|
|
|
$this->assertTrue($arrColl->containsKey(0)); |
122
|
|
|
$this->assertTrue($arrColl->containsKey(1)); |
123
|
|
|
$this->assertTrue($arrColl->containsKey(2)); |
124
|
|
|
$this->assertFalse($arrColl->containsKey(3)); |
125
|
|
|
$arrColl[] = 'test'; |
126
|
|
|
$this->assertTrue($arrColl->containsKey(3)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testArrayAccessOffsetGetAllowsUseOfSquareBracketsForGetting() |
130
|
|
|
{ |
131
|
|
|
$exp = $this->fixtures['assoc']; |
132
|
|
|
$coll = Factory::create($exp); |
133
|
|
|
$this->assertEquals('first', $coll->offsetGet('1st')); |
134
|
|
|
$this->assertEquals('first', $coll['1st']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \RuntimeException |
139
|
|
|
* @expectedExceptionMessage Unknown offset: foo |
140
|
|
|
*/ |
141
|
|
|
public function testArrayAccessOffsetGetThrowsExceptionIfIndexDoesNotExist() |
142
|
|
|
{ |
143
|
|
|
$exp = $this->fixtures['assoc']; |
144
|
|
|
$coll = Factory::create($exp); |
145
|
|
|
$foo = $coll['foo']; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|