SetTestCase::testAddAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Collections\Tests\Units;
11
12
use Cubiche\Core\Collections\SetInterface;
13
use Cubiche\Core\Specification\Criteria;
14
15
/**
16
 * SetTestCase class.
17
 *
18
 * @method protected SetInterface emptyCollection()
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
abstract class SetTestCase extends CollectionTestCase
24
{
25
    /**
26
     * @param int $size
27
     *
28
     * @return SetInterface
29
     */
30
    protected function randomCollection($size = null)
31
    {
32
        $collection = $this->emptyCollection();
33
        $collection->addAll($this->randomValues($size));
34
35
        return $collection;
36
    }
37
38
    /**
39
     * Test class.
40
     */
41
    public function testClass()
42
    {
43
        $this
44
            ->testedClass
45
                ->implements(SetInterface::class)
46
        ;
47
    }
48
49
    /**
50
     * Test count.
51
     */
52
    public function testCount()
53
    {
54
        $this
55
            ->given($collection = $this->emptyCollection())
56
            ->then()
57
                ->integer($collection->count())
58
                    ->isEqualTo(0)
59
            ->and()
60
            ->when($collection->add($this->uniqueValue()))
61
            ->then()
62
                ->integer($collection->count())
63
                    ->isEqualTo(1)
64
        ;
65
    }
66
67
    /**
68
     * Test add.
69
     */
70 View Code Duplication
    public function testAdd()
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...
71
    {
72
        $this
73
            ->given($collection = $this->emptyCollection())
74
            ->and($unique = $this->uniqueValue())
75
            ->when($collection->add($unique))
76
            ->and($collection->add($unique))
77
            ->then()
78
                ->set($collection)
79
                    ->contains($unique)
80
                    ->size()
81
                        ->isEqualTo(1)
82
        ;
83
    }
84
85
    /**
86
     * Test add.
87
     */
88
    public function testAddAll()
89
    {
90
        $this
91
            ->given($collection = $this->emptyCollection())
92
            ->and($items = [1, 2, 3, 4, 5, 6])
93
            ->when($collection->addAll($items))
94
            ->and($collection->addAll($items))
95
            ->then()
96
                ->set($collection)
97
                    ->containsValues($items)
98
                    ->size()
99
                        ->isEqualTo(\count($items))
100
            ->and()
101
            ->then()
102
                ->exception(function () use ($collection) {
103
                    $collection->addAll(100);
0 ignored issues
show
Documentation introduced by
100 is of type integer, but the function expects a array|object<Traversable>.

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...
104
                })->isInstanceOf(\InvalidArgumentException::class)
105
        ;
106
    }
107
108
    /**
109
     * Test remove.
110
     */
111 View Code Duplication
    public function testRemove()
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...
112
    {
113
        $this
114
            ->given(
115
                $unique = $this->uniqueValue(),
116
                $emptyCollection = $this->emptyCollection()
117
            )
118
            ->when($emptyCollection->add($unique))
119
            ->then()
120
                ->set($emptyCollection)
121
                    ->contains($unique)
122
            ->and()
123
            ->when($result = $emptyCollection->remove($unique))
124
            ->then()
125
                ->set($emptyCollection)
126
                    ->notContains($unique)
127
                ->boolean($result)
128
                    ->isTrue()
129
        ;
130
131
        $this
132
            ->given(
133
                $unique = $this->uniqueValue(),
134
                $randomCollection = $this->randomCollection()
135
            )
136
            ->when($randomCollection->add($unique))
137
            ->then()
138
                ->set($randomCollection)
139
                    ->contains($unique)
140
            ->and()
141
            ->when($randomCollection->remove($unique))
142
            ->then()
143
                ->set($randomCollection)
144
                    ->notContains($unique)
145
            ->and()
146
            ->when($result = $randomCollection->remove('foo'))
147
            ->then()
148
                ->boolean($result)
149
                    ->isFalse()
150
        ;
151
    }
152
153
    /*
154
     * Test removeAll.
155
     */
156 View Code Duplication
    public function testRemoveAll()
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...
157
    {
158
        $this
159
            ->given(
160
                $unique = $this->uniqueValue(),
161
                $random = $this->randomValue(),
162
                $collection = $this->emptyCollection()
163
            )
164
            ->and($collection->addAll([$unique, $random]))
165
            ->then()
166
                ->set($collection)
167
                    ->contains($unique)
168
                ->set($collection)
169
                    ->contains($random)
170
            ->and()
171
            ->when($collection->removeAll([$unique, $random]))
172
            ->then()
173
                ->boolean($collection->isEmpty())
174
                    ->isTrue()
175
        ;
176
    }
177
178
    /**
179
     * Test find.
180
     */
181 View Code Duplication
    public function testFind()
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...
182
    {
183
        parent::testFind();
184
185
        $this
186
            ->given(
187
                $unique = $this->uniqueValue(),
188
                $criteria = Criteria::same($unique),
189
                $emptyCollection = $this->emptyCollection()
190
            )
191
            ->when($emptyCollection->add($unique))
192
            ->and($findResult = $emptyCollection->find($criteria))
193
            ->then()
194
                ->set($findResult)
195
                    ->size()
196
                        ->isEqualTo(1)
197
                ->array($findResult->toArray())
198
                ->contains($unique)
199
        ;
200
201
        $this
202
            ->given(
203
                $unique = $this->uniqueValue(),
204
                $criteria = Criteria::same($unique),
205
                $randomCollection = $this->randomCollection()
206
            )
207
            ->when($randomCollection->add($unique))
208
            ->and($findResult = $randomCollection->find($criteria))
209
            ->then()
210
                ->array($findResult->toArray())
211
                    ->contains($unique)
212
        ;
213
    }
214
215
    /**
216
     * Test findOne.
217
     */
218 View Code Duplication
    public function testFindOne()
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
        $this
221
            ->given(
222
                $unique = $this->uniqueValue(),
223
                $criteria = Criteria::eq($unique),
224
                $emptyCollection = $this->emptyCollection()
225
            )
226
            ->when($findResult = $emptyCollection->findOne($criteria))
227
            ->then()
228
                ->variable($findResult)
229
                    ->isNull()
230
            ->and()
231
            ->when(
232
                $emptyCollection->add($unique),
233
                $findResult = $emptyCollection->findOne($criteria)
234
            )
235
            ->then()
236
                ->variable($findResult)
237
                    ->isEqualTo($unique)
238
        ;
239
240
        $this
241
            ->given(
242
                $unique = $this->uniqueValue(),
243
                $criteria = Criteria::eq($unique),
244
                $randomCollection = $this->randomCollection()
245
            )
246
            ->when($findResult = $randomCollection->findOne($criteria))
247
            ->then()
248
                ->variable($findResult)
249
                    ->isNull()
250
            ->and()
251
            ->when(
252
                $randomCollection->add($unique),
253
                $findResult = $randomCollection->findOne($criteria)
254
            )
255
            ->then()
256
                ->variable($findResult)
257
                    ->isEqualTo($unique)
258
        ;
259
    }
260
}
261