1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataStructure\Container; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
6
|
|
|
|
7
|
|
|
class ImmutableContainerTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
public function testInterface() |
10
|
|
|
{ |
11
|
|
|
$cont = new ImmutableContainer(); |
12
|
|
|
|
13
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Container\ContainerInterface', $cont); |
14
|
|
|
$this->assertInstanceOf('Serializable', $cont); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
View Code Duplication |
public function testConstructor() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c']; |
20
|
|
|
$cont = new ImmutableContainer($params); |
21
|
|
|
|
22
|
|
|
$this->assertEquals($params, $cont->getAll()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b']); |
28
|
|
|
$result = $cont->add('baz', 'c'); |
29
|
|
|
|
30
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll()); |
31
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $result->getAll()); |
32
|
|
|
$this->assertNotSame($cont, $result); |
33
|
|
|
$this->assertInstanceOf(ImmutableContainer::class, $result); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @expectedException \Graze\DataStructure\Exception\RegisteredKeyException |
38
|
|
|
*/ |
39
|
|
|
public function testAddDuplicate() |
40
|
|
|
{ |
41
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
42
|
|
|
|
43
|
|
|
$cont->add('baz', 'd'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testForAll() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c']; |
49
|
|
|
$seen = []; |
50
|
|
|
|
51
|
|
|
$cont = new ImmutableContainer($params); |
52
|
|
|
$cont->forAll(function ($value, $key) use (&$seen) { |
53
|
|
|
$seen[$key] = $value; |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($params, $seen); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGet() |
60
|
|
|
{ |
61
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
62
|
|
|
|
63
|
|
|
$this->assertEquals('a', $cont->get('foo')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetMissing() |
67
|
|
|
{ |
68
|
|
|
$cont = new ImmutableContainer(); |
69
|
|
|
|
70
|
|
|
$this->assertNull($cont->get('foo')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetIterator() |
74
|
|
|
{ |
75
|
|
|
$cont = new ImmutableContainer(); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf('Iterator', $cont->getIterator()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testHasIsTrue() |
81
|
|
|
{ |
82
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
83
|
|
|
|
84
|
|
|
$this->assertTrue($cont->has('foo')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testHasIsFalse() |
88
|
|
|
{ |
89
|
|
|
$cont = new ImmutableContainer(['FOO' => 'a', 'bar' => 'b', 'baz' => 'c']); |
90
|
|
|
|
91
|
|
|
$this->assertFalse($cont->has('foo')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
97
|
|
|
$result = $cont->remove('bar'); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
100
|
|
|
$this->assertEquals(['foo' => 'a', 'baz' => 'c'], $result->getAll()); |
101
|
|
|
$this->assertNotSame($cont, $result); |
102
|
|
|
$this->assertInstanceOf(ImmutableContainer::class, $result); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testRemoveMissing() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b']); |
108
|
|
|
$result = $cont->remove('baz'); |
109
|
|
|
|
110
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll()); |
111
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $result->getAll()); |
112
|
|
|
$this->assertSame($cont, $result); |
113
|
|
|
$this->assertInstanceOf(ImmutableContainer::class, $result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testSet() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b']); |
119
|
|
|
$result = $cont->set('baz', 'c'); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll()); |
122
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $result->getAll()); |
123
|
|
|
$this->assertNotSame($cont, $result); |
124
|
|
|
$this->assertInstanceOf(ImmutableContainer::class, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testSetDuplicate() |
128
|
|
|
{ |
129
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
130
|
|
|
$result = $cont->set('baz', 'd'); |
131
|
|
|
|
132
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
133
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'd'], $result->getAll()); |
134
|
|
|
$this->assertNotSame($cont, $result); |
135
|
|
|
$this->assertInstanceOf(ImmutableContainer::class, $result); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testSerialize() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
141
|
|
|
|
142
|
|
|
$this->assertEquals( |
143
|
|
|
'C:48:"Graze\DataStructure\Container\ImmutableContainer":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}', |
144
|
|
|
serialize($cont) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testUnserialize() |
149
|
|
|
{ |
150
|
|
|
$cont = unserialize('C:48:"Graze\DataStructure\Container\ImmutableContainer":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}'); |
151
|
|
|
|
152
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function testArrayAccessUnset() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
158
|
|
|
|
159
|
|
|
unset($cont['baz']); |
160
|
|
|
$this->assertTrue($cont->has('baz')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
public function testArrayAccessSet() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$cont = new ImmutableContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
166
|
|
|
|
167
|
|
|
$cont['baz'] = 'd'; |
168
|
|
|
$this->assertEquals('c', $cont->get('baz')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function testExtractedChildDoesNotModifyParent() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$cont = new ImmutableContainer(['a' => 'b', 'c' => new Container(['d' => 'e'])]); |
174
|
|
|
|
175
|
|
|
$child = $cont->get('c'); |
176
|
|
|
|
177
|
|
|
$child->set('d', 'f'); |
178
|
|
|
|
179
|
|
|
$this->assertEquals( |
180
|
|
|
['a' => 'b', 'c' => new Container(['d' => 'e'])], |
181
|
|
|
$cont->getAll(), |
182
|
|
|
'modifying a child object should not modify the parent container' |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.