Completed
Push — refactor ( 8eceff...e79a5f )
by Bogdan
01:19
created

ObjectBiMapTest::testWeakRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.0856
cc 1
eloc 13
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 26 and the first side effect is on line 225.

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\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)
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...
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;
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...
66
        $this->assertSame(1, $vmap->count());
67
        $this->assertSame(1, $map->count());
68
69
        $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...
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;
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...
96
        $this->assertSame(1, $map->count());
97
        $this->assertSame(1, $vmap->count());
98
99
        $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...
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;
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...
112
        $this->assertSame(1, $map->count());
113
        $this->assertSame(1, $vmap->count());
114
115
        $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...
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;
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...
139
        $this->assertSame(1, $map->count());
140
        $this->assertSame(1, $vmap->count());
141
142
        $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...
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;
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...
155
        $this->assertSame(1, $map->count());
156
        $this->assertSame(1, $vmap->count());
157
158
        $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...
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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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;
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...
191
        $this->assertSame(1, $map->count());
192
        $this->assertSame(1, $vmap->count());
193
194
        echo PHP_EOL;
195
196
        $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...
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;
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...
218
        $this->assertSame(1, $map->count());
219
        $this->assertSame(1, $vmap->count());
220
221
        $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...
222
        $this->assertSame(0, $map->count());
223
        $this->assertSame(0, $vmap->count());
224
    }
225
}
226