File: CollectionTest.php

package info (click to toggle)
php-ramsey-collection 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: php: 3,239; xml: 31; makefile: 22
file content (291 lines) | stat: -rw-r--r-- 9,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php

declare(strict_types=1);

namespace Ramsey\Collection\Test;

use Ramsey\Collection\Collection;
use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Exception\InvalidPropertyOrMethod;
use Ramsey\Collection\Exception\NoSuchElementException;
use Ramsey\Collection\Exception\UnsupportedOperationException;
use Ramsey\Collection\Test\Mock\Bar;
use Ramsey\Collection\Test\Mock\BarCollection;
use Ramsey\Collection\Test\Mock\Foo;
use Ramsey\Collection\Test\Mock\FooCollection;
use stdClass;

use function serialize;
use function unserialize;

/**
 * Tests for Collection, as well as coverage for AbstractCollection
 */
class CollectionTest extends TestCase
{
    public function testConstructorSetsType(): void
    {
        $collection = new Collection('string');

        $this->assertSame('string', $collection->getType());
    }

    public function testConstructorWithData(): void
    {
        $collection = new Collection('string', ['foo', 'bar']);

        $this->assertCount(2, $collection);
    }

    public function testOffsetSet(): void
    {
        /** @var Collection<int> $collection */
        $collection = new Collection('integer');
        $collection[] = $this->faker->numberBetween();

        // Ensure that an exception is thrown when attempting to add
        // an invalid type for this collection
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Value must be of type integer');

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress InvalidArgument
         */
        $collection[] = $this->faker->text();
    }

    public function testOffsetSetPosition(): void
    {
        $offset = $this->faker->numberBetween(0, 100);
        $value = $this->faker->numberBetween(0, 100);

        /** @var Collection<int> $collection */
        $collection = new Collection('int');
        $collection->offsetSet($offset, $value);

        $this->assertSame($value, $collection->offsetGet($offset));
    }

    public function testAdd(): void
    {
        /** @var Collection<int> $collection */
        $collection = new Collection('integer');

        $this->assertTrue($collection->add($this->faker->numberBetween()));
    }

    public function testAddMayAddSameObjectMultipleTimes(): void
    {
        $expectedCount = 4;

        $obj1 = new stdClass();
        $obj1->name = $this->faker->name();

        /** @var Collection<stdClass> $collection1 */
        $collection1 = new Collection('stdClass');

        /** @var Collection<stdClass> $collection2 */
        $collection2 = new Collection('stdClass');

        // Add the same object multiple times
        for ($i = 0; $i < $expectedCount; $i++) {
            $collection1[] = $obj1;
        }

        // Test the add() method
        for ($i = 0; $i < $expectedCount; $i++) {
            $collection2->add($obj1);
        }

        $this->assertCount($expectedCount, $collection1);
        $this->assertCount($expectedCount, $collection2);
    }

    public function testContains(): void
    {
        $name = $this->faker->name();

        $obj1 = new stdClass();
        $obj1->name = $name;

        // Object with same properties but different identity
        $obj2 = new stdClass();
        $obj2->name = $name;

        /** @var Collection<stdClass> $collection */
        $collection = new Collection('stdClass');
        $collection->add($obj1);

        $this->assertTrue($collection->contains($obj1));
        $this->assertFalse($collection->contains($obj2));
    }

    public function testContainsNonStrict(): void
    {
        $name = $this->faker->name();

        $obj1 = new stdClass();
        $obj1->name = $name;

        // Object with same properties but different identity
        $obj2 = new stdClass();
        $obj2->name = $name;

        /** @var Collection<stdClass> $collection */
        $collection = new Collection('stdClass');
        $collection->add($obj1);

        $this->assertTrue($collection->contains($obj1, false));
        $this->assertTrue($collection->contains($obj2, false));
    }

    public function testRemove(): void
    {
        $obj1 = new stdClass();
        $obj1->name = $this->faker->name();

        /** @var Collection<stdClass> $collection */
        $collection = new Collection('stdClass');

        // Add the same object multiple times
        $collection->add($obj1);
        $collection->add($obj1);
        $collection->add($obj1);

        $this->assertTrue($collection->remove($obj1));
        $this->assertTrue($collection->remove($obj1));
        $this->assertTrue($collection->remove($obj1));
        $this->assertFalse($collection->remove($obj1));
    }

