CollectionTest::testMake()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Test\Common;
4
5
use PHPUnit\Framework\TestCase;
6
use Guillermoandrae\Common\Collection;
7
use Guillermoandrae\Common\CollectionInterface;
8
9
class CollectionTest extends TestCase
10
{
11
    /**
12
     * @var array
13
     */
14
    private array $items = [
15
        ['name' => 'Me', 'age' => 30],
16
        ['name' => 'You', 'age' => 21],
17
        ['name' => 'Her', 'age' => 28],
18
        ['name' => 'Him', 'age' => 42],
19
    ];
20
21
    /**
22
     * @var CollectionInterface
23
     */
24
    private CollectionInterface $collection;
25
26
    public function testMake()
27
    {
28
        $items = [1, 3, 4, 7];
29
        $collection = Collection::make($items);
30
        $this->assertInstanceOf(CollectionInterface::class, $collection);
31
    }
32
33
    public function testAll()
34
    {
35
        $this->assertSame($this->items, $this->collection->all());
36
    }
37
38
    public function testFirst()
39
    {
40
        $this->assertSame($this->items[0], $this->collection->first());
41
    }
42
43
    public function testLast()
44
    {
45
        $this->assertSame($this->items[3], $this->collection->last());
46
        $this->collection->set('test', 'last');
47
        $this->assertSame('last', $this->collection->last());
48
    }
49
50
    public function testRandom()
51
    {
52
        $this->assertArrayHasKey('age', $this->collection->random());
53
    }
54
55
    public function testGet()
56
    {
57
        $this->assertSame($this->items[2], $this->collection->get(2));
58
    }
59
60
    public function testGetWithDefault()
61
    {
62
        $default = 'test';
63
        $this->assertSame($default, $this->collection->get(40, $default));
64
    }
65
66
    public function testHas()
67
    {
68
        $this->assertFalse($this->collection->has('test'));
69
    }
70
71
    public function testRemove()
72
    {
73
        $collection = new Collection(['test', 'this', 'method']);
74
        $collection->remove(1);
75
        $this->assertCount(2, $collection);
76
    }
77
78
    public function testCount()
79
    {
80
        $this->assertSame(count($this->collection->all()), $this->collection->count());
81
    }
82
83
    public function testIsEmpty()
84
    {
85
        $this->assertTrue((new Collection())->isEmpty());
86
    }
87
88
    public function testIsNotEmpty()
89
    {
90
        $this->assertFalse($this->collection->isEmpty());
91
    }
92
93
    public function testShuffle()
94
    {
95
        $collection = new Collection(range(0, 50, 5));
96
        $collection1 = $collection->shuffle();
97
        $collection2 = $collection->shuffle();
98
        $this->assertSameSize($collection1, $collection2);
99
    }
100
101
    public function testLimit()
102
    {
103
        $slice = $this->collection->limit(1, 2);
104
        $this->assertSame(2, $slice->count());
105
    }
106
107
    public function testSortBy()
108
    {
109
        $sorted = $this->collection->sortBy('age');
110
        $this->assertTrue($sorted->first()['age'] < $sorted->last()['age']);
111
    }
112
113
    public function testSortByReverse()
114
    {
115
        $sorted = $this->collection->sortBy('age', true);
116
        $this->assertTrue($sorted->first()['age'] > $sorted->last()['age']);
117
    }
118
119
    public function testFilter()
120
    {
121
        $collection = new Collection([1, 2, 3]);
122
        $filteredCollection = $collection->filter(function ($var) {
123
            return($var & 1);
124
        });
125
        $this->assertCount(2, $filteredCollection);
126
    }
127
128
    public function testMap()
129
    {
130
        $collection = new Collection([1, 2, 3]);
131
        $mappedCollection = $collection->map(function ($var) {
132
            return($var + 3);
133
        });
134
        $this->assertSame([4, 5, 6], $mappedCollection->all());
135
    }
136
137
    public function testToArray()
138
    {
139
        $this->assertSame($this->items, $this->collection->toArray());
140
    }
141
142
    public function testToJson()
143
    {
144
        $this->assertSame(json_encode($this->items), (string) $this->collection);
145
    }
146
147
    protected function setUp(): void
148
    {
149
        $this->collection = new Collection($this->items);
150
    }
151
}
152