Passed
Branch develop (bae466)
by Paul
06:12
created

BuilderTest::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Compatibility;
6
use GeminiLabs\SiteReviews\Contracts\BuilderContract;
7
use GeminiLabs\SiteReviews\Controllers\PublicController;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
10
/**
11
 * Test case for the Plugin.
12
 * @group plugin
13
 */
14
class BuilderTest extends \WP_UnitTestCase
0 ignored issues
show
Bug introduced by
The type WP_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
{
16
    public function test_build_html_element(): void
17
    {
18
        $this->assertEquals('<span></span>', $this->builder()->span());
19
        $this->assertEquals('<span>foo</span>', $this->builder()->span('foo'));
20
        $this->assertEquals(
21
            '<span class="xxx">foo</span>',
22
            $this->builder()->span('foo', [
23
                'class' => 'xxx',
24
            ])
25
        );
26
        $this->assertEquals(
27
            '<span class="xxx">foo</span>',
28
            $this->builder()->span('foo', [
29
                'class' => 'xxx',
30
                'text' => 'bar',
31
            ])
32
        );
33
        $this->assertEquals(
34
            '<span class="xxx"></span>',
35
            $this->builder()->span([
36
                'class' => 'xxx',
37
                'foo' => 'bar',
38
            ])
39
        );
40
    }
41
42
    public function test_build_input(): void
43
    {
44
        $this->assertEquals(
45
            '<input type="text" value="" />',
46
            $this->builder()->input()
47
        );
48
        $this->assertEquals(
49
            '<input type="text" id="foo" name="foo" value="" />',
50
            $this->builder()->input([
51
                'id' => 'foo',
52
                'name' => 'foo',
53
            ])
54
        );
55
        $this->assertEquals(
56
            '<input type="submit" id="foo" name="bar" value="" />',
57
            $this->builder()->input([
58
                'id' => 'foo',
59
                'max' => 10, // invalid for submit type
60
                'min' => 0, // invalid for submit type
61
                'name' => 'bar',
62
                'type' => 'submit',
63
                'step' => 2, // invalid for submit type
64
            ])
65
        );
66
    }
67
68
    public function test_build_input_choice(): void
69
    {
70
        $this->assertEquals(
71
            '<input type="checkbox" id="foo" name="foo" value="" />',
72
            $this->builder()->input([
73
                'id' => 'foo',
74
                'name' => 'foo',
75
                'type' => 'checkbox',
76
            ])
77
        );
78
        $this->assertEquals(
79
            '<label for="foo">'.
80
                '<input type="checkbox" id="foo" checked name="foo" value="1" /> bar'.
81
            '</label>',
82
            $this->builder()->input([
83
                'checked' => true,
84
                'id' => 'foo',
85
                'label' => 'bar',
86
                'name' => 'foo',
87
                'type' => 'checkbox',
88
                'value' => 1,
89
            ])
90
        );
91
    }
92
93
    public function test_build_input_choices(): void
94
    {
95
        $this->assertEquals(
96
            '<label for="foo-1">'.
97
                '<input type="checkbox" id="foo-1" name="foo" value="a" /> A'.
98
            '</label>',
99
            $this->builder()->input([
100
                'id' => 'foo',
101
                'name' => 'foo',
102
                'options' => [
103
                    'a' => 'A',
104
                ],
105
                'type' => 'checkbox',
106
            ])
107
        );
108
        $this->assertEquals(
109
            '<label for="foo-1">'.
110
                '<input type="checkbox" id="foo-1" name="foo" value="a" /> A'.
111
            '</label>'.
112
            '<label for="foo-2">'.
113
                '<input type="checkbox" id="foo-2" name="foo" value="b" /> B'.
114
            '</label>',
115
            $this->builder()->input([
116
                'id' => 'foo',
117
                'name' => 'foo',
118
                'options' => [
119
                    'a' => 'A',
120
                    'b' => 'B',
121
                ],
122
                'type' => 'checkbox',
123
            ])
124
        );
125
        $this->assertEquals(
126
            '<label for="foo-1">'.
127
                '<input type="checkbox" id="foo-1" name="foo" value="a" /> A, description'.
128
            '</label>'.
129
            '<label for="foo-2">'.
130
                '<input type="checkbox" id="foo-2" name="foo" value="b" /> B, description'.
131
            '</label>',
132
            $this->builder()->input([
133
                'id' => 'foo',
134
                'name' => 'foo',
135
                'options' => [
136
                    'a' => ['A', 'description'],
137
                    'b' => ['B', 'description'],
138
                ],
139
                'type' => 'checkbox',
140
            ])
141
        );
142
    }
143
144
    public function test_build_input_choices_where_value_determines_checked(): void
145
    {
146
        $this->assertEquals(
147
            '<label for="foo-1">'.
148
                '<input type="checkbox" id="foo-1" name="foo" value="a" /> A'.
149
            '</label>',
150
            $this->builder()->input([
151
                'id' => 'foo',
152
                'label' => 'bar',
153
                'name' => 'foo',
154
                'options' => [
155
                    'a' => 'A',
156
                ],
157
                'type' => 'checkbox',
158
                'value' => 1,
159
            ])
160
        );
161
        $this->assertEquals(
162
            '<label for="foo-1">'.
163
                '<input type="checkbox" id="foo-1" checked name="foo" value="a" /> A'.
164
            '</label>'.
165
            '<label for="foo-2">'.
166
                '<input type="checkbox" id="foo-2" name="foo" value="b" /> B'.
167
            '</label>',
168
            $this->builder()->input([
169
                'id' => 'foo',
170
                'label' => 'bar',
171
                'name' => 'foo',
172
                'options' => [
173
                    'a' => 'A',
174
                    'b' => 'B',
175
                ],
176
                'type' => 'checkbox',
177
                'value' => 'a',
178
            ])
179
        );
180
        $this->assertEquals(
181
            '<label for="foo-1">'.
182
                '<input type="checkbox" id="foo-1" checked name="foo" value="a" /> A'.
183
            '</label>'.
184
            '<label for="foo-2">'.
185
                '<input type="checkbox" id="foo-2" checked name="foo" value="b" /> B'.
186
            '</label>',
187
            $this->builder()->input([
188
                'id' => 'foo',
189
                'label' => 'bar',
190
                'name' => 'foo',
191
                'options' => [
192
                    'a' => 'A',
193
                    'b' => 'B',
194
                ],
195
                'type' => 'checkbox',
196
                'value' => ['a', 'b'],
197
            ])
198
        );
199
    }
200
201
    public function test_build_input_with_invalid_type(): void
202
    {
203
        $this->assertEquals(
204
            '<label for="foo">bar</label>'.
205
            '<input type="text" id="foo" name="foo" value="1" />',
206
            $this->builder()->input([
207
                'id' => 'foo',
208
                'label' => 'bar',
209
                'name' => 'foo',
210
                'type' => 'rating', // invalid input type
211
                'value' => 1,
212
            ])
213
        );
214
    }
215
216
    public function test_build_select(): void
217
    {
218
        $this->assertEquals(
219
            '<label for="foo">Select one</label>'.
220
            '<select id="foo" name="bar">'.
221
                '<option value="3">Three</option>'.
222
                '<option title="The Number 8" value="8">Eight</option>'.
223
                '<optgroup label="9">'.
224
                    '<option value="0">Nine</option>'.
225
                '</optgroup>'.
226
                '<optgroup label="Letters">'.
227
                    '<option value="a">A</option>'.
228
                    '<option value="b">B</option>'.
229
                    '<option title="The Letter C" value="c">C</option>'.
230
                '</optgroup>'.
231
            '</select>',
232
            $this->builder()->select([
233
                'id' => 'foo',
234
                'label' => 'Select one',
235
                'name' => 'bar',
236
                'options' => [
237
                    3 => 'Three',
238
                    8 => [
239
                        'text' => 'Eight',
240
                        'title' => 'The Number 8',
241
                    ],
242
                    9 => [
243
                        'Nine',
244
                    ],
245
                    'Letters' => [
246
                        'a' => 'A',
247
                        'b' => 'B',
248
                        'c' => [
249
                            'text' => 'C',
250
                            'title' => 'The Letter C',
251
                        ],
252
                        [
253
                            'd' => 'D',
254
                            'e' => 'E',
255
                            'f' => [
256
                                'text' => 'F',
257
                                'title' => 'The Letter F',
258
                            ],
259
                        ],
260
                    ],
261
                ],
262
            ])
263
        );
264
    }
265
266
    public function test_build_textarea(): void
267
    {
268
        $this->assertEquals(
269
            '<label for="foo">bar</label>'.
270
            '<textarea id="foo" name="foo">foobar</textarea>',
271
            $this->builder()->textarea([
272
                'id' => 'foo',
273
                'label' => 'bar',
274
                'name' => 'foo',
275
                'value' => 'foobar',
276
            ])
277
        );
278
    }
279
280
    protected function builder(): BuilderContract
281
    {
282
        return new Builder();
283
    }
284
}
285