Completed
Push — refactor ( 40adc6...f69de8 )
by Bogdan
01:20
created

ObjectBiMapTest::testValuesForWeakValueMap()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php declare(strict_types=1);
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 273.

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.

Loading history...
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\ObjectMap;
21
use Pinepain\ObjectMaps\ObjectMapInterface;
22
use stdClass;
23
24
25
class ObjectBiMapTest extends AbstractObjectMapInterfaceTest
26
{
27
    public function buildMap(int $behavior = ObjectMapInterface::DEFAULT)
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
28
    {
29
        return new ObjectBiMap($behavior);
30
    }
31
32
    public function testValuesForRegularMap()
33
    {
34
        $map = $this->buildMap();
35
36
        $key   = new stdClass();
37
        $value = new stdClass();
38
39
        $map->put($key, $value);
40
41
        $this->assertTrue($map->has($key));
42
        $this->assertFalse($map->has($value));
43
44
        $vmap = $map->values();
45
46
        $this->assertInstanceOf(ObjectBiMapInterface::class, $vmap);
47
48
        $this->assertFalse($vmap->has($key));
49
        $this->assertTrue($vmap->has($value));
50
51
        $map->clear();
52
53
        $this->assertSame(0, $map->count());
54
        $this->assertSame(0, $vmap->count());
55
56
        $vmap->put($key, $value);
57
58
        $this->assertSame(1, $vmap->count());
59
        $this->assertSame(1, $map->count());
60
61
        $this->assertFalse($map->has($key));
62
        $this->assertTrue($map->has($value));
63
64
        $key = null;
0 ignored issues
show
Unused Code introduced by
$key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
        $this->assertSame(1, $vmap->count());
66
        $this->assertSame(1, $map->count());
67
68
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
        $this->assertSame(1, $vmap->count());
70
        $this->assertSame(1, $map->count());
71
    }
72
73
    public function testValuesForWeakKeyMap()
74
    {
75
        $map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY);
76
77
        $key   = new stdClass();
78
        $value = new stdClass();
79
80
        $map->put($key, $value);
81
82
        $this->assertSame(1, $map->count());
83
84
        $vmap = $map->values();
85
86
        $this->assertInstanceOf(ObjectBiMapInterface::class, $vmap);
87
88
        $this->assertFalse($vmap->has($key));
89
        $this->assertTrue($vmap->has($value));
90
91
        $this->assertSame(1, $map->count());
92
        $this->assertSame(1, $vmap->count());
93
94
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
        $this->assertSame(1, $map->count());
96
        $this->assertSame(1, $vmap->count());
97
98
        $key = null;
0 ignored issues
show
Unused Code introduced by
$key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
        $this->assertSame(0, $map->count());
100
        $this->assertSame(0, $vmap->count());
101
102
        $key   = new stdClass();
103
        $value = new stdClass();
104
105
        $vmap->put($key, $value);
106
107
        $this->assertSame(1, $map->count());
108
        $this->assertSame(1, $vmap->count());
109
110
        $key = null;
0 ignored issues
show
Unused Code introduced by
$key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
        $this->assertSame(1, $map->count());
112
        $this->assertSame(1, $vmap->count());
113
114
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
        $this->assertSame(0, $map->count());
116
        $this->assertSame(0, $vmap->count());
117
    }
118
119
    public function testValuesForWeakValueMap()
120
    {
121
        $map = $this->buildMap(ObjectBiMapInterface::WEAK_VALUE);
122
123
        $key   = new stdClass();
124
        $value = new stdClass();
125
126
        $map->put($key, $value);
127
128
        $this->assertSame(1, $map->count());
129
130
        $vmap = $map->values();
131
132
        $this->assertInstanceOf(ObjectBiMapInterface::class, $vmap);
133
134
        $this->assertSame(1, $map->count());
135
        $this->assertSame(1, $vmap->count());
136
137
        $key = null;
0 ignored issues
show
Unused Code introduced by
$key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
        $this->assertSame(1, $map->count());
139
        $this->assertSame(1, $vmap->count());
140
141
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
        $this->assertSame(0, $map->count());
143
        $this->assertSame(0, $vmap->count());
144
145
        $key   = new stdClass();
146
        $value = new stdClass();
147
148
        $vmap->put($key, $value);
149
150
        $this->assertSame(1, $map->count());
151
        $this->assertSame(1, $vmap->count());
152
153
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
        $this->assertSame(1, $map->count());
155
        $this->assertSame(1, $vmap->count());
156
157
        $key = null;
0 ignored issues
show
Unused Code introduced by
$key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
        $this->assertSame(0, $map->count());
159
        $this->assertSame(0, $vmap->count());
160
    }
161
162
163
    public function testValuesForWeakKeyValueMap()
