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