Passed
Push — master ( f02495...4136df )
by Alexander
01:44
created

AccordionTest::testRender()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 103
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 80
nc 1
nop 0
dl 0
loc 103
rs 8.4362
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4\Tests;
6
7
use Yiisoft\Yii\Bootstrap4\Accordion;
8
use Yiisoft\Widget\Exception\InvalidConfigException;
9
10
/**
11
 * Tests for Accordion widget
12
 *
13
 * AccordionTest
14
 */
15
final class AccordionTest extends TestCase
16
{
17
    public function testRender(): void
18
    {
19
        Accordion::counter(0);
20
21
        $html = Accordion::widget()
22
            ->items([
23
                [
24
                    'label' => 'Collapsible Group Item #1',
25
                    'content' => [
26
                        'test content1',
27
                        'test content2'
28
                    ],
29
                ],
30
                [
31
                    'label' => 'Collapsible Group Item #2',
32
                    'content' => 'Das ist das Haus vom Nikolaus',
33
                    'contentOptions' => [
34
                        'class' => 'testContentOptions'
35
                    ],
36
                    'options' => [
37
                        'class' => 'testClass',
38
                        'id' => 'testId'
39
                    ],
40
                    'footer' => 'Footer'
41
                ],
42
                [
43
                    'label' => '<h1>Collapsible Group Item #3</h1>',
44
                    'content' => [
45
                        '<h2>test content1</h2>',
46
                        '<h2>test content2</h2>'
47
                    ],
48
                    'contentOptions' => [
49
                        'class' => 'testContentOptions2'
50
                    ],
51
                    'options' => [
52
                        'class' => 'testClass2',
53
                        'id' => 'testId2'
54
                    ],
55
                    'encode' => false,
56
                    'footer' => 'Footer2'
57
                ],
58
                [
59
                    'label' => '<h1>Collapsible Group Item #4</h1>',
60
                    'content' => [
61
                        '<h2>test content1</h2>',
62
                        '<h2>test content2</h2>'
63
                    ],
64
                    'contentOptions' => [
65
                        'class' => 'testContentOptions3'
66
                    ],
67
                    'options' => [
68
                        'class' => 'testClass3',
69
                        'id' => 'testId3'
70
                    ],
71
                    'encode' => true,
72
                    'footer' => 'Footer3'
73
                ],
74
            ])
75
            ->render();
76
77
        $expectedHtml = <<<HTML
78
<div id="w0-accordion" class="accordion">
79
<div class="card"><div id="w0-accordion-collapse0-heading" class="card-header"><h5 class="mb-0"><button type="button" id="w1-button" class="btn-link btn" data-toggle="collapse" data-target="#w0-accordion-collapse0" aria-expanded="true" aria-controls="w0-accordion-collapse0">Collapsible Group Item #1</button>
80
</h5></div>
81
<div id="w0-accordion-collapse0" class="collapse show" aria-labelledby="w0-accordion-collapse0-heading" data-parent="#w0-accordion">
82
<ul class="list-group">
83
<li class="list-group-item">test content1</li>
84
<li class="list-group-item">test content2</li>
85
</ul>
86
87
</div></div>
88
<div id="testId" class="testClass card"><div id="w0-accordion-collapse1-heading" class="card-header"><h5 class="mb-0"><button type="button" id="w2-button" class="btn-link btn" data-toggle="collapse" data-target="#w0-accordion-collapse1" aria-expanded="false" aria-controls="w0-accordion-collapse1">Collapsible Group Item #2</button>
89
</h5></div>
90
<div id="w0-accordion-collapse1" class="testContentOptions collapse" aria-labelledby="w0-accordion-collapse1-heading" data-parent="#w0-accordion">
91
<div class="card-body">Das ist das Haus vom Nikolaus</div>
92
93
<div class="card-footer">Footer</div>
94
</div></div>
95
<div id="testId2" class="testClass2 card"><div id="w0-accordion-collapse2-heading" class="card-header"><h5 class="mb-0"><button type="button" id="w3-button" class="btn-link btn" data-toggle="collapse" data-target="#w0-accordion-collapse2" aria-expanded="false" aria-controls="w0-accordion-collapse2"><h1>Collapsible Group Item #3</h1></button>
96
</h5></div>
97
<div id="w0-accordion-collapse2" class="testContentOptions2 collapse" aria-labelledby="w0-accordion-collapse2-heading" data-parent="#w0-accordion">
98
<ul class="list-group">
99
<li class="list-group-item"><h2>test content1</h2></li>
100
<li class="list-group-item"><h2>test content2</h2></li>
101
</ul>
102
103
<div class="card-footer">Footer2</div>
104
</div></div>
105
<div id="testId3" class="testClass3 card"><div id="w0-accordion-collapse3-heading" class="card-header"><h5 class="mb-0"><button type="button" id="w4-button" class="btn-link btn" data-toggle="collapse" data-target="#w0-accordion-collapse3" aria-expanded="false" aria-controls="w0-accordion-collapse3">&lt;h1&gt;Collapsible Group Item #4&lt;/h1&gt;</button>
106
</h5></div>
107
<div id="w0-accordion-collapse3" class="testContentOptions3 collapse" aria-labelledby="w0-accordion-collapse3-heading" data-parent="#w0-accordion">
108
<ul class="list-group">
109
<li class="list-group-item"><h2>test content1</h2></li>
110
<li class="list-group-item"><h2>test content2</h2></li>
111
</ul>
112
113
<div class="card-footer">Footer3</div>
114
</div></div>
115
</div>
116
117
HTML;
118
119
        $this->assertEqualsWithoutLE($expectedHtml, $html);
120
    }
121
122
    public function invalidItemsProvider(): array
123
    {
124
        return [
125
            [ ['content'] ], // only content without label key
126
            [ [[]] ], // only content array without label
127
            [ [['content' => 'test']] ], // only content array without label
128
        ];
129
    }
130
131
    /**
132
     * @dataProvider invalidItemsProvider
133
     */
134
    public function testMissingLabel($items): void
135
    {
136
        $this->expectException(InvalidConfigException::class);
137
138
        Accordion::widget()
139
            ->items($items)
140
            ->render();
141
    }
142
143
    public function testAutoCloseItems(): void
144
    {
145
        $items = [
146
            [
147
                'label' => 'Item 1',
148
                'content' => 'Content 1',
149
            ],
150
            [
151
                'label' => 'Item 2',
152
                'content' => 'Content 2',
153
            ],
154
        ];
155
156
        Accordion::counter(0);
157
158
        $html = Accordion::widget()
159
            ->items($items)
160
            ->render();
161
162
        $this->assertStringContainsString('data-parent="', $html);
163
164
        $html = Accordion::widget()
165
            ->autoCloseItems(false)
166
            ->items($items)
167
            ->render();
168
169
        $this->assertStringNotContainsString('data-parent="', $html);
170
    }
171
172
    /**
173
     * @depends testRender
174
     */
175
    public function testItemToggleTag(): void
176
    {
177
        $items = [
178
            [
179
                'label' => 'Item 1',
180
                'content' => 'Content 1',
181
            ],
182
            [
183
                'label' => 'Item 2',
184
                'content' => 'Content 2',
185
            ],
186
        ];
187
188
        Accordion::counter(0);
189
190
        $html = Accordion::widget()
191
            ->items($items)
192
            ->itemToggleOptions([
193
                'tag' => 'a',
194
                'class' => 'custom-toggle',
195
            ])
196
            ->render();
197
198
        $this->assertStringContainsString(
199
            '<h5 class="mb-0"><a type="button" class="custom-toggle" href="#w0-accordion-collapse0"',
200
            $html
201
        );
202
        $this->assertStringNotContainsString('<button', $html);
203
204
        $html = Accordion::widget()
205
            ->items($items)
206
            ->itemToggleOptions([
207
                'tag' => 'a',
208
                'class' => ['widget' => 'custom-toggle'],
209
            ])
210
            ->render();
211
212
        $this->assertStringContainsString(
213
            '<h5 class="mb-0"><a type="button" class="custom-toggle" href="#w1-accordion-collapse0"',
214
            $html
215
        );
216
        $this->assertStringNotContainsString('collapse-toggle', $html);
217
    }
218
}
219