1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Collections\Tests\Units\ArrayCollection; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap; |
13
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface; |
14
|
|
|
use Cubiche\Core\Collections\Tests\Units\HashMapTestCase; |
15
|
|
|
use Cubiche\Core\Equatable\Tests\Fixtures\Value; |
16
|
|
|
use Cubiche\Core\Specification\Criteria; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ArrayHashMapTests class. |
20
|
|
|
* |
21
|
|
|
* @method protected ArrayHashMapInterface randomCollection($size = null) |
22
|
|
|
* |
23
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ArrayHashMapTests extends HashMapTestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function emptyCollection() |
31
|
|
|
{ |
32
|
|
|
return new ArrayHashMap(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function randomValue() |
39
|
|
|
{ |
40
|
|
|
return new Value(\rand(0, 100)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected function uniqueValue() |
47
|
|
|
{ |
48
|
|
|
return new Value(1000); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
* Test create. |
53
|
|
|
*/ |
54
|
|
|
public function testCreate() |
55
|
|
|
{ |
56
|
|
|
$this |
57
|
|
|
->given($collection = $this->randomCollection()) |
58
|
|
|
->then() |
59
|
|
|
->hashmap($collection) |
60
|
|
|
->isInstanceOf(ArrayHashMapInterface::class) |
61
|
|
|
; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* Test get. |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function testGet() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this |
70
|
|
|
->given($collection = $this->randomCollection()) |
71
|
|
|
->and($unique = $this->uniqueValue()) |
72
|
|
|
->then() |
73
|
|
|
->variable($collection->get('foo')) |
74
|
|
|
->isNull() |
75
|
|
|
->and() |
76
|
|
|
->when($collection->set('foo', $unique)) |
77
|
|
|
->then() |
78
|
|
|
->variable($collection->get('foo')) |
79
|
|
|
->isEqualTo($unique) |
80
|
|
|
; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* |
84
|
|
|
* Test containsValue. |
85
|
|
|
*/ |
86
|
|
|
public function testContainsValue() |
87
|
|
|
{ |
88
|
|
|
$this |
89
|
|
|
->given( |
90
|
|
|
$unique = $this->uniqueValue(), |
91
|
|
|
$collection = $this->randomCollection() |
92
|
|
|
) |
93
|
|
|
->then() |
94
|
|
|
->boolean($collection->containsValue($unique)) |
95
|
|
|
->isFalse() |
96
|
|
|
->and() |
97
|
|
|
->when($collection->set('bar', $unique)) |
98
|
|
|
->then() |
99
|
|
|
->boolean($collection->containsValue($unique)) |
100
|
|
|
->isTrue() |
101
|
|
|
; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* |
105
|
|
|
* Test keys. |
106
|
|
|
*/ |
107
|
|
|
public function testKeys() |
108
|
|
|
{ |
109
|
|
|
$this |
110
|
|
|
->given($collection = $this->emptyCollection()) |
111
|
|
|
->then() |
112
|
|
|
->variable($collection->keys()->findOne(Criteria::eq('foo'))) |
113
|
|
|
->isNull() |
114
|
|
|
->variable($collection->keys()->findOne(Criteria::eq('bar'))) |
115
|
|
|
->isNull() |
116
|
|
|
->and() |
117
|
|
|
->when($collection->set('foo', 12)) |
118
|
|
|
->and($collection->set('bar', 'baz')) |
119
|
|
|
->then() |
120
|
|
|
->variable($collection->keys()->findOne(Criteria::eq('foo'))) |
121
|
|
|
->isNotNull() |
122
|
|
|
->variable($collection->keys()->findOne(Criteria::eq('bar'))) |
123
|
|
|
->isNotNull() |
124
|
|
|
; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* Test sort. |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testSort() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this |
133
|
|
|
->given( |
134
|
|
|
$comparator = $this->comparator(), |
135
|
|
|
$reverseComparator = $comparator->reverse(), |
136
|
|
|
$collection = $this->randomCollection() |
137
|
|
|
) |
138
|
|
|
->when($collection->sort()) |
139
|
|
|
->then() |
140
|
|
|
->hashmap($collection) |
141
|
|
|
->isSortedUsing($comparator) |
142
|
|
|
->and |
143
|
|
|
->when($collection->sort($reverseComparator)) |
144
|
|
|
->then() |
145
|
|
|
->hashmap($collection) |
146
|
|
|
->isSortedUsing($reverseComparator) |
147
|
|
|
->hashmap($collection) |
148
|
|
|
->isNotSortedUsing($comparator) |
149
|
|
|
; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/* |
153
|
|
|
* Test offsetUnset. |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function testOffsetUnset() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$this |
158
|
|
|
->given( |
159
|
|
|
$key = 0, |
160
|
|
|
$unique = $this->uniqueValue(), |
161
|
|
|
$collection = $this->emptyCollection() |
162
|
|
|
) |
163
|
|
|
->when($collection[$key] = $unique) |
164
|
|
|
->then() |
165
|
|
|
->variable($collection[$key]) |
166
|
|
|
->isEqualTo($unique) |
167
|
|
|
->and() |
168
|
|
|
->when(function () use ($collection, $key) { |
169
|
|
|
unset($collection[$key]); |
170
|
|
|
}) |
171
|
|
|
->then() |
172
|
|
|
->variable($collection[$key]) |
173
|
|
|
->isNull() |
174
|
|
|
; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.