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

Completed
Pull Request — master (#1453)
by Oliver
03:25
created

CrudPanelCreateTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 345
Duplicated Lines 29.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 103
loc 345
rs 10
c 1
b 1
f 0
wmc 17
lcom 1
cbo 4

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreate() 0 17 1
A testCreateWithOneToOneRelationship() 0 4 1
A testCreateWithOneToManyRelationship() 0 23 1
A testCreateWithManyToOneRelationship() 0 19 1
A testCreateWithManyToManyRelationship() 0 18 1
A testGetRelationFields() 13 13 1
A testGetRelationFieldsCreateForm() 9 9 1
A testGetRelationFieldsUpdateForm() 9 9 1
A testGetRelationFieldsUnknownForm() 0 13 1
A testGetRelationFieldsDotNotation() 9 9 1
A testGetRelationFieldsNoRelations() 0 8 1
A testGetRelationFieldsNoFields() 0 6 1
A testGetRelationFieldsWithPivot() 9 9 1
A testGetRelationFieldsWithPivotNoRelations() 0 9 1
A testSyncPivot() 18 18 1
A testSyncPivotUnknownData() 18 18 1
A testSyncPivotUnknownModel() 18 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Faker\Factory;
6
use Backpack\CRUD\Tests\Unit\Models\User;
7
use Backpack\CRUD\Tests\Unit\Models\Article;
8
9
class CrudPanelCreateTest extends BaseDBCrudPanelTest
10
{
11
    private $nonRelationshipField = [
12
        'name' => 'field1',
13
        'label' => 'Field1',
14
    ];
15
16
    private $userInputFieldsNoRelationships = [
17
        [
18
            'name' => 'id',
19
            'type' => 'hidden',
20
        ], [
21
            'name' => 'name',
22
        ], [
23
            'name' => 'email',
24
            'type' => 'email',
25
        ], [
26
            'name' => 'password',
27
            'type' => 'password',
28
        ],
29
    ];
30
31
    private $articleInputFieldsOneToMany = [
32
        [
33
            'name' => 'id',
34
            'type' => 'hidden',
35
        ], [
36
            'name' => 'content',
37
        ], [
38
            'name' => 'tags',
39
        ], [
40
            'label' => 'Author',
41
            'type' => 'select',
42
            'name' => 'user_id',
43
            'entity' => 'user',
44
            'attribute' => 'name',
45
        ],
46
    ];
47
48
    private $userInputFieldsManyToMany = [
49
        [
50
            'name' => 'id',
51
            'type' => 'hidden',
52
        ], [
53
            'name' => 'name',
54
        ], [
55
            'name' => 'email',
56
            'type' => 'email',
57
        ], [
58
            'name' => 'password',
59
            'type' => 'password',
60
        ], [
61
            'label' => 'Roles',
62
            'type' => 'select_multiple',
63
            'name' => 'roles',
64
            'entity' => 'roles',
65
            'attribute' => 'name',
66
            'pivot' => true,
67
        ],
68
    ];
69
70
    private $userInputFieldsDotNotation = [
71
        [
72
            'name' => 'id',
73
            'type' => 'hidden',
74
        ], [
75
            'name' => 'name',
76
        ], [
77
            'name' => 'email',
78
            'type' => 'email',
79
        ], [
80
            'name' => 'password',
81
            'type' => 'password',
82
        ], [
83
            'label' => 'Roles',
84
            'type' => 'select_multiple',
85
            'name' => 'roles',
86
            'entity' => 'roles',
87
            'attribute' => 'name',
88
            'pivot' => true,
89
        ], [
90
            'label' => 'Street',
91
            'name' => 'street',
92
            'entity' => 'accountDetails.addresses',
93
            'attribute' => 'street',
94
        ],
95
    ];
96
97
    private $userInputFieldsManyToOne = [
98
        [
99
            'name' => 'id',
100
            'type' => 'hidden',
101
        ], [
102
            'name' => 'name',
103
        ], [
104
            'name' => 'email',
105
            'type' => 'email',
106
        ], [
107
            'name' => 'password',
108
            'type' => 'password',
109
        ], [
110
            'name' => 'articles',
111
            'type' => 'select2_many',
112
            'entity' => 'articles',
113
            'attribute' => 'content',
114
        ],
115
    ];
116
117
    public function testCreate()
118
    {
119
        $this->crudPanel->setModel(User::class);
120
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
121
        $faker = Factory::create();
122
        $inputData = [
123
            'name' => $faker->name,
124
            'email' => $faker->safeEmail,
125
            'password' => bcrypt($faker->password()),
126
        ];
127
128
        $entry = $this->crudPanel->create($inputData);
129
130
        $this->assertInstanceOf(User::class, $entry);
131
        $this->assertEntryEquals($inputData, $entry);
132
        $this->assertEmpty($entry->articles);
133
    }
134
135
    public function testCreateWithOneToOneRelationship()
136
    {
137
        $this->markTestIncomplete('Not yet implemented');
138
    }
139
140
    public function testCreateWithOneToManyRelationship()
141
    {
142
        $this->crudPanel->setModel(Article::class);
143
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
144
        $faker = Factory::create();
145
        $inputData = [
146
            'content' => $faker->text(),
147
            'tags' => $faker->words(3, true),
148
            'user_id' => 1,
149
            'metas' => null,
150
            'extras' => null,
151
            'cast_metas' => null,
152
            'cast_tags' => null,
153
            'cast_extras' => null,
154
        ];
155
156
        $entry = $this->crudPanel->create($inputData);
157
        $userEntry = User::find(1);
0 ignored issues
show
Unused Code introduced by
$userEntry is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
        $article = Article::where('user_id', 1)->with('user')->get()->last();
159
160
        $this->assertEntryEquals($inputData, $entry);
161
        $this->assertEquals($article->toArray(), $entry->toArray());
162
    }
163
164
    public function testCreateWithManyToOneRelationship()
165
    {
166
        $this->crudPanel->setModel(User::class);
167
        $this->crudPanel->addFields($this->userInputFieldsManyToOne);
168
        $faker = Factory::create();
169
        $inputData = [
170
            'name' => $faker->name,
171
            'email' => $faker->safeEmail,
172
            'password' => bcrypt($faker->password()),
173
            'remember_token' => null,
174
            'roles' => [1, 2],
175
            'articles' => [1, 2],
176
        ];
177
178
        $entry = $this->crudPanel->create($inputData);
179
180
        $this->assertInstanceOf(User::class, $entry);
181
        $this->assertEntryEquals($inputData, $entry);
182
    }
183
184
    public function testCreateWithManyToManyRelationship()
185
    {
186
        $this->crudPanel->setModel(User::class);
187
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
188
        $faker = Factory::create();
189
        $inputData = [
190
            'name' => $faker->name,
191
            'email' => $faker->safeEmail,
192
            'password' => bcrypt($faker->password()),
193
            'remember_token' => null,
194
            'roles' => [1, 2],
195
        ];
196
197
        $entry = $this->crudPanel->create($inputData);
198
199
        $this->assertInstanceOf(User::class, $entry);
200
        $this->assertEntryEquals($inputData, $entry);
201
    }
202
203 View Code Duplication
    public function testGetRelationFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $this->markTestIncomplete('Not correctly implemented');
206
207
        $this->crudPanel->setModel(User::class);
208
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
209
210
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
211
        //       for relationship fields in the update fields.
212
        $relationFields = $this->crudPanel->getRelationFields('both');
213
214
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
215
    }
216
217 View Code Duplication
    public function testGetRelationFieldsCreateForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        $this->crudPanel->setModel(User::class);
220
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
221
222
        $relationFields = $this->crudPanel->getRelationFields('create');
223
224
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
225
    }
