XFilter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 12
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterSource() 0 3 1
A filterKeys() 0 3 1
1
<?php
2
3
namespace TraitsTests;
4
5
6
use kalanis\kw_input\Interfaces;
7
use kalanis\kw_input\Entries;
8
use kalanis\kw_input\Traits;
9
10
11
class FilterTest extends ATraitsTest
12
{
13
    public function testFilterKeyNoParam(): void
14
    {
15
        $lib = new XFilter();
16
        $this->checkAllEntries($lib->filterKeys(null, $this->inputEntries()));
17
    }
18
19
    public function testFilterKeyUnknownParam(): void
20
    {
21
        $lib = new XFilter();
22
        $this->assertEmpty($lib->filterKeys('not-exists', $this->inputEntries()));
23
    }
24
25
    public function testFilterKeySoloParam(): void
26
    {
27
        $lib = new XFilter();
28
        $data = $lib->filterKeys('bar', $this->inputEntries());
29
30
        $item = reset($data);
31
        $this->assertNotFalse($item);
32
        /** @var Entries\Entry $item */
33
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $item->getSource());
34
        $this->assertEquals('bar', $item->getKey());
35
        $this->assertEquals('hjyxn', $item->getValue());
36
37
        $item = next($data);
38
        $this->assertNotFalse($item);
39
        /** @var Entries\Entry $item */
40
        $this->assertEquals(Interfaces\IEntry::SOURCE_ENV, $item->getSource());
41
        $this->assertEquals('bar', $item->getKey());
42
        $this->assertEquals('shshh', $item->getValue());
43
44
        $item = next($data);
45
        $this->assertFalse($item);
46
    }
47
48
    public function testFilterSourceNoParam(): void
49
    {
50
        $lib = new XFilter();
51
        $this->checkAllEntries($lib->filterSource([], $this->inputEntries()));
52
    }
53
54
    public function testFilterSourceUnknownParam(): void
55
    {
56
        $lib = new XFilter();
57
        $this->assertEmpty($lib->filterSource([Entries\Entry::SOURCE_JSON], $this->inputEntries()));
58
    }
59
60
    public function testFilterSourceSoloParam(): void
61
    {
62
        $lib = new XFilter();
63
        $data = $lib->filterSource([Entries\Entry::SOURCE_ENV], $this->inputEntries());
64
65
        $item = reset($data);
66
        $this->assertNotFalse($item);
67
        /** @var Entries\Entry $item */
68
        $this->assertEquals(Interfaces\IEntry::SOURCE_ENV, $item->getSource());
69
        $this->assertEquals('foo', $item->getKey());
70
        $this->assertEquals('mjgdf', $item->getValue());
71
72
        $item = next($data);
73
        $this->assertNotFalse($item);
74
        /** @var Entries\Entry $item */
75
        $this->assertEquals(Interfaces\IEntry::SOURCE_ENV, $item->getSource());
76
        $this->assertEquals('bar', $item->getKey());
77
        $this->assertEquals('shshh', $item->getValue());
78
79
        $item = next($data);
80
        $this->assertFalse($item);
81
    }
82
83
    protected function checkAllEntries(array $data): void
84
    {
85
        $item = reset($data);
86
        $this->assertNotFalse($item);
87
        /** @var Entries\Entry $item */
88
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $item->getSource());
89
        $this->assertEquals('foo', $item->getKey());
90
        $this->assertEquals('fhdfh', $item->getValue());
91
92
        $item = next($data);
93
        $this->assertNotFalse($item);
94
        /** @var Entries\Entry $item */
95
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $item->getSource());
96
        $this->assertEquals('bar', $item->getKey());
97
        $this->assertEquals('hjyxn', $item->getValue());
98
99
        $item = next($data);
100
        $this->assertNotFalse($item);
101
        /** @var Entries\Entry $item */
102
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $item->getSource());
103
        $this->assertEquals('baz', $item->getKey());
104
        $this->assertEquals('gnbgy', $item->getValue());
105
106
        $item = next($data);
107
        $this->assertNotFalse($item);
108
        /** @var Entries\Entry $item */
109
        $this->assertEquals(Interfaces\IEntry::SOURCE_ENV, $item->getSource());
110
        $this->assertEquals('foo', $item->getKey());
111
        $this->assertEquals('mjgdf', $item->getValue());
112
113
        $item = next($data);
114
        $this->assertNotFalse($item);
115
        /** @var Entries\Entry $item */
116
        $this->assertEquals(Interfaces\IEntry::SOURCE_ENV, $item->getSource());
117
        $this->assertEquals('bar', $item->getKey());
118
        $this->assertEquals('shshh', $item->getValue());
119
120
        $item = next($data);
121
        $this->assertFalse($item);
122
    }
123
}
124
125
126
class XFilter
127
{
128
    use Traits\TFilter;
129
130
    public function filterKeys(?string $entryKey, array $availableEntries): array
131
    {
132
        return $this->whichKeys($entryKey, $availableEntries);
133
    }
134
135
    public function filterSource(array $entrySources, array $entriesWithKeys): array
136
    {
137
        return $this->whichSource($entrySources, $entriesWithKeys);
138
    }
139
}
140