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 — v6-livewire-widget ( bb8951...53543e )
by Pedro
14:58
created

me()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

261
        $button2 = CrudButton::add('modelFunction')->model_function(/** @scrutinizer ignore-type */ function () {
Loading history...
262
            return 'test';
263
        })->section('top')->makeFirst();
264
        $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->last()->toArray());
265
        $button1->makeLast();
266
        $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
267
    }
268
269
    private function getButtonByName($name)
270
    {
271
        return $this->crudPanel->buttons()->first(function ($value) use ($name) {
272
            return $value->name == $name;
273
        });
274
    }
275
276
    private function addDefaultButtons()
277
    {
278
        CrudButton::name($this->topViewButton);
279
        CrudButton::name($this->lineViewButton);
280
        CrudButton::name($this->bottomViewButton);
281
        CrudButton::name($this->topModelFunctionButton);
282
    }
283
284
    private function addTestButton($buttonName)
285
    {
286
        CrudButton::name(array_values($this->{$buttonName}));
287
    }
288
289
    public function testMovingTheButtonUsingPosition()
290
    {
291
        $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
292
        $button2 = CrudButton::name('lineTest2')->to('line')->view('crud::buttons.test')->type('view')->position('beginning');
293
        $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
294
        $button2->position('end');
295
        $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->first()->toArray());
296
    }
297
298
    public function testThrowsErrorInUnknownPosition()
299
    {
300
        try {
301
            $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view')->position('unknown');
0 ignored issues
show
Unused Code introduced by
The assignment to $button1 is dead and can be removed.
Loading history...
302
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
303
        }
304
        $this->assertEquals(
305
            new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Unknown button position - please use \'beginning\' or \'end\'.'),
306
            $e
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
307
        );
308
    }
309
}
310