Completed
Pull Request — master (#52)
by Wilmer
01:36
created

MenuTest::testEncodeLabelTrue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 26
rs 9.7
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Widget\Tests;
5
6
use Yiisoft\Tests\TestCase;
7
use Yiisoft\Widget\Menu;
8
9
/**
10
 * MenuTest.
11
 */
12
class MenuTest extends TestCase
13
{
14
    public function testEncodeLabelTrue(): void
15
    {
16
        ob_start();
17
        ob_implicit_flush(0);
18
19
        echo Menu::widget()
20
            ->encodeLabels(true)
21
            ->items([
22
                [
23
                    'encode' => false,
24
                    'label'  => '<span class="glyphicon glyphicon-user"></span> Users',
25
                    'url'    => '#',
26
                ],
27
                [
28
                    'encode' => true,
29
                    'label'  => 'Authors & Publications',
30
                    'url'    => '#',
31
                ],
32
            ]);
33
34
        $expected = <<<'HTML'
35
<ul><li><a href="#"><span class="glyphicon glyphicon-user"></span> Users</a></li>
36
<li><a href="#">Authors &amp; Publications</a></li></ul>
37
HTML;
38
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
39
        ob_end_clean();
40
    }
41
42
    public function testEncodeLabelFalse(): void
43
    {
44
        ob_start();
45
        ob_implicit_flush(0);
46
47
        echo Menu::widget()
48
            ->encodeLabels(false)
49
            ->items([
50
                [
51
                    'encode' => false,
52
                    'label'  => '<span class="glyphicon glyphicon-user"></span> Users',
53
                    'url'    => '#',
54
                ],
55
                [
56
                    'encode' => true,
57
                    'label'  => 'Authors & Publications',
58
                    'url'    => '#',
59
                ],
60
            ]);
61
62
        $expected = <<<'HTML'
63
<ul><li><a href="#"><span class="glyphicon glyphicon-user"></span> Users</a></li>
64
<li><a href="#">Authors &amp; Publications</a></li></ul>
65
HTML;
66
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
67
        ob_end_clean();
68
    }
69
70
    /**
71
     * @see https://github.com/yiisoft/yii2/issues/8064
72
     */
73
    public function testTagOption(): void
74
    {
75
        ob_start();
76
        ob_implicit_flush(0);
77
78
        echo Menu::widget()
79
            ->encodeLabels(true)
80
            ->options([
81
                'tag' => false,
82
            ])
83
            ->items([
84
                [
85
                    'label'   => 'item1',
86
                    'url'     => '#',
87
                    'options' => ['tag' => 'div'],
88
                ],
89
                [
90
                    'label'   => 'item2',
91
                    'url'     => '#',
92
                    'options' => ['tag' => false],
93
                ],
94
            ]);
95
96
        $expected = <<<'HTML'
97
<div><a href="#">item1</a></div>
98
<a href="#">item2</a>
99
HTML;
100
101
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
102
103
        ob_end_clean();
104
105
        ob_start();
106
        ob_implicit_flush(0);
107
108
        echo Menu::widget()
109
            ->encodeLabels(true)
110
            ->options([
111
                'tag' => false,
112
            ])
113
            ->items([
114
                [
115
                    'label' => 'item1',
116
                    'url'   => '#',
117
                ],
118
                [
119
                    'label' => 'item2',
120
                    'url'   => '#',
121
                ],
122
            ])
123
            ->itemOptions(['tag' => false]);
124
125
        $expected = <<<'HTML'
126
<a href="#">item1</a>
127
<a href="#">item2</a>
128
HTML;
129
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
130
        ob_end_clean();
131
    }
132
133
    public function testItemTemplate(): void
134
    {
135
        ob_start();
136
        ob_implicit_flush(0);
137
138
        echo Menu::widget()
139
            ->labelTemplate('')
140
            ->linkTemplate('')
141
            ->items([
142
                [
143
                    'label'    => 'item1',
144
                    'url'      => '#',
145
                    'template' => 'label: {label}; url: {url}',
146
                ],
147
                [
148
                    'label'    => 'item2',
149
                    'template' => 'label: {label}',
150
                ],
151
                [
152
                    'label' => 'item3 (no template)',
153
                ],
154
            ]);
155
156
        $expected = <<<'HTML'
157
<ul><li>label: item1; url: #</li>
158
<li>label: item2</li>
159
<li></li></ul>
160
HTML;
161
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
162
        ob_end_clean();
163
    }
164
165
    public function testActiveItemClosure(): void
166
    {
167
        ob_start();
168
        ob_implicit_flush(0);
169
170
        echo Menu::widget()
171
            ->linkTemplate('')
172
            ->labelTemplate('')
173
            ->items([
174
                [
175
                    'label'    => 'item1',
176
                    'url'      => '#',
177
                    'template' => 'label: {label}; url: {url}',
178
                    'active'   => function ($item, $hasActiveChild, $isItemActive, $widget) {
179
                        return isset($item, $hasActiveChild, $isItemActive, $widget);
180
                    },
181
                ],
182
                [
183
                    'label'    => 'item2',
184
                    'template' => 'label: {label}',
185
                    'active'   => false,
186
                ],
187
                [
188
                    'label'  => 'item3 (no template)',
189
                    'active' => 'somestring',
190
                ],
191
            ]);
192
193
        $expected = <<<'HTML'
194
<ul><li class="active">label: item1; url: #</li>
195
<li>label: item2</li>
196
<li class="active"></li></ul>
197
HTML;
198
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
199
        ob_end_clean();
200
    }
201
202
    public function testItemClassAsArray(): void
203
    {
204
        ob_start();
205
        ob_implicit_flush(0);
206
207
        echo Menu::widget()
208
            ->encodeLabels(true)
209
            ->activeCssClass('item-active')
210
            ->items([
211
                [
212
                    'label'   => 'item1',
213
                    'url'     => '#',
214
                    'active'  => true,
215
                    'options' => [
216
                        'class' => [
217
                            'someclass',
218
                        ],
219
                    ],
220
                ],
221
                [
222
                    'label'   => 'item2',
223
                    'url'     => '#',
224
                    'options' => [
225
                        'class' => [
226
                            'another-class',
227
                            'other--class',
228
                            'two classes',
229
                        ],
230
                    ],
231
                ],
232
                [
233
                    'label' => 'item3',
234
                    'url'   => '#',
235
                ],
236
                [
237
                    'label'   => 'item4',
238
                    'url'     => '#',
239
                    'options' => [
240
                        'class' => [
241
                            'some-other-class',
242
                            'foo_bar_baz_class',
243
                        ],
244
                    ],
245
                ],
246
            ]);
247
248
        $expected = <<<'HTML'
249
<ul><li class="someclass item-active"><a href="#">item1</a></li>
250
<li class="another-class other--class two classes"><a href="#">item2</a></li>
251
<li><a href="#">item3</a></li>
252
<li class="some-other-class foo_bar_baz_class"><a href="#">item4</a></li></ul>
253
HTML;
254
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
255
        ob_end_clean();
256
    }
257
258
    public function testItemClassAsString(): void
259
    {
260
        ob_start();
261
        ob_implicit_flush(0);
262
263
        echo Menu::widget()
264
            ->encodeLabels(true)
265
            ->activeCssClass('item-active')
266
            ->items([
267
                [
268
                    'label'   => 'item1',
269
                    'url'     => '#',
270
                    'options' => [
271
                        'class' => 'someclass',
272
                    ],
273
                ],
274
                [
275
                    'label' => 'item2',
276
                    'url'   => '#',
277
                ],
278
                [
279
                    'label'   => 'item3',
280
                    'url'     => '#',
281
                    'options' => [
282
                        'class' => 'some classes',
283
                    ],
284
                ],
285
                [
286
                    'label'   => 'item4',
287
                    'url'     => '#',
288
                    'active'  => true,
289
                    'options' => [
290
                        'class' => 'another-class other--class two classes',
291
                    ],
292
                ],
293
            ]);
294
295
        $expected = <<<'HTML'
296
<ul><li class="someclass"><a href="#">item1</a></li>
297
<li><a href="#">item2</a></li>
298
<li class="some classes"><a href="#">item3</a></li>
299
<li class="another-class other--class two classes item-active"><a href="#">item4</a></li></ul>
300
HTML;
301
        $this->assertEqualsWithoutLE($expected, ob_get_contents());
302
        ob_end_clean();
303
    }
304
}
305