Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#4819)
by Cristian
30:07 queued 15:23
created

CrudPanelButtonsTest::testRemoveUnknownButtons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 6
c 2
b 2
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudButton;
6
7
/**
8
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Buttons
9
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudButton
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\Enums\ButtonStackEnum
11
 * @covers Backpack\CRUD\app\Library\CrudPanel\Enums\ButtonPositionEnum
12
 */
13
class CrudPanelButtonsTest extends BaseCrudPanelTest
14
{
15
    private $defaultButtonNames = [];
16
17
    private $topViewButton;
18
19
    private $lineViewButton;
20
21
    private $bottomViewButton;
22
23
    private $topModelFunctionButton;
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
29
        $this->crudPanel->setOperation('list');
30
31
        $this->topViewButton = [
32
            'name' => 'topViewButton',
33
            'stack' => 'top',
34
            'type' => 'view',
35
            'content' => 'crud::buttons.show',
36
        ];
37
        $this->lineViewButton = [
38
            'name' => 'lineViewButton',
39
            'stack' => 'line',
40
            'type' => 'view',
41
            'content' => 'crud::buttons.show',
42
            'position' => null,
43
        ];
44
        $this->bottomViewButton = [
45
            'name' => 'bottomViewButton',
46
            'stack' => 'bottom',
47
            'type' => 'view',
48
            'content' => 'crud::buttons.show',
49
            'position' => null,
50
        ];
51
        $this->topModelFunctionButton = [
52
            'name' => 'topModelFunctionButton',
53
            'stack' => 'top',
54
            'type' => 'model_function',
55
            'content' => 'crud::buttons.show',
56
            'position' => null,
57
        ];
58
    }
59
60
    public function testItCanAddMultipleButtons()
61
    {
62
        $this->addDefaultButtons();
63
64
        $this->assertcount(4, $this->crudPanel->buttons());
65
    }
66
67
    public function testCanAddButtonToSpecificStack()
68
    {
69
        $this->addDefaultButtons();
70
        $expectedButton = $this->topViewButton;
71
        $expectedButton['name'] = 'topViewButtonCustomName';
72
        $expectedButton['stack'] = 'top';
73
        $expectedButton['position'] = null;
74
75
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
76
77
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
78
        $this->assertCount(3, $this->crudPanel->getButtonsForStack($expectedButton['stack']));
79
    }
80
81
    public function testAddButtonBottomUnknownStackName()
82
    {
83
        $this->expectException(\Exception::class);
84
85
        $expectedButton = $this->topViewButton;
86
87
        $this->crudPanel->addButton('unknownStackName', $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
88
    }
89
90
    public function testAddButtonsWithSameName()
91
    {
92
        $expectedButton = $this->topViewButton;
93
94
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
95
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
96
97
        $this->assertCount(1, $this->crudPanel->buttons());
98
99
        $expectedButton2 = $this->bottomViewButton;
100
        CrudButton::name($expectedButton2);
101
        CrudButton::name($expectedButton2);
102
103
        $this->assertCount(2, $this->crudPanel->buttons());
104
    }
105
106
    public function testAddButtonsWithSameNameWithoutReplacing()
107
    {
108
        $this->markTestIncomplete('This no longer makes sense in Backpack 4.1. Button names are unique now.');
109
110
        $expectedButton = $this->topViewButton;
111
112
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], false, false);
113
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], false, false);
114
115
        $this->assertEquals(count($this->defaultButtonNames) + 2, count($this->crudPanel->buttons()));
116
    }
117
118
    public function testAddButtonBeginning()
119
    {
120
        $this->addTestButton('topViewButton');
121
122
        $expectedButton = $this->bottomViewButton;
123
124
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'beginning');
125
126
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->first()->toArray());
127
    }
128
129
    public function testAddButtonEnd()
130
    {
131
        $this->addTestButton('lineViewButton');
132
133
        $expectedButton = $this->lineViewButton;
134
135
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'end');
136
137
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
138
    }
139
140
    public function testAddButtonUnknownPosition()
141
    {
142
        $this->expectException(\Exception::class);
143
144
        $expectedButton = $this->lineViewButton;
145
146
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'unknownPosition');
147
    }
148
149
    public function testAddButtonFromModelFunction()
150
    {
151
        $expectedButton = $this->topModelFunctionButton;
152
153
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
154
155
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
156
    }
157
158
    public function testAddButtonFromView()
159
    {
160
        $expectedButton = $this->topViewButton;
161
        $viewName = 'someViewName';
162
163
        $this->crudPanel->addButtonFromView($expectedButton['stack'], $expectedButton['name'], $viewName);
164
165
        $backpackButtonViewPackage = 'crud::buttons.';
166
        $actualButton = $this->crudPanel->buttons()->last();
167
168
        $this->assertEquals($expectedButton['stack'], $actualButton->stack);
169
        $this->assertEquals($expectedButton['name'], $actualButton->name);
170
        $this->assertEquals($backpackButtonViewPackage.$viewName, $actualButton->content);
171
    }