    public function testSubclassBehavior(): void
    {
        $fooCollection = new FooCollection();

        $fooCollection[] = new Foo();
        $fooCollection[] = new Foo();
        $fooCollection[] = new Foo();

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Value must be of type ' . Foo::class);

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress InvalidArgument
         */
        $fooCollection[] = new stdClass();
    }

    public function testColumnByProperty(): void
    {
        $bar1 = new Bar(1, 'a');
        $bar2 = new Bar(2, 'b');
        $bar3 = new Bar(3, 'c');
        $barCollection = new BarCollection([$bar1, $bar2, $bar3]);

        $this->assertSame(['a', 'b', 'c'], $barCollection->column('name'));
    }

    public function testColumnByMethod(): void
    {
        $bar1 = new Bar(1, 'a');
        $bar2 = new Bar(2, 'b');
        $bar3 = new Bar(3, 'c');
        $barCollection = new BarCollection([$bar1, $bar2, $bar3]);

        $this->assertSame([1, 2, 3], $barCollection->column('getId'));
    }

    public function testColumnByArrayKey(): void
    {
        /** @var Collection<array{id: int, name: string}> $collection */
        $collection = new Collection('array', [
            ['id' => 1, 'name' => 'a'],
            ['id' => 2, 'name' => 'b'],
            ['id' => 3, 'name' => 'c'],
        ]);

        $this->assertSame([1, 2, 3], $collection->column('id'));
        $this->assertSame(['a', 'b', 'c'], $collection->column('name'));
    }

    public function testColumnShouldRaiseExceptionOnUndefinedPropertyOrMethod(): void
    {
        $bar1 = new Bar(1, 'a');
        $barCollection = new BarCollection([$bar1]);

        $this->expectException(InvalidPropertyOrMethod::class);
        $this->expectExceptionMessage('Method or property "fu" not defined in Ramsey\Collection\Test\Mock\Bar');

        $barCollection->column('fu');
    }

    public function testColumnShouldRaiseExceptionWhenNotSupported(): void
    {
        /** @var Collection<int> $collection */
        $collection = new Collection('int', [1, 2, 3, 4]);

        $this->expectException(UnsupportedOperationException::class);
        $this->expectExceptionMessage('The collection type "int" does not support the $propertyOrMethod parameter');

        $collection->column('foo');
    }

    public function testFirstShouldRaiseExceptionOnEmptyCollection(): void
    {
        $barCollection = new BarCollection();

        $this->expectException(NoSuchElementException::class);
        $this->expectExceptionMessage('Can\'t determine first item. Collection is empty');
        $barCollection->first();
    }

    public function testFirst(): void
    {
        $bar1 = new Bar(1, 'a');
        $bar2 = new Bar(2, 'b');
        $bar3 = new Bar(3, 'c');
        $barCollection = new BarCollection([$bar1, $bar2, $bar3]);

        $this->assertSame($bar1, $barCollection->first());
        // Make sure the collection stays unchanged
        $this->assertSame([$bar1, $bar2, $bar3], $barCollection->toArray());
    }

    public function testLastShouldRaiseExceptionOnEmptyCollection(): void
    {
        $barCollection = new BarCollection();

        $this->expectException(NoSuchElementException::class);
        $this->expectExceptionMessage('Can\'t determine last item. Collection is empty');
        $barCollection->last();
    }

    public function testLast(): void
    {
        $bar1 = new Bar(1, 'a');
        $bar2 = new Bar(2, 'b');
        $bar3 = new Bar(3, 'c');
        $barCollection = new BarCollection([$bar1, $bar2, $bar3]);

        $this->assertSame($bar3, $barCollection->last());
        // Make sure the collection stays unchanged
        $this->assertSame([$bar1, $bar2, $bar3], $barCollection->toArray());
    }

    public function testSerializable(): void
    {
        $bar1 = new Bar(1, 'a');
        $bar2 = new Bar(2, 'b');
        $bar3 = new Bar(3, 'c');
        $barCollection = new BarCollection([$bar1, $bar2, $bar3]);

        $collectionSerialized = serialize($barCollection);
        $barCollection2 = unserialize($collectionSerialized);

        $this->assertInstanceOf(BarCollection::class, $barCollection2);
        $this->assertContainsOnlyInstancesOf(Bar::class, $barCollection2);
        $this->assertEquals($barCollection, $barCollection2);
    }
}