1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Apicart\Utils\Tests\Arrays; |
4
|
|
|
|
5
|
|
|
use Apicart\Utils\Arrays\Collection; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
final class CollectionTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private const TEST_DATASET = [1, 'b', false, 'a']; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Collection |
15
|
|
|
*/ |
16
|
|
|
private $collection; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp(): void |
20
|
|
|
{ |
21
|
|
|
$this->collection = new Collection(function () { |
22
|
|
|
return self::TEST_DATASET; |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function testGetArray(): void |
28
|
|
|
{ |
29
|
|
|
self::assertSame(self::TEST_DATASET, $this->collection->toArray()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function testColumn(): void |
34
|
|
|
{ |
35
|
|
|
self::assertSame(null, $this->collection->column()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function testFirst(): void |
40
|
|
|
{ |
41
|
|
|
self::assertSame(1, $this->collection->first()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function testLast(): void |
46
|
|
|
{ |
47
|
|
|
self::assertSame('a', $this->collection->last()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public function testGetIterator(): void |
52
|
|
|
{ |
53
|
|
|
$iterator = $this->collection->getIterator(); |
54
|
|
|
foreach ($iterator as $key => $value) { |
55
|
|
|
self::assertSame(self::TEST_DATASET[$key], $value); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function testOffsetExists(): void |
61
|
|
|
{ |
62
|
|
|
self::assertTrue($this->collection->offsetExists(1)); |
63
|
|
|
self::assertFalse($this->collection->offsetExists(5)); |
64
|
|
|
|
65
|
|
|
self::assertTrue(isset($this->collection[1])); |
66
|
|
|
self::assertFalse(isset($this->collection[5])); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function testOffsetGet(): void |
71
|
|
|
{ |
72
|
|
|
self::assertSame('b', $this->collection->offsetGet(1)); |
73
|
|
|
self::assertSame(1, $this->collection[0]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
public function testOffsetSet(): void |
78
|
|
|
{ |
79
|
|
|
$this->collection->offsetSet('foo', 'bar'); |
80
|
|
|
self::assertSame('bar', $this->collection['foo']); |
81
|
|
|
|
82
|
|
|
$this->collection['tag'] = 'new'; |
83
|
|
|
self::assertSame('new', $this->collection->offsetGet('tag')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function testOffsetUnset(): void |
88
|
|
|
{ |
89
|
|
|
self::assertTrue(isset($this->collection[2])); |
90
|
|
|
$this->collection->offsetUnset(2); |
91
|
|
|
self::assertFalse(isset($this->collection[2])); |
92
|
|
|
|
93
|
|
|
self::assertTrue(isset($this->collection[0])); |
94
|
|
|
unset($this->collection[0]); |
95
|
|
|
self::assertFalse(isset($this->collection[0])); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
public function testCount(): void |
100
|
|
|
{ |
101
|
|
|
self::assertSame(count(self::TEST_DATASET), $this->collection->count()); |
102
|
|
|
self::assertSame(count(self::TEST_DATASET), count($this->collection)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
public function testToArray(): void |
107
|
|
|
{ |
108
|
|
|
self::assertSame(self::TEST_DATASET, $this->collection->toArray()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|