Passed
Push — html ( 51e1ba...d230bc )
by Peter
02:59
created

AttributesTest::testMergeFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
            'null'                    => [
16
                null,
17
                [],
18
            ],
19
            'empty'                   => [
20
                [],
21
                [],
22
            ],
23
            'single-null-attribute'   => [
24
                ['foo' => null],
25
                ['foo' => new Attribute('foo')],
26
            ],
27
            'single-string-attribute' => [
28
                ['foo' => 'bar'],
29
                ['foo' => new Attribute('foo', 'bar')],
30
            ],
31
            'single-array-attribute'  => [
32
                ['foo' => ['bar', 'baz']],
33
                ['foo' => new Attribute('foo', 'bar', 'baz')],
34
            ],
35
            'mixed-attributes'        => [
36
                ['foo' => ['bar', 'baz'], 'bar' => null],
37
                ['foo' => new Attribute('foo', 'bar', 'baz'), 'bar' => new Attribute('bar')],
38
            ],
39
        ];
40
    }
41
42
    /**
43
     * @dataProvider fromArrayProvider
44
     *
45
     * @param array|null              $attributes
46
     * @param array<string,Attribute> $expectedResult
47
     */
48
    public function testFromArray(?array $attributes, array $expectedResult): void
49
    {
50
        $actualResult = Attributes::fromArray($attributes);
51
52
        $this->assertEquals($expectedResult, $actualResult);
53
    }
54
55
    public function testFromArrayThrowExceptionIfItReceivesAnAttribute(): void
56
    {
57
        $this->expectException(\AssertionError::class);
58
59
        $attributes = [
60
            'key' => new Attribute('key'),
61
        ];
62
63
        Attributes::fromArray($attributes);
64
    }
65
66
    public function testFromArrayThrowExceptioIfIReceivesANonStringScalar(): void
67
    {
68
        $this->expectException(\AssertionError::class);
69
70
        $attributes = [
71
            'key' => false,
72
        ];
73
74
        Attributes::fromArray($attributes);
75
    }
76
77
    /**
78
     * @return array[]
79
     */
80
    public function mergeProvider(): array
81
    {
82
        $foo0  = new Attribute('foo');
83
        $foo1  = new Attribute('foo', 'bar');
84
        $foo2  = new Attribute('foo', 'baz');
85
        $foo12 = new Attribute('foo', 'bar', 'baz');
86
        $bar1  = new Attribute('bar', 'foo');
87
        $bar11 = new Attribute('bar', 'foo', 'foo');
88
89
        return [
90
            'all empty'                  => [[], [], []],
91
            'combine empty attributes 1' => [['foo' => $foo1], ['foo' => $foo0], ['foo' => $foo1]],
92
            'combine empty attributes 2' => [['foo' => $foo0], ['foo' => $foo1], ['foo' => $foo1]],
93
            'combine attributes'         => [['foo' => $foo1], ['bar' => $bar1], ['foo' => $foo1, 'bar' => $bar1]],
94
            'combine attribute values 1' => [['foo' => $foo1], ['foo' => $foo2], ['foo' => $foo12]],
95
            'combine attribute values 2' => [
96
                ['foo' => $foo1, 'bar' => $bar1],
97
                ['foo' => $foo2, 'bar' => $bar1],
98
                ['foo' => $foo12, 'bar' => $bar11],
99
            ],
100
        ];
101
    }
102
103
    /**
104
     * @dataProvider mergeProvider
105
     *
106
     * @param array<string,Attribute> $attributes
107
     * @param array<string,Attribute> $attributes2
108
     * @param array<string,Attribute> $expectedResult
109
     */
110
    public function testMerge(array $attributes, array $attributes2, array $expectedResult): void
111
    {
112
        $actualResult = Attributes::merge($attributes, $attributes2);
113
114
        $this->assertEquals($expectedResult, $actualResult);
115
    }
116
117
    /**
118
     * @dataProvider mergeProvider
119
     *
120
     * @param array<string,Attribute> $attributes
121
     * @param array<string,Attribute> $attributes2
122
     * @param array<string,Attribute> $expectedResult
123
     */
124
    public function testMergeItem(array $attributes, array $attributes2, array $expectedResult): void
125
    {
126
        $actualResult = Attributes::mergeItem($attributes, ...array_values($attributes2));
127
128
        $this->assertEquals($expectedResult, $actualResult);
129
    }
130
131
    /**
132
     * @return array[]
133
     */
134
    public function replaceProvider(): array
135
    {
136
        $foo0 = new Attribute('foo');
137
        $foo1 = new Attribute('foo', 'bar');
138
        $foo2 = new Attribute('foo', 'baz');
139
        $bar1 = new Attribute('bar', 'foo');
140
141
        return [
142
            'all empty'                  => [[], [], []],
143
            'combine empty attributes 1' => [['foo' => $foo1], ['foo' => $foo0], ['foo' => $foo0]],
144
            'combine empty attributes 2' => [['foo' => $foo0], ['foo' => $foo1], ['foo' => $foo1]],
145
            'combine attributes'         => [['foo' => $foo1], ['bar' => $bar1], ['foo' => $foo1, 'bar' => $bar1]],
146
            'combine attribute values 1' => [['foo' => $foo1], ['foo' => $foo2], ['foo' => $foo2]],
147
            'combine attribute values 2' => [
148
                ['foo' => $foo1, 'bar' => $bar1],
149
                ['foo' => $foo2, 'bar' => $bar1],
150
                ['foo' => $foo2, 'bar' => $bar1],
151
            ],
152
        ];
153
    }
154
155
    /**
156
     * @dataProvider replaceProvider
157
     *
158
     * @param array<string,Attribute> $attributes
159
     * @param array<string,Attribute> $attributes2
160
     * @param array<string,Attribute> $expectedResult
161
     */
