Passed
Pull Request — master (#4)
by Chad
02:16
created

ArraysTest::extractKeyIsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use PHPUnit\Framework\TestCase;
6
use TraderInteractive\Exceptions\FilterException;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\Filter\Arrays
10
 */
11
final class ArraysTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @covers ::filter
16
     */
17
    public function filterBasicPass()
18
    {
19
        $this->assertSame(['boo'], Arrays::filter(['boo']));
20
    }
21
22
    /**
23
     * @test
24
     * @covers ::filter
25
     * @expectedException \TraderInteractive\Exceptions\FilterException
26
     * @expectedExceptionMessage Value '1' is not an array
27
     */
28
    public function filterFailNotArray()
29
    {
30
        Arrays::filter(1);
31
    }
32
33
    /**
34
     * @test
35
     * @covers ::filter
36
     * @expectedException \TraderInteractive\Exceptions\FilterException
37
     * @expectedExceptionMessage $value count of 0 is less than 1
38
     */
39
    public function filterFailEmpty()
40
    {
41
        Arrays::filter([]);
42
    }
43
44
    /**
45
     * @test
46
     * @covers ::filter
47
     * @expectedException \TraderInteractive\Exceptions\FilterException
48
     * @expectedExceptionMessage $value count of 1 is less than 2
49
     */
50
    public function filterCountLessThanMin()
51
    {
52
        Arrays::filter([0], 2);
53
    }
54
55
    /**
56
     * @test
57
     * @covers ::filter
58
     * @expectedException \TraderInteractive\Exceptions\FilterException
59
     * @expectedExceptionMessage $value count of 2 is greater than 1
60
     */
61
    public function filterCountGreaterThanMax()
62
    {
63
        Arrays::filter([0, 1], 1, 1);
64
    }
65
66
    /**
67
     * @test
68
     * @covers ::in
69
     */
70
    public function inPassStrict()
71
    {
72
        $this->assertSame('boo', Arrays::in('boo', ['boo']));
73
    }
74
75
    /**
76
     * @test
77
     * @covers ::in
78
     */
79
    public function inFailStrict()
80
    {
81
        try {
82
            Arrays::in('0', [0]);
83
            $this->fail();
84
        } catch (FilterException $e) {
85
            $this->assertSame("Value '0' is not in array array (\n  0 => 0,\n)", $e->getMessage());
86
        }
87
    }
88
89
    /**
90
     * @test
91
     * @covers ::in
92
     */
93
    public function inFailNotStrict()
94
    {
95
        try {
96
            Arrays::in('boo', ['foo'], false);
97
            $this->fail();
98
        } catch (FilterException $e) {
99
            $this->assertSame("Value 'boo' is not in array array (\n  0 => 'foo',\n)", $e->getMessage());
100
        }
101
    }
102
103
    /**
104
     * @test
105
     * @covers ::in
106
     */
107
    public function inPassNotStrict()
108
    {
109
        $this->assertSame('0', Arrays::in('0', [0], false));
110
    }
111
112
    /**
113
     * Verifies the basic behavior of the flatten filter.
114
     *
115
     * @test
116
     * @covers ::flatten
117
     */
118
    public function flatten()
119
    {
120
        $this->assertSame([1, 2, 3, 4, 5], Arrays::flatten([[1, 2], [[3, [4, 5]]]]));
121
    }
122
123
    /**
124
     * @test
125
     * @covers ::arrayize
126
     */
127
    public function arrayizeReturnsInputIfItIsAnArray()
128
    {
129
        $this->assertSame([1, 2, 3, 4, 5], Arrays::arrayize([1, 2, 3, 4, 5]));
130
    }
131
132
    /**
133
     * @test
134
     * @covers ::arrayize
135
     */
136
    public function arrayizeWrapsNonArrayValue()
137
    {
138
        $value = new \StdClass();
139
        $this->assertSame([$value], Arrays::arrayize($value));
140
    }
141
142
    /**
143
     * @test
144
     * @covers ::arrayize
145
     */
146
    public function arrayizeConvertsNullToEmptyArray()
147
    {
148
        $this->assertSame([], Arrays::arrayize(null));
149
    }
150
151
    /**
152
     * @test
153
     * @covers ::extract
154
     */
155
    public function extractKeyNotRequired()
156
    {
157
        $input = ['a', 'b', 'color' => 'green', 'shape' => 'trapezoid', 4];
158
        $this->assertNull(Arrays::extract($input, 'foo', false));
159
    }
160
161
    /**
162
     * @test
163
     * @covers ::extract
164
     * @expectedException \TraderInteractive\Exceptions\FilterException
165
     * @expectedExceptionMessage Array did not contain element at index 'foo'
166
     */
167
    public function extractKeyRequired()
168
    {
169
        $input = ['a', 'b', 'color' => 'green', 'shape' => 'trapezoid', 4];
170
        Arrays::extract($input, 'foo', true);
171
    }
172
173
    /**
174
     * @test
175
     * @covers ::extract
176
     * @expectedException \InvalidArgumentException
177
     * @expectedExceptionMessage $key was not a string or integer
178
     */
179
    public function extractKeyNotStringOrInteger()
180
    {
181
        $input = ['a', 'b', 'color' => 'green', 'shape' => 'trapezoid', 4];
182
        Arrays::extract($input, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer|string expected by parameter $key of TraderInteractive\Filter\Arrays::extract(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
        Arrays::extract($input, /** @scrutinizer ignore-type */ false);
Loading history...
183
    }
184
185
    /**
186
     * @test
187
     * @covers ::extract
188
     */
189
    public function extractKeyIsString()
190
    {
191
        $input = ['a', 'b', 'color' => 'green', 'shape' => 'trapezoid', 4];
192
        $this->assertSame($input['shape'], Arrays::extract($input, 'shape'));
193
    }
194
195
    /**
196
     * @test
197
     * @covers ::extract
198
     */
199
    public function extractKeyIsInteger()
200
    {
201
        $input = ['a', 'b', 'color' => 'green', 'shape' => 'trapezoid', 4];
202
        $this->assertSame($input[1], Arrays::extract($input, 1));
203
    }
204
}
205