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
|
|
|
|
18
|
|
|
use Pinepain\ObjectMaps\ObjectBiMap; |
19
|
|
|
use Pinepain\ObjectMaps\ObjectBiMapInterface; |
20
|
|
|
use Pinepain\ObjectMaps\ObjectMapInterface; |
21
|
|
|
use Ref\WeakReference; |
22
|
|
|
use SebastianBergmann\CodeCoverage\Report\PHP; |
23
|
|
|
use stdClass; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class ObjectBiMapTest extends AbstractObjectMapInterfaceTest |
27
|
|
|
{ |
28
|
|
|
public function buildMap(int $behavior = ObjectMapInterface::DEFAULT) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
return new ObjectBiMap($behavior); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testValuesForRegularMap() |
34
|
|
|
{ |
35
|
|
|
$map = $this->buildMap(); |
36
|
|
|
|
37
|
|
|
$key = new stdClass(); |
38
|
|
|
$value = new stdClass(); |
39
|
|
|
|
40
|
|
|
$map->put($key, $value); |
41
|
|
|
|
42
|
|
|
$this->assertTrue($map->has($key)); |
43
|
|
|
$this->assertFalse($map->has($value)); |
44
|
|
|
|
45
|
|
|
$vmap = $map->values(); |
46
|
|
|
|
47
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
48
|
|
|
|
49
|
|
|
$this->assertFalse($vmap->has($key)); |
50
|
|
|
$this->assertTrue($vmap->has($value)); |
51
|
|
|
|
52
|
|
|
$map->clear(); |
53
|
|
|
|
54
|
|
|
$this->assertSame(0, $map->count()); |
55
|
|
|
$this->assertSame(0, $vmap->count()); |
56
|
|
|
|
57
|
|
|
$vmap->put($key, $value); |
58
|
|
|
|
59
|
|
|
$this->assertSame(1, $vmap->count()); |
60
|
|
|
$this->assertSame(1, $map->count()); |
61
|
|
|
|
62
|
|
|
$this->assertFalse($map->has($key)); |
63
|
|
|
$this->assertTrue($map->has($value)); |
64
|
|
|
|
65
|
|
|
$key = null; |
|
|
|
|
66
|
|
|
$this->assertSame(1, $vmap->count()); |
67
|
|
|
$this->assertSame(1, $map->count()); |
68
|
|
|
|
69
|
|
|
$value = null; |
|
|
|
|
70
|
|
|
$this->assertSame(1, $vmap->count()); |
71
|
|
|
$this->assertSame(1, $map->count()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testValuesForWeakKeyMap() |
75
|
|
|
{ |
76
|
|
|
$map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY); |
77
|
|
|
|
78
|
|
|
$key = new stdClass(); |
79
|
|
|
$value = new stdClass(); |
80
|
|
|
|
81
|
|
|
$map->put($key, $value); |
82
|
|
|
|
83
|
|
|
$this->assertSame(1, $map->count()); |
84
|
|
|
|
85
|
|
|
$vmap = $map->values(); |
86
|
|
|
|
87
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
88
|
|
|
|
89
|
|
|
$this->assertFalse($vmap->has($key)); |
90
|
|
|
$this->assertTrue($vmap->has($value)); |
91
|
|
|
|
92
|
|
|
$this->assertSame(1, $map->count()); |
93
|
|
|
$this->assertSame(1, $vmap->count()); |
94
|
|
|
|
95
|
|
|
$value = null; |
|
|
|
|
96
|
|
|
$this->assertSame(1, $map->count()); |
97
|
|
|
$this->assertSame(1, $vmap->count()); |
98
|
|
|
|
99
|
|
|
$key = null; |
|
|
|
|
100
|
|
|
$this->assertSame(0, $map->count()); |
101
|
|
|
$this->assertSame(0, $vmap->count()); |
102
|
|
|
|
103
|
|
|
$key = new stdClass(); |
104
|
|
|
$value = new stdClass(); |
105
|
|
|
|
106
|
|
|
$vmap->put($key, $value); |
107
|
|
|
|
108
|
|
|
$this->assertSame(1, $map->count()); |
109
|
|
|
$this->assertSame(1, $vmap->count()); |
110
|
|
|
|
111
|
|
|
$key = null; |
|
|
|
|
112
|
|
|
$this->assertSame(1, $map->count()); |
113
|
|
|
$this->assertSame(1, $vmap->count()); |
114
|
|
|
|
115
|
|
|
$value = null; |
|
|
|
|
116
|
|
|
$this->assertSame(0, $map->count()); |
117
|
|
|
$this->assertSame(0, $vmap->count()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testValuesForWeakValueMap() |
121
|
|
|
{ |
122
|
|
|
$map = $this->buildMap(ObjectBiMapInterface::WEAK_VALUE); |
123
|
|
|
|
124
|
|
|
$key = new stdClass(); |
125
|
|
|
$value = new stdClass(); |
126
|
|
|
|
127
|
|
|
$map->put($key, $value); |
128
|
|
|
|
129
|
|
|
$this->assertSame(1, $map->count()); |
130
|
|
|
|
131
|
|
|
$vmap = $map->values(); |
132
|
|
|
|
133
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
134
|
|
|
|
135
|
|
|
$this->assertSame(1, $map->count()); |
136
|
|
|
$this->assertSame(1, $vmap->count()); |
137
|
|
|
|
138
|
|
|
$key = null; |
|
|
|
|
139
|
|
|
$this->assertSame(1, $map->count()); |
140
|
|
|
$this->assertSame(1, $vmap->count()); |
141
|
|
|
|
142
|
|
|
$value = null; |
|
|
|
|
143
|
|
|
$this->assertSame(0, $map->count()); |
144
|
|
|
$this->assertSame(0, $vmap->count()); |
145
|
|
|
|
146
|
|
|
$key = new stdClass(); |
147
|
|
|
$value = new stdClass(); |
148
|
|
|
|
149
|
|
|
$vmap->put($key, $value); |
150
|
|
|
|
151
|
|
|
$this->assertSame(1, $map->count()); |
152
|
|
|
$this->assertSame(1, $vmap->count()); |
153
|
|
|
|
154
|
|
|
$value = null; |
|
|
|
|
155
|
|
|
$this->assertSame(1, $map->count()); |
156
|
|
|
$this->assertSame(1, $vmap->count()); |
157
|
|
|
|
158
|
|
|
$key = null; |
|
|
|
|
159
|
|
|
$this->assertSame(0, $map->count()); |
160
|
|
|
$this->assertSame(0, $vmap->count()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
public function testValuesForWeakKeyValueMap() |
165
|
|
|
{ |
166
|
|
|
// $map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY_VALUE); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$map = new ObjectBiMap(ObjectBiMapInterface::WEAK_KEY_VALUE); |
169
|
|
|
|
170
|
|
|
$key_1 = new stdClass(); |
171
|
|
|
$value_1 = new stdClass(); |
172
|
|
|
|
173
|
|
|
$key_2 = new stdClass(); |
174
|
|
|
$value_2 = new stdClass(); |
175
|
|
|
|
176
|
|
|
$map->put($key_1, $value_1); |
177
|
|
|
echo PHP_EOL; |
178
|
|
|
$map->put($key_2, $value_2); |
179
|
|
|
echo PHP_EOL; |
180
|
|
|
|
181
|
|
|
$this->assertSame(2, $map->count()); |
182
|
|
|
|
183
|
|
|
$vmap = $map->values(); |
184
|
|
|
|
185
|
|
|
$this->assertInstanceOf(ObjectBiMapInterface::class, $vmap); |
186
|
|
|
|
187
|
|
|
$this->assertSame(2, $map->count()); |
188
|
|
|
$this->assertSame(2, $vmap->count()); |
189
|
|
|
|
190
|
|
|
$key_1 = null; |
|
|
|
|
191
|
|
|
$this->assertSame(1, $map->count()); |
192
|
|
|
$this->assertSame(1, $vmap->count()); |
193
|
|
|
|
194
|
|
|
echo PHP_EOL; |
195
|
|
|
|
196
|
|
|
$value_2 = null; |
|
|
|
|
197
|
|
|
$this->assertSame(0, $map->count()); |
198
|
|
|
$this->assertSame(0, $vmap->count()); |
199
|
|
|
|
200
|
|
|
echo PHP_EOL; |
201
|
|
|
|
202
|
|
|
$key_1 = new stdClass(); |
203
|
|
|
$value_1 = new stdClass(); |
204
|
|
|
|
205
|
|
|
$key_2 = new stdClass(); |
206
|
|
|
$value_2 = new stdClass(); |
207
|
|
|
|
208
|
|
|
$vmap->put($key_1, $value_1); |
209
|
|
|
echo PHP_EOL; |
210
|
|
|
|
211
|
|
|
$vmap->put($key_2, $value_2); |
212
|
|
|
echo PHP_EOL; |
213
|
|
|
|
214
|
|
|
$this->assertSame(2, $map->count()); |
215
|
|
|
$this->assertSame(2, $vmap->count()); |
216
|
|
|
|
217
|
|
|
$key_1 = null; |
|
|
|
|
218
|
|
|
$this->assertSame(1, $map->count()); |
219
|
|
|
$this->assertSame(1, $vmap->count()); |
220
|
|
|
|
221
|
|
|
$value_2 = null; |
|
|
|
|
222
|
|
|
$this->assertSame(0, $map->count()); |
223
|
|
|
$this->assertSame(0, $vmap->count()); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.