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 TextareaTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return array[] |
19
|
|
|
*/ |
20
|
|
|
public function renderProvider(): array |
21
|
|
|
{ |
22
|
|
|
$attribs = StubAttributeFactory::createAttributes(); |
23
|
|
|
|
24
|
|
|
return [ |
25
|
|
|
'simple' => [ |
26
|
|
|
'abc', |
27
|
|
|
'bcd', |
28
|
|
|
'val', |
29
|
|
|
null, |
30
|
|
|
null, |
31
|
|
|
null, |
32
|
|
|
'<textarea id="abc" rows="3" name="bcd">val</textarea>', |
33
|
|
|
], |
34
|
|
|
'missing translations' => [ |
35
|
|
|
'abc', |
36
|
|
|
'bcd', |
37
|
|
|
'val', |
38
|
|
|
null, |
39
|
|
|
[], |
40
|
|
|
null, |
41
|
|
|
'<textarea id="abc" rows="3" name="bcd">val</textarea>', |
42
|
|
|
], |
43
|
|
|
'extra attributes' => [ |
44
|
|
|
'abc', |
45
|
|
|
'bcd', |
46
|
|
|
'val', |
47
|
|
|
$attribs, |
48
|
|
|
[], |
49
|
|
|
null, |
50
|
|
|
"<textarea foo=\"foo baz\" bar=\"bar baz\" id=\"abc\" rows=\"3\" name=\"bcd\">val</textarea>", |
51
|
|
|
], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider renderProvider |
57
|
|
|
* |
58
|
|
|
* @param string $inputId |
59
|
|
|
* @param string $name |
60
|
|
|
* @param string $value |
61
|
|
|
* @param Attribute[]|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->createTextarea($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->getAttribute(Html5::ATTR_VALUE)->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 Textarea('id', 'name'); |
119
|
|
|
|
120
|
|
|
$sut->setValue($value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testGetName(): void |
124
|
|
|
{ |
125
|
|
|
$expectedResult = 'foo'; |
126
|
|
|
|
127
|
|
|
$sut = new Textarea('id', $expectedResult); |
128
|
|
|
|
129
|
|
|
$actualResult = $sut->getName(); |
130
|
|
|
|
131
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testRemoveAttributeThrowsExceptionWhenTryingToRemoveProtectedAttributes(): void |
135
|
|
|
{ |
136
|
|
|
$this->expectException(\RuntimeException::class); |
137
|
|
|
|
138
|
|
|
$sut = new Textarea('id', 'foo'); |
139
|
|
|
|
140
|
|
|
$sut->removeAttribute(Html5::ATTR_NAME); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $inputId |
145
|
|
|
* @param string $name |
146
|
|
|
* @param string $value |
147
|
|
|
* @param Attribute[]|null $attributes |
148
|
|
|
* @param string[]|null $translations |
149
|
|
|
* @param string|null $tag |
150
|
|
|
* |
151
|
|
|
* @return Textarea |
152
|
|
|
*/ |
153
|
|
|
private function createTextarea( |
154
|
|
|
string $inputId, |
155
|
|
|
string $name, |
156
|
|
|
string $value, |
157
|
|
|
?array $attributes, |
158
|
|
|
?array $translations, |
159
|
|
|
?string $tag |
160
|
|
|
): Textarea { |
161
|
|
|
$translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations); |
162
|
|
|
|
163
|
|
|
$textarea = new Textarea($inputId, $name, $value, [], $attributes, $tag); |
164
|
|
|
|
165
|
|
|
$textarea->setTranslator($translatorMock); |
166
|
|
|
|
167
|
|
|
return $textarea; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|