Completed
Push — master ( 9531a5...140f69 )
by Guillermo A.
28s queued 12s
created

CollectionTest::testLast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 $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 $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
    }
47
48
    public function testRandom()
49
    {
50
        $this->assertArrayHasKey('age', $this->collection->random());
51
    }
52
53
    public function testGet()
54
    {
55
        $this->assertSame($this->items[2], $this->collection->get(2));
56
    }
57
58
    public function testGetWithDefault()
59
    {
60
        $default = 'test';
61
        $this->assertSame($default, $this->collection->get(40, $default));
62
    }
63
64
    public function testHas()
65
    {
66
        $this->assertFalse($this->collection->has('test'));
67
    }
68
69
    public function testRemove()
70
    {
71
        $collection = new Collection(['test', 'this', 'method']);
72
        $collection->remove(1);
73
        $this->assertCount(2, $collection);
74
    }
75
76
    public function testCount()
77
    {
78
        $this->assertSame(count($this->collection->all()), $this->collection->count());
79
    }
80
81
    public function testIsEmpty()
82
    {
83
        $this->assertTrue((new Collection())->isEmpty());
84
    }
85
86
    public function testIsNotEmpty()
87
    {
88
        $this->assertFalse($this->collection->isEmpty());
89
    }
90
91
    public function testShuffle()
92
    {
93
        $collection = new Collection(range(0, 50, 5));
94
        $collection1 = $collection->shuffle();
95
        $collection2 = $collection->shuffle();
96
        $this->assertSameSize($collection1, $collection2);
97
    }
98
99
    public function testLimit()
100
    {
101
        $slice = $this->collection->limit(1, 2);
102
        $this->assertSame(2, $slice->count());
103
    }
104
105
    public function testSortBy()
106
    {
107
        $sorted = $this->collection->sortBy('age');
108
        $this->assertTrue($sorted->first()['age'] < $sorted->last()['age']);
109
    }
110
111
    public function testSortByReverse()
112
    {
113
        $sorted = $this->collection->sortBy('age', true);
114
        $this->assertTrue($sorted->first()['age'] > $sorted->last()['age']);
115
    }
116
117
    public function testFilter()
118
    {
119
        $collection = new Collection([1, 2, 3]);
120
        $filteredCollection = $collection->filter(function ($var) {
121
            return($var & 1);
122
        });
123
        $this->assertCount(2, $filteredCollection);
124
    }
125
126
    public function testMap()
127
    {
128
        $collection = new Collection([1, 2, 3]);
129
        $mappedCollection = $collection->map(function ($var) {
130
            return($var + 3);
131
        });
132
        $this->assertSame([4, 5, 6], $mappedCollection->all());
133
    }
134
135
    public function testToArray()
136
    {
137
        $this->assertSame($this->items, $this->collection->toArray());
138
    }
139
140
    public function testToJson()
141
    {
142
        $this->assertSame(json_encode($this->items), (string) $this->collection);
143
    }
144
145
    protected function setUp(): void
146
    {
147
        $this->collection = new Collection($this->items);
148
    }
149
}
150