1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataStructure\Collection; |
4
|
|
|
|
5
|
|
|
use Graze\Sort as s; |
6
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
7
|
|
|
|
8
|
|
|
class ImmutableCollectionTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testInterface() |
11
|
|
|
{ |
12
|
|
|
$coll = new ImmutableCollection(); |
13
|
|
|
|
14
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\CollectionInterface', $coll); |
15
|
|
|
$this->assertInstanceOf('Serializable', $coll); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testConstructor() |
19
|
|
|
{ |
20
|
|
|
$items = ['foo', 'bar', 'baz']; |
21
|
|
|
$coll = new ImmutableCollection($items); |
22
|
|
|
|
23
|
|
|
$this->assertEquals($items, $coll->getAll()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testAdd() |
27
|
|
|
{ |
28
|
|
|
$coll = new ImmutableCollection(['foo', 'bar']); |
29
|
|
|
$result = $coll->add('baz'); |
30
|
|
|
|
31
|
|
|
$this->assertEquals(['foo', 'bar'], $coll->getAll()); |
32
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $result->getAll()); |
33
|
|
|
$this->assertNotSame($coll, $result); |
34
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testAddDuplicate() |
38
|
|
|
{ |
39
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
40
|
|
|
$result = $coll->add('baz'); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $coll->getAll()); |
43
|
|
|
$this->assertEquals(['foo', 'bar', 'baz', 'baz'], $result->getAll()); |
44
|
|
|
$this->assertNotSame($coll, $result); |
45
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testContainsIsTrue() |
49
|
|
|
{ |
50
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
51
|
|
|
|
52
|
|
|
$this->assertTrue($coll->contains('foo')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testContainsIsFalse() |
56
|
|
|
{ |
57
|
|
|
$coll = new ImmutableCollection(['FOO', 'bar', 'baz']); |
58
|
|
|
|
59
|
|
|
$this->assertFalse($coll->contains('foo')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testContainsIsStrict() |
63
|
|
|
{ |
64
|
|
|
$coll = new ImmutableCollection([0, 1]); |
65
|
|
|
|
66
|
|
|
$this->assertFalse($coll->contains(false)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testCount() |
70
|
|
|
{ |
71
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
72
|
|
|
|
73
|
|
|
$this->assertEquals(3, count($coll)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testFilter() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
79
|
|
|
$result = $coll->filter(function ($item) { |
80
|
|
|
return 'foo' !== $item; |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $coll->getAll()); |
84
|
|
|
$this->assertEquals(['bar', 'baz'], $result->getAll()); |
85
|
|
|
$this->assertNotSame($coll, $result); |
86
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testGetIterator() |
90
|
|
|
{ |
91
|
|
|
$coll = new ImmutableCollection(); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf('Iterator', $coll->getIterator()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testMap() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
99
|
|
|
$result = $coll->map(function ($item) { |
100
|
|
|
return $item[0]; |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(['f', 'b', 'b'], $result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function testReduce() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$coll = new ImmutableCollection(['foo', 'bar', 'baz']); |
109
|
|
|
$result = $coll->reduce(function ($carry, $item) { |
110
|
|
|
return $carry . $item; |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
$this->assertEquals('foobarbaz', $result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testSort() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$coll = new ImmutableCollection([2, 3, 1]); |
119
|
|
|
$result = $coll->sort(s\comparison_fn(function ($item) { |
120
|
|
|
return $item; |
121
|
|
|
})); |
122
|
|
|
|
123
|
|
|
$this->assertEquals([2, 3, 1], $coll->getAll()); |
124
|
|
|
$this->assertEquals([1, 2, 3], $result->getAll()); |
125
|
|
|
$this->assertNotSame($coll, $result); |
126
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testSortOnAsc() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$coll = new ImmutableCollection([2, 3, 1]); |
132
|
|
|
$result = $coll->sortOn(function ($item) { |
133
|
|
|
return $item; |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
$this->assertEquals([2, 3, 1], $coll->getAll()); |
137
|
|
|
$this->assertEquals([1, 2, 3], $result->getAll()); |
138
|
|
|
$this->assertNotSame($coll, $result); |
139
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function testSortOnDesc() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$coll = new ImmutableCollection([2, 3, 1]); |
145
|
|
|
$result = $coll->sortOn(function ($item) { |
146
|
|
|
return $item; |
147
|
|
|
}, s\DESC); |
148
|
|
|
|
149
|
|
|
$this->assertEquals([2, 3, 1], $coll->getAll()); |
150
|
|
|
$this->assertEquals([3, 2, 1], $result->getAll()); |
151
|
|
|
$this->assertNotSame($coll, $result); |
152
|
|
|
$this->assertInstanceOf('Graze\DataStructure\Collection\ImmutableCollection', $result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testSerialize() |
156
|
|
|
{ |
157
|
|
|
$cont = new ImmutableCollection(['foo', 'bar', 'baz']); |
158
|
|
|
|
159
|
|
|
$this->assertEquals('C:50:"Graze\DataStructure\Collection\ImmutableCollection":48:{a:3:{i:0;s:3:"foo";i:1;s:3:"bar";i:2;s:3:"baz";}}', serialize($cont)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testUnserialize() |
163
|
|
|
{ |
164
|
|
|
$cont = unserialize('C:50:"Graze\DataStructure\Collection\ImmutableCollection":48:{a:3:{i:0;s:3:"foo";i:1;s:3:"bar";i:2;s:3:"baz";}}'); |
165
|
|
|
|
166
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $cont->getAll()); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.