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

Test Failed
Push — refactor-request-anonymous-fun... ( 8b16a7...9c4532 )
by Pedro
12:40
created

testItCanGetTheRequiredFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\Tests\Unit\Http\Requests\UserRequest;
6
use Backpack\CRUD\Tests\Unit\Models\User;
7
8
/**
9
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Validation
10
 */
11
class CrudPanelValidationTest extends BaseDBCrudPanelTest
12
{
13
    public function testItThrowsValidationExceptions()
14
    {
15
        $this->crudPanel->setModel(User::class);
16
        $this->crudPanel->setValidation(UserRequest::class);
17
18
        $request = request()->create('users/', 'POST', [
19
            'email'    => '[email protected]',
20
            'password' => 'test',
21
        ]);
22
23
        $this->crudPanel->setRequest($request);
24
        $this->expectException(\Illuminate\Validation\ValidationException::class);
25
        $validatedRequest = $this->crudPanel->validateRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedRequest is dead and can be removed.
Loading history...
26
    }
27
28
    public function testItMergesFieldValidationWithRequestValidation()
29
    {
30
        $this->crudPanel->setModel(User::class);
31
        $this->crudPanel->setValidation(UserRequest::class);
32
33
        $request = request()->create('users/', 'POST', [
34
            'name'     => 'test name',
35
            'email'    => '[email protected]',
36
            'password' => 'test',
37
        ]);
38
39
        $request->setRouteResolver(function () use ($request) {
40
            return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Unit\Http\Controllers\UserCrudController', 'create']))->bind($request);
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\Tests\Unit\CrudPanel\Route was not found. Did you mean Route? If so, make sure to prefix the type with \.
Loading history...
41
        });
42
43
        $this->crudPanel->addFields(
44
            [
45
                'name'            => 'email',
46
                'validationRules' => 'required',
47
            ],
48
            [
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with array('name' => 'name'). ( Ignorable by Annotation )

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

48
        $this->crudPanel->/** @scrutinizer ignore-call */ 
49
                          addFields(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
                'name' => 'name',
50
            ],
51
            [
52
                'name' => 'password',
53
            ]
54
        );
55
56
        $this->crudPanel->setRequest($request);
57
58
        $this->crudPanel->validateRequest();
59
60
        $this->assertEquals(['email'], array_keys($this->crudPanel->getOperationSetting('validationRules')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...ting('validationRules') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        $this->assertEquals(['email'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('validationRules')));
Loading history...
61
    }
62
63
    public function testItMergesAllKindsOfValidation()
64
    {
65
        $this->crudPanel->setModel(User::class);
66
67
        $this->crudPanel->setOperation('create');
68
        $this->crudPanel->setValidation([
69
            'password' => 'required',
70
        ]);
71
        $this->crudPanel->setValidation(UserRequest::class);
72
        $request = request()->create('users/', 'POST', [
73
            'name'     => 'test name',
74
            'email'    => '[email protected]',
75
            'password' => 'test',
76
        ]);
77
78
        $request->setRouteResolver(function () use ($request) {
79
            return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Unit\Http\Controllers\UserCrudController', 'create']))->bind($request);
80
        });
81
82
        $this->crudPanel->addFields(
83
            [
84
                'name'            => 'email',
85
                'validationRules' => 'required',
86
            ],
87
            [
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...\CrudPanel::addFields() has too many arguments starting with array('name' => 'name'). ( Ignorable by Annotation )

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

87
        $this->crudPanel->/** @scrutinizer ignore-call */ 
88
                          addFields(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
88
                'name' => 'name',
89
            ],
90
            [
91
                'name' => 'password',
92
            ]
93
        );
94
95
        $this->crudPanel->setRequest($request);
96
97
        $validatedRequest = $this->crudPanel->validateRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedRequest is dead and can be removed.
Loading history...
98
99
        $this->assertEquals(['password', 'email'], array_keys($this->crudPanel->getOperationSetting('validationRules')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...ting('validationRules') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

99
        $this->assertEquals(['password', 'email'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('validationRules')));
Loading history...
100
    }
101
102
    public function testItCanGetTheValidationFromFields()
103
    {
104
        $this->crudPanel->setModel(User::class);
105
        $this->crudPanel->setOperation('create');
106
107
        $request = request()->create('users/', 'POST', [
108
            'name'     => 'test name',
109
            'email'    => '[email protected]',
110
            'password' => 'test',
111
        ]);
112
113
        $request->setRouteResolver(function () use ($request) {
114
            return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Unit\Http\Controllers\UserCrudController', 'create']))->bind($request);
115
        });
116
117
        $this->crudPanel->addField([
118
            'name'            => 'email',
119
            'validationRules' => 'required',
120
        ]);
121
122
        $this->crudPanel->addField([
123
            'name'               => 'name',
124
            'validationRules'    => 'required',
125
            'validationMessages' => [
126
                'required' => 'required ma friend',
127
            ],
128
        ]);
129
130
        $this->crudPanel->addField([
131
            'name'      => 'password',
132
            'subfields' => [
133
                [
134
                    'name'               => 'test',
135
                    'validationRules'    => 'required',
136
                    'validationMessages' => [
137
                        'required' => 'required ma friend',
138
                    ],
139
                ],
140
            ],
141
        ]);
142
143
        $this->crudPanel->setRequest($request);
144
145
        $this->crudPanel->setValidation();
146
147
        $validatedRequest = $this->crudPanel->validateRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedRequest is dead and can be removed.
Loading history...
148
149
        $this->assertEquals(['email', 'name', 'password.*.test'], array_keys($this->crudPanel->getOperationSetting('validationRules')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...ting('validationRules') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

149
        $this->assertEquals(['email', 'name', 'password.*.test'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('validationRules')));
Loading history...
150
    }
151
152
    public function testItThrowsExceptionWithInvalidaValidationClass()
153
    {
154
        $this->crudPanel->setModel(User::class);
155
        $this->crudPanel->setOperation('create');
156
157
        try {
158
            $this->crudPanel->setValidation('\Backpack\CRUD\Tests\Unit\Models\User');
159
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
160
        }
161
        $this->assertEquals(
162
            new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Please pass setValidation() nothing, a rules array or a FormRequest class.'),
163
            $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...
164
        );
165
    }
166
167
    public function testItCanDisableTheValidation()
168
    {
169
        $this->crudPanel->setModel(User::class);
170
        $this->crudPanel->setOperation('create');
171
        $this->crudPanel->setValidation([
172
            'name'     => 'required',
173
            'password' => 'required',
174
        ]);
175
        $this->crudPanel->setValidation(UserRequest::class);
176
        $this->assertEquals(['name', 'password'], array_keys($this->crudPanel->getOperationSetting('validationRules')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...ting('validationRules') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

176
        $this->assertEquals(['name', 'password'], array_keys(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('validationRules')));
Loading history...
177
178
        $this->crudPanel->disableValidation();
179
        $this->assertEquals([], $this->crudPanel->getOperationSetting('validationRules'));
180
        $this->assertEquals([], $this->crudPanel->getOperationSetting('validationMessages'));
181
        $this->assertEquals([], $this->crudPanel->getOperationSetting('requiredFields'));
182
        $this->assertEquals(false, $this->crudPanel->getFormRequest());
183
    }
184
185
    public function testItCanGetTheRequiredFields()
186
    {
187
        $this->crudPanel->setModel(User::class);
188
        $this->crudPanel->setOperation('create');
189
        $this->assertFalse($this->crudPanel->isRequired('test'));
190
        $this->crudPanel->setValidation([
191
            'email'     => 'required',
192
            'password.*.test' => 'required',
193
        ]);
194
       
195
        $this->crudPanel->setValidation(UserRequest::class);
196
        $this->assertEquals(['email', 'password[test]', 'name'], array_values($this->crudPanel->getOperationSetting('requiredFields')));
0 ignored issues
show
Bug introduced by
It seems like $this->crudPanel->getOpe...tting('requiredFields') can also be of type null; however, parameter $array of array_values() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

196
        $this->assertEquals(['email', 'password[test]', 'name'], array_values(/** @scrutinizer ignore-type */ $this->crudPanel->getOperationSetting('requiredFields')));
Loading history...
197
        $this->assertTrue($this->crudPanel->isRequired('email'));
198
        $this->assertTrue($this->crudPanel->isRequired('password.test'));
199
        $this->assertTrue($this->crudPanel->isRequired('name'));
200
       
201
    }
202
}
203