1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\MenuBundle\Tests\Menu; |
11
|
|
|
|
12
|
|
|
use Core23\MenuBundle\Menu\ConfigBuilder; |
13
|
|
|
use Knp\Menu\FactoryInterface; |
14
|
|
|
use Knp\Menu\ItemInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
final class ConfigBuilderTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private $factory; |
21
|
|
|
|
22
|
|
|
private $translator; |
23
|
|
|
|
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->factory = $this->prophesize(FactoryInterface::class); |
27
|
|
|
$this->translator = $this->prophesize(TranslatorInterface::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testBuildMenuWithoutItems(): void |
31
|
|
|
{ |
32
|
|
|
$builder = new ConfigBuilder( |
33
|
|
|
$this->factory->reveal(), |
34
|
|
|
$this->translator->reveal() |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
38
|
|
|
|
39
|
|
|
$this->factory->createItem('main', [ |
40
|
|
|
'attributes' => [ |
41
|
|
|
'class' => 'nav', |
42
|
|
|
], |
43
|
|
|
'childrenAttributes' => [ |
44
|
|
|
'class' => 'nav nav-pills', |
45
|
|
|
], |
46
|
|
|
]) |
47
|
|
|
->willReturn($mainMenu) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([], [])); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testBuildMenuWithOptions(): void |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$builder = new ConfigBuilder( |
56
|
|
|
$this->factory->reveal(), |
57
|
|
|
$this->translator->reveal() |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
61
|
|
|
|
62
|
|
|
$this->factory->createItem('main', [ |
63
|
|
|
'attributes' => [ |
64
|
|
|
'class' => 'nav', |
65
|
|
|
], |
66
|
|
|
'childrenAttributes' => [ |
67
|
|
|
'class' => 'nav nav-pills', |
68
|
|
|
], |
69
|
|
|
'foo' => 'bar', |
70
|
|
|
]) |
71
|
|
|
->willReturn($mainMenu) |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([], [ |
75
|
|
|
'foo' => 'bar', |
76
|
|
|
])); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testBuildMenuWithMenuAttributes(): void |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$builder = new ConfigBuilder( |
82
|
|
|
$this->factory->reveal(), |
83
|
|
|
$this->translator->reveal() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
87
|
|
|
|
88
|
|
|
$this->factory->createItem('main', [ |
89
|
|
|
'attributes' => [ |
90
|
|
|
'class' => 'nav', |
91
|
|
|
], |
92
|
|
|
'childrenAttributes' => [ |
93
|
|
|
'attr-foo' => 'custom', |
94
|
|
|
], |
95
|
|
|
]) |
96
|
|
|
->willReturn($mainMenu) |
97
|
|
|
; |
98
|
|
|
|
99
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([ |
100
|
|
|
'attributes' => [ |
101
|
|
|
'attr-foo' => 'custom', |
102
|
|
|
], |
103
|
|
|
], [])); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testBuildMenuWithItems(): void |
107
|
|
|
{ |
108
|
|
|
$builder = new ConfigBuilder( |
109
|
|
|
$this->factory->reveal(), |
110
|
|
|
$this->translator->reveal() |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$item = $this->prophesize(ItemInterface::class); |
114
|
|
|
|
115
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
116
|
|
|
$mainMenu->addChild($item); |
117
|
|
|
|
118
|
|
|
$this->factory->createItem('main', [ |
119
|
|
|
'attributes' => [ |
120
|
|
|
'class' => 'nav', |
121
|
|
|
], |
122
|
|
|
'childrenAttributes' => [ |
123
|
|
|
'class' => 'nav nav-pills', |
124
|
|
|
], |
125
|
|
|
]) |
126
|
|
|
->willReturn($mainMenu) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$this->factory->createItem('my-label', [ |
130
|
|
|
'route' => 'my-route', |
131
|
|
|
'routeParameters' => [ |
132
|
|
|
'paramKey' => 'value', |
133
|
|
|
], |
134
|
|
|
'linkAttributes' => ['class' => 'my-class'], |
135
|
|
|
'extras' => [ |
136
|
|
|
'safe_label' => true, |
137
|
|
|
'translation_domain' => false, |
138
|
|
|
], |
139
|
|
|
]) |
140
|
|
|
->willReturn($item) |
141
|
|
|
; |
142
|
|
|
|
143
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([ |
144
|
|
|
'items' => [ |
145
|
|
|
[ |
146
|
|
|
'label' => 'my-label', |
147
|
|
|
'route' => 'my-route', |
148
|
|
|
'routeParams' => [ |
149
|
|
|
'paramKey' => 'value', |
150
|
|
|
], |
151
|
|
|
'class' => 'my-class', |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
], [])); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testBuildMenuWithTranslation(): void |
158
|
|
|
{ |
159
|
|
|
$builder = new ConfigBuilder( |
160
|
|
|
$this->factory->reveal(), |
161
|
|
|
$this->translator->reveal() |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$item = $this->prophesize(ItemInterface::class); |
165
|
|
|
|
166
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
167
|
|
|
$mainMenu->addChild($item); |
168
|
|
|
|
169
|
|
|
$this->factory->createItem('main', [ |
170
|
|
|
'attributes' => [ |
171
|
|
|
'class' => 'nav', |
172
|
|
|
], |
173
|
|
|
'childrenAttributes' => [ |
174
|
|
|
'class' => 'nav nav-pills', |
175
|
|
|
], |
176
|
|
|
]) |
177
|
|
|
->willReturn($mainMenu) |
178
|
|
|
; |
179
|
|
|
|
180
|
|
|
$this->factory->createItem('My label', [ |
181
|
|
|
'route' => 'my-route', |
182
|
|
|
'routeParameters' => [], |
183
|
|
|
'linkAttributes' => [], |
184
|
|
|
'extras' => [ |
185
|
|
|
'safe_label' => true, |
186
|
|
|
'translation_domain' => false, |
187
|
|
|
], |
188
|
|
|
]) |
189
|
|
|
->willReturn($item) |
190
|
|
|
; |
191
|
|
|
|
192
|
|
|
$this->translator->trans('my-label', [], 'App') |
193
|
|
|
->willReturn('My label') |
194
|
|
|
; |
195
|
|
|
|
196
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([ |
197
|
|
|
'items' => [ |
198
|
|
|
[ |
199
|
|
|
'label' => 'my-label', |
200
|
|
|
'label_catalogue' => 'App', |
201
|
|
|
'route' => 'my-route', |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
], [])); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testBuildMenuWithIcon(): void |
208
|
|
|
{ |
209
|
|
|
$builder = new ConfigBuilder( |
210
|
|
|
$this->factory->reveal(), |
211
|
|
|
$this->translator->reveal() |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$item = $this->prophesize(ItemInterface::class); |
215
|
|
|
|
216
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
217
|
|
|
$mainMenu->addChild($item); |
218
|
|
|
|
219
|
|
|
$this->factory->createItem('main', [ |
220
|
|
|
'attributes' => [ |
221
|
|
|
'class' => 'nav', |
222
|
|
|
], |
223
|
|
|
'childrenAttributes' => [ |
224
|
|
|
'class' => 'nav nav-pills', |
225
|
|
|
], |
226
|
|
|
]) |
227
|
|
|
->willReturn($mainMenu) |
228
|
|
|
; |
229
|
|
|
|
230
|
|
|
$this->factory->createItem('<i class="fa fa-test"></i> my-label', [ |
231
|
|
|
'route' => 'my-route', |
232
|
|
|
'routeParameters' => [], |
233
|
|
|
'linkAttributes' => [], |
234
|
|
|
'extras' => [ |
235
|
|
|
'safe_label' => true, |
236
|
|
|
'translation_domain' => false, |
237
|
|
|
], |
238
|
|
|
]) |
239
|
|
|
->willReturn($item) |
240
|
|
|
; |
241
|
|
|
|
242
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([ |
243
|
|
|
'items' => [ |
244
|
|
|
[ |
245
|
|
|
'label' => 'my-label', |
246
|
|
|
'icon' => 'fa fa-test', |
247
|
|
|
'route' => 'my-route', |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
], [])); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testBuildMenuWithChildren(): void |
254
|
|
|
{ |
255
|
|
|
$builder = new ConfigBuilder( |
256
|
|
|
$this->factory->reveal(), |
257
|
|
|
$this->translator->reveal() |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$subitem = $this->prophesize(ItemInterface::class); |
261
|
|
|
|
262
|
|
|
$item = $this->prophesize(ItemInterface::class); |
263
|
|
|
$item->addChild($subitem); |
264
|
|
|
|
265
|
|
|
$mainMenu = $this->prophesize(ItemInterface::class); |
266
|
|
|
$mainMenu->addChild($item); |
267
|
|
|
|
268
|
|
|
$this->factory->createItem('main', [ |
269
|
|
|
'attributes' => [ |
270
|
|
|
'class' => 'nav', |
271
|
|
|
], |
272
|
|
|
'childrenAttributes' => [ |
273
|
|
|
'class' => 'nav nav-pills', |
274
|
|
|
], |
275
|
|
|
]) |
276
|
|
|
->willReturn($mainMenu) |
277
|
|
|
; |
278
|
|
|
|
279
|
|
|
$this->factory->createItem('my-label <b class="caret caret-menu"></b>', [ |
280
|
|
|
'route' => 'my-route', |
281
|
|
|
'routeParameters' => [], |
282
|
|
|
'linkAttributes' => [ |
283
|
|
|
'class' => 'dropdown-toggle', |
284
|
|
|
'data-toggle' => 'dropdown', |
285
|
|
|
'data-target' => '#', |
286
|
|
|
], |
287
|
|
|
'extras' => [ |
288
|
|
|
'safe_label' => true, |
289
|
|
|
'translation_domain' => false, |
290
|
|
|
], |
291
|
|
|
'attributes' => [ |
292
|
|
|
'class' => 'dropdown', |
293
|
|
|
], |
294
|
|
|
'childrenAttributes' => [ |
295
|
|
|
'class' => 'dropdown-menu', |
296
|
|
|
], |
297
|
|
|
'label' => 'my-label <b class="caret caret-menu"></b>', |
298
|
|
|
]) |
299
|
|
|
->willReturn($item) |
300
|
|
|
; |
301
|
|
|
|
302
|
|
|
$this->factory->createItem('my-sub-label', [ |
303
|
|
|
'route' => 'my-sub-route', |
304
|
|
|
'routeParameters' => [], |
305
|
|
|
'linkAttributes' => [], |
306
|
|
|
'extras' => [ |
307
|
|
|
'safe_label' => true, |
308
|
|
|
'translation_domain' => false, |
309
|
|
|
], |
310
|
|
|
]) |
311
|
|
|
->willReturn($item) |
312
|
|
|
; |
313
|
|
|
|
314
|
|
|
static::assertSame($mainMenu->reveal(), $builder->buildMenu([ |
315
|
|
|
'items' => [ |
316
|
|
|
[ |
317
|
|
|
'label' => 'my-label', |
318
|
|
|
'route' => 'my-route', |
319
|
|
|
'class' => 'my-class', |
320
|
|
|
'children' => [ |
321
|
|
|
[ |
322
|
|
|
'label' => 'my-sub-label', |
323
|
|
|
'route' => 'my-sub-route', |
324
|
|
|
], |
325
|
|
|
], |
326
|
|
|
], |
327
|
|
|
], |
328
|
|
|
], [])); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.