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
Push — CrudButton-fixees ( a4c9f3...1a6e15 )
by Pedro
15:19
created

CrudPanelButtonsTest::testAddButtonEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 10
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
        
132
        $this->addTestButton('lineViewButton');
133
134
        $expectedButton = $this->lineViewButton;
135
136
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'end');
137
138
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
139
    }
140
141
    public function testAddButtonUnknownPosition()
142
    {
143
144
        $this->expectException(\Exception::class);
145
146
        $expectedButton = $this->lineViewButton;
147
148
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'unknownPosition');
149
    }
150
151
    public function testAddButtonFromModelFunction()
152
    {
153
        $expectedButton = $this->topModelFunctionButton;
154
155
        $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']);
156
157
        $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray());
158
    }
159
160
    public function testAddButtonFromView()
161
    {
162
        $expectedButton = $this->topViewButton;
163
        $viewName = 'someViewName';
164
165
        $this->crudPanel->addButtonFromView($expectedButton['stack'], $expectedButton['name'], $viewName);
166
167
        $backpackButtonViewPackage = 'crud::buttons.';
168
        $actualButton = $this->crudPanel->buttons()->last();
169
170
        $this->assertEquals($expectedButton['stack'], $actualButton->stack);
171
        $this->assertEquals($expectedButton['name'], $actualButton->name);
172
        $this->assertEquals($backpackButtonViewPackage.$viewName, $actualButton->content);
173
    }
174
175
    public function testRemoveButton()
176
    {
177
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
178
        $this->crudPanel->removeButton('update');
179
180
        $this->assertCount(0, $this->crudPanel->buttons());
181
        $this->assertNull($this->getButtonByName('update'));
182
    }
183
    
184
    public function testRemoveButtons()
185
    {
186
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
187
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
188
        $this->crudPanel->removeButtons(['show', 'update']);
189
190
        $this->assertCount(0, $this->crudPanel->buttons());
191
        $this->assertNull($this->getButtonByName('show'));
192
        $this->assertNull($this->getButtonByName('update'));
193
    }
194
     
195
    public function testRemoveUnknownButtons()
196
    {
197
        $buttonNames = [
198
            'someButtonName',
199
            'someOtherButtonName',
200
        ];
201
202
        $this->addDefaultButtons();
203
        $this->crudPanel->removeButtons($buttonNames);
204
205
        $this->assertCount(4, $this->crudPanel->buttons());
206
    }
207
     
208
    public function testRemoveUnknownButton()
209
    {
210
211
        $this->addTestButton('topViewButton');
212
213
        $this->crudPanel->removeButton('someButtonName');
214
215
        $this->assertCount(1, $this->crudPanel->buttons());
216
    }
217
     
218
    public function testRemoveAllButtons()
219
    {
220
        $this->addDefaultButtons();
221
        $this->crudPanel->removeAllButtons();
222
223
        $this->assertEmpty($this->crudPanel->buttons());
224
    }
225
     
226
    public function testRemoveButtonFromStack()
227
    {
228
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
229
230
        $button = $this->crudPanel->buttons()->first();
231
232
        $this->crudPanel->removeButtonFromStack($button->name, $button->stack);
233
234
        $this->assertCount(0, $this->crudPanel->buttons());
235
        $this->assertNull($this->getButtonByName($button->name));
236
    }
237
    
238
    public function testRemoveUnknownButtonFromStack()
239
    {   
240
        $this->addTestButton('lineViewButton');
241
        $this->crudPanel->removeButtonFromStack('someButtonName', 'line');
242
243
        $this->assertCount(1, $this->crudPanel->buttons());
244
    }
245
    
246
    public function testRemoveButtonFromUnknownStack()
247
    {
248
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
249
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
250
251
        $button = $this->crudPanel->buttons()->first();
252
253
        $this->crudPanel->removeButtonFromStack($button->name, 'someStackName');
254
255
        $this->assertCount(2, $this->crudPanel->buttons());
256
    }
257
     
258
    public function testRemoveAllButtonsFromStack()
259
    {
260
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
261
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
262
263
        $this->crudPanel->removeAllButtonsFromStack('line');
264
265
        $this->assertCount(0, $this->crudPanel->buttons());
266
    }
267
     
268
    public function testRemoveAllButtonsFromUnknownStack()
269
    {
270
        $this->addTestButton('lineViewButton');
271
272
        $this->crudPanel->removeAllButtonsFromStack('someStackName');
273
274
        $this->assertCount(1, $this->crudPanel->buttons());
275
    }
276
    
277
    public function testOrderButtons()
278
    {
279
        $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
280
        $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
281
        $this->crudPanel->addButton('line', 'test', 'view', 'crud::buttons.test', 'end');
282
283
        $this->crudPanel->orderButtons('line', ['show', 'test']);
284
285
        $this->assertEquals(['show', 'test', 'update'], $this->crudPanel->buttons()->pluck('name')->toArray());
286
    }
287
288
    public function testAddButtonFluently()
289
    {
290
        $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
291
        $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

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