1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataStructure\Container; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
7
|
|
|
use Serializable; |
8
|
|
|
use Traversable; |
9
|
|
|
|
10
|
|
|
class ContainerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testInterface() |
13
|
|
|
{ |
14
|
|
|
$cont = new Container(); |
15
|
|
|
|
16
|
|
|
$this->assertInstanceOf(ContainerInterface::class, $cont); |
17
|
|
|
$this->assertInstanceOf(Serializable::class, $cont); |
18
|
|
|
$this->assertInstanceOf(ArrayAccess::class, $cont); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testConstructor() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c']; |
24
|
|
|
$cont = new Container($params); |
25
|
|
|
|
26
|
|
|
$this->assertEquals($params, $cont->getAll()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testAdd() |
30
|
|
|
{ |
31
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b']); |
32
|
|
|
$result = $cont->add('baz', 'c'); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
35
|
|
|
$this->assertSame($cont, $result); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @expectedException \Graze\DataStructure\Exception\RegisteredKeyException |
40
|
|
|
*/ |
41
|
|
|
public function testAddDuplicate() |
42
|
|
|
{ |
43
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
44
|
|
|
|
45
|
|
|
$cont->add('baz', 'd'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testForAll() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c']; |
51
|
|
|
$seen = []; |
52
|
|
|
|
53
|
|
|
$cont = new Container($params); |
54
|
|
|
$cont->forAll(function ($value, $key) use (&$seen) { |
55
|
|
|
$seen[$key] = $value; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$this->assertEquals($params, $seen); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGet() |
62
|
|
|
{ |
63
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
64
|
|
|
|
65
|
|
|
$this->assertEquals('a', $cont->get('foo')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetMissing() |
69
|
|
|
{ |
70
|
|
|
$cont = new Container(); |
71
|
|
|
|
72
|
|
|
$this->assertNull($cont->get('foo')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetIterator() |
76
|
|
|
{ |
77
|
|
|
$cont = new Container(); |
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf('Iterator', $cont->getIterator()); |
80
|
|
|
$this->assertInstanceOf(Traversable::class, $cont); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testIteration() |
84
|
|
|
{ |
85
|
|
|
$cont = new Container(['a' => 'b', 'c' => 'd']); |
86
|
|
|
|
87
|
|
|
foreach ($cont as $key => $value) { |
88
|
|
|
switch ($key) { |
89
|
|
|
case 'a': |
90
|
|
|
$this->assertEquals('b', $value); |
91
|
|
|
break; |
92
|
|
|
case 'c': |
93
|
|
|
$this->assertEquals('d', $value); |
94
|
|
|
break; |
95
|
|
|
default: |
96
|
|
|
$this->fail('unknown key, expecting `a` or `c`'); |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testHasIsTrue() |
103
|
|
|
{ |
104
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
105
|
|
|
|
106
|
|
|
$this->assertTrue($cont->has('foo')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testHasIsFalse() |
110
|
|
|
{ |
111
|
|
|
$cont = new Container(['FOO' => 'a', 'bar' => 'b', 'baz' => 'c']); |
112
|
|
|
|
113
|
|
|
$this->assertFalse($cont->has('foo')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
119
|
|
|
$result = $cont->remove('bar'); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(['foo' => 'a', 'baz' => 'c'], $cont->getAll()); |
122
|
|
|
$this->assertSame($cont, $result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public function testRemoveMissing() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b']); |
128
|
|
|
$result = $cont->remove('baz'); |
129
|
|
|
|
130
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll()); |
131
|
|
|
$this->assertSame($cont, $result); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testSet() |
135
|
|
|
{ |
136
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b']); |
137
|
|
|
$result = $cont->set('baz', 'c'); |
138
|
|
|
|
139
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
140
|
|
|
$this->assertSame($cont, $result); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testSetDuplicate() |
144
|
|
|
{ |
145
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
146
|
|
|
$result = $cont->set('baz', 'd'); |
147
|
|
|
|
148
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'd'], $cont->getAll()); |
149
|
|
|
$this->assertSame($cont, $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function testSerialize() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
155
|
|
|
|
156
|
|
|
$this->assertEquals( |
157
|
|
|
'C:39:"Graze\DataStructure\Container\Container":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}', |
158
|
|
|
serialize($cont) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testUnserialize() |
163
|
|
|
{ |
164
|
|
|
$cont = unserialize('C:39:"Graze\DataStructure\Container\Container":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}'); |
165
|
|
|
|
166
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testArrayGet() |
170
|
|
|
{ |
171
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
172
|
|
|
|
173
|
|
|
$this->assertEquals('a', $cont['foo']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testArraySet() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b']); |
179
|
|
|
$cont['baz'] = 'c'; |
180
|
|
|
|
181
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testArrayHasIsTrue() |
185
|
|
|
{ |
186
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
187
|
|
|
|
188
|
|
|
$this->assertTrue(isset($cont['foo'])); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testArrayHasIsFalse() |
192
|
|
|
{ |
193
|
|
|
$cont = new Container(['FOO' => 'a', 'bar' => 'b', 'baz' => 'c']); |
194
|
|
|
|
195
|
|
|
$this->assertFalse(isset($cont['foo'])); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
View Code Duplication |
public function testArrayRemove() |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']); |
201
|
|
|
unset($cont['bar']); |
202
|
|
|
|
203
|
|
|
$this->assertEquals(['foo' => 'a', 'baz' => 'c'], $cont->getAll()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testArrayRemoveMissing() |
207
|
|
|
{ |
208
|
|
|
$cont = new Container(['foo' => 'a', 'bar' => 'b']); |
209
|
|
|
unset($cont['baz']); |
210
|
|
|
|
211
|
|
|
$this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll()); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.