1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/php-object-maps PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Pinepain\ObjectMaps\Tests; |
16
|
|
|
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Pinepain\ObjectMaps\ObjectBiMapInterface; |
19
|
|
|
use Pinepain\ObjectMaps\ObjectMapInterface; |
20
|
|
|
use stdClass; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
abstract class AbstractObjectMapInterfaceTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param int $behavior |
27
|
|
|
* |
28
|
|
|
* @return ObjectMapInterface | ObjectBiMapInterface |
29
|
|
|
*/ |
30
|
|
|
abstract public function buildMap(int $behavior = ObjectMapInterface::DEFAULT); |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testMap() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$map = $this->buildMap(); |
35
|
|
|
|
36
|
|
|
$key = new stdClass(); |
37
|
|
|
$value = new stdClass(); |
38
|
|
|
|
39
|
|
|
$this->assertSame(0, $map->count()); |
40
|
|
|
$map->put($key, $value); |
41
|
|
|
$this->assertSame(1, $map->count()); |
42
|
|
|
|
43
|
|
|
$value = null; |
|
|
|
|
44
|
|
|
$this->assertSame(1, $map->count()); |
45
|
|
|
|
46
|
|
|
$key = null; |
|
|
|
|
47
|
|
|
$this->assertSame(1, $map->count()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testWeakKeyMap() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$map = $this->buildMap(ObjectMapInterface::WEAK_KEY); |
53
|
|
|
|
54
|
|
|
$key = new stdClass(); |
55
|
|
|
$value = new stdClass(); |
56
|
|
|
|
57
|
|
|
$this->assertSame(0, $map->count()); |
58
|
|
|
$map->put($key, $value); |
59
|
|
|
$this->assertSame(1, $map->count()); |
60
|
|
|
|
61
|
|
|
$value = null; |
|
|
|
|
62
|
|
|
$this->assertSame(1, $map->count()); |
63
|
|
|
|
64
|
|
|
$key = null; |
|
|
|
|
65
|
|
|
$this->assertSame(0, $map->count()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testWeakValueMap() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$map = $this->buildMap(ObjectMapInterface::WEAK_VALUE); |
71
|
|
|
|
72
|
|
|
$key = new stdClass(); |
73
|
|
|
$value = new stdClass(); |
74
|
|
|
|
75
|
|
|
$this->assertSame(0, $map->count()); |
76
|
|
|
$map->put($key, $value); |
77
|
|
|
$this->assertSame(1, $map->count()); |
78
|
|
|
|
79
|
|
|
$key = null; |
|
|
|
|
80
|
|
|
$this->assertSame(1, $map->count()); |
81
|
|
|
|
82
|
|
|
$value = null; |
|
|
|
|
83
|
|
|
$this->assertSame(0, $map->count()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testWeakKeyValueMap() |
87
|
|
|
{ |
88
|
|
|
$map = $this->buildMap(ObjectMapInterface::WEAK_KEY_VALUE); |
89
|
|
|
|
90
|
|
|
$key = new stdClass(); |
91
|
|
|
$value = new stdClass(); |
92
|
|
|
|
93
|
|
|
$this->assertSame(0, $map->count()); |
94
|
|
|
$map->put($key, $value); |
95
|
|
|
$this->assertSame(1, $map->count()); |
96
|
|
|
|
97
|
|
|
$key = null; |
|
|
|
|
98
|
|
|
$this->assertSame(0, $map->count()); |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$key = new stdClass(); |
102
|
|
|
$value = new stdClass(); |
103
|
|
|
|
104
|
|
|
$this->assertSame(0, $map->count()); |
105
|
|
|
$map->put($key, $value); |
106
|
|
|
$this->assertSame(1, $map->count()); |
107
|
|
|
|
108
|
|
|
$value = null; |
|
|
|
|
109
|
|
|
$this->assertSame(0, $map->count()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testPutAndGet() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$map = $this->buildMap(); |
115
|
|
|
|
116
|
|
|
$key = new stdClass(); |
117
|
|
|
$value = new stdClass(); |
118
|
|
|
|
119
|
|
|
$map->put($key, $value); |
120
|
|
|
$this->assertSame($value, $map->get($key)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException |
125
|
|
|
* @expectedExceptionMessage Key is not an object |
126
|
|
|
*/ |
127
|
|
|
public function testPutKeyNotAnObjectFails() |
128
|
|
|
{ |
129
|
|
|
$map = $this->buildMap(); |
130
|
|
|
|
131
|
|
|
$map->put('foo', 'bar'); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException |
136
|
|
|
* @expectedExceptionMessage Value is not an object |
137
|
|
|
*/ |
138
|
|
|
public function testPutValueNotAnObjectFails() |
139
|
|
|
{ |
140
|
|
|
$map = $this->buildMap(); |
141
|
|
|
|
142
|
|
|
$map->put(new stdClass(), 'bar'); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\OverflowException |
147
|
|
|
* @expectedExceptionMessage Value with such key already exists |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function testPutExistentKeyFails() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$map = $this->buildMap(); |
152
|
|
|
|
153
|
|
|
$key = new stdClass(); |
154
|
|
|
$value = new stdClass(); |
155
|
|
|
|
156
|
|
|
$map->put($key, $value); |
157
|
|
|
$map->put($key, $value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException |
162
|
|
|
* @expectedExceptionMessage Key is not an object |
163
|
|
|
*/ |
164
|
|
|
public function testGetByNonObjectFails() |
165
|
|
|
{ |
166
|
|
|
$map = $this->buildMap(); |
167
|
|
|
|
168
|
|
|
$map->get('test'); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException |
173
|
|
|
* @expectedExceptionMessage Value with such key not found |
174
|
|
|
*/ |
175
|
|
|
public function testGetNonexistentKeyFails() |
176
|
|
|
{ |
177
|
|
|
$map = $this->buildMap(); |
178
|
|
|
|
179
|
|
|
$map->get(new stdClass()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException |
184
|
|
|
* @expectedExceptionMessage Key is not an object |
185
|
|
|
*/ |
186
|
|
|
public function testHasByNonObjectFails() |
187
|
|
|
{ |
188
|
|
|
$map = $this->buildMap(); |
189
|
|
|
|
190
|
|
|
$map->has('test'); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testHasNonexistentKey() |
194
|
|
|
{ |
195
|
|
|
$map = $this->buildMap(); |
196
|
|
|
|
197
|
|
|
$this->assertFalse($map->has(new stdClass())); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testHasExistentKey() |
201
|
|
|
{ |
202
|
|
|
$map = $this->buildMap(); |
203
|
|
|
|
204
|
|
|
$key = new stdClass(); |
205
|
|
|
$map->put($key, new stdClass()); |
206
|
|
|
|
207
|
|
|
$this->assertTrue($map->has($key)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException |
212
|
|
|
* @expectedExceptionMessage Key is not an object |
213
|
|
|
*/ |
214
|
|
|
public function testRemoveByNonObjectFails() |
215
|
|
|
{ |
216
|
|
|
$map = $this->buildMap(); |
217
|
|
|
|
218
|
|
|
$map->remove('test'); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException |
223
|
|
|
* @expectedExceptionMessage Value with such key not found |
224
|
|
|
*/ |
225
|
|
|
public function testRemoveNonexistentKeyFails() |
226
|
|
|
{ |
227
|
|
|
$map = $this->buildMap(); |
228
|
|
|
|
229
|
|
|
$map->remove(new stdClass()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testRemove() |
233
|
|
|
{ |
234
|
|
|
$map = $this->buildMap(); |
235
|
|
|
|
236
|
|
|
$key = new stdClass(); |
237
|
|
|
$map->put($key, new stdClass()); |
238
|
|
|
|
239
|
|
|
$this->assertTrue($map->has($key)); |
240
|
|
|
|
241
|
|
|
$map->remove($key); |
242
|
|
|
|
243
|
|
|
$this->assertFalse($map->has($key)); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testCount() |
247
|
|
|
{ |
248
|
|
|
$map = $this->buildMap(); |
249
|
|
|
|
250
|
|
|
$this->assertSame(0, $map->count()); |
251
|
|
|
|
252
|
|
|
$key = new stdClass(); |
253
|
|
|
$map->put($key, new stdClass()); |
254
|
|
|
|
255
|
|
|
$this->assertSame(1, $map->count()); |
256
|
|
|
|
257
|
|
|
$map->remove($key); |
258
|
|
|
|
259
|
|
|
$this->assertSame(0, $map->count()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testClear() |
263
|
|
|
{ |
264
|
|
|
$map = $this->buildMap(); |
265
|
|
|
|
266
|
|
|
$this->assertSame(0, $map->count()); |
267
|
|
|
|
268
|
|
|
$map->put(new stdClass(), new stdClass()); |
269
|
|
|
$this->assertSame(1, $map->count()); |
270
|
|
|
$map->put(new stdClass(), new stdClass()); |
271
|
|
|
$this->assertSame(2, $map->count()); |
272
|
|
|
|
273
|
|
|
$map->clear(); |
274
|
|
|
|
275
|
|
|
$this->assertSame(0, $map->count()); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
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.