Passed
Push — html ( 4d98c2...e7bfd2 )
by Peter
03:09
created

testSetContentThrowsExceptionIfContentIsNotNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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