162
    public function testReplace(array $attributes, array $attributes2, array $expectedResult): void
163
    {
164
        $actualResult = Attributes::replace($attributes, $attributes2);
165
166
        $this->assertEquals($expectedResult, $actualResult);
167
    }
168
169
    /**
170
     * @dataProvider replaceProvider
171
     *
172
     * @param array<string,Attribute> $attributes
173
     * @param array<string,Attribute> $attributes2
174
     * @param array<string,Attribute> $expectedResult
175
     */
176
    public function testReplaceItem(array $attributes, array $attributes2, array $expectedResult): void
177
    {
178
        $actualResult = Attributes::replaceItem($attributes, ...array_values($attributes2));
179
180
        $this->assertEquals($expectedResult, $actualResult);
181
    }
182
183
    /**
184
     * @return array[]
185
     */
186
    public function mergeFailureProvider(): array
187
    {
188
        $foo0  = new Attribute('foo');
189
        $foo1  = new Attribute('foo', 'bar');
190
        $foo2  = new Attribute('foo', 'baz');
191
        $foo12 = new Attribute('foo', 'bar', 'baz');
192
        $bar1  = new Attribute('bar', 'foo');
193
        $bar11 = new Attribute('bar', 'foo', 'foo');
194
195
        return [
196
            'all empty'                  => [[], [], []],
197
            'combine empty attributes 1' => [['foo' => $foo1], ['foo' => $foo0], ['foo' => $foo1]],
198
            'combine empty attributes 2' => [['foo' => $foo0], ['foo' => $foo1], ['foo' => $foo1]],
199
            'combine attributes'         => [['foo' => $foo1], ['bar' => $bar1], ['foo' => $foo1, 'bar' => $bar1]],
200
            'combine attribute values 1' => [['foo' => $foo1], ['foo' => $foo2], ['foo' => $foo12]],
201
            'combine attribute values 2' => [
202
                ['foo' => $foo1, 'bar' => $bar1],
203
                ['foo' => $foo2, 'bar' => $bar1],
204
                ['foo' => $foo12, 'bar' => $bar11],
205
            ],
206
        ];
207
    }
208
209
    /**
210
     * @dataProvider mergeFailureProvider
211
     *
212
     * @param array<string,Attribute> $attributes
213
     * @param array<string,Attribute> $attributes2
214
     * @param array<string,Attribute> $expectedResult
215
     */
216
    public function testMergeFailure(array $attributes, array $attributes2, array $expectedResult): void
217
    {
218
        $actualResult = Attributes::merge($attributes, $attributes2);
219
220
        $this->assertEquals($expectedResult, $actualResult);
221
    }
222
223
    /**
224
     * @return array[]
225
     */
226
    public function isEqualProvider(): array
227
    {
228
        return [
229
            'key-mismatch'          => [
230
                [
231
                    'foo' => new Attribute('foo'),
232
                ],
233
                [
234
                    'bar' => new Attribute('bar'),
235
                ],
236
                false,
237
            ],
238
            'value-mismatch'        => [
239
                [
240
                    'foo' => new Attribute('foo', 'bar'),
241
                ],
242
                [
243
                    'foo' => new Attribute('foo', 'bar', 'baz'),
244
                ],
245
                false,
246
            ],
247
            'second-value-mismatch' => [
248
                [
249
                    'foo' => new Attribute('foo', 'bar'),
250
                    'bar' => new Attribute('bar', ''),
251
                ],
252
                [
253
                    'foo' => new Attribute('foo', 'bar'),
254
                    'bar' => new Attribute('bar'),
255
                ],
256
                false,
257
            ],
258
            'two-attributes'        => [
259
                [
260
                    'foo' => new Attribute('foo', 'bar'),
261
                    'bar' => new Attribute('bar', 'foo', 'baz'),
262
                ],
263
                [
264
                    'foo' => new Attribute('foo', 'bar'),
265
                    'bar' => new Attribute('bar', 'foo', 'baz'),
266
                ],
267
                true,
268
            ],
269
        ];
270
    }
271
272
    /**
273
     * @dataProvider isEqualProvider
274
     *
275
     * @param array<string,Attribute> $attributes
276
     * @param array<string,Attribute> $attributes2
277
     * @param bool                    $expectedResult
278
     */
279
    public function testIsEqual(array $attributes, array $attributes2, bool $expectedResult): void
280
    {
281
        $actualResult = Attributes::isEqual($attributes, $attributes2);
282
283
        $this->assertEquals($expectedResult, $actualResult);
284
    }
285
286
    /**
287
     * @return array[]
288
     */
289
    public function toStringProvider(): array
290
    {
291
        return [
292
            'empty'            => [[], ''],
293
            'null-attribute'   => [['foo' => new Attribute('foo')], ' foo'],
294
            'empty-attribute'  => [['foo' => new Attribute('foo', '')], ' foo=""'],
295
            'simple-attribute' => [['foo' => new Attribute('foo', 'bar')], ' foo="bar"'],
296
            'two-attributes'   => [
297
                [
298
                    'foo' => new Attribute('foo', 'bar'),
299
                    'bar' => new Attribute('bar', 'foo', 'baz'),
300
                ],
301
                ' foo="bar" bar="foo baz"',
302
            ],
303
        ];
304
    }
305
306
    /**
307
     * @dataProvider toStringProvider
308
     *
309
     * @param array<string,Attribute> $attributes
310
     * @param string                  $expectedResult
311
     */
312
    public function testToString(array $attributes, string $expectedResult): void
313
    {
314
        $actualResult = Attributes::toString($attributes);
315
316
        $this->assertEquals($expectedResult, $actualResult);
317
    }
318
}
319