1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Form\Component; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
8
|
|
|
use AbterPhp\Framework\Html\Attribute; |
9
|
|
|
use AbterPhp\Framework\Html\Helper\Attributes; |
10
|
|
|
use AbterPhp\Framework\Html\INode; |
11
|
|
|
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory; |
12
|
|
|
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class OptionTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return array[] |
19
|
|
|
*/ |
20
|
|
|
public function renderProvider(): array |
21
|
|
|
{ |
22
|
|
|
$attribs = StubAttributeFactory::createAttributes(); |
23
|
|
|
$str = Attributes::toString($attribs); |
24
|
|
|
|
25
|
|
|
return [ |
26
|
|
|
'simple' => ['abc', 'ABC', false, null, null, null, "<option value=\"abc\">ABC</option>"], |
27
|
|
|
'attributes' => ['abc', 'ABC', false, $attribs, null, null, "<option$str value=\"abc\">ABC</option>"], |
28
|
|
|
'w/o translations' => ['abc', 'ABC', false, null, [], null, "<option value=\"abc\">ABC</option>",], |
29
|
|
|
'custom tag' => ['abc', 'ABC', false, null, null, 'foo', "<foo value=\"abc\">ABC</foo>"], |
30
|
|
|
'w translations' => ['abc', 'ABC', false, null, ['ABC' => '+'], null, "<option value=\"abc\">+</option>"], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @dataProvider renderProvider |
36
|
|
|
* |
37
|
|
|
* @param string $value |
38
|
|
|
* @param INode[]|INode|string|null $content |
39
|
|
|
* @param bool $isSelected |
40
|
|
|
* @param array|null $attributes |
41
|
|
|
* @param string[]|null $translations |
42
|
|
|
* @param string|null $tag |
43
|
|
|
* @param string $expectedResult |
44
|
|
|
*/ |
45
|
|
|
public function testRender( |
46
|
|
|
string $value, |
47
|
|
|
$content, |
48
|
|
|
bool $isSelected, |
49
|
|
|
?array $attributes, |
50
|
|
|
?array $translations, |
51
|
|
|
?string $tag, |
52
|
|
|
string $expectedResult |
53
|
|
|
): void { |
54
|
|
|
$sut = $this->createOption($value, $content, $isSelected, $attributes, $translations, $tag); |
55
|
|
|
|
56
|
|
|
$actualResult1 = (string)$sut; |
57
|
|
|
$actualResult2 = (string)$sut; |
58
|
|
|
|
59
|
|
|
$this->assertSame($expectedResult, $actualResult1); |
60
|
|
|
$this->assertSame($expectedResult, $actualResult2); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testRemoveThrowsExceptionWhenTryingToRemoveProtectedAttributes(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(\RuntimeException::class); |
66
|
|
|
|
67
|
|
|
$sut = new Option('foo', 'Foo'); |
68
|
|
|
|
69
|
|
|
$sut->removeAttribute(Html5::ATTR_VALUE); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetValueReturnsEmptyStringIfValueIsNull(): void |
73
|
|
|
{ |
74
|
|
|
$sut = new Option('foo', 'Foo'); |
75
|
|
|
|
76
|
|
|
$sut->getAttribute(Html5::ATTR_VALUE)->reset(); |
77
|
|
|
|
78
|
|
|
$actualResult = $sut->getValue(); |
79
|
|
|
|
80
|
|
|
$this->assertSame('', $actualResult); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $value |
85
|
|
|
* @param INode[]|INode|string|null $content |
86
|
|
|
* @param bool $isSelected |
87
|
|
|
* @param array<string,Attribute>|null $attributes |
88
|
|
|
* @param string[]|null $translations |
89
|
|
|
* @param string|null $tag |
90
|
|
|
* |
91
|
|
|
* @return Option |
92
|
|
|
*/ |
93
|
|
|
protected function createOption( |
94
|
|
|
string $value, |
95
|
|
|
$content, |
96
|
|
|
bool $isSelected, |
97
|
|
|
?array $attributes, |
98
|
|
|
?array $translations, |
99
|
|
|
?string $tag |
100
|
|
|
): Option { |
101
|
|
|
$translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations); |
102
|
|
|
|
103
|
|
|
$option = new Option($value, $content, $isSelected, [], $attributes, $tag); |
104
|
|
|
|
105
|
|
|
$option->setTranslator($translatorMock); |
106
|
|
|
|
107
|
|
|
return $option; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|