ArrayListTests::emptyCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\ArrayCollection;
11
12
use Cubiche\Core\Collections\ArrayCollection\ArrayList;
13
use Cubiche\Core\Collections\ArrayCollection\ArrayListInterface;
14
use Cubiche\Core\Collections\Exception\InvalidKeyException;
15
use Cubiche\Core\Collections\Tests\Units\ListTestCase;
16
use Cubiche\Core\Equatable\Tests\Fixtures\Value;
17
18
/**
19
 * ArrayListTests class.
20
 *
21
 * @method protected ArrayListInterface randomCollection($size = null)
22
 *
23
 * @author Ivannis Suárez Jerez <[email protected]>
24
 */
25
class ArrayListTests extends ListTestCase
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function emptyCollection()
31
    {
32
        return new ArrayList();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function randomValue()
39
    {
40
        return new Value(\rand(0, 100));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function uniqueValue()
47
    {
48
        return new Value(1000);
49
    }
50
51
    /*
52
     * Test create.
53
     */
54
    public function testCreate()
55
    {
56
        $this
57
            ->given($collection = $this->randomCollection())
58
            ->then()
59
                ->list($collection)
60
                    ->isInstanceOf(ArrayListInterface::class)
61
        ;
62
    }
63
64
    /*
65
     * Test contains.
66
     */
67 View Code Duplication
    public function testContains()
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...
68
    {
69
        $this
70
            ->given(
71
                $unique = $this->uniqueValue(),
72
                $collection = $this->randomCollection()
73
            )
74
            ->then()
75
                ->boolean($collection->contains($unique))
76
                    ->isFalse()
77
            ->and
78
            ->when($collection->add($unique))
79
            ->then()
80
                ->boolean($collection->contains($unique))
81
                    ->isTrue()
82
        ;
83
    }
84
85
    /*
86
     * Test removeAt.
87
     */
88
    public function testRemoveAt()
89
    {
90
        $this
91
            ->given(
92
                $unique = $this->uniqueValue(),
93
                $collection = $this->emptyCollection()
94
            )
95
            ->and($collection->add($unique))
96
            ->and($key = $collection->indexOf('foo'))
97
            ->when($element = $collection->removeAt($key))
98
            ->then()
99
                ->variable($element)
100
                    ->isNull()
101
        ;
102
103
        $this
104
            ->given(
105
                $unique = $this->uniqueValue(),
106
                $collection = $this->emptyCollection()
107
            )
108
            ->and($collection->add($unique))
109
            ->and($key = $collection->indexOf($unique))
110
            ->when($element = $collection->removeAt($key))
111
            ->then()
112
                ->boolean($collection->contains($unique))
113
                    ->isFalse()
114
                ->variable($element)
115
                    ->isEqualTo($unique)
116
        ;
117
    }
118
119
    /*
120
     * Test sort.
121
     */
122 View Code Duplication
    public function testSort()
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...
123
    {
124
        $this
125
            ->given(
126
                $comparator = $this->comparator(),
127
                $reverseComparator = $comparator->reverse(),
128
                $collection = $this->randomCollection()
129
            )
130
            ->when($collection->sort())
131
            ->then()
132
                ->list($collection)
133
                    ->isSortedUsing($comparator)
134
            ->and
135
            ->when($collection->sort($reverseComparator))
136
            ->then()
137
                ->list($collection)
138
                    ->isSortedUsing($reverseComparator)
139
                ->list($collection)
140
                    ->isNotSortedUsing($comparator)
141
        ;
142
    }
143
144
    /*
145
     * Test validateKey.
146
     */
147
    public function testValidateKey()
148
    {
149
        $this
150
            ->given(
151
                $key = 0,
152
                $collection = $this->emptyCollection()
153
            )
154
            ->then()
155
                ->boolean(isset($collection[$key]))
156
                    ->isFalse()
157
            ->and()
158
            ->then()
159
                ->exception(function () use ($collection) {
160
                    $collection->removeAt('foo');
161
                })->isInstanceOf(InvalidKeyException::class)
162
        ;
163
    }
164
165
    /*
166
     * Test offsetExists.
167
     */
168
    public function testOffsetExists()
169
    {
170
        $this
171
            ->given(
172
                $key = 0,
173
                $unique = $this->uniqueValue(),
174
                $collection = $this->emptyCollection()
175
            )
176
            ->then()
177
                ->boolean(isset($collection[$key]))
178
                    ->isFalse()
179
            ->and
180
            ->when($collection->add($unique))
181
            ->then()
182
                ->boolean(isset($collection[$key]))
183
                    ->isTrue()
184
        ;
185
    }
186
187
    /*
188
     * Test offsetGet.
189
     */
190 View Code Duplication
    public function testOffsetGet()
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...
191
    {
192
        $this
193
            ->given(
194
                $key = 0,
195
                $unique = $this->uniqueValue(),
196
                $collection = $this->emptyCollection()
197
            )
198
            ->then()
199
                ->variable($collection[$key])
200
                    ->isNull()
201
            ->and
202
            ->when($collection->add($unique))
203
            ->then()
204
                ->variable($collection[$key])
205
                    ->isEqualTo($unique)
206
        ;
207
    }
208
209
    /*
210
     * Test offsetSet.
211
     */
212 View Code Duplication
    public function testOffsetSet()
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...
213
    {
214
        $this
215
            ->given(
216
                $key = 5,
217
                $unique = $this->uniqueValue(),
218
                $collection = $this->emptyCollection()
219
            )
220
            ->when($collection[$key] = $unique)
221
            ->then()
222
                ->variable($collection[$key])
223
                    ->isNull()
224
                ->variable($collection[0])
225
                    ->isEqualTo($unique)
226
        ;
227
    }
228
229
    /*
230
     * Test offsetUnset.
231
     */
232 View Code Duplication
    public function testOffsetUnset()
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...
233
    {
234
        $this
235
            ->given(
236
                $key = 0,
237
                $unique = $this->uniqueValue(),
238
                $collection = $this->emptyCollection()
239
            )
240
            ->when($collection[$key] = $unique)
241
            ->then()
242
                ->variable($collection[$key])
243
                    ->isEqualTo($unique)
244
            ->and()
245
            ->when(function () use ($collection, $key) {
246
                unset($collection[$key]);
247
            })
248
            ->then()
249
                ->variable($collection[$key])
250
                    ->isNull()
251
        ;
252
    }
253
}
254