Passed
Push — html ( 1dd7b6 )
by Peter
08:14
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\AttributesHelper;
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 = AttributesHelper::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 testGetNameReturnEmptyStringIfAttributeIsNull(): void
176
    {
177
        $expectedResult = '';
178
179
        $sut = new Select('id', 'foo');
180
181
        $sut->getAttribute(Html5::ATTR_NAME)->reset();
182
183
        $actualResult = $sut->getName();
184
185
        $this->assertEquals($expectedResult, $actualResult);
186
    }
187
188
    /**
189
     * @param string               $inputId
190
     * @param string               $name
191
     * @param string               $value
192
     * @param array<string,string> $options
193
     * @param Attribute[]|null     $attributes
194
     * @param string[]|null        $translations
195
     * @param string|null          $tag
196
     *
197
     * @return Select
198
     */
199
    protected function createSelect(
200
        string $inputId,
201
        string $name,
202
        string $value,
203
        array $options,
204
        ?array $attributes,
205
        ?array $translations,
206
        ?string $tag
207
    ): Select {
208
        $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations);
209
210
        $select = new Select($inputId, $name, [], $attributes, $tag);
211
212
        foreach ($options as $k => $v) {
213
            $select->add(new Option($k, $v, $value == $k));
214
        }
215
216
        $select->setTranslator($translatorMock);
217
218
        return $select;
219
    }
220
}
221