Passed
Pull Request — master (#1)
by Peter
07:07
created

SelectTest::testRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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