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; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\Exception\InvalidKeyException; |
13
|
|
|
use Cubiche\Core\Collections\HashMapInterface; |
14
|
|
|
use Cubiche\Core\Specification\Criteria; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* HashMapTestCase class. |
18
|
|
|
* |
19
|
|
|
* @method protected HashMapInterface emptyCollection() |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class HashMapTestCase extends CollectionTestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param int $size |
28
|
|
|
* |
29
|
|
|
* @return HashMapInterface |
30
|
|
|
*/ |
31
|
|
|
protected function randomCollection($size = null) |
32
|
|
|
{ |
33
|
|
|
$collection = $this->emptyCollection(); |
34
|
|
|
foreach ($this->randomValues($size) as $key => $randomValue) { |
35
|
|
|
$collection->set($key, $randomValue); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $collection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test class. |
43
|
|
|
*/ |
44
|
|
|
public function testClass() |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->testedClass |
48
|
|
|
->implements(HashMapInterface::class) |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Test set. |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function testSet() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this |
58
|
|
|
->given($collection = $this->emptyCollection()) |
59
|
|
|
->and($unique = $this->uniqueValue()) |
60
|
|
|
->then() |
61
|
|
|
->hashmap($collection) |
62
|
|
|
->notContainsKey('foo') |
63
|
|
|
->and() |
64
|
|
|
->when($collection->set('foo', $unique)) |
65
|
|
|
->then() |
66
|
|
|
->hashmap($collection) |
67
|
|
|
->containsKey('foo') |
68
|
|
|
->exception(function () use ($collection) { |
69
|
|
|
$collection->set(new \StdClass(), 'value'); |
70
|
|
|
})->isInstanceOf(InvalidKeyException::class) |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/* |
75
|
|
|
* Test removeAt. |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function testRemoveAt() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this |
80
|
|
|
->given( |
81
|
|
|
$unique = $this->uniqueValue(), |
82
|
|
|
$collection = $this->emptyCollection() |
83
|
|
|
) |
84
|
|
|
->then() |
85
|
|
|
->hashmap($collection) |
86
|
|
|
->notContainsKey('foo') |
87
|
|
|
->and() |
88
|
|
|
->when($collection->set('foo', $unique)) |
89
|
|
|
->then() |
90
|
|
|
->hashmap($collection) |
91
|
|
|
->containsKey('foo') |
92
|
|
|
->and() |
93
|
|
|
->when($element = $collection->removeAt('bar')) |
94
|
|
|
->then() |
95
|
|
|
->variable($element) |
96
|
|
|
->isNull() |
97
|
|
|
->and() |
98
|
|
|
->when($element = $collection->removeAt('foo')) |
99
|
|
|
->then() |
100
|
|
|
->variable($element) |
101
|
|
|
->isEqualTo($unique) |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test find. |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testFind() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
parent::testFind(); |
111
|
|
|
|
112
|
|
|
$this |
113
|
|
|
->given( |
114
|
|
|
$unique = $this->uniqueValue(), |
115
|
|
|
$criteria = Criteria::same($unique), |
116
|
|
|
$emptyCollection = $this->emptyCollection() |
117
|
|
|
) |
118
|
|
|
->when($emptyCollection->set('foo', $unique)) |
119
|
|
|
->and($findResult = $emptyCollection->find($criteria)) |
120
|
|
|
->then() |
121
|
|
|
->hashmap($findResult) |
122
|
|
|
->size() |
123
|
|
|
->isEqualTo(1) |
124
|
|
|
->array($findResult->toArray()) |
125
|
|
|
->contains($unique) |
126
|
|
|
; |
127
|
|
|
|
128
|
|
|
$this |
129
|
|
|
->given( |
130
|
|
|
$unique = $this->uniqueValue(), |
131
|
|
|
$criteria = Criteria::same($unique), |
132
|
|
|
$randomCollection = $this->randomCollection() |
133
|
|
|
) |
134
|
|
|
->when($randomCollection->set('bar', $unique)) |
135
|
|
|
->and($findResult = $randomCollection->find($criteria)) |
136
|
|
|
->then() |
137
|
|
|
->array($findResult->toArray()) |
138
|
|
|
->contains($unique) |
139
|
|
|
; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.