Completed
Push — master ( ecb243...bb1b22 )
by Pablo
03:58
created

CollectionTest::itShouldReverseObjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace PhpValueObjects\Tests\Collection;
5
6
use PhpValueObjects\Collection\Exception\InvalidCollectionObjectException;
7
use PhpValueObjects\Tests\BaseUnitTestCase;
8
use stdClass;
9
10
final class CollectionTest extends BaseUnitTestCase
11
{
12
    private $objects;
13
    private $collection;
14
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
        $this->objects = [
19
            new ObjectForTest(1),
20
            new ObjectForTest(2),
21
            new ObjectForTest(3),
22
            new ObjectForTest(4),
23
            new ObjectForTest(5),
24
        ];
25
        $this->collection = new Collection($this->objects);
26
    }
27
28
    protected function tearDown()
29
    {
30
        parent::tearDown();
31
        $this->objects = null;
32
        $this->collection = null;
33
    }
34
35
    /**
36
     * @test
37
     */
38
    public function itShouldThrowInvalidCollectionObjectException(): void
39
    {
40
        $this->expectException(InvalidCollectionObjectException::class);
41
42
        new Collection([new stdClass(), new stdClass()]);
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function itShouldReturnCollection(): void
49
    {
50
        $this->assertSame($this->objects, $this->collection->getCollection());
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function itShouldReturnEmptyCollection(): void
57
    {
58
        $collection = new Collection([]);
59
60
        $this->assertEmpty($collection->getCollection());
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function itShouldReturnFirst(): void
67
    {
68
        /** @var ObjectForTest $object */
69
        $object = $this->collection->first();
70
        $this->assertSame(1, $object->getId());
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function itShouldReturnLast(): void
77
    {
78
        /** @var ObjectForTest $object */
79
        $object = $this->collection->last();
80
        $this->assertSame(5, $object->getId());
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function itShouldReturnKey(): void
87
    {
88
        $key = $this->collection->key();
89
        $this->assertSame(0, $key);
90
    }
91
92
    /**
93
     * @test
94
     */
95 View Code Duplication
    public function itShouldReturnNext(): void
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...
96
    {
97
        $first = $this->collection->first();
98
        $this->assertSame(1, $first->getId());
99
        $next = $this->collection->next();
100
        $this->assertSame(2, $next->getId());
101
    }
102
103
    /**
104
     * @test
105
     */
106 View Code Duplication
    public function itShouldReturnCurrent(): void
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...
107
    {
108
        $this->collection->first();
109
        $this->collection->next();
110
        $current = $this->collection->current();
111
        $this->assertSame(2, $current->getId());
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function itShouldRemoveKey(): void
118
    {
119
        $this->collection->remove(4);
120
        $this->assertFalse(array_key_exists(4, $this->collection->getCollection()));
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function itShouldRemoveElement(): void
127
    {
128
        $element = $this->collection->first();
129
        $response = $this->collection->removeElement($element);
130
        $this->assertTrue($response);
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function itShouldReturnContains(): void
137
    {
138
        $element = $this->collection->last();
139
        $this->assertTrue($this->collection->contains($element));
140
    }
141
142
    /**
143
     * @test
144
     */
145
    public function itShouldReturnElementByKey(): void
146
    {
147
        $this->assertInstanceOf(ObjectForTest::class, $this->collection->get(3));
148
        $this->assertNull($this->collection->get(25));
149
    }
150
151
    /**
152
     * @test
153
     */
154
    public function itShouldReturnKeys(): void
155
    {
156
        $keys = $this->collection->getKeys();
157
        $this->assertNotEmpty($keys);
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function itShouldReturnValues(): void
164
    {
165
        $values = $this->collection->getValues();
166
        $this->assertNotEmpty($values);
167
    }
168
169
    /**
170
     * @test
171
     */
172
    public function itShouldReturnCount(): void
173
    {
174
        $this->assertSame(5, $this->collection->count());
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function itShouldSetObject(): void
181
    {
182
        $new = new ObjectForTest(7);
183
        $this->collection->set(3, $new);
184
        $this->assertSame($this->collection->get(3)->getId(), $new->getId());
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function itShouldAddObject(): void
191
    {
192
        $new = new ObjectForTest(25);
193
        $this->collection->add($new);
194
        $this->assertSame($new->getId(), $this->collection->last()->getId());
195
    }
196
197
    /**
198
     * @test
199
     */
200
    public function itShouldClearObjects(): void
201
    {
202
        $this->assertFalse($this->collection->isEmpty());
203
        $this->collection->clear();
204
        $this->assertTrue($this->collection->isEmpty());
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function itShouldApplyReverseObjects(): void
211
    {
212
        $this->collection->add(new ObjectForTest(1));
213
        $this->collection->add(new ObjectForTest(2));
214
        $this->collection->add(new ObjectForTest(3));
215
216
        $this->collection->applyReverse();
217
        $this->assertSame(1, $this->collection->last()->getId());
218
    }
219
220
    /**
221
     * @test
222
     */
223
    public function itShouldReverseObjects(): void
224
    {
225
        $this->collection->add(new ObjectForTest(1));
226
        $this->collection->add(new ObjectForTest(2));
227
        $this->collection->add(new ObjectForTest(3));
228
229
        $data = $this->collection->reverse();
230
        $this->assertSame(1, end($data)->getId());
231
    }
232
233
234
}
235