172
173
    public function testRemoveButton()
174
    {
175
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
176
        $this->crudPanel->removeButton('update');
177
178
        $this->assertCount(0, $this->crudPanel->buttons());
179
        $this->assertNull($this->getButtonByName('update'));
180
    }
181
182
    public function testRemoveButtons()
183
    {
184
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
185
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
186
        $this->crudPanel->removeButtons(['show', 'update']);
187
188
        $this->assertCount(0, $this->crudPanel->buttons());
189
        $this->assertNull($this->getButtonByName('show'));
190
        $this->assertNull($this->getButtonByName('update'));
191
    }
192
193
    public function testRemoveUnknownButtons()
194
    {
195
        $buttonNames = [
196
            'someButtonName',
197
            'someOtherButtonName',
198
        ];
199
200
        $this->addDefaultButtons();
201
        $this->crudPanel->removeButtons($buttonNames);
202
203
        $this->assertCount(4, $this->crudPanel->buttons());
204
    }
205
206
    public function testRemoveUnknownButton()
207
    {
208
        $this->addTestButton('topViewButton');
209
210
        $this->crudPanel->removeButton('someButtonName');
211
212
        $this->assertCount(1, $this->crudPanel->buttons());
213
    }
214
215
    public function testRemoveAllButtons()
216
    {
217
        $this->addDefaultButtons();
218
        $this->crudPanel->removeAllButtons();
219
220
        $this->assertEmpty($this->crudPanel->buttons());
221
    }
222
223
    public function testRemoveButtonFromStack()
224
    {
225
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
226
227
        $button = $this->crudPanel->buttons()->first();
228
229
        $this->crudPanel->removeButtonFromStack($button->name, $button->stack);
230
231
        $this->assertCount(0, $this->crudPanel->buttons());
232
        $this->assertNull($this->getButtonByName($button->name));
233
    }
234
235
    public function testRemoveUnknownButtonFromStack()
236
    {
237
        $this->addTestButton('lineViewButton');
238
        $this->crudPanel->removeButtonFromStack('someButtonName', 'line');
239
240
        $this->assertCount(1, $this->crudPanel->buttons());
241
    }
242
243
    public function testRemoveButtonFromUnknownStack()
244
    {
245
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
246
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
247
248
        $button = $this->crudPanel->buttons()->first();
249
250
        $this->crudPanel->removeButtonFromStack($button->name, 'someStackName');
251
252
        $this->assertCount(2, $this->crudPanel->buttons());
253
    }
254
255
    public function testRemoveAllButtonsFromStack()
256
    {
257
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
258
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
259
260
        $this->crudPanel->removeAllButtonsFromStack('line');
261
262
        $this->assertCount(0, $this->crudPanel->buttons());
263
    }
264
265
    public function testRemoveAllButtonsFromUnknownStack()
266
    {
267
        $this->addTestButton('lineViewButton');
268
269
        $this->crudPanel->removeAllButtonsFromStack('someStackName');
270
271
        $this->assertCount(1, $this->crudPanel->buttons());
272
    }
273
274
    public function testOrderButtons()
275
    {
276
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
277
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
278
        $this->crudPanel->addButton('line', 'test', 'view', 'crud::buttons.test', 'end');
279
280
        $this->crudPanel->orderButtons('line', ['show', 'test']);
281
282
        $this->assertEquals(['show', 'test', 'update'], $this->crudPanel->buttons()->pluck('name')->toArray());
283
    }
284
285
    public function testAddButtonFluently()
286
    {
287
        $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
288
        $button2 = CrudButton::add('modelFunction')->model_function(function () {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $value of Backpack\CRUD\app\Librar...utton::model_function(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
        $button2 = CrudButton::add('modelFunction')->model_function(/** @scrutinizer ignore-type */ function () {
Loading history...
289
            return 'test';
290
        })->section('top')->makeFirst();
291
        $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->last()->toArray());
292
        $button1->makeLast();
293
        $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
294
    }
295
296
    private function getButtonByName($name)
297
    {
298
        return $this->crudPanel->buttons()->first(function ($value) use ($name) {
299
            return $value->name == $name;
300
        });
301
    }
302
303
    private function addDefaultButtons()
304
    {
305
        CrudButton::name($this->topViewButton);
306
        CrudButton::name($this->lineViewButton);
307
        CrudButton::name($this->bottomViewButton);
308
        CrudButton::name($this->topModelFunctionButton);
309
    }
310
311
    private function addTestButton($buttonName)
312
    {
313
        CrudButton::name(array_values($this->{$buttonName}));
314
    }
315
}
316