These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 Core23\MenuBundle\Menu\ConfigBuilderInterface; |
||
14 | use Knp\Menu\FactoryInterface; |
||
15 | use Knp\Menu\ItemInterface; |
||
16 | use PHPUnit\Framework\TestCase; |
||
17 | use Symfony\Component\Translation\TranslatorInterface; |
||
18 | |||
19 | class ConfigBuilderTest extends TestCase |
||
20 | { |
||
21 | private $factory; |
||
22 | |||
23 | private $translator; |
||
24 | |||
25 | protected function setUp() |
||
26 | { |
||
27 | $this->factory = $this->prophesize(FactoryInterface::class); |
||
28 | $this->translator = $this->prophesize(TranslatorInterface::class); |
||
29 | } |
||
30 | |||
31 | public function testItIsInstantiable(): void |
||
32 | { |
||
33 | $builder = new ConfigBuilder( |
||
34 | $this->factory->reveal(), |
||
35 | $this->translator->reveal() |
||
36 | ); |
||
37 | |||
38 | $this->assertInstanceOf(ConfigBuilderInterface::class, $builder); |
||
39 | } |
||
40 | |||
41 | public function testBuildMenuWithoutItems(): void |
||
42 | { |
||
43 | $builder = new ConfigBuilder( |
||
44 | $this->factory->reveal(), |
||
45 | $this->translator->reveal() |
||
46 | ); |
||
47 | |||
48 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
49 | |||
50 | $this->factory->createItem('main', [ |
||
51 | 'attributes' => [ |
||
52 | 'class' => 'nav', |
||
53 | ], |
||
54 | 'childrenAttributes' => [ |
||
55 | 'class' => 'nav nav-pills', |
||
56 | ], |
||
57 | ]) |
||
58 | ->willReturn($mainMenu) |
||
59 | ; |
||
60 | |||
61 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([], [])); |
||
62 | } |
||
63 | |||
64 | View Code Duplication | public function testBuildMenuWithOptions(): void |
|
0 ignored issues
–
show
|
|||
65 | { |
||
66 | $builder = new ConfigBuilder( |
||
67 | $this->factory->reveal(), |
||
68 | $this->translator->reveal() |
||
69 | ); |
||
70 | |||
71 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
72 | |||
73 | $this->factory->createItem('main', [ |
||
74 | 'attributes' => [ |
||
75 | 'class' => 'nav', |
||
76 | ], |
||
77 | 'childrenAttributes' => [ |
||
78 | 'class' => 'nav nav-pills', |
||
79 | ], |
||
80 | 'foo' => 'bar', |
||
81 | ]) |
||
82 | ->willReturn($mainMenu) |
||
83 | ; |
||
84 | |||
85 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([], [ |
||
86 | 'foo' => 'bar', |
||
87 | ])); |
||
88 | } |
||
89 | |||
90 | View Code Duplication | public function testBuildMenuWithMenuAttributes(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
91 | { |
||
92 | $builder = new ConfigBuilder( |
||
93 | $this->factory->reveal(), |
||
94 | $this->translator->reveal() |
||
95 | ); |
||
96 | |||
97 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
98 | |||
99 | $this->factory->createItem('main', [ |
||
100 | 'attributes' => [ |
||
101 | 'class' => 'nav', |
||
102 | ], |
||
103 | 'childrenAttributes' => [ |
||
104 | 'attr-foo' => 'custom', |
||
105 | ], |
||
106 | ]) |
||
107 | ->willReturn($mainMenu) |
||
108 | ; |
||
109 | |||
110 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([ |
||
111 | 'attributes' => [ |
||
112 | 'attr-foo' => 'custom', |
||
113 | ], |
||
114 | ], [])); |
||
115 | } |
||
116 | |||
117 | public function testBuildMenuWithItems(): void |
||
118 | { |
||
119 | $builder = new ConfigBuilder( |
||
120 | $this->factory->reveal(), |
||
121 | $this->translator->reveal() |
||
122 | ); |
||
123 | |||
124 | $item = $this->prophesize(ItemInterface::class); |
||
125 | |||
126 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
127 | $mainMenu->addChild($item); |
||
128 | |||
129 | $this->factory->createItem('main', [ |
||
130 | 'attributes' => [ |
||
131 | 'class' => 'nav', |
||
132 | ], |
||
133 | 'childrenAttributes' => [ |
||
134 | 'class' => 'nav nav-pills', |
||
135 | ], |
||
136 | ]) |
||
137 | ->willReturn($mainMenu) |
||
138 | ; |
||
139 | |||
140 | $this->factory->createItem('my-label', [ |
||
141 | 'route' => 'my-route', |
||
142 | 'routeParameters' => [ |
||
143 | 'paramKey' => 'value', |
||
144 | ], |
||
145 | 'linkAttributes' => ['class' => 'my-class'], |
||
146 | 'extras' => [ |
||
147 | 'safe_label' => true, |
||
148 | 'translation_domain' => false, |
||
149 | ], |
||
150 | ]) |
||
151 | ->willReturn($item) |
||
152 | ; |
||
153 | |||
154 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([ |
||
155 | 'items' => [ |
||
156 | [ |
||
157 | 'label' => 'my-label', |
||
158 | 'route' => 'my-route', |
||
159 | 'routeParams' => [ |
||
160 | 'paramKey' => 'value', |
||
161 | ], |
||
162 | 'class' => 'my-class', |
||
163 | ], |
||
164 | ], |
||
165 | ], [])); |
||
166 | } |
||
167 | |||
168 | public function testBuildMenuWithTranslation(): void |
||
169 | { |
||
170 | $builder = new ConfigBuilder( |
||
171 | $this->factory->reveal(), |
||
172 | $this->translator->reveal() |
||
173 | ); |
||
174 | |||
175 | $item = $this->prophesize(ItemInterface::class); |
||
176 | |||
177 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
178 | $mainMenu->addChild($item); |
||
179 | |||
180 | $this->factory->createItem('main', [ |
||
181 | 'attributes' => [ |
||
182 | 'class' => 'nav', |
||
183 | ], |
||
184 | 'childrenAttributes' => [ |
||
185 | 'class' => 'nav nav-pills', |
||
186 | ], |
||
187 | ]) |
||
188 | ->willReturn($mainMenu) |
||
189 | ; |
||
190 | |||
191 | $this->factory->createItem('My label', [ |
||
192 | 'route' => 'my-route', |
||
193 | 'routeParameters' => [], |
||
194 | 'linkAttributes' => [], |
||
195 | 'extras' => [ |
||
196 | 'safe_label' => true, |
||
197 | 'translation_domain' => false, |
||
198 | ], |
||
199 | ]) |
||
200 | ->willReturn($item) |
||
201 | ; |
||
202 | |||
203 | $this->translator->trans('my-label', [], 'App') |
||
204 | ->willReturn('My label') |
||
205 | ; |
||
206 | |||
207 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([ |
||
208 | 'items' => [ |
||
209 | [ |
||
210 | 'label' => 'my-label', |
||
211 | 'label_catalogue' => 'App', |
||
212 | 'route' => 'my-route', |
||
213 | ], |
||
214 | ], |
||
215 | ], [])); |
||
216 | } |
||
217 | |||
218 | public function testBuildMenuWithIcon(): void |
||
219 | { |
||
220 | $builder = new ConfigBuilder( |
||
221 | $this->factory->reveal(), |
||
222 | $this->translator->reveal() |
||
223 | ); |
||
224 | |||
225 | $item = $this->prophesize(ItemInterface::class); |
||
226 | |||
227 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
228 | $mainMenu->addChild($item); |
||
229 | |||
230 | $this->factory->createItem('main', [ |
||
231 | 'attributes' => [ |
||
232 | 'class' => 'nav', |
||
233 | ], |
||
234 | 'childrenAttributes' => [ |
||
235 | 'class' => 'nav nav-pills', |
||
236 | ], |
||
237 | ]) |
||
238 | ->willReturn($mainMenu) |
||
239 | ; |
||
240 | |||
241 | $this->factory->createItem('<i class="fa fa-test"></i> my-label', [ |
||
242 | 'route' => 'my-route', |
||
243 | 'routeParameters' => [], |
||
244 | 'linkAttributes' => [], |
||
245 | 'extras' => [ |
||
246 | 'safe_label' => true, |
||
247 | 'translation_domain' => false, |
||
248 | ], |
||
249 | ]) |
||
250 | ->willReturn($item) |
||
251 | ; |
||
252 | |||
253 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([ |
||
254 | 'items' => [ |
||
255 | [ |
||
256 | 'label' => 'my-label', |
||
257 | 'icon' => 'fa fa-test', |
||
258 | 'route' => 'my-route', |
||
259 | ], |
||
260 | ], |
||
261 | ], [])); |
||
262 | } |
||
263 | |||
264 | public function testBuildMenuWithChildren(): void |
||
265 | { |
||
266 | $builder = new ConfigBuilder( |
||
267 | $this->factory->reveal(), |
||
268 | $this->translator->reveal() |
||
269 | ); |
||
270 | |||
271 | $subitem = $this->prophesize(ItemInterface::class); |
||
272 | |||
273 | $item = $this->prophesize(ItemInterface::class); |
||
274 | $item->addChild($subitem); |
||
275 | |||
276 | $mainMenu = $this->prophesize(ItemInterface::class); |
||
277 | $mainMenu->addChild($item); |
||
278 | |||
279 | $this->factory->createItem('main', [ |
||
280 | 'attributes' => [ |
||
281 | 'class' => 'nav', |
||
282 | ], |
||
283 | 'childrenAttributes' => [ |
||
284 | 'class' => 'nav nav-pills', |
||
285 | ], |
||
286 | ]) |
||
287 | ->willReturn($mainMenu) |
||
288 | ; |
||
289 | |||
290 | $this->factory->createItem('my-label <b class="caret caret-menu"></b>', [ |
||
291 | 'route' => 'my-route', |
||
292 | 'routeParameters' => [], |
||
293 | 'linkAttributes' => [ |
||
294 | 'class' => 'dropdown-toggle', |
||
295 | 'data-toggle' => 'dropdown', |
||
296 | 'data-target' => '#', |
||
297 | ], |
||
298 | 'extras' => [ |
||
299 | 'safe_label' => true, |
||
300 | 'translation_domain' => false, |
||
301 | ], |
||
302 | 'attributes' => [ |
||
303 | 'class' => 'dropdown', |
||
304 | ], |
||
305 | 'childrenAttributes' => [ |
||
306 | 'class' => 'dropdown-menu', |
||
307 | ], |
||
308 | 'label' => 'my-label <b class="caret caret-menu"></b>', |
||
309 | ]) |
||
310 | ->willReturn($item) |
||
311 | ; |
||
312 | |||
313 | $this->factory->createItem('my-sub-label', [ |
||
314 | 'route' => 'my-sub-route', |
||
315 | 'routeParameters' => [], |
||
316 | 'linkAttributes' => [], |
||
317 | 'extras' => [ |
||
318 | 'safe_label' => true, |
||
319 | 'translation_domain' => false, |
||
320 | ], |
||
321 | ]) |
||
322 | ->willReturn($item) |
||
323 | ; |
||
324 | |||
325 | $this->assertSame($mainMenu->reveal(), $builder->buildMenu([ |
||
326 | 'items' => [ |
||
327 | [ |
||
328 | 'label' => 'my-label', |
||
329 | 'route' => 'my-route', |
||
330 | 'class' => 'my-class', |
||
331 | 'children' => [ |
||
332 | [ |
||
333 | 'label' => 'my-sub-label', |
||
334 | 'route' => 'my-sub-route', |
||
335 | ], |
||
336 | ], |
||
337 | ], |
||
338 | ], |
||
339 | ], [])); |
||
340 | } |
||
341 | } |
||
342 |
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.