Completed
Push — refactor ( a403e4...c06c99 )
by Bogdan
02:18
created

AbstractObjectMapInterfaceTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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()
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...
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;
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...
44
        $this->assertSame(1, $map->count());
45
46
        $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...
47
        $this->assertSame(1, $map->count());
48
    }
49
50 View Code Duplication
    public function testWeakKeyMap()
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...
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;
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...
62
        $this->assertSame(1, $map->count());
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(0, $map->count());
66
    }
67
68 View Code Duplication
    public function testWeakValueMap()
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...
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;
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...
80
        $this->assertSame(1, $map->count());
81
82
        $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...
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;
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...
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;
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...
109
        $this->assertSame(0, $map->count());
110
    }
111
112 View Code Duplication
    public function testPutAndGet()
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...
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');
0 ignored issues
show
Documentation introduced by
'foo' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'bar' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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');
0 ignored issues
show
Documentation introduced by
'bar' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
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...
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');
0 ignored issues
show
Documentation introduced by
'test' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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');
0 ignored issues
show
Documentation introduced by
'test' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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');
0 ignored issues
show
Documentation introduced by
'test' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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