1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Navigation; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Html\INode; |
8
|
|
|
use AbterPhp\Framework\Html\ITag; |
9
|
|
|
use AbterPhp\Framework\Html\Node; |
10
|
|
|
use AbterPhp\Framework\Html\Tag; |
11
|
|
|
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory; |
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class DropdownTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testDefaultToString(): void |
18
|
|
|
{ |
19
|
|
|
$sut = $this->createDropdown(); |
20
|
|
|
|
21
|
|
|
$this->assertSame('<div><ul></ul></div>', (string)$sut); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testToStringWithoutWrapper(): void |
25
|
|
|
{ |
26
|
|
|
$sut = $this->createDropdown(); |
27
|
|
|
|
28
|
|
|
$sut->setWrapper(null); |
29
|
|
|
|
30
|
|
|
$this->assertSame('<ul></ul>', (string)$sut); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function toStringWithTranslationProvider(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
['AAA', ['AAA' => 'BBB'], '<div><ul><li>BBB</li></ul></div>'], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function toStringReturnsRawContentByDefaultProvider(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'IItem' => [new Item('foo'), '<li>foo</li>'], |
50
|
|
|
'IItem[]' => [[new Item('foo')], '<li>foo</li>'], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function toStringCanReturnTranslatedContentProvider(): array |
58
|
|
|
{ |
59
|
|
|
$translations = ['foo' => 'bar']; |
60
|
|
|
|
61
|
|
|
return [ |
62
|
|
|
'IItem' => [new Item('foo'), $translations, '<li>bar</li>'], |
63
|
|
|
'IItem[]' => [[new Item('foo')], $translations, '<li>bar</li>'], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function findProvider(): array |
71
|
|
|
{ |
72
|
|
|
$node1 = new Item('1'); |
73
|
|
|
$node2 = new Item('2'); |
74
|
|
|
|
75
|
|
|
return [ |
76
|
|
|
[[], $node1, null], |
77
|
|
|
[[$node2], $node1, null], |
78
|
|
|
[[$node1, $node2], $node1, 0], |
79
|
|
|
[[$node1, $node2], $node2, 1], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function findFirstChildProvider(): array |
87
|
|
|
{ |
88
|
|
|
$item0 = new Item('0'); |
89
|
|
|
$item1 = (new Item('1'))->setIntent('foo'); |
90
|
|
|
$item2 = (new Item('2'))->setIntent('bar'); |
91
|
|
|
$item3 = (new Item('3'))->setIntent('foo', 'bar'); |
92
|
|
|
$content = [$item0, $item1, $item2, $item3]; |
93
|
|
|
|
94
|
|
|
return [ |
95
|
|
|
'INode-no-intent' => [$content, INode::class, [], $item0], |
96
|
|
|
'INode-foo-intent' => [$content, INode::class, ['foo'], $item1], |
97
|
|
|
'INode-bar-intent' => [$content, INode::class, ['bar'], $item2], |
98
|
|
|
'INode-foo-and-bar-intent' => [$content, INode::class, ['foo', 'bar'], $item3], |
99
|
|
|
'IComponent-foo-intent' => [$content, ITag::class, ['foo'], $item1], |
100
|
|
|
'Component-foo-intent' => [$content, Tag::class, ['foo'], $item1], |
101
|
|
|
'fail-INode-baz-intent' => [$content, INode::class, ['baz'], null], |
102
|
|
|
'fail-INode-foo-and-baz-intent' => [$content, INode::class, ['foo', 'baz'], null], |
103
|
|
|
'fail-Node-foo-intent' => [$content, Node::class, ['foo'], null], |
104
|
|
|
'Item-foo-intent' => [$content, Item::class, ['foo'], $item1], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetPrefixGetsEmptyCollectionByDefault(): void |
109
|
|
|
{ |
110
|
|
|
$sut = $this->createDropdown(); |
111
|
|
|
|
112
|
|
|
$actualResult = $sut->getPrefix(); |
113
|
|
|
|
114
|
|
|
$this->assertInstanceOf(INode::class, $actualResult); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testGetPrefixGetsLastPrefixSet(): void |
118
|
|
|
{ |
119
|
|
|
$sut = $this->createDropdown(); |
120
|
|
|
|
121
|
|
|
/** @var ITag|MockObject $collectionStub */ |
122
|
|
|
$collectionStub = $this->createMock(INode::class); |
123
|
|
|
|
124
|
|
|
$sut->setPrefix($collectionStub); |
125
|
|
|
|
126
|
|
|
$actualResult = $sut->getPrefix(); |
127
|
|
|
|
128
|
|
|
$this->assertSame($collectionStub, $actualResult); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetPostfixGetsEmptyCollectionByDefault(): void |
132
|
|
|
{ |
133
|
|
|
$sut = $this->createDropdown(); |
134
|
|
|
|
135
|
|
|
$actualResult = $sut->getPostfix(); |
136
|
|
|
|
137
|
|
|
$this->assertInstanceOf(INode::class, $actualResult); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGetPostfixGetsLastPrefixSet(): void |
141
|
|
|
{ |
142
|
|
|
$sut = $this->createDropdown(); |
143
|
|
|
|
144
|
|
|
/** @var ITag|MockObject $collectionStub */ |
145
|
|
|
$collectionStub = $this->createMock(INode::class); |
146
|
|
|
|
147
|
|
|
$sut->setPostfix($collectionStub); |
148
|
|
|
|
149
|
|
|
$actualResult = $sut->getPostfix(); |
150
|
|
|
|
151
|
|
|
$this->assertSame($collectionStub, $actualResult); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetWrapperCanReturnNull(): void |
155
|
|
|
{ |
156
|
|
|
$sut = $this->createDropdown(); |
157
|
|
|
|
158
|
|
|
$sut->setWrapper(null); |
159
|
|
|
|
160
|
|
|
$actualResult = $sut->getWrapper(); |
161
|
|
|
|
162
|
|
|
$this->assertNull($actualResult); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testGetWrapperReturnsComponentByDefault(): void |
166
|
|
|
{ |
167
|
|
|
$sut = $this->createDropdown(); |
168
|
|
|
|
169
|
|
|
$actualResult = $sut->getWrapper(); |
170
|
|
|
|
171
|
|
|
$this->assertInstanceOf(ITag::class, $actualResult); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testGetWrapperReturnsLastSetWrapper(): void |
175
|
|
|
{ |
176
|
|
|
$sut = $this->createDropdown(); |
177
|
|
|
|
178
|
|
|
/** @var ITag $componentStub */ |
179
|
|
|
$componentStub = $this->createMock(ITag::class); |
180
|
|
|
|
181
|
|
|
$sut->setWrapper($componentStub); |
182
|
|
|
|
183
|
|
|
$actualResult = $sut->getWrapper(); |
184
|
|
|
|
185
|
|
|
$this->assertSame($componentStub, $actualResult); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param INode[]|INode|string|null $content |
190
|
|
|
* |
191
|
|
|
* @return Dropdown |
192
|
|
|
*/ |
193
|
|
|
protected function createDropdown($content = null): Dropdown |
194
|
|
|
{ |
195
|
|
|
return new Dropdown($content); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|