Completed
Push — master ( d6f0bd...41a21b )
by Ivannis Suárez
05:43
created

ArrayListTests::testOffsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 1
eloc 12
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\EquatableObject;
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 EquatableObject(\rand(0, 100));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function uniqueValue()
47
    {
48
        return new EquatableObject(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
                $unique = $this->uniqueValue(),
153
                $collection = $this->emptyCollection()
154
            )
155
            ->then()
156
                ->boolean(isset($collection[$key]))
157
                    ->isFalse()
158
            ->and()
159
            ->then()
160
                ->exception(function () use ($collection) {
161
                    $collection->removeAt('foo');
162
                })->isInstanceOf(InvalidKeyException::class)
163
        ;
164
    }
165
166
    /*
167
     * Test offsetExists.
168
     */
169
    public function testOffsetExists()
170
    {
171
        $this
172
            ->given(
173
                $key = 0,
174
                $unique = $this->uniqueValue(),
175
                $collection = $this->emptyCollection()
176
            )
177
            ->then()
178
                ->boolean(isset($collection[$key]))
179
                    ->isFalse()
180
            ->and
181
            ->when($collection->add($unique))
182
            ->then()
183
                ->boolean(isset($collection[$key]))
184
                    ->isTrue()
185
        ;
186
    }
187
188
    /*
189
     * Test offsetGet.
190
     */
191 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...
192
    {
193
        $this
194
            ->given(
195
                $key = 0,
196
                $unique = $this->uniqueValue(),
197
                $collection = $this->emptyCollection()
198
            )
199
            ->then()
200
                ->variable($collection[$key])
201
                    ->isNull()
202
            ->and
203
            ->when($collection->add($unique))
204
            ->then()
205
                ->variable($collection[$key])
206
                    ->isEqualTo($unique)
207
        ;
208
    }
209
210
    /*
211
     * Test offsetSet.
212
     */
213 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...
214
    {
215
        $this
216
            ->given(
217
                $key = 5,
218
                $unique = $this->uniqueValue(),
219
                $collection = $this->emptyCollection()
220
            )
221
            ->when($collection[$key] = $unique)
222
            ->then()
223
                ->variable($collection[$key])
224
                    ->isNull()
225
                ->variable($collection[0])
226
                    ->isEqualTo($unique)
227
        ;
228
    }
229
230
    /*
231
     * Test offsetUnset.
232
     */
233 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...
234
    {
235
        $this
236
            ->given(
237
                $key = 0,
238
                $unique = $this->uniqueValue(),
239
                $collection = $this->emptyCollection()
240
            )
241
            ->when($collection[$key] = $unique)
242
            ->then()
243
                ->variable($collection[$key])
244
                    ->isEqualTo($unique)
245
            ->and()
246
            ->when(function () use ($collection, $key) {
247
                unset($collection[$key]);
248
            })
249
            ->then()
250
                ->variable($collection[$key])
251
                    ->isNull()
252
        ;
253
    }
254
}
255