We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 |
||
5 | class CrudPanelFieldsTest extends BaseCrudPanelTest |
||
6 | { |
||
7 | private $oneTextFieldArray = [ |
||
|
|||
8 | 'name' => 'field1', |
||
9 | 'label' => 'Field1', |
||
10 | 'type' => 'text', |
||
11 | ]; |
||
12 | |||
13 | private $expectedOneTextFieldArray = [ |
||
14 | 'field1' => [ |
||
15 | 'name' => 'field1', |
||
16 | 'label' => 'Field1', |
||
17 | 'type' => 'text', |
||
18 | ], |
||
19 | ]; |
||
20 | |||
21 | private $unknownFieldTypeArray = [ |
||
22 | 'name' => 'field1', |
||
23 | 'type' => 'unknownType', |
||
24 | ]; |
||
25 | |||
26 | private $invalidTwoFieldsArray = [ |
||
27 | [ |
||
28 | 'keyOne' => 'field1', |
||
29 | 'keyTwo' => 'Field1', |
||
30 | ], |
||
31 | [ |
||
32 | 'otherKey' => 'field2', |
||
33 | ], |
||
34 | ]; |
||
35 | |||
36 | private $twoTextFieldsArray = [ |
||
37 | [ |
||
38 | 'name' => 'field1', |
||
39 | 'label' => 'Field1', |
||
40 | 'type' => 'text', |
||
41 | ], |
||
42 | [ |
||
43 | 'name' => 'field2', |
||
44 | 'label' => 'Field2', |
||
45 | ], |
||
46 | ]; |
||
47 | |||
48 | private $expectedTwoTextFieldsArray = [ |
||
49 | 'field1' => [ |
||
50 | 'name' => 'field1', |
||
51 | 'label' => 'Field1', |
||
52 | 'type' => 'text', |
||
53 | ], |
||
54 | 'field2' => [ |
||
55 | 'name' => 'field2', |
||
56 | 'label' => 'Field2', |
||
57 | 'type' => 'text', |
||
58 | ], |
||
59 | ]; |
||
60 | |||
61 | private $threeTextFieldsArray = [ |
||
62 | [ |
||
63 | 'name' => 'field1', |
||
64 | 'label' => 'Field1', |
||
65 | 'type' => 'text', |
||
66 | ], |
||
67 | [ |
||
68 | 'name' => 'field2', |
||
69 | 'label' => 'Field2', |
||
70 | ], |
||
71 | [ |
||
72 | 'name' => 'field3', |
||
73 | ], |
||
74 | ]; |
||
75 | |||
76 | private $expectedThreeTextFieldsArray = [ |
||
77 | 'field1' => [ |
||
78 | 'name' => 'field1', |
||
79 | 'label' => 'Field1', |
||
80 | 'type' => 'text', |
||
81 | ], |
||
82 | 'field2' => [ |
||
83 | 'name' => 'field2', |
||
84 | 'label' => 'Field2', |
||
85 | 'type' => 'text', |
||
86 | ], |
||
87 | 'field3' => [ |
||
88 | 'name' => 'field3', |
||
89 | 'label' => 'Field3', |
||
90 | 'type' => 'text', |
||
91 | ], |
||
92 | ]; |
||
93 | |||
94 | private $multipleFieldTypesArray = [ |
||
95 | [ |
||
96 | 'name' => 'field1', |
||
97 | 'label' => 'Field1', |
||
98 | ], |
||
99 | [ |
||
100 | 'name' => 'field2', |
||
101 | 'type' => 'address', |
||
102 | ], |
||
103 | [ |
||
104 | 'name' => 'field3', |
||
105 | 'type' => 'address', |
||
106 | ], |
||
107 | [ |
||
108 | 'name' => 'field4', |
||
109 | 'type' => 'checkbox', |
||
110 | ], |
||
111 | [ |
||
112 | 'name' => 'field5', |
||
113 | 'type' => 'date', |
||
114 | ], |
||
115 | [ |
||
116 | 'name' => 'field6', |
||
117 | 'type' => 'email', |
||
118 | ], |
||
119 | [ |
||
120 | 'name' => 'field7', |
||
121 | 'type' => 'hidden', |
||
122 | ], |
||
123 | [ |
||
124 | 'name' => 'field8', |
||
125 | 'type' => 'password', |
||
126 | ], |
||
127 | [ |
||
128 | 'name' => 'field9', |
||
129 | 'type' => 'select2', |
||
130 | ], |
||
131 | [ |
||
132 | 'name' => 'field10', |
||
133 | 'type' => 'select2_multiple', |
||
134 | ], |
||
135 | [ |
||
136 | 'name' => 'field11', |
||
137 | 'type' => 'table', |
||
138 | ], |
||
139 | [ |
||
140 | 'name' => 'field12', |
||
141 | 'type' => 'url', |
||
142 | ], |
||
143 | ]; |
||
144 | |||
145 | private $expectedMultipleFieldTypesArray = [ |
||
146 | 'field1' => [ |
||
147 | 'name' => 'field1', |
||
148 | 'label' => 'Field1', |
||
149 | 'type' => 'text', |
||
150 | ], |
||
151 | 'field2' => [ |
||
152 | 'name' => 'field2', |
||
153 | 'type' => 'address', |
||
154 | 'label' => 'Field2', |
||
155 | ], |
||
156 | 'field3' => [ |
||
157 | 'name' => 'field3', |
||
158 | 'type' => 'address', |
||
159 | 'label' => 'Field3', |
||
160 | ], |
||
161 | 'field4' => [ |
||
162 | 'name' => 'field4', |
||
163 | 'type' => 'checkbox', |
||
164 | 'label' => 'Field4', |
||
165 | ], |
||
166 | 'field5' => [ |
||
167 | 'name' => 'field5', |
||
168 | 'type' => 'date', |
||
169 | 'label' => 'Field5', |
||
170 | ], |
||
171 | 'field6' => [ |
||
172 | 'name' => 'field6', |
||
173 | 'type' => 'email', |
||
174 | 'label' => 'Field6', |
||
175 | ], |
||
176 | 'field7' => [ |
||
177 | 'name' => 'field7', |
||
178 | 'type' => 'hidden', |
||
179 | 'label' => 'Field7', |
||
180 | ], |
||
181 | 'field8' => [ |
||
182 | 'name' => 'field8', |
||
183 | 'type' => 'password', |
||
184 | 'label' => 'Field8', |
||
185 | ], |
||
186 | 'field9' => [ |
||
187 | 'name' => 'field9', |
||
188 | 'type' => 'select2', |
||
189 | 'label' => 'Field9', |
||
190 | ], |
||
191 | 'field10' => [ |
||
192 | 'name' => 'field10', |
||
193 | 'type' => 'select2_multiple', |
||
194 | 'label' => 'Field10', |
||
195 | ], |
||
196 | 'field11' => [ |
||
197 | 'name' => 'field11', |
||
198 | 'type' => 'table', |
||
199 | 'label' => 'Field11', |
||
200 | ], |
||
201 | 'field12' => [ |
||
202 | 'name' => 'field12', |
||
203 | 'type' => 'url', |
||
204 | 'label' => 'Field12', |
||
205 | ], |
||
206 | ]; |
||
207 | |||
208 | View Code Duplication | public function testAddFieldByName() |
|
209 | { |
||
210 | $this->crudPanel->addField('field1'); |
||
211 | |||
212 | $this->assertEquals(1, count($this->crudPanel->create_fields)); |
||
213 | $this->assertEquals(1, count($this->crudPanel->update_fields)); |
||
214 | $this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->create_fields); |
||
215 | $this->assertEquals($this->expectedOneTextFieldArray, $this->crudPanel->update_fields); |
||
216 | } |
||
217 | |||
218 | public function testAddFieldsByName() |
||
219 | { |
||
220 | $this->crudPanel->addFields(['field1', 'field2', 'field3']); |
||
221 | |||
222 | $this->assertEquals(3, count($this->crudPanel->create_fields)); |
||
223 | $this->assertEquals(3, count($this->crudPanel->update_fields)); |
||
224 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->create_fields); |
||
225 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->update_fields); |
||
226 | } |
||
227 | |||
228 | View Code Duplication | public function testAddFieldsAsArray() |
|
229 | { |
||
230 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
231 | |||
232 | $this->assertEquals(3, count($this->crudPanel->create_fields)); |
||
233 | $this->assertEquals(3, count($this->crudPanel->update_fields)); |
||
234 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->create_fields); |
||
235 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->update_fields); |
||
236 | } |
||
237 | |||
238 | View Code Duplication | public function testAddFieldsDifferentTypes() |
|
239 | { |
||
240 | $this->crudPanel->addFields($this->multipleFieldTypesArray); |
||
241 | |||
242 | $this->assertEquals(12, count($this->crudPanel->create_fields)); |
||
243 | $this->assertEquals(12, count($this->crudPanel->update_fields)); |
||
244 | $this->assertEquals($this->expectedMultipleFieldTypesArray, $this->crudPanel->create_fields); |
||
245 | $this->assertEquals($this->expectedMultipleFieldTypesArray, $this->crudPanel->update_fields); |
||
246 | } |
||
247 | |||
248 | public function testAddFieldsInvalidArray() |
||
249 | { |
||
250 | $this->setExpectedException(\ErrorException::class); |
||
251 | |||
252 | $this->crudPanel->addFields($this->invalidTwoFieldsArray); |
||
253 | } |
||
254 | |||
255 | public function testAddFieldWithInvalidType() |
||
256 | { |
||
257 | $this->markTestIncomplete('Not correctly implemented'); |
||
258 | |||
259 | // TODO: should we validate field types and throw an error if they're not in the pre-defined list of fields or |
||
260 | // in the list of custom field? |
||
261 | $this->crudPanel->addFields($this->unknownFieldTypeArray); |
||
262 | } |
||
263 | |||
264 | View Code Duplication | public function testAddFieldsForCreateForm() |
|
265 | { |
||
266 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'create'); |
||
267 | |||
268 | $this->assertEquals(3, count($this->crudPanel->create_fields)); |
||
269 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->create_fields); |
||
270 | $this->assertEmpty($this->crudPanel->update_fields); |
||
271 | } |
||
272 | |||
273 | View Code Duplication | public function testAddFieldsForUpdateForm() |
|
274 | { |
||
275 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'update'); |
||
276 | |||
277 | $this->assertEquals(3, count($this->crudPanel->update_fields)); |
||
278 | $this->assertEquals($this->expectedThreeTextFieldsArray, $this->crudPanel->update_fields); |
||
279 | $this->assertEmpty($this->crudPanel->create_fields); |
||
280 | } |
||
281 | |||
282 | View Code Duplication | public function testBeforeField() |
|
283 | { |
||
284 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
285 | |||
286 | $this->crudPanel->beforeField('field2'); |
||
287 | |||
288 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
289 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[1]]); |
||
290 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
291 | |||
292 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
293 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[1]]); |
||
294 | $this->assertEquals(['field1', 'field3', 'field2'], $updateKeys); |
||
295 | } |
||
296 | |||
297 | View Code Duplication | public function testBeforeFieldFirstField() |
|
311 | |||
312 | View Code Duplication | public function testBeforeFieldLastField() |
|
326 | |||
327 | View Code Duplication | public function testBeforeFieldCreateForm() |
|
328 | { |
||
329 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
330 | |||
331 | $this->crudPanel->beforeField('field1', 'create'); |
||
332 | |||
333 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
334 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[0]]); |
||
335 | $this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
||
336 | |||
337 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
338 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[2]]); |
||
339 | $this->assertEquals(['field1', 'field2', 'field3'], $updateKeys); |
||
340 | } |
||
341 | |||
342 | View Code Duplication | public function testBeforeFieldUpdateForm() |
|
343 | { |
||
344 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
345 | |||
346 | $this->crudPanel->beforeField('field1', 'update'); |
||
347 | |||
348 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
349 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[2]]); |
||
350 | $this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
||
351 | |||
352 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
353 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[0]]); |
||
354 | $this->assertEquals(['field3', 'field1', 'field2'], $updateKeys); |
||
355 | } |
||
356 | |||
357 | View Code Duplication | public function testBeforeFieldForDifferentFieldsInCreateAndUpdate() |
|
358 | { |
||
359 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'create'); |
||
360 | $this->crudPanel->addFields($this->twoTextFieldsArray, 'update'); |
||
361 | |||
362 | $this->crudPanel->beforeField('field1'); |
||
363 | |||
364 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
365 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[0]]); |
||
366 | $this->assertEquals(['field3', 'field1', 'field2'], $createKeys); |
||
367 | |||
368 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
369 | $this->assertEquals($this->expectedTwoTextFieldsArray['field2'], $this->crudPanel->update_fields[$updateKeys[0]]); |
||
370 | $this->assertEquals(['field2', 'field1'], $updateKeys); |
||
371 | } |
||
372 | |||
373 | View Code Duplication | public function testBeforeUnknownField() |
|
374 | { |
||
375 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
376 | |||
377 | $this->crudPanel->beforeField('field4'); |
||
378 | |||
379 | $this->assertEquals(3, count($this->crudPanel->create_fields)); |
||
380 | $this->assertEquals(3, count($this->crudPanel->update_fields)); |
||
381 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->create_fields)); |
||
382 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->update_fields)); |
||
383 | } |
||
384 | |||
385 | View Code Duplication | public function testAfterField() |
|
386 | { |
||
387 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
388 | |||
389 | $this->crudPanel->afterField('field1'); |
||
390 | |||
391 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
392 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[1]]); |
||
393 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
394 | |||
395 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
396 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[1]]); |
||
397 | $this->assertEquals(['field1', 'field3', 'field2'], $updateKeys); |
||
398 | } |
||
399 | |||
400 | View Code Duplication | public function testAfterFieldLastField() |
|
414 | |||
415 | View Code Duplication | public function testAfterFieldCreateForm() |
|
416 | { |
||
417 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
418 | |||
419 | $this->crudPanel->afterField('field1', 'create'); |
||
420 | |||
421 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
422 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[1]]); |
||
423 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
424 | |||
425 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
426 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[2]]); |
||
427 | $this->assertEquals(['field1', 'field2', 'field3'], $updateKeys); |
||
428 | } |
||
429 | |||
430 | View Code Duplication | public function testAfterFieldUpdateForm() |
|
431 | { |
||
432 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
433 | |||
434 | $this->crudPanel->afterField('field1', 'update'); |
||
435 | |||
436 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
437 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[2]]); |
||
438 | $this->assertEquals(['field1', 'field2', 'field3'], $createKeys); |
||
439 | |||
440 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
441 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->update_fields[$updateKeys[1]]); |
||
442 | $this->assertEquals(['field1', 'field3', 'field2'], $updateKeys); |
||
443 | } |
||
444 | |||
445 | View Code Duplication | public function testAfterFieldForDifferentFieldsInCreateAndUpdate() |
|
446 | { |
||
447 | $this->crudPanel->addFields($this->threeTextFieldsArray, 'create'); |
||
448 | $this->crudPanel->addFields($this->twoTextFieldsArray, 'update'); |
||
449 | |||
450 | $this->crudPanel->afterField('field1'); |
||
451 | |||
452 | $createKeys = array_keys($this->crudPanel->create_fields); |
||
453 | $this->assertEquals($this->expectedThreeTextFieldsArray['field3'], $this->crudPanel->create_fields[$createKeys[1]]); |
||
454 | $this->assertEquals(['field1', 'field3', 'field2'], $createKeys); |
||
455 | |||
456 | $updateKeys = array_keys($this->crudPanel->update_fields); |
||
457 | $this->assertEquals($this->expectedTwoTextFieldsArray['field2'], $this->crudPanel->update_fields[$updateKeys[1]]); |
||
458 | $this->assertEquals(['field1', 'field2'], $updateKeys); |
||
459 | } |
||
460 | |||
461 | View Code Duplication | public function testAfterUnknownField() |
|
472 | |||
473 | public function testRemoveFieldsByName() |
||
474 | { |
||
475 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
476 | |||
477 | $this->crudPanel->removeFields(['field1']); |
||
478 | |||
479 | $this->assertEquals(2, count($this->crudPanel->create_fields)); |
||
480 | $this->assertEquals(2, count($this->crudPanel->update_fields)); |
||
481 | $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->create_fields)); |
||
482 | $this->assertEquals(['field2', 'field3'], array_keys($this->crudPanel->update_fields)); |
||
483 | } |
||
484 | |||
485 | View Code Duplication | public function testRemoveFieldsByNameInvalidArray() |
|
486 | { |
||
487 | $this->markTestIncomplete('Not correctly implemented'); |
||
488 | |||
489 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
490 | |||
491 | // TODO: this should not work because the method specifically asks for an array of field keys, but it does |
||
492 | // because the removeField method will actually work with arrays instead of a string |
||
493 | $this->crudPanel->removeFields($this->twoTextFieldsArray); |
||
494 | |||
495 | $this->assertEquals(3, count($this->crudPanel->create_fields)); |
||
496 | $this->assertEquals(3, count($this->crudPanel->update_fields)); |
||
497 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->create_fields)); |
||
498 | $this->assertEquals(array_keys($this->expectedThreeTextFieldsArray), array_keys($this->crudPanel->update_fields)); |
||
499 | } |
||
500 | |||
501 | View Code Duplication | public function testRemoveFieldsFromCreateForm() |
|
512 | |||
513 | View Code Duplication | public function testRemoveFieldsFromUpdateForm() |
|
524 | |||
525 | View Code Duplication | public function testRemoveUnknownFields() |
|
536 | |||
537 | View Code Duplication | public function testOrderFields() |
|
538 | { |
||
539 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
540 | |||
541 | $this->crudPanel->orderFields(['field2', 'field1', 'field3']); |
||
542 | |||
543 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->create_fields)); |
||
544 | $this->assertEquals(['field2', 'field1', 'field3'], array_keys($this->crudPanel->update_fields)); |
||
545 | } |
||
546 | |||
547 | View Code Duplication | public function testOrderFieldsCreateForm() |
|
548 | { |
||
549 | $this->crudPanel->addFields($this->threeTextFieldsArray); |
||
550 | |||
551 | $this->crudPanel->orderFields(['field2', 'field1', 'field3'], 'create'); |
||
552 | |||
556 | |||
557 | View Code Duplication | public function testOrderFieldsUpdateForm() |
|
566 | |||
567 | View Code Duplication | public function testOrderFieldsIncompleteList() |
|
576 | |||
577 | View Code Duplication | public function testOrderFieldsEmptyList() |
|
586 | |||
587 | public function testOrderFieldsUnknownList() |
||
596 | |||
597 | View Code Duplication | public function testOrderColumnsMixedList() |
|
606 | |||
607 | public function testCheckIfFieldIsFirstOfItsType() |
||
615 | |||
616 | public function testCheckIfUnknownFieldIsFirstOfItsType() |
||
622 | |||
623 | public function testCheckIfInvalidFieldIsFirstOfItsType() |
||
629 | |||
630 | public function testDecodeJsonCastedAttributes() |
||
636 | } |
||
637 |
This check marks private properties in classes that are never used. Those properties can be removed.