Passed
Push — html ( 49f55d...51e1ba )
by Peter
03:18
created

AttributesTest::mergeProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Helper;
6
7
use AbterPhp\Framework\Html\Attribute;
8
use PHPUnit\Framework\TestCase;
9
10
class AttributesTest extends TestCase
11
{
12
    public function fromArrayProvider(): array
13
    {
14
        return [
15
            'empty'                   => [
16
                [],
17
                [],
18
            ],
19
            'single-null-attribute'   => [
20
                ['foo' => null],
21
                ['foo' => new Attribute('foo')],
22
            ],
23
            'single-string-attribute' => [
24
                ['foo' => 'bar'],
25
                ['foo' => new Attribute('foo', 'bar')],
26
            ],
27
            'single-array-attribute'  => [
28
                ['foo' => ['bar', 'baz']],
29
                ['foo' => new Attribute('foo', 'bar', 'baz')],
30
            ],
31
            'mixed-attributes'        => [
32
                ['foo' => ['bar', 'baz'], 'bar' => null],
33
                ['foo' => new Attribute('foo', 'bar', 'baz'), 'bar' => new Attribute('bar')],
34
            ],
35
        ];
36
    }
37
38
    /**
39
     * @dataProvider fromArrayProvider
40
     *
41
     * @param array                   $attributes
42
     * @param array<string,Attribute> $expectedResult
43
     */
44
    public function testFromArray(array $attributes, array $expectedResult): void
45
    {
46
        $actualResult = Attributes::fromArray($attributes);
47
48
        $this->assertEquals($expectedResult, $actualResult);
49
    }
50
51
    /**
52
     * @return array[]
53
     */
54
    public function mergeProvider(): array
55
    {
56
        $foo0  = new Attribute('foo');
57
        $foo1  = new Attribute('foo', 'bar');
58
        $foo2  = new Attribute('foo', 'baz');
59
        $foo12 = new Attribute('foo', 'bar', 'baz');
60
        $bar1  = new Attribute('bar', 'foo');
61
        $bar11 = new Attribute('bar', 'foo', 'foo');
62
63
        return [
64
            'all empty'                  => [[], [], []],
65
            'combine empty attributes 1' => [['foo' => $foo1], ['foo' => $foo0], ['foo' => $foo1]],
66
            'combine empty attributes 2' => [['foo' => $foo0], ['foo' => $foo1], ['foo' => $foo1]],
67
            'combine attributes'         => [['foo' => $foo1], ['bar' => $bar1], ['foo' => $foo1, 'bar' => $bar1]],
68
            'combine attribute values 1' => [['foo' => $foo1], ['foo' => $foo2], ['foo' => $foo12]],
69
            'combine attribute values 2' => [
70
                ['foo' => $foo1, 'bar' => $bar1],
71
                ['foo' => $foo2, 'bar' => $bar1],
72
                ['foo' => $foo12, 'bar' => $bar11]
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider mergeProvider
79
     *
80
     * @param array<string,Attribute> $attributes
81
     * @param array<string,Attribute> $attributes2
82
     * @param array<string,Attribute> $expectedResult
83
     */
84
    public function testMerge(array $attributes, array $attributes2, array $expectedResult): void
85
    {
86
        $actualResult = Attributes::merge($attributes, $attributes2);
87
88
        $this->assertEquals($expectedResult, $actualResult);
89
    }
90
91
    /**
92
     * @dataProvider mergeProvider
93
     *
94
     * @param array<string,Attribute> $attributes
95
     * @param array<string,Attribute> $attributes2
96
     * @param array<string,Attribute> $expectedResult
97
     */
98
    public function testMergeItem(array $attributes, array $attributes2, array $expectedResult): void
99
    {
100
        $actualResult = Attributes::mergeItem($attributes, ...array_values($attributes2));
101
102
        $this->assertEquals($expectedResult, $actualResult);
103
    }
104
105
    /**
106
     * @return array[]
107
     */
108
    public function replaceProvider(): array
109
    {
110
        $foo0  = new Attribute('foo');
111
        $foo1  = new Attribute('foo', 'bar');
112
        $foo2  = new Attribute('foo', 'baz');
113
        $bar1  = new Attribute('bar', 'foo');
114
115
        return [
116
            'all empty'                  => [[], [], []],
117
            'combine empty attributes 1' => [['foo' => $foo1], ['foo' => $foo0], ['foo' => $foo0]],
118
            'combine empty attributes 2' => [['foo' => $foo0], ['foo' => $foo1], ['foo' => $foo1]],
119
            'combine attributes'         => [['foo' => $foo1], ['bar' => $bar1], ['foo' => $foo1, 'bar' => $bar1]],
120
            'combine attribute values 1' => [['foo' => $foo1], ['foo' => $foo2], ['foo' => $foo2]],
121
            'combine attribute values 2' => [
122
                ['foo' => $foo1, 'bar' => $bar1],
123
                ['foo' => $foo2, 'bar' => $bar1],
124
                ['foo' => $foo2, 'bar' => $bar1]
125
            ],
126
        ];
127
    }
128
129
    /**
130
     * @dataProvider replaceProvider
131
     *
132
     * @param array<string,Attribute> $attributes
133
     * @param array<string,Attribute> $attributes2
134
     * @param array<string,Attribute> $expectedResult
135
     */
136
    public function testReplace(array $attributes, array $attributes2, array $expectedResult): void
137
    {
138
        $actualResult = Attributes::replace($attributes, $attributes2);
139
140
        $this->assertEquals($expectedResult, $actualResult);
141
    }
142
143
    /**
144
     * @dataProvider replaceProvider
145
     *
146
     * @param array<string,Attribute> $attributes
147
     * @param array<string,Attribute> $attributes2
148
     * @param array<string,Attribute> $expectedResult
149
     */
150
    public function testReplaceItem(array $attributes, array $attributes2, array $expectedResult): void
151
    {
152
        $actualResult = Attributes::replaceItem($attributes, ...array_values($attributes2));
153
154
        $this->assertEquals($expectedResult, $actualResult);
155
    }
156
157
    /**
158
     * @return array[]
159
     */
160
    public function isEqualProvider(): array
161
    {
162
        return [
163
            'key-mismatch'          => [
164
                [
165
                    'foo' => new Attribute('foo'),
166
                ],
167
                [
168
                    'bar' => new Attribute('bar'),
169
                ],
170
                false,
171
            ],
172
            'value-mismatch'        => [
173
                [
174
                    'foo' => new Attribute('foo', 'bar'),
175
                ],
176
                [
177
                    'foo' => new Attribute('foo', 'bar', 'baz'),
178
                ],
179
                false,
180
            ],
181
            'second-value-mismatch' => [
182
                [
183
                    'foo' => new Attribute('foo', 'bar'),
184
                    'bar' => new Attribute('bar', ''),
185
                ],
186
                [
187
                    'foo' => new Attribute('foo', 'bar'),
188
                    'bar' => new Attribute('bar'),
189
                ],
190
                false,
191
            ],
192
            'two-attributes'        => [
193
                [
194
                    'foo' => new Attribute('foo', 'bar'),
195
                    'bar' => new Attribute('bar', 'foo', 'baz'),
196
                ],
197
                [
198
                    'foo' => new Attribute('foo', 'bar'),
199
                    'bar' => new Attribute('bar', 'foo', 'baz'),
200
                ],
201
                true,
202
            ],
203
        ];
204
    }
205
206
    /**
207
     * @dataProvider isEqualProvider
208
     *
209
     * @param array<string,Attribute> $attributes
210
     * @param array<string,Attribute> $attributes2
211
     * @param bool                    $expectedResult
212
     */
213
    public function testIsEqual(array $attributes, array $attributes2, bool $expectedResult): void
214
    {
215
        $actualResult = Attributes::isEqual($attributes, $attributes2);
216
217
        $this->assertEquals($expectedResult, $actualResult);
218
    }
219
220
    /**
221
     * @return array[]
222
     */
223
    public function toStringProvider(): array
224
    {
225
        return [
226
            'empty'            => [[], ''],
227
            'null-attribute'   => [['foo' => new Attribute('foo')], ' foo'],
228
            'empty-attribute'  => [['foo' => new Attribute('foo', '')], ' foo=""'],
229
            'simple-attribute' => [['foo' => new Attribute('foo', 'bar')], ' foo="bar"'],
230
            'two-attributes'   => [
231
                [
232
                    'foo' => new Attribute('foo', 'bar'),
233
                    'bar' => new Attribute('bar', 'foo', 'baz'),
234
                ],
235
                ' foo="bar" bar="foo baz"',
236
            ],
237
        ];
238
    }
239
240
    /**
241
     * @dataProvider toStringProvider
242
     *
243
     * @param array<string,Attribute> $attributes
244
     * @param string                  $expectedResult
245
     */
246
    public function testToString(array $attributes, string $expectedResult): void
247
    {
248
        $actualResult = Attributes::toString($attributes);
249
250
        $this->assertEquals($expectedResult, $actualResult);
251
    }
252
}
253