164
    {
165
        $map = $this->buildMap(ObjectBiMapInterface::WEAK_KEY_VALUE);
166
167
        $key_1   = new stdClass();
168
        $value_1 = new stdClass();
169
170
        $key_2   = new stdClass();
171
        $value_2 = new stdClass();
172
173
        $map->put($key_1, $value_1);
174
        $map->put($key_2, $value_2);
175
176
        $this->assertSame(2, $map->count());
177
178
        $vmap = $map->values();
179
180
        $this->assertInstanceOf(ObjectBiMapInterface::class, $vmap);
181
182
        $this->assertSame(2, $map->count());
183
        $this->assertSame(2, $vmap->count());
184
185
        $key_1 = null;
0 ignored issues
show
Unused Code introduced by
$key_1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
186
        $this->assertSame(1, $map->count());
187
        $this->assertSame(1, $vmap->count());
188
189
        $value_2 = null;
0 ignored issues
show
Unused Code introduced by
$value_2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
        $this->assertSame(0, $map->count());
191
        $this->assertSame(0, $vmap->count());
192
193
        $key_1   = new stdClass();
194
        $value_1 = new stdClass();
195
196
        $key_2   = new stdClass();
197
        $value_2 = new stdClass();
198
199
        $vmap->put($key_1, $value_1);
200
        $vmap->put($key_2, $value_2);
201
202
        $this->assertSame(2, $map->count());
203
        $this->assertSame(2, $vmap->count());
204
205
        $key_1 = null;
0 ignored issues
show
Unused Code introduced by
$key_1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
206
        $this->assertSame(1, $map->count());
207
        $this->assertSame(1, $vmap->count());
208
209
        $value_2 = null;
0 ignored issues
show
Unused Code introduced by
$value_2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
210
        $this->assertSame(0, $map->count());
211
        $this->assertSame(0, $vmap->count());
212
    }
213
214
    /**
215
     * @expectedException \Pinepain\ObjectMaps\Exceptions\OverflowException
216
     * @expectedExceptionMessage Key with such value already exists
217
     */
218 View Code Duplication
    public function testKeyValuesOutOfSyncPutFails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
219
    {
220
        $key_1   = new stdClass();
221
        $value_1 = new stdClass();
222
223
        $key_2   = new stdClass();
224
        $value_2 = new stdClass();
225
226
        $keys_map   = new ObjectMap();
227
        $values_map = new ObjectMap();
228
229
        $keys_map->put($key_1, $value_1);
230
        $values_map->put($value_2, $key_2);
231
232
        $map = new class($keys_map, $values_map) extends ObjectBiMap
233
        {
234
            public function __construct(ObjectMapInterface $keys, ObjectMapInterface $values)
235
            {
236
                $this->keys   = $keys;
0 ignored issues
show
Documentation Bug introduced by
$keys is of type object<Pinepain\ObjectMaps\ObjectMapInterface>, but the property $keys was declared to be of type object<Pinepain\ObjectMaps\ObjectMap>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
237
                $this->values = $values;
0 ignored issues
show
Documentation Bug introduced by
$values is of type object<Pinepain\ObjectMaps\ObjectMapInterface>, but the property $values was declared to be of type object<Pinepain\ObjectMaps\ObjectMap>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
238
            }
239
        };
240
241
        $map->put($key_2, $value_2);
242
    }
243
244
    /**
245
     * @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException
246
     * @expectedExceptionMessage Key with such value not found
247
     */
248 View Code Duplication
    public function testKeyValuesOutOfSyncRemoveFails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
249
    {
250
        $key_1   = new stdClass();
251
        $value_1 = new stdClass();
252
253
        $key_2   = new stdClass();
254
        $value_2 = new stdClass();
255
256
        $keys_map   = new ObjectMap();
257
        $values_map = new ObjectMap();
258
259
        $keys_map->put($key_1, $value_1);
260
        $values_map->put($value_2, $key_2);
261
262
        $map = new class($keys_map, $values_map) extends ObjectBiMap
263
        {
264
            public function __construct(ObjectMapInterface $keys, ObjectMapInterface $values)
265
            {
266
                $this->keys   = $keys;
0 ignored issues
show
Documentation Bug introduced by
$keys is of type object<Pinepain\ObjectMaps\ObjectMapInterface>, but the property $keys was declared to be of type object<Pinepain\ObjectMaps\ObjectMap>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
267
                $this->values = $values;
0 ignored issues
show
Documentation Bug introduced by
$values is of type object<Pinepain\ObjectMaps\ObjectMapInterface>, but the property $values was declared to be of type object<Pinepain\ObjectMaps\ObjectMap>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
268
            }
269
        };
270
271
        $map->remove($key_1);
272
    }
273
}
274