Completed
Push — master ( 880b7a...33b151 )
by Tom
09:34 queued 06:03
created

ArrayFunctionsTest::mergeArraysProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace N98\Util;
4
5
/**
6
 * Class ArrayFunctionsTest
7
 *
8
 * @covers N98\Util\ArrayFunctions
9
 */
10
class ArrayFunctionsTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @test
14
     * @param array $a
15
     * @param array $b
16
     * @param array $expected
17
     * @dataProvider mergeArraysProvider
18
     */
19
    public function mergeArrays(array $a, array $b, array $expected)
20
    {
21
        $this->assertEquals($expected, ArrayFunctions::mergeArrays($a, $b));
22
    }
23
24
    /**
25
     * @return array
26
     */
27
    public function mergeArraysProvider()
28
    {
29
        return array(
30
            array(
31
                array(),
32
                array('Foo', 'Bar'),
33
                array('Foo', 'Bar')
34
            ),
35
            array(
36
                array('Foo', 'Bar'),
37
                array(),
38
                array('Foo', 'Bar')
39
            ),
40
            array(
41
                array('Foo'),
42
                array('Bar'),
43
                array('Foo', 'Bar')
44
            ),
45
            array(
46
                array('Foo', array('Bar')),
47
                array('Bar'),
48
                array('Foo', array('Bar'), 'Bar')
49
            ),
50
51
            /**
52
             * Override Bar->Bar
53
             */
54
            array(
55
                array('Foo', 'Bar' => array('Bar' => 1)),
56
                array('Bar' => array('Bar' => 2)),
57
                array('Foo', 'Bar' => array('Bar' => 2))
58
            ),
59
        );
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function columnOrderArrayTable()
66
    {
67
        $headers = array('foo', 'bar', 'baz');
68
        $table = array(
69
            array('foo' => 'A1', 'baz' => 'C1', 'B1', 'D1'),
70
            array('A2', 'B2', 'C2', 'D2'),
71
            array(null, null, null, 'foo' => 'A3'),
72
        );
73
74
        $actual = ArrayFunctions::columnOrderArrayTable($headers, $table);
75
        $this->assertInternalType('array', $actual);
76
        $this->assertCount(count($table), $actual);
77
        $expected = array(
78
            array('foo' => 'A1', 'bar' => 'B1', 'baz' => 'C1', 'D1'),
79
            array('foo' => 'A2', 'bar' => 'B2', 'baz' => 'C2', 'D2'),
80
            array('foo' => 'A3', 'bar' => null, 'baz' => null, null),
81
        );
82
        $this->assertEquals($expected, $actual);
83
        $this->assertSame($expected, $actual);
84
    }
85
86
    /**
87
     * @test
88
     * @dataProvider provideColumnOrderings
89
     */
90
    public function columnOrder($columns, $array, $expected)
91
    {
92
        $actual = ArrayFunctions::columnOrder($columns, $array);
93
        $this->assertInternalType('array', $actual);
94
        $this->assertEquals($expected, $actual);
95
        $this->assertSame($expected, $actual);
96
    }
97
98
    /**
99
     * @see columnOrder
100
     * @return array
101
     */
102
    public function provideColumnOrderings()
103
    {
104
        return array(
105
            array(
106
                array('foo', 'bar', 'baz'),
107
                array('A', 'B', 'C'),
108
                array('foo' => 'A', 'bar' => 'B', 'baz' => 'C'),
109
            ),
110
            array(
111
                array('foo', 'bar', 'baz'),
112
                array('A', 'B', 'C', 'D'),
113
                array('foo' => 'A', 'bar' => 'B', 'baz' => 'C', 'D'),
114
            ),
115
            array(
116
                array('foo', 'bar', 'baz'),
117
                array('A', 'B', 'C'),
118
                array('foo' => 'A', 'bar' => 'B', 'baz' => 'C'),
119
            ),
120
            array(
121
                array('foo', 'bar', 'baz'),
122
                array('buz' => 'D', 'A', 'B', 'C'),
123
                array('foo' => 'A', 'bar' => 'B', 'baz' => 'C', 'buz' => 'D'),
124
            ),
125
            array(
126
                array('foo', 'bar', 'baz'),
127
                array('foo' => 'A', 'baz' => 'C', 'B', 'D'),
128
                array('foo' => 'A', 'bar' => 'B', 'baz' => 'C', 'D'),
129
            ),
130
            array(
131
                array('foo', 'bar', 'baz'),
132
                array('foo' => 'A', 'baz' => 'C'),
133
                array('foo' => 'A', 'bar' => null, 'baz' => 'C'),
134
            ),
135
        );
136
    }
137
138
    /**
139
     * @see matrixFilterByValue
140
     * @see matrixFilterStartsWith
141
     * @return array
142
     */
143
    public function provideMatrix()
144
    {
145
        return array(
146
            array(
147
                array(
148
                    array('foo' => 'bar'),
149
                    array('foo' => 'baz'),
150
                    array('foo' => 'zaz'),
151
                )
152
            )
153
        );
154
    }
155
156
    /**
157
     * @param array $matrix
158
     * @test
159
     * @dataProvider provideMatrix
160
     */
161
    public function matrixFilterByValue(array $matrix)
162
    {
163
        $this->assertCount(3, $matrix);
164
        $filtered = ArrayFunctions::matrixFilterByValue($matrix, 'foo', 'bar');
165
        $this->assertCount(1, $filtered);
166
    }
167
168
    /**
169
     * @param array $matrix
170
     * @test
171
     * @dataProvider provideMatrix
172
     */
173
    public function matrixFilterStartsWith(array $matrix)
174
    {
175
        $this->assertCount(3, $matrix);
176
        $filtered = ArrayFunctions::matrixFilterStartswith($matrix, 'foo', 'ba');
177
        $this->assertCount(2, $filtered);
178
    }
179
}
180