WCAGElementTest::testButtonWithRole()   A
last analyzed

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
use PHPUnit\Framework\TestCase;
4
use HexMakina\Marker\WCAGElement;
5
6
class WCAGElementTest extends TestCase
7
{
8
    public function testImgWithAlt()
9
    {
10
        $src = 'image.jpg';
11
        $alt = 'An image';
12
        $attributes = ['class' => 'img-class'];
13
14
        $result = WCAGElement::img($src, $alt, $attributes);
15
16
        $this->assertStringStartsWith('<img', $result);
17
        $this->assertStringContainsString('src="image.jpg"', $result);
18
        $this->assertStringContainsString('alt="An image"', $result);
19
        $this->assertStringContainsString('class="img-class"', $result);
20
        $this->assertStringEndsWith('/>', $result);
21
    }
22
23
    public function testImgWithEmptyAlt()
24
    {
25
        $expected = '<img src="image.jpg" alt=""/>';
26
27
        $img = WCAGElement::img('image.jpg');
28
        $this->assertEquals($expected, (string)$img);
29
30
        $img = WCAGElement::img('image.jpg', '');
31
        $this->assertEquals($expected, (string)$img);
32
33
        $img = WCAGElement::img('image.jpg', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $alt of HexMakina\Marker\WCAGElement::img(). ( Ignorable by Annotation )

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

33
        $img = WCAGElement::img('image.jpg', /** @scrutinizer ignore-type */ false);
Loading history...
34
        $this->assertEquals($expected, (string)$img);
35
36
        $img = WCAGElement::img('image.jpg', 0);
37
        $this->assertEquals('<img src="image.jpg" alt="0"/>', (string)$img);
38
    }
39
40
    public function testFigureWithCaption()
41
    {
42
        $content = '<img src="image.jpg" alt="An image">';
43
        $caption = 'This is a caption';
44
        $attributes = ['class' => 'figure-class'];
45
46
        $result = (string)WCAGElement::figure($content, $caption, $attributes);
47
48
        $this->assertStringContainsString('<figcaption>This is a caption</figcaption>', $result);
49
        $this->assertStringContainsString('class="figure-class"', $result);
50
    }
51
52
    public function testFigureWithoutCaption()
53
    {
54
        $this->expectException(\InvalidArgumentException::class);
55
        $this->expectExceptionMessage("The <figcaption> is required inside <figure>.");
56
57
        WCAGElement::figure('<img src="image.jpg" alt="An image">', '');
58
    }
59
60
    public function testButtonWithContent()
61
    {
62
        $content = 'Click me';
63
        $attributes = ['class' => 'btn-class'];
64
65
        $result = WCAGElement::button($content, $attributes);
66
67
        $this->assertStringContainsString('Click me', $result);
68
        $this->assertStringContainsString('class="btn-class"', $result);
69
        $this->assertStringContainsString('role="button"', $result);
70
    }
71
72
    public function testButtonWithoutContent()
73
    {
74
        $this->expectException(\InvalidArgumentException::class);
75
        $this->expectExceptionMessage("Button content is required.");
76
77
        WCAGElement::button('');
78
    }
79
80
    public function testButtonWithRole()
81
    {
82
        $result = WCAGElement::button('Click me', ['role' => 'submit']);
83
        $this->assertStringContainsString('role="submit"', $result);
84
    }
85
86
    public function testInput()
87
    {
88
        $result = WCAGElement::input('text', 'name', ['class' => 'input-class', 'value' => '1']);
89
        $this->assertStringStartsWith('<input', $result);
90
        $this->assertStringContainsString('type="text"', $result);
91
        $this->assertStringContainsString('name="name"', $result);
92
        $this->assertStringContainsString('value="1"', $result);
93
        $this->assertStringContainsString('class="input-class"', $result);
94
        $this->assertStringEndsWith('/>', $result);
95
    }
96
97
    public function testInputWithoutType()
98
    {
99
        $this->expectException(\InvalidArgumentException::class);
100
        $this->expectExceptionMessage("Input type is required");
101
102
        WCAGElement::input('', 'name');
103
    }
104
105
    public function testInputWithoutName()
106
    {
107
        $this->expectException(\InvalidArgumentException::class);
108
        $this->expectExceptionMessage("Input name is required");
109
110
        WCAGElement::input('text', '');
111
    }
112
113
    public function testInputWithRequired()
114
    {
115
        $result = WCAGElement::input('text', 'name', ['required' => 'required']);
116
        $this->assertStringContainsString('required', $result);
117
        $this->assertStringContainsString('aria-required="true"', $result);
118
119
        $result = WCAGElement::input('text', 'name', ['required']);
120
        $this->assertStringContainsString('required', $result);
121
        $this->assertStringContainsString('aria-required="true"', $result);
122
123
        $result = WCAGElement::input('text', 'name', ['required' => '']);
124
        $this->assertStringContainsString('required', $result);
125
        $this->assertStringNotContainsString('aria-required="true"', $result);
126
    }
127
128
    public function testAWithHref()
129
    {
130
        $content = 'Click me';
131
        $href = 'https://example.com';
132
        $attributes = ['class' => 'link-class'];
133
134
        $result = WCAGElement::a($href, $content, $attributes);
135
        $this->assertStringStartsWith('<a', $result);
136
        $this->assertStringContainsString('href="https://example.com"', $result);
137
        $this->assertStringContainsString('class="link-class"', $result);
138
        $this->assertStringContainsString('Click me', $result);
139
        $this->assertStringEndsWith('</a>', $result);
140
    }
141
142
    public function testAWithoutHref()
143
    {
144
        $this->expectException(\InvalidArgumentException::class);
145
        $this->expectExceptionMessage("Anchor href is required");
146
147
        WCAGElement::a('', 'Click me');
148
    }
149
150
    public function testArea()
151
    {
152
        $result = WCAGElement::area('An area', ['shape' => 'rect', 'coords' => '34,44,270,350']);
153
        $this->assertStringStartsWith('<area', $result);
154
        $this->assertStringContainsString('alt="An area"', $result);
155
        $this->assertStringContainsString('shape="rect"', $result);
156
        $this->assertStringContainsString('coords="34,44,270,350"', $result);
157
        $this->assertStringEndsWith('/>', $result);
158
    }
159
160
    public function testAreaWithoutAlt()
161
    {
162
        $this->expectException(\InvalidArgumentException::class);
163
        $this->expectExceptionMessage("Area alt is required");
164
165
        WCAGElement::area('', ['shape' => 'rect', 'coords' => '34,44,270,350']);
166
    }
167
168
    public function testSelect()
169
    {
170
        $result = WCAGElement::select('name', [], ['class' => 'select-class']);
171
        $this->assertStringStartsWith('<select', $result);
172
        $this->assertStringContainsString('name="name"', $result);
173
        $this->assertStringContainsString('class="select-class"', $result);
174
        $this->assertStringEndsWith('</select>', $result);
175
    }
176
177
    public function testSelectWithoutName()
178
    {
179
        $this->expectException(\InvalidArgumentException::class);
180
        $this->expectExceptionMessage("Select name is required");
181
182
        WCAGElement::select('', []);
183
    }
184
185
    public function testIframe()
186
    {
187
        $result = (string)WCAGElement::iframe('https://example.com', 'title-test', ['class' => 'iframe-class']);
188
        $this->assertStringStartsWith('<iframe', $result);
189
        $this->assertStringContainsString('src="https://example.com"', $result);
190
        $this->assertStringContainsString('class="iframe-class"', $result);
191
        $this->assertStringContainsString('title="title-test"', $result);
192
        $this->assertStringEndsWith('/>', $result);
193
    }
194
195
    public function testAudio()
196
    {
197
        $result = (string)WCAGElement::audio('audio.mp3', ['controls' => 'controls', 'class' => 'audio-class']);
198
        $this->assertStringStartsWith('<audio', $result);
199
        $this->assertStringContainsString('src="audio.mp3"', $result);
200
        $this->assertStringContainsString('controls="controls"', $result);
201
        $this->assertStringContainsString('class="audio-class"', $result);
202
        $this->assertStringEndsWith('/>', $result);
203
204
        $result = WCAGElement::audio('audio.mp3', ['controls' => '']);
205
        $this->assertStringContainsString('controls=""', $result);
206
    }
207
208
    public function testAudioWithoutSrc()
209
    {
210
        $this->expectException(\InvalidArgumentException::class);
211
        $this->expectExceptionMessage("Source attribute is required");
212
213
        WCAGElement::audio('', []);
214
    }
215
216
    public function testVideo()
217
    {
218
        $result = (string)WCAGElement::video('video.mp4', ['class' => 'video-class']);
219
        $this->assertStringStartsWith('<video', $result);
220
        $this->assertStringContainsString('src="video.mp4"', $result);
221
        $this->assertStringContainsString('controls="controls"', $result);
222
        $this->assertStringContainsString('class="video-class"', $result);
223
        $this->assertStringEndsWith('/>', $result);
224
    }
225
226
    public function testVideoWithoutSrc()
227
    {
228
        $this->expectException(\InvalidArgumentException::class);
229
        $this->expectExceptionMessage("Source attribute is required");
230
231
        WCAGElement::video('', []);
232
    }
233
234
    public function testHTML()
235
    {
236
        $this->expectException(\InvalidArgumentException::class);
237
        $this->expectExceptionMessage("Language attribute is required");
238
        $result = (string)WCAGElement::html('html content');
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
239
240
        $result = (string)WCAGElement::html('html content', ['lang' => 'en']);
241
        $this->assertStringContainsString('lang="en"', $result);
242
243
    }
244
245
    public function testTh()
246
    {
247
        $this->expectException(\InvalidArgumentException::class);
248
        $this->expectExceptionMessage("Scope attribute is required");
249
        $result = (string)WCAGElement::th('', 'Header');
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
250
251
        $this->expectException(\InvalidArgumentException::class);
252
        $this->expectExceptionMessage("Header content is required");
253
        $result = (string)WCAGElement::th('col', '');
254
255
        $result = (string)WCAGElement::th('col', 'Header');
256
        $this->assertStringContainsString('scope="col"', $result);
257
    }
258
}