FilterTraitTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
eloc 21
c 2
b 1
f 1
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A test_splice() 0 7 1
A testFilter() 0 14 1
A test_only() 0 11 1
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
    public function testFilter()
15
    {
16
        $collection = new Collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
17
        static::assertEquals([1 => ['id' => 2, 'name' => 'World']], $collection->filter(function ($item) {
18
            return $item['id'] == 2;
19
        })->all());
20
21
        $collection = new Collection(['', 'Hello', '', 'World']);
22
        static::assertEquals(['Hello', 'World'], $collection->filter()->values());
23
24
        $collection = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
25
        static::assertEquals(['first' => 'Hello', 'second' => 'World'], $collection->filter(function ($item, $key) {
26
            return $key != 'id';
27
        })->all());
28
    }
29
30
    public function test_only()
31
    {
32
        $data = new Collection(['first' => 'John', 'last' => 'Smith', 'email' => '[email protected]']);
33
34
        static::assertEquals($data->all(), $data->only(null)->all());
35
36
        static::assertEquals(['first' => 'John'], $data->only(['first', 'missing'])->all());
37
        static::assertEquals(['first' => 'John'], $data->only('first', 'missing')->all());
38
39
        static::assertEquals(['first' => 'John', 'email' => '[email protected]'], $data->only(['first', 'email'])->all());
40
        static::assertEquals(['first' => 'John', 'email' => '[email protected]'], $data->only('first', 'email')->all());
41
    }
42
43
    public function test_splice()
44
    {
45
        $data = new Collection(['first' => 'John', 'last' => 'Smith', 'email' => '[email protected]']);
46
47
        $dataSpliced = $data->splice(0, 1);
48
        static::assertInstanceOf(Collection::class, $dataSpliced);
49
        self::assertSame(['first' => 'John'], $dataSpliced->all());
50
    }
51
}
52