|
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\Html\Attribute; |
|
9
|
|
|
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory; |
|
10
|
|
|
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use stdClass; |
|
14
|
|
|
|
|
15
|
|
|
class InputTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @return array[] |
|
19
|
|
|
*/ |
|
20
|
|
|
public function renderProvider(): array |
|
21
|
|
|
{ |
|
22
|
|
|
$attributes = StubAttributeFactory::createAttributes(); |
|
23
|
|
|
|
|
24
|
|
|
return [ |
|
25
|
|
|
'simple' => [ |
|
26
|
|
|
'abc', |
|
27
|
|
|
'bcd', |
|
28
|
|
|
'val', |
|
29
|
|
|
null, |
|
30
|
|
|
null, |
|
31
|
|
|
null, |
|
32
|
|
|
'<input id="abc" type="text" name="bcd" value="val">', |
|
33
|
|
|
], |
|
34
|
|
|
'missing translations' => [ |
|
35
|
|
|
'abc', |
|
36
|
|
|
'bcd', |
|
37
|
|
|
'val', |
|
38
|
|
|
null, |
|
39
|
|
|
[], |
|
40
|
|
|
null, |
|
41
|
|
|
'<input id="abc" type="text" name="bcd" value="val">', |
|
42
|
|
|
], |
|
43
|
|
|
'extra attributes' => [ |
|
44
|
|
|
'abc', |
|
45
|
|
|
'bcd', |
|
46
|
|
|
'val', |
|
47
|
|
|
$attributes, |
|
48
|
|
|
[], |
|
49
|
|
|
null, |
|
50
|
|
|
"<input foo=\"foo baz\" bar=\"bar baz\" id=\"abc\" type=\"text\" name=\"bcd\" value=\"val\">", |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider renderProvider |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $inputId |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* @param string $value |
|
61
|
|
|
* @param array|null $attributes |
|
62
|
|
|
* @param string[]|null $translations |
|
63
|
|
|
* @param string|null $tag |
|
64
|
|
|
* @param string $expectedResult |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testRender( |
|
67
|
|
|
string $inputId, |
|
68
|
|
|
string $name, |
|
69
|
|
|
string $value, |
|
70
|
|
|
?array $attributes, |
|
71
|
|
|
?array $translations, |
|
72
|
|
|
?string $tag, |
|
73
|
|
|
string $expectedResult |
|
74
|
|
|
): void { |
|
75
|
|
|
$sut = $this->createInput($inputId, $name, $value, $attributes, $translations, $tag); |
|
76
|
|
|
|
|
77
|
|
|
$actualResult = (string)$sut; |
|
78
|
|
|
$repeatedResult = (string)$sut; |
|
79
|
|
|
|
|
80
|
|
|
$this->assertSame($actualResult, $repeatedResult); |
|
81
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testSetValueSetsAttribute(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$expectedResult = 'foo'; |
|
87
|
|
|
|
|
88
|
|
|
$sut = new Textarea('id', 'name'); |
|
89
|
|
|
|
|
90
|
|
|
$sut->setValue($expectedResult); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals($expectedResult, $sut->getValue()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setValueFailureProvider(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
'array' => [[]], |
|
102
|
|
|
'stdclass' => [new stdClass()], |
|
103
|
|
|
'int' => [123], |
|
104
|
|
|
'bool' => [false], |
|
105
|
|
|
'float' => [123.53], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @dataProvider setValueFailureProvider |
|
111
|
|
|
* |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testSetValueThrowsExceptionOnInvalid($value): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
117
|
|
|
|
|
118
|
|
|
$sut = new Input('id', 'name'); |
|
119
|
|
|
|
|
120
|
|
|
$sut->setValue($value); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testRemoveAttributeThrowsExceptionWhenTryingToRemoveProtectedAttributes(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->expectException(\RuntimeException::class); |
|
126
|
|
|
|
|
127
|
|
|
$sut = new Input('id', 'foo'); |
|
128
|
|
|
|
|
129
|
|
|
$sut->removeAttribute(Html5::ATTR_NAME); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testGetName(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$expectedResult = 'foo'; |
|
135
|
|
|
|
|
136
|
|
|
$sut = new Input('id', $expectedResult); |
|
137
|
|
|
|
|
138
|
|
|
$actualResult = $sut->getName(); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testGetNameReturnEmptyStringIfAttributeIsNull(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$expectedResult = ''; |
|
146
|
|
|
|
|
147
|
|
|
$sut = new Input('id', $expectedResult); |
|
148
|
|
|
|
|
149
|
|
|
$sut->setAttribute(new Attribute(Html5::ATTR_NAME)); |
|
150
|
|
|
|
|
151
|
|
|
$actualResult = $sut->getName(); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $inputId |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* @param string $value |
|
160
|
|
|
* @param array<string,Attribute>|null $attributes |
|
161
|
|
|
* @param string[]|null $translations |
|
162
|
|
|
* @param string|null $tag |
|
163
|
|
|
* |
|
164
|
|
|
* @return Input |
|
165
|
|
|
*/ |
|
166
|
|
|
private function createInput( |
|
167
|
|
|
string $inputId, |
|
168
|
|
|
string $name, |
|
169
|
|
|
string $value, |
|
170
|
|
|
?array $attributes, |
|
171
|
|
|
?array $translations, |
|
172
|
|
|
?string $tag |
|
173
|
|
|
): Input { |
|
174
|
|
|
$translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations); |
|
175
|
|
|
|
|
176
|
|
|
$input = new Input($inputId, $name, $value, [], $attributes, $tag); |
|
177
|
|
|
|
|
178
|
|
|
$input->setTranslator($translatorMock); |
|
179
|
|
|
|
|
180
|
|
|
return $input; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|