We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 49 |
Total Lines | 767 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 2 |
Complex classes like CrudPanelColumnsTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CrudPanelColumnsTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CrudPanelColumnsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
||
16 | { |
||
17 | private $oneColumnArray = [ |
||
18 | 'name' => 'column1', |
||
19 | 'label' => 'Column1', |
||
20 | ]; |
||
21 | |||
22 | private $expectedOneColumnArray = [ |
||
23 | 'column1' => [ |
||
24 | 'label' => 'Column1', |
||
25 | 'name' => 'column1', |
||
26 | 'key' => 'column1', |
||
27 | 'type' => 'text', |
||
28 | 'tableColumn' => false, |
||
29 | 'orderable' => false, |
||
30 | 'searchLogic' => false, |
||
31 | 'priority' => 0, |
||
32 | ], |
||
33 | ]; |
||
34 | |||
35 | private $otherOneColumnArray = [ |
||
36 | 'name' => 'column4', |
||
37 | 'label' => 'Column4', |
||
38 | ]; |
||
39 | |||
40 | private $twoColumnsArray = [ |
||
41 | [ |
||
42 | 'name' => 'column1', |
||
43 | 'label' => 'Column1', |
||
44 | ], |
||
45 | [ |
||
46 | 'name' => 'column2', |
||
47 | 'label' => 'Column2', |
||
48 | ], |
||
49 | ]; |
||
50 | |||
51 | private $expectedTwoColumnsArray = [ |
||
52 | 'column1' => [ |
||
53 | 'name' => 'column1', |
||
54 | 'key' => 'column1', |
||
55 | 'label' => 'Column1', |
||
56 | 'type' => 'text', |
||
57 | 'tableColumn' => false, |
||
58 | 'orderable' => false, |
||
59 | 'searchLogic' => false, |
||
60 | 'priority' => 0, |
||
61 | |||
62 | ], |
||
63 | 'column2' => [ |
||
64 | 'name' => 'column2', |
||
65 | 'key' => 'column2', |
||
66 | 'label' => 'Column2', |
||
67 | 'type' => 'text', |
||
68 | 'tableColumn' => false, |
||
69 | 'orderable' => false, |
||
70 | 'searchLogic' => false, |
||
71 | 'priority' => 1, |
||
72 | ], |
||
73 | ]; |
||
74 | |||
75 | private $threeColumnsArray = [ |
||
76 | [ |
||
77 | 'name' => 'column1', |
||
78 | 'label' => 'Column1', |
||
79 | ], |
||
80 | [ |
||
81 | 'name' => 'column2', |
||
82 | 'label' => 'Column2', |
||
83 | ], |
||
84 | [ |
||
85 | 'name' => 'column3', |
||
86 | 'label' => 'Column3', |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | private $expectedThreeColumnsArray = [ |
||
91 | 'column1' => [ |
||
92 | 'name' => 'column1', |
||
93 | 'key' => 'column1', |
||
94 | 'label' => 'Column1', |
||
95 | 'type' => 'text', |
||
96 | 'tableColumn' => false, |
||
97 | 'orderable' => false, |
||
98 | 'searchLogic' => false, |
||
99 | 'priority' => 0, |
||
100 | ], |
||
101 | 'column2' => [ |
||
102 | 'name' => 'column2', |
||
103 | 'key' => 'column2', |
||
104 | 'label' => 'Column2', |
||
105 | 'type' => 'text', |
||
106 | 'tableColumn' => false, |
||
107 | 'orderable' => false, |
||
108 | 'searchLogic' => false, |
||
109 | 'priority' => 1, |
||
110 | ], |
||
111 | 'column3' => [ |
||
112 | 'name' => 'column3', |
||
113 | 'key' => 'column3', |
||
114 | 'label' => 'Column3', |
||
115 | 'type' => 'text', |
||
116 | 'tableColumn' => false, |
||
117 | 'orderable' => false, |
||
118 | 'searchLogic' => false, |
||
119 | 'priority' => 2, |
||
120 | ], |
||
121 | ]; |
||
122 | |||
123 | private $expectedRelationColumnsArrayWithoutPro = [ |
||
124 | 'accountDetails' => [ |
||
125 | 'name' => 'accountDetails', |
||
126 | 'label' => 'AccountDetails', |
||
127 | 'type' => 'text', |
||
128 | 'key' => 'accountDetails', |
||
129 | 'priority' => 0, |
||
130 | 'tableColumn' => false, |
||
131 | 'orderable' => false, |
||
132 | 'searchLogic' => false, |
||
133 | 'entity' => 'accountDetails', |
||
134 | 'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
||
135 | 'relation_type' => 'HasOne', |
||
136 | 'attribute' => 'nickname', |
||
137 | ], |
||
138 | 'accountDetails__nickname' => [ |
||
139 | 'name' => 'accountDetails.nickname', |
||
140 | 'label' => 'AccountDetails.nickname', |
||
141 | 'type' => 'text', |
||
142 | 'key' => 'accountDetails__nickname', |
||
143 | 'priority' => 1, |
||
144 | 'attribute' => 'nickname', |
||
145 | 'tableColumn' => false, |
||
146 | 'orderable' => false, |
||
147 | 'searchLogic' => false, |
||
148 | 'relation_type' => 'HasOne', |
||
149 | 'entity' => 'accountDetails.nickname', |
||
150 | 'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
||
151 | ], |
||
152 | 'accountDetails__user' => [ |
||
153 | 'name' => 'accountDetails.user', |
||
154 | 'label' => 'AccountDetails.user', |
||
155 | 'type' => 'select', |
||
156 | 'key' => 'accountDetails__user', |
||
157 | 'priority' => 2, |
||
158 | 'tableColumn' => false, |
||
159 | 'orderable' => false, |
||
160 | 'searchLogic' => false, |
||
161 | 'relation_type' => 'BelongsTo', |
||
162 | 'entity' => 'accountDetails.user', |
||
163 | 'model' => 'Backpack\CRUD\Tests\Config\Models\User', |
||
164 | 'attribute' => 'name', |
||
165 | ], |
||
166 | ]; |
||
167 | |||
168 | private $expectedRelationColumnsArrayWithPro = [ |
||
169 | 'accountDetails' => [ |
||
170 | 'name' => 'accountDetails', |
||
171 | 'label' => 'AccountDetails', |
||
172 | 'type' => 'relationship', |
||
173 | 'key' => 'accountDetails', |
||
174 | 'priority' => 0, |
||
175 | 'tableColumn' => false, |
||
176 | 'orderable' => false, |
||
177 | 'searchLogic' => false, |
||
178 | 'entity' => 'accountDetails', |
||
179 | 'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
||
180 | 'relation_type' => 'HasOne', |
||
181 | 'attribute' => 'nickname', |
||
182 | ], |
||
183 | 'accountDetails__nickname' => [ |
||
184 | 'name' => 'accountDetails.nickname', |
||
185 | 'label' => 'AccountDetails.nickname', |
||
186 | 'type' => 'relationship', |
||
187 | 'key' => 'accountDetails__nickname', |
||
188 | 'priority' => 1, |
||
189 | 'attribute' => 'nickname', |
||
190 | 'tableColumn' => false, |
||
191 | 'orderable' => false, |
||
192 | 'searchLogic' => false, |
||
193 | 'relation_type' => 'HasOne', |
||
194 | 'entity' => 'accountDetails.nickname', |
||
195 | 'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
||
196 | ], |
||
197 | 'accountDetails__user' => [ |
||
198 | 'name' => 'accountDetails.user', |
||
199 | 'label' => 'AccountDetails.user', |
||
200 | 'type' => 'relationship', |
||
201 | 'key' => 'accountDetails__user', |
||
202 | 'priority' => 2, |
||
203 | 'tableColumn' => false, |
||
204 | 'orderable' => false, |
||
205 | 'searchLogic' => false, |
||
206 | 'relation_type' => 'BelongsTo', |
||
207 | 'entity' => 'accountDetails.user', |
||
208 | 'model' => 'Backpack\CRUD\Tests\Config\Models\User', |
||
209 | 'attribute' => 'name', |
||
210 | ], |
||
211 | ]; |
||
212 | |||
213 | private $relationColumnArray = [ |
||
214 | 'name' => 'nickname', |
||
215 | 'type' => 'select', |
||
216 | 'entity' => 'accountDetails', |
||
217 | 'attribute' => 'nickname', |
||
218 | ]; |
||
219 | |||
220 | private $expectedRelationColumnArray = [ |
||
221 | 'nickname' => [ |
||
222 | 'name' => 'nickname', |
||
223 | 'type' => 'select', |
||
224 | 'entity' => 'accountDetails', |
||
225 | 'attribute' => 'nickname', |
||
226 | 'label' => 'Nickname', |
||
227 | 'model' => 'Backpack\CRUD\Tests\Config\Models\AccountDetails', |
||
228 | 'key' => 'nickname', |
||
229 | 'tableColumn' => false, |
||
230 | 'orderable' => false, |
||
231 | 'searchLogic' => false, |
||
232 | 'priority' => 0, |
||
233 | 'relation_type' => 'HasOne', |
||
234 | ], |
||
235 | ]; |
||
236 | |||
237 | private $nestedRelationColumnArray = [ |
||
238 | 'name' => 'accountDetails.article', |
||
239 | ]; |
||
240 | |||
241 | private $secondNestedRelationColumnArray = [ |
||
242 | 'name' => 'accountDetails.article', |
||
243 | 'attribute' => 'content', |
||
244 | 'key' => 'ac_article_content', |
||
245 | ]; |
||
246 | |||
247 | private $expectedNestedRelationColumnArrayWithPro = [ |
||
248 | 'accountDetails__article' => [ |
||
249 | 'name' => 'accountDetails.article', |
||
250 | 'type' => 'relationship', |
||
251 | 'entity' => 'accountDetails.article', |
||
252 | 'label' => 'AccountDetails.article', |
||
253 | 'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
||
254 | 'key' => 'accountDetails__article', |
||
255 | 'tableColumn' => false, |
||
256 | 'orderable' => false, |
||
257 | 'searchLogic' => false, |
||
258 | 'priority' => 0, |
||
259 | 'relation_type' => 'BelongsTo', |
||
260 | 'attribute' => 'content', |
||
261 | ], |
||
262 | 'ac_article_content' => [ |
||
263 | 'name' => 'accountDetails.article', |
||
264 | 'type' => 'relationship', |
||
265 | 'entity' => 'accountDetails.article', |
||
266 | 'label' => 'AccountDetails.article', |
||
267 | 'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
||
268 | 'key' => 'ac_article_content', |
||
269 | 'tableColumn' => false, |
||
270 | 'orderable' => false, |
||
271 | 'searchLogic' => false, |
||
272 | 'priority' => 1, |
||
273 | 'relation_type' => 'BelongsTo', |
||
274 | 'attribute' => 'content', |
||
275 | ], |
||
276 | ]; |
||
277 | |||
278 | private $expectedNestedRelationColumnArrayWithoutPro = [ |
||
279 | 'accountDetails__article' => [ |
||
280 | 'name' => 'accountDetails.article', |
||
281 | 'type' => 'select', |
||
282 | 'entity' => 'accountDetails.article', |
||
283 | 'label' => 'AccountDetails.article', |
||
284 | 'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
||
285 | 'key' => 'accountDetails__article', |
||
286 | 'tableColumn' => false, |
||
287 | 'orderable' => false, |
||
288 | 'searchLogic' => false, |
||
289 | 'priority' => 0, |
||
290 | 'relation_type' => 'BelongsTo', |
||
291 | 'attribute' => 'content', |
||
292 | ], |
||
293 | 'ac_article_content' => [ |
||
294 | 'name' => 'accountDetails.article', |
||
295 | 'type' => 'select', |
||
296 | 'entity' => 'accountDetails.article', |
||
297 | 'label' => 'AccountDetails.article', |
||
298 | 'model' => 'Backpack\CRUD\Tests\Config\Models\Article', |
||
299 | 'key' => 'ac_article_content', |
||
300 | 'tableColumn' => false, |
||
301 | 'orderable' => false, |
||
302 | 'searchLogic' => false, |
||
303 | 'priority' => 1, |
||
304 | 'relation_type' => 'BelongsTo', |
||
305 | 'attribute' => 'content', |
||
306 | ], |
||
307 | ]; |
||
308 | |||
309 | /** |
||
310 | * Setup the test environment. |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | protected function setUp(): void |
||
315 | { |
||
316 | parent::setUp(); |
||
317 | |||
318 | $this->crudPanel->setOperation('list'); |
||
319 | } |
||
320 | |||
321 | public function testAddColumnByName() |
||
322 | { |
||
323 | $this->crudPanel->addColumn('column1'); |
||
324 | |||
325 | $this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
||
326 | } |
||
327 | |||
328 | public function testAddColumnsByName() |
||
329 | { |
||
330 | $this->crudPanel->addColumns(['column1', 'column2']); |
||
331 | |||
332 | $this->assertEquals(2, count($this->crudPanel->columns())); |
||
333 | $this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
||
334 | } |
||
335 | |||
336 | public function testAddColumnAsArray() |
||
337 | { |
||
338 | $this->crudPanel->addColumn($this->oneColumnArray); |
||
339 | |||
340 | $this->assertEquals($this->expectedOneColumnArray, $this->crudPanel->columns()); |
||
341 | } |
||
342 | |||
343 | public function testAddColumnsAsArray() |
||
344 | { |
||
345 | $this->crudPanel->addColumns($this->twoColumnsArray); |
||
346 | |||
347 | $this->assertEquals(2, count($this->crudPanel->columns())); |
||
348 | $this->assertEquals($this->expectedTwoColumnsArray, $this->crudPanel->columns()); |
||
349 | } |
||
350 | |||
351 | public function testAddColumnNotArray() |
||
352 | { |
||
353 | $this->expectException(PHP_MAJOR_VERSION == 7 ? \ErrorException::class : \TypeError::class); |
||
354 | // Why? When calling count() on a non-countable entity, |
||
355 | // PHP 7.x will through ErrorException, but PHP 8.x will throw TypeError. |
||
356 | |||
357 | $this->crudPanel->addColumns('column1'); |
||
|
|||
358 | } |
||
359 | |||
360 | public function testAddRelationsByName() |
||
361 | { |
||
362 | $this->crudPanel->setModel(User::class); |
||
363 | $this->crudPanel->addColumn('accountDetails'); |
||
364 | $this->crudPanel->addColumn('accountDetails.nickname'); |
||
365 | $this->crudPanel->addColumn('accountDetails.user'); |
||
366 | |||
367 | if (backpack_pro()) { |
||
368 | $this->assertEquals($this->expectedRelationColumnsArrayWithPro, $this->crudPanel->columns()); |
||
369 | } else { |
||
370 | $this->assertEquals($this->expectedRelationColumnsArrayWithoutPro, $this->crudPanel->columns()); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | public function testAddRelationColumn() |
||
375 | { |
||
376 | $this->crudPanel->setModel(User::class); |
||
377 | $this->crudPanel->addColumn($this->relationColumnArray); |
||
378 | |||
379 | $this->assertEquals($this->expectedRelationColumnArray, $this->crudPanel->columns()); |
||
380 | } |
||
381 | |||
382 | public function testAddNestedRelationColumn() |
||
383 | { |
||
384 | $this->crudPanel->setModel(User::class); |
||
385 | $this->crudPanel->addColumn($this->nestedRelationColumnArray); |
||
386 | $this->crudPanel->addColumn($this->secondNestedRelationColumnArray); |
||
387 | |||
388 | if (backpack_pro()) { |
||
389 | $this->assertEquals($this->expectedNestedRelationColumnArrayWithPro, $this->crudPanel->columns()); |
||
390 | } else { |
||
391 | $this->assertEquals($this->expectedNestedRelationColumnArrayWithoutPro, $this->crudPanel->columns()); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | public function testMoveColumnBefore() |
||
396 | { |
||
397 | $this->crudPanel->addColumns($this->twoColumnsArray); |
||
398 | |||
399 | $this->crudPanel->beforeColumn('column1'); |
||
400 | |||
401 | $keys = array_keys($this->crudPanel->columns()); |
||
402 | $this->assertEquals($this->expectedTwoColumnsArray['column2'], $this->crudPanel->columns()[$keys[0]]); |
||
403 | $this->assertEquals(['column2', 'column1'], $keys); |
||
404 | } |
||
405 | |||
406 | public function testMoveColumnBeforeUnknownColumnName() |
||
407 | { |
||
408 | $this->crudPanel->addColumns($this->twoColumnsArray); |
||
409 | |||
410 | $this->crudPanel->beforeColumn('column3'); |
||
411 | |||
412 | $this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
||
413 | } |
||
414 | |||
415 | public function testMoveColumnAfter() |
||
416 | { |
||
417 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
418 | |||
419 | $this->crudPanel->afterColumn('column1'); |
||
420 | |||
421 | $keys = array_keys($this->crudPanel->columns()); |
||
422 | $this->assertEquals($this->expectedThreeColumnsArray['column3'], $this->crudPanel->columns()[$keys[1]]); |
||
423 | $this->assertEquals(['column1', 'column3', 'column2'], $keys); |
||
424 | } |
||
425 | |||
426 | public function testMoveColumnAfterUnknownColumnName() |
||
427 | { |
||
428 | $this->crudPanel->addColumns($this->twoColumnsArray); |
||
429 | |||
430 | $this->crudPanel->afterColumn('column3'); |
||
431 | |||
432 | $this->assertEquals(array_keys($this->expectedTwoColumnsArray), array_keys($this->crudPanel->columns())); |
||
433 | } |
||
434 | |||
435 | public function testRemoveColumnByName() |
||
436 | { |
||
437 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
438 | |||
439 | $this->crudPanel->removeColumn('column1'); |
||
440 | |||
441 | $this->assertEquals(2, count($this->crudPanel->columns())); |
||
442 | $this->assertEquals(['column2', 'column3'], array_keys($this->crudPanel->columns())); |
||
443 | $this->assertNotContains($this->oneColumnArray, $this->crudPanel->columns()); |
||
444 | } |
||
445 | |||
446 | public function testRemoveUnknownColumnName() |
||
447 | { |
||
448 | $unknownColumnName = 'column4'; |
||
449 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
450 | |||
451 | $this->crudPanel->removeColumn($unknownColumnName); |
||
452 | |||
453 | $this->assertEquals(3, count($this->crudPanel->columns())); |
||
454 | $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
||
455 | $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
||
456 | } |
||
457 | |||
458 | public function testRemoveColumnsByName() |
||
459 | { |
||
460 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
461 | |||
462 | $this->crudPanel->removeColumns($this->twoColumnsArray); |
||
463 | |||
464 | $this->assertEquals(1, count($this->crudPanel->columns())); |
||
465 | $this->assertEquals(['column3'], array_keys($this->crudPanel->columns())); |
||
466 | $this->assertNotEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
467 | } |
||
468 | |||
469 | public function testRemoveUnknownColumnsByName() |
||
470 | { |
||
471 | $unknownColumnNames = ['column4', 'column5']; |
||
472 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
473 | |||
474 | $this->crudPanel->removeColumns($unknownColumnNames); |
||
475 | |||
476 | $this->assertEquals(3, count($this->crudPanel->columns())); |
||
477 | $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
||
478 | $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
||
479 | } |
||
480 | |||
481 | public function testSetColumnDetails() |
||
482 | { |
||
483 | $this->markTestIncomplete('Not correctly implemented'); |
||
484 | |||
485 | // TODO: refactor crud panel sync method |
||
486 | } |
||
487 | |||
488 | public function testSetColumnsDetails() |
||
489 | { |
||
490 | $this->markTestIncomplete('Not correctly implemented'); |
||
491 | |||
492 | // TODO: refactor crud panel sync method |
||
493 | } |
||
494 | |||
495 | public function testOrderColumns() |
||
496 | { |
||
497 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
498 | |||
499 | $this->crudPanel->orderColumns(['column2', 'column1', 'column3']); |
||
500 | |||
501 | $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
||
502 | } |
||
503 | |||
504 | public function testOrderColumnsIncompleteList() |
||
505 | { |
||
506 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
507 | |||
508 | $this->crudPanel->orderColumns(['column2', 'column3']); |
||
509 | |||
510 | $this->assertEquals(['column2', 'column3', 'column1'], array_keys($this->crudPanel->columns())); |
||
511 | } |
||
512 | |||
513 | public function testOrderColumnsEmptyList() |
||
514 | { |
||
515 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
516 | |||
517 | $this->crudPanel->orderColumns([]); |
||
518 | |||
519 | $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
520 | } |
||
521 | |||
522 | public function testOrderColumnsUnknownList() |
||
523 | { |
||
524 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
525 | |||
526 | $this->crudPanel->orderColumns(['column4', 'column5', 'column6']); |
||
527 | |||
528 | $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
529 | } |
||
530 | |||
531 | public function testOrderColumnsMixedList() |
||
532 | { |
||
533 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
534 | |||
535 | $this->crudPanel->orderColumns(['column2', 'column5', 'column6']); |
||
536 | |||
537 | $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
||
538 | } |
||
539 | |||
540 | public function testItCanChangeTheColumnKey() |
||
541 | { |
||
542 | $this->crudPanel->column('test'); |
||
543 | |||
544 | $this->assertEquals('test', $this->crudPanel->columns()['test']['key']); |
||
545 | |||
546 | $this->crudPanel->column('test')->key('new_key'); |
||
547 | |||
548 | $this->assertEquals('new_key', $this->crudPanel->columns()['new_key']['key']); |
||
549 | } |
||
550 | |||
551 | public function testItCanAddAFluentColumn() |
||
552 | { |
||
553 | $this->crudPanel->setModel(User::class); |
||
554 | |||
555 | $this->crudPanel->column('my_column')->label('my_column'); |
||
556 | |||
557 | $this->assertCount(1, $this->crudPanel->columns()); |
||
558 | |||
559 | $this->assertEquals([ |
||
560 | 'name' => 'my_column', |
||
561 | 'type' => 'text', |
||
562 | 'label' => 'my_column', |
||
563 | 'key' => 'my_column', |
||
564 | 'priority' => 0, |
||
565 | 'tableColumn' => false, |
||
566 | 'orderable' => false, |
||
567 | 'searchLogic' => false, |
||
568 | ], $this->crudPanel->columns()['my_column']); |
||
569 | } |
||
570 | |||
571 | public function testItCanMakeAColumnFirstFluently() |
||
572 | { |
||
573 | $this->crudPanel->column('test1'); |
||
574 | $this->crudPanel->column('test2')->makeFirst(); |
||
575 | $crudColumns = $this->crudPanel->columns(); |
||
576 | $firstColumn = reset($crudColumns); |
||
577 | $this->assertEquals($firstColumn['name'], 'test2'); |
||
578 | } |
||
579 | |||
580 | public function testItCanMakeAColumnLastFluently() |
||
581 | { |
||
582 | $this->crudPanel->column('test1'); |
||
583 | $this->crudPanel->column('test2'); |
||
584 | $this->crudPanel->column('test1')->makeLast(); |
||
585 | $crudColumns = $this->crudPanel->columns(); |
||
586 | $firstColumn = reset($crudColumns); |
||
587 | $this->assertEquals($firstColumn['name'], 'test2'); |
||
588 | } |
||
589 | |||
590 | public function testItCanPlaceColumnsFluently() |
||
591 | { |
||
592 | $this->crudPanel->column('test1'); |
||
593 | $this->crudPanel->column('test2'); |
||
594 | $this->crudPanel->column('test3')->after('test1'); |
||
595 | |||
596 | $crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
||
597 | $this->assertEquals($crudColumnsNames, ['test1', 'test3', 'test2']); |
||
598 | |||
599 | $this->crudPanel->column('test4')->before('test1'); |
||
600 | $crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
||
601 | $this->assertEquals($crudColumnsNames, ['test4', 'test1', 'test3', 'test2']); |
||
602 | } |
||
603 | |||
604 | public function testItCanRemoveColumnAttributesFluently() |
||
605 | { |
||
606 | $this->crudPanel->column('test1')->type('test'); |
||
607 | $this->assertEquals($this->crudPanel->columns()['test1']['type'], 'test'); |
||
608 | $this->crudPanel->column('test1')->forget('type'); |
||
609 | $this->assertNull($this->crudPanel->columns()['test1']['type'] ?? null); |
||
610 | } |
||
611 | |||
612 | public function testItCanRemoveColumnFluently() |
||
613 | { |
||
614 | $this->crudPanel->column('test1')->type('test'); |
||
615 | $this->assertCount(1, $this->crudPanel->columns()); |
||
616 | $this->crudPanel->column('test1')->remove(); |
||
617 | $this->assertCount(0, $this->crudPanel->columns()); |
||
618 | } |
||
619 | |||
620 | public function testItCanAddAColumnToCrudFromClass() |
||
621 | { |
||
622 | CrudColumn::name('test'); |
||
623 | $this->assertCount(1, $this->crudPanel->columns()); |
||
624 | } |
||
625 | |||
626 | public function testItCanAddAFluentColumnUsingArray() |
||
627 | { |
||
628 | $this->crudPanel->column($this->oneColumnArray); |
||
629 | $this->assertCount(1, $this->crudPanel->columns()); |
||
630 | } |
||
631 | |||
632 | public function testItCanAddAFluentColumnUsingArrayWithoutName() |
||
633 | { |
||
634 | $this->crudPanel->column(['type' => 'text']); |
||
635 | $this->assertCount(1, $this->crudPanel->columns()); |
||
636 | } |
||
637 | |||
638 | public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled() |
||
639 | { |
||
640 | $this->expectException(\Exception::class); |
||
641 | $this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insuficient parameters provided in column: [articles].'); |
||
642 | $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']); |
||
643 | } |
||
644 | |||
645 | public function testItThrowsExceptionIfRouteNotFound() |
||
651 | } |
||
652 | |||
653 | public function testColumnLinkToWithRouteNameOnly() |
||
654 | { |
||
655 | $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show'); |
||
656 | $columnArray = $this->crudPanel->columns()['articles']; |
||
657 | $reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
||
658 | $arguments = $reflection->getClosureUsedVariables(); |
||
659 | $this->crudPanel->entry = Article::first(); |
||
660 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
661 | $this->assertEquals('articles.show', $arguments['route']); |
||
662 | $this->assertCount(1, $arguments['parameters']); |
||
663 | $this->assertEquals('http://localhost/admin/articles/1/show', $url); |
||
664 | } |
||
665 | |||
666 | public function testColumnLinkToWithRouteNameAndAdditionalParameters() |
||
667 | { |
||
668 | $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']); |
||
669 | $columnArray = $this->crudPanel->columns()['articles']; |
||
670 | $reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
||
671 | $arguments = $reflection->getClosureUsedVariables(); |
||
672 | $this->assertEquals('articles.show', $arguments['route']); |
||
673 | $this->assertCount(3, $arguments['parameters']); |
||
674 | $this->crudPanel->entry = Article::first(); |
||
675 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
676 | $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url); |
||
677 | } |
||
678 | |||
679 | public function testColumnLinkToWithCustomParameters() |
||
680 | { |
||
681 | $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']); |
||
682 | $columnArray = $this->crudPanel->columns()['articles']; |
||
683 | $this->crudPanel->entry = Article::first(); |
||
684 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
685 | $this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url); |
||
686 | } |
||
687 | |||
688 | public function testColumnLinkToWithCustomClosureParameters() |
||
689 | { |
||
690 | $this->crudPanel->column('articles') |
||
691 | ->entity('articles') |
||
692 | ->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
||
693 | $columnArray = $this->crudPanel->columns()['articles']; |
||
694 | $this->crudPanel->entry = Article::first(); |
||
695 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
696 | $this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url); |
||
697 | } |
||
698 | |||
699 | public function testColumnLinkToDontAutoInferParametersIfAllProvided() |
||
700 | { |
||
701 | $this->crudPanel->column('articles') |
||
702 | ->entity('articles') |
||
703 | ->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
||
704 | $columnArray = $this->crudPanel->columns()['articles']; |
||
705 | $this->crudPanel->entry = Article::first(); |
||
706 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
707 | $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
||
708 | } |
||
709 | |||
710 | public function testColumnLinkToAutoInferAnySingleParameter() |
||
719 | } |
||
720 | |||
721 | public function testColumnLinkToWithClosure() |
||
722 | { |
||
723 | $this->crudPanel->column('articles') |
||
724 | ->entity('articles') |
||
725 | ->linkTo(fn ($entry) => route('articles.show', $entry->content)); |
||
726 | $columnArray = $this->crudPanel->columns()['articles']; |
||
727 | $this->crudPanel->entry = Article::first(); |
||
728 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
729 | $this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url); |
||
730 | } |
||
731 | |||
732 | public function testColumnArrayDefinitionLinkToRouteAsClosure() |
||
733 | { |
||
734 | $this->crudPanel->setModel(User::class); |
||
735 | $this->crudPanel->column([ |
||
736 | 'name' => 'articles', |
||
737 | 'entity' => 'articles', |
||
738 | 'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']), |
||
739 | ]); |
||
740 | $columnArray = $this->crudPanel->columns()['articles']; |
||
741 | $this->crudPanel->entry = Article::first(); |
||
742 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
743 | $this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url); |
||
744 | } |
||
745 | |||
746 | public function testColumnArrayDefinitionLinkToRouteNameOnly() |
||
758 | } |
||
759 | |||
760 | public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters() |
||
782 | } |
||
783 | } |
||
784 |