Completed
Push — master ( 2820ae...deb522 )
by Gabriel
02:22
created

FilterTraitTest::testFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 14
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Nip\Collections\Tests\Traits;
4
5
use Nip\Collections\Collection;
6
use Nip\Collections\Tests\AbstractTest;
7
8
/**
9
 * Class FilterTraitTest
10
 * @package Nip\Collections\Tests\Traits
11
 */
12
class FilterTraitTest extends AbstractTest
13
{
14
15
    public function testFilter()
16
    {
17
        $collection = new Collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
18
        static::assertEquals([1 => ['id' => 2, 'name' => 'World']], $collection->filter(function ($item) {
19
            return $item['id'] == 2;
20
        })->all());
21
22
        $collection = new Collection(['', 'Hello', '', 'World']);
23
        static::assertEquals(['Hello', 'World'], $collection->filter()->values());
24
25
        $collection = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
26
        static::assertEquals(['first' => 'Hello', 'second' => 'World'], $collection->filter(function ($item, $key) {
27
            return $key != 'id';
28
        })->all());
29
    }
30
31
    public function test_only()
32
    {
33
        $data = new Collection(['first' => 'John', 'last' => 'Smith', 'email' => '[email protected]']);
34
35
        static::assertEquals($data->all(), $data->only(null)->all());
36
37
        static::assertEquals(['first' => 'John'], $data->only(['first', 'missing'])->all());
38
        static::assertEquals(['first' => 'John'], $data->only('first', 'missing')->all());
39
40
        static::assertEquals(['first' => 'John', 'email' => '[email protected]'], $data->only(['first', 'email'])->all());
41
        static::assertEquals(['first' => 'John', 'email' => '[email protected]'], $data->only('first', 'email')->all());
42
    }
43
44
    public function test_splice()
45
    {
46
        $data = new Collection(['first' => 'John', 'last' => 'Smith', 'email' => '[email protected]']);
47
48
        $dataSpliced = $data->splice(0, 1);
49
        static::assertInstanceOf(Collection::class, $dataSpliced);
50
        self::assertSame(['first' => 'John'], $dataSpliced->all());
51
    }
52
}
53