226
227 View Code Duplication
    public function testGetRelationFieldsUpdateForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
    {
229
        $this->crudPanel->setModel(User::class);
230
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'update');
231
232
        $relationFields = $this->crudPanel->getRelationFields('update');
233
234
        $this->assertEquals($this->crudPanel->update_fields['roles'], array_last($relationFields));
235
    }
236
237
    public function testGetRelationFieldsUnknownForm()
238
    {
239
        $this->markTestIncomplete('Not correctly implemented');
240
241
        $this->expectException(\InvalidArgumentException::class);
242
243
        $this->crudPanel->setModel(User::class);
244
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
245
246
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
247
        //       update fields.
248
        $this->crudPanel->getRelationFields('unknownForm');
249
    }
250
251 View Code Duplication
    public function testGetRelationFieldsDotNotation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
    {
253
        $this->crudPanel->setModel(User::class);
254
        $this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create');
255
256
        $relationFields = $this->crudPanel->getRelationFields('create');
257
258
        $this->assertEquals($this->crudPanel->create_fields['street'], array_last($relationFields));
259
    }
260
261
    public function testGetRelationFieldsNoRelations()
262
    {
263
        $this->crudPanel->addField($this->nonRelationshipField);
264
265
        $relationFields = $this->crudPanel->getRelationFields();
266
267
        $this->assertEmpty($relationFields);
268
    }
