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
Push — master ( 861d7d...2ad8d1 )
by Cristian
04:38
created

testGetRelationFieldsDotNotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 9
loc 9
rs 9.6666
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
    public function testCreate()
98
    {
99
        $this->crudPanel->setModel(User::class);
100
        $this->crudPanel->addFields($this->userInputFieldsNoRelationships);
101
        $faker = Factory::create();
102
        $inputData = [
103
            'name' => $faker->name,
104
            'email' => $faker->safeEmail,
105
            'password' => bcrypt($faker->password()),
106
        ];
107
108
        $entry = $this->crudPanel->create($inputData);
109
110
        $this->assertInstanceOf(User::class, $entry);
111
        $this->assertEntryEquals($inputData, $entry);
112
        $this->assertEmpty($entry->articles);
113
    }
114
115
    public function testCreateWithOneToOneRelationship()
116
    {
117
        $this->markTestIncomplete('Not yet implemented');
118
    }
119
120
    public function testCreateWithOneToManyRelationship()
121
    {
122
        $this->crudPanel->setModel(Article::class);
123
        $this->crudPanel->addFields($this->articleInputFieldsOneToMany);
124
        $faker = Factory::create();
125
        $inputData = [
126
            'content' => $faker->text(),
127
            'tags' => $faker->words(3, true),
128
            'user_id' => 1,
129
            'metas' => null,
130
            'extras' => null,
131
        ];
132
133
        $entry = $this->crudPanel->create($inputData);
134
        $userEntry = User::find(1);
135
136
        $this->assertEntryEquals($inputData, $entry);
137
        $this->assertEquals($userEntry->articles->last()->toArray(), $entry->toArray());
138
    }
139
140
    public function testCreateWithManyToManyRelationship()
141
    {
142
        $this->crudPanel->setModel(User::class);
143
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
144
        $faker = Factory::create();
145
        $inputData = [
146
            'name' => $faker->name,
147
            'email' => $faker->safeEmail,
148
            'password' => bcrypt($faker->password()),
149
            'remember_token' => null,
150
            'roles' => [1, 2],
151
        ];
152
153
        $entry = $this->crudPanel->create($inputData);
154
155
        $this->assertInstanceOf(User::class, $entry);
156
        $this->assertEntryEquals($inputData, $entry);
157
    }
158
159 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...
160
    {
161
        $this->markTestIncomplete('Not correctly implemented');
162
163
        $this->crudPanel->setModel(User::class);
164
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
165
166
        // TODO: fix method and documentation. when 'both' is passed as the $form value, the getRelationFields searches
167
        //       for relationship fields in the update fields.
168
        $relationFields = $this->crudPanel->getRelationFields('both');
169
170
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
171
    }
172
173 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...
174
    {
175
        $this->crudPanel->setModel(User::class);
176
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'create');
177
178
        $relationFields = $this->crudPanel->getRelationFields('create');
179
180
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
181
    }
182
183 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...
184
    {
185
        $this->crudPanel->setModel(User::class);
186
        $this->crudPanel->addFields($this->userInputFieldsManyToMany, 'update');
187
188
        $relationFields = $this->crudPanel->getRelationFields('update');
189
190
        $this->assertEquals($this->crudPanel->update_fields['roles'], array_last($relationFields));
191
    }
192
193
    public function testGetRelationFieldsUnknownForm()
194
    {
195
        $this->markTestIncomplete('Not correctly implemented');
196
197
        $this->setExpectedException(\InvalidArgumentException::class);
198
199
        $this->crudPanel->setModel(User::class);
200
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
201
202
        // TODO: this should throw an invalid argument exception but instead it searches for relationship fields in the
203
        //       update fields.
204
        $this->crudPanel->getRelationFields('unknownForm');
205
    }
206
207 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...
208
    {
209
        $this->crudPanel->setModel(User::class);
210
        $this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create');
211
212
        $relationFields = $this->crudPanel->getRelationFields('create');
213
214
        $this->assertEquals($this->crudPanel->create_fields['street'], array_last($relationFields));
215
    }
216
217
    public function testGetRelationFieldsNoRelations()
218
    {
219
        $this->crudPanel->addField($this->nonRelationshipField);
220
221
        $relationFields = $this->crudPanel->getRelationFields();
222
223
        $this->assertEmpty($relationFields);
224
    }
225
226
    public function testGetRelationFieldsNoFields()
227
    {
228
        $relationFields = $this->crudPanel->getRelationFields();
229
230
        $this->assertEmpty($relationFields);
231
    }
232
233 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...
234
    {
235
        $this->crudPanel->setModel(User::class);
236
        $this->crudPanel->addFields($this->userInputFieldsDotNotation, 'create');
237
238
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot('create');
239
240
        $this->assertEquals($this->crudPanel->create_fields['roles'], array_last($relationFields));
241
    }
242
243
    public function testGetRelationFieldsWithPivotNoRelations()
244
    {
245
        $this->crudPanel->setModel(User::class);
246
        $this->crudPanel->addFields($this->nonRelationshipField, 'create');
247
248
        $relationFields = $this->crudPanel->getRelationFieldsWithPivot('create');
249
250
        $this->assertEmpty($relationFields);
251
    }
252
253 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...
254
    {
255
        $this->crudPanel->setModel(User::class);
256
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
257
        $faker = Factory::create();
258
        $inputData = [
259
            'name' => $faker->name,
260
            'email' => $faker->safeEmail,
261
            'password' => bcrypt($faker->password()),
262
            'remember_token' => null,
263
            'roles' => [1, 2],
264
        ];
265
266
        $entry = User::find(1);
267
        $this->crudPanel->syncPivot($entry, $inputData);
268
269
        $this->assertEquals($inputData['roles'], $entry->roles->pluck('id')->toArray());
270
    }
271
272 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...
273
    {
274
        $this->crudPanel->setModel(User::class);
275
        $this->crudPanel->addFields($this->nonRelationshipField);
276
        $faker = Factory::create();
277
        $inputData = [
278
            'name' => $faker->name,
279
            'email' => $faker->safeEmail,
280
            'password' => bcrypt($faker->password()),
281
            'remember_token' => null,
282
            'roles' => [1, 2],
283
        ];
284
285
        $entry = User::find(1);
286
        $this->crudPanel->syncPivot($entry, $inputData);
287
288
        $this->assertEquals(1, $entry->roles->count());
289
    }
290
291
    public function testSyncPivotUnknownModel()
292
    {
293
        $this->setExpectedException(\BadMethodCallException::class);
294
295
        $this->crudPanel->setModel(User::class);
296
        $this->crudPanel->addFields($this->userInputFieldsManyToMany);
297
        $faker = Factory::create();
298
        $inputData = [
299
            'name' => $faker->name,
300
            'email' => $faker->safeEmail,
301
            'password' => bcrypt($faker->password()),
302
            'remember_token' => null,
303
            'roles' => [1, 2],
304
        ];
305
306
        $entry = Article::find(1);
307
        $this->crudPanel->syncPivot($entry, $inputData);
308
    }
309
}
310