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

SelectTest::testRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 17
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Element;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Component\Option;
9
use AbterPhp\Framework\Html\Attribute;
10
use AbterPhp\Framework\Html\Helper\Attributes;
11
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory;
12
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory;
13
use InvalidArgumentException;
14
use PHPUnit\Framework\TestCase;
15
use stdClass;
16
17
class SelectTest extends TestCase
18
{
19
    /**
20
     * @return array[]
21
     */
22
    public function renderProvider(): array
23
    {
24
        $attribs = StubAttributeFactory::createAttributes();
25
        $str     = Attributes::toString($attribs);
26
27
        return [
28
            'simple'               => [
29
                'abc',
30
                'bcd',
31
                'val',
32
                [],
33
                null,
34
                null,
35
                null,
36
                '<select id="abc" name="bcd"></select>',
37
            ],
38
            'missing translations' => [
39
                'abc',
40
                'bcd',
41
                'val',
42
                [],
43
                null,
44
                [],
45
                null,
46
                '<select id="abc" name="bcd"></select>',
47
            ],
48
            'extra attributes'     => [
49
                'abc',
50
                'bcd',
51
                'val',
52
                [],
53
                $attribs,
54
                [],
55
                null,
56
                "<select$str id=\"abc\" name=\"bcd\"></select>",
57
            ],
58
            'options'              => [
59
                'abc',
60
                'bcd',
61
                'val',
62
                ['bde' => 'BDE', 'cef' => 'CEF'],
63
                $attribs,
64
                [],
65
                null,
66
                "<select$str id=\"abc\" name=\"bcd\"><option value=\"bde\">BDE</option>\n<option value=\"cef\">CEF</option></select>", // phpcs:ignore
67
            ],
68
            'option selected'      => [
69
                'abc',
70
                'bcd',
71
                'cef',
72
                ['bde' => 'BDE', 'cef' => 'CEF'],
73
                $attribs,
74
                [],
75
                null,
76
                "<select$str id=\"abc\" name=\"bcd\"><option value=\"bde\">BDE</option>\n<option value=\"cef\" selected>CEF</option></select>", // phpcs:ignore
77
            ],
78
        ];
79
    }
80
81
    /**
82
     * @dataProvider renderProvider
83
     *
84
     * @param string               $inputId
85
     * @param string               $name
86
     * @param string               $value
87
     * @param array<string,string> $options
88
     * @param Attribute[]|null     $attributes
89
     * @param string[]|null        $translations
90
     * @param string|null          $tag
91
     * @param string               $expectedResult
92
     */
93
    public function testRender(
94
        string $inputId,
95
        string $name,
96
        string $value,
97
        array $options,
98
        ?array $attributes,
99
        ?array $translations,
100
        ?string $tag,
101
        string $expectedResult
102
    ): void {
103
        $sut = $this->createSelect($inputId, $name, $value, $options, $attributes, $translations, $tag);
104
105
        $actualResult   = (string)$sut;
106
        $repeatedResult = (string)$sut;
107
108
        $this->assertSame($actualResult, $repeatedResult);
109
        $this->assertSame($expectedResult, $actualResult);
110
    }
111
112
    public function testSetValueSetsOptionsSelected(): void
113
    {
114
        $sut = new Select('id', 'name');
115
116
        $option1 = new Option('1', 'foo', true);
117
        $option2 = new Option('2', 'bar', false);
118
119
        $sut->add($option1, $option2);
120
121
        $sut->setValue('2');
122
123
        $this->assertStringNotContainsString(Html5::ATTR_SELECTED, (string)$option1);
124
        $this->assertStringContainsString(Html5::ATTR_SELECTED, (string)$option2);
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function setValueFailureProvider(): array
131
    {
132
        return [
133
            'array'    => [[]],
134
            'stdclass' => [new stdClass()],
135
            'int'      => [123],
136
            'bool'     => [false],
137
            'float'    => [123.53],
138
        ];
139
    }
140
141
    /**
142
     * @dataProvider setValueFailureProvider
143
     *
144
     * @param mixed $value
145
     */
146
    public function testSetValueThrowsExceptionOnInvalid($value): void
147
    {
148
        $this->expectException(InvalidArgumentException::class);
149
150
        $sut = new Select('id', 'foo');
151
152
        $sut->setValue($value);
153
    }
154
155
    public function testRemoveAttributeThrowsExceptionWhenTryingToRemoveProtectedAttributes(): void
156
    {
157
        $this->expectException(\RuntimeException::class);
158
159
        $sut = new Select('id', 'foo');
160
161
        $sut->removeAttribute(Html5::ATTR_NAME);
162
    }
163
164
    public function testGetName(): void
165
    {
166
        $expectedResult = 'foo';
167
168
        $sut = new Select('id', $expectedResult);
169
170
        $actualResult = $sut->getName();
171
172
        $this->assertEquals($expectedResult, $actualResult);
173
    }
174
175
    public function testGetGetValue(): void
176
    {
177
        $expectedResult = 'bar';
178
179
        $sut = $this->createSelect('id', 'name', $expectedResult, ['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz']);
180
181
        $actualResult = $sut->getValue();
182
183
        $this->assertEquals($expectedResult, $actualResult);
184
    }
185
186
    public function testSetContent(): void
187
    {
188
        $expectedResult = 'bar';
189
190
        $sut = $this->createSelect('id', 'name', 'bar', ['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz']);
191
192
        $actualResult = $sut->getValue();
193
194
        $this->assertEquals($expectedResult, $actualResult);
195
    }
196
197
    public function testGetNameReturnEmptyStringIfAttributeIsNull(): void
198
    {
199
        $expectedResult = '';
200
201
        $sut = new Select('id', 'foo');
202
203
        $sut->getAttribute(Html5::ATTR_NAME)->reset();
204
205
        $actualResult = $sut->getName();
206
207
        $this->assertEquals($expectedResult, $actualResult);
208
    }
209
210
    /**
211
     * @param string               $inputId
212
     * @param string               $name
213
     * @param string               $value
214
     * @param array<string,string> $options
215
     * @param Attribute[]|null     $attributes
216
     * @param string[]|null        $translations
217
     * @param string|null          $tag
218
     *
219
     * @return Select
220
     */
221
    protected function createSelect(
222
        string $inputId,
223
        string $name,
224
        string $value,
225
        array $options,
226
        ?array $attributes = null,
227
        ?array $translations = null,
228
        ?string $tag = null
229
    ): Select {
230
        $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations);
231
232
        $select = new Select($inputId, $name, [], $attributes, $tag);
233
234
        foreach ($options as $k => $v) {
235
            $select->add(new Option($k, $v, $value == $k));
236
        }
237
238
        $select->setTranslator($translatorMock);
239
240
        return $select;
241
    }
242
}
243