269
270
    public function testGetRelationFieldsNoFields()
271
    {
272
        $relationFields = $this->crudPanel->getRelationFields();
273
274
        $this->assertEmpty($relationFields);
275
    }
276
277 View Code Duplication
    public function testGetRelationFieldsWithPivot()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
    {
279
        $this->crudPanel->setModel(User::class);
280
        $this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create');
281
282
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot('create');
283
284
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
285
    }
286
287
    public function testGetRelationFieldsWithPivotNoRelations()
288
    {
289
        $this->crudPanel->setModel(User::class);
290
        $this->crudPanel->addFields($this->nonRelationshipField, 'create');
291
292
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot('create');
293
294
        $this->assertEmpty($relationFields);
295
    }
296
297 View Code Duplication
    public function testSyncPivot()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
    {
299
        $this->crudPanel->setModel(User::class);
300
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
301
        $faker = Factory::create();
302
        $inputData = [
303
            'name' => $faker->name,
304
            'email' => $faker->safeEmail,
305
            'password' => bcrypt($faker->password()),
306
            'remember_token' => null,
307
            'roles' => [1, 2],
308
        ];
309
310
        $entry = User::find(1);
311
        $this->crudPanel->syncPivot($entry, $inputData);
312
313
        $this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray());
314
    }
315
316 View Code Duplication
    public function testSyncPivotUnknownData()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
    {
318
        $this->crudPanel->setModel(User::class);
319
        $this->crudPanel->addFields($this->nonRelationshipField);
320
        $faker = Factory::create();
321
        $inputData = [
322
            'name' => $faker->name,
323
            'email' => $faker->safeEmail,
324
            'password' => bcrypt($faker->password()),
325
            'remember_token' => null,
326
            'roles' => [1, 2],
327
        ];
328
329
        $entry = User::find(1);
330
        $this->crudPanel->syncPivot($entry, $inputData);
331
332
        $this->assertEquals(1, $entry->roles->count());
333
    }
334
335 View Code Duplication
    public function testSyncPivotUnknownModel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
    {
337
        $this->expectException(\BadMethodCallException::class);
338
339
        $this->crudPanel->setModel(User::class);
340
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
341
        $faker = Factory::create();
342
        $inputData = [
343
            'name' => $faker->name,
344
            'email' => $faker->safeEmail,
345
            'password' => bcrypt($faker->password()),
346
            'remember_token' => null,
347
            'roles' => [1, 2],
348
        ];
349
350
        $entry = Article::find(1);
351
        $this->crudPanel->syncPivot($entry, $inputData);
352
    }
353
}
354