Passed
Push — html ( d230bc...4d98c2 )
by Peter
03:13
created

testFromArrayThrowsExceptionIfIReceivesANonStringScalar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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