We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 57 |
| Total Lines | 851 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 4 | Features | 3 |
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() |
||
| 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() |
||
| 444 | } |
||
| 445 | |||
| 446 | public function testItCanRemoveAllColumns() |
||
| 447 | { |
||
| 448 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 449 | |||
| 450 | $this->crudPanel->removeAllColumns(); |
||
| 451 | |||
| 452 | $this->assertEmpty($this->crudPanel->columns()); |
||
| 453 | } |
||
| 454 | |||
| 455 | public function testItCanSetColumnDetails() |
||
| 456 | { |
||
| 457 | $this->crudPanel->addColumns(['column1', 'column2', 'column3', 'column4', 'column5']); |
||
| 458 | |||
| 459 | $this->crudPanel->setColumnsDetails(['column1', 'column2'], ['label' => 'New Label']); |
||
| 460 | $this->crudPanel->setColumnDetails('column3', ['label' => 'Old Label']); |
||
| 461 | $this->crudPanel->modifyColumn('column4', ['label' => 'Alias Label']); |
||
| 462 | $this->crudPanel->setColumnLabel('column5', 'Setting Label'); |
||
| 463 | |||
| 464 | $this->assertEquals('New Label', $this->crudPanel->columns()['column1']['label']); |
||
| 465 | $this->assertEquals('New Label', $this->crudPanel->columns()['column2']['label']); |
||
| 466 | $this->assertEquals('Old Label', $this->crudPanel->columns()['column3']['label']); |
||
| 467 | $this->assertEquals('Alias Label', $this->crudPanel->columns()['column4']['label']); |
||
| 468 | $this->assertEquals('Setting Label', $this->crudPanel->columns()['column5']['label']); |
||
| 469 | } |
||
| 470 | |||
| 471 | public function testItCanFindAColumnById() |
||
| 472 | { |
||
| 473 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 474 | |||
| 475 | $column = $this->crudPanel->findColumnById(1); |
||
| 476 | |||
| 477 | $this->assertEquals('column2', $column['name']); |
||
| 478 | } |
||
| 479 | |||
| 480 | public function testItCanGetAndSetActionsColumnPriority() |
||
| 481 | { |
||
| 482 | $this->assertEquals(1, $this->crudPanel->getActionsColumnPriority()); |
||
| 483 | $this->crudPanel->setActionsColumnPriority(2); |
||
| 484 | $this->assertEquals(2, $this->crudPanel->getActionsColumnPriority()); |
||
| 485 | } |
||
| 486 | |||
| 487 | public function testItCanGetAndSetColumnsRemovingPreviouslySet() |
||
| 488 | { |
||
| 489 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 490 | |||
| 491 | $this->crudPanel->setColumns('column4'); |
||
| 492 | |||
| 493 | $this->assertEquals(1, count($this->crudPanel->columns())); |
||
| 494 | $this->assertEquals(['column4'], array_keys($this->crudPanel->columns())); |
||
| 495 | |||
| 496 | $this->crudPanel->setColumns(['column5', 'column6']); |
||
| 497 | $this->assertEquals(2, count($this->crudPanel->columns())); |
||
| 498 | $this->assertEquals(['column5', 'column6'], array_keys($this->crudPanel->columns())); |
||
| 499 | |||
| 500 | $this->crudPanel->setColumns(['column7', [ |
||
| 501 | 'name' => 'column8', |
||
| 502 | ]]); |
||
| 503 | $this->assertEquals(2, count($this->crudPanel->columns())) ; |
||
| 504 | $this->assertEquals(['column7', 'column8'], array_keys($this->crudPanel->columns())); |
||
| 505 | } |
||
| 506 | |||
| 507 | |||
| 508 | |||
| 509 | public function testRemoveUnknownColumnName() |
||
| 510 | { |
||
| 511 | $unknownColumnName = 'column4'; |
||
| 512 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 513 | |||
| 514 | $this->crudPanel->removeColumn($unknownColumnName); |
||
| 515 | |||
| 516 | $this->assertEquals(3, count($this->crudPanel->columns())); |
||
| 517 | $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
||
| 518 | $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
||
| 519 | } |
||
| 520 | |||
| 521 | public function testRemoveColumnsByName() |
||
| 522 | { |
||
| 523 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 524 | |||
| 525 | $this->crudPanel->removeColumns($this->twoColumnsArray); |
||
| 526 | |||
| 527 | $this->assertEquals(1, count($this->crudPanel->columns())); |
||
| 528 | $this->assertEquals(['column3'], array_keys($this->crudPanel->columns())); |
||
| 529 | $this->assertNotEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
| 530 | } |
||
| 531 | |||
| 532 | public function testRemoveUnknownColumnsByName() |
||
| 533 | { |
||
| 534 | $unknownColumnNames = ['column4', 'column5']; |
||
| 535 | $this->crudPanel->addColumns(['column1', 'column2', 'column3']); |
||
| 536 | |||
| 537 | $this->crudPanel->removeColumns($unknownColumnNames); |
||
| 538 | |||
| 539 | $this->assertEquals(3, count($this->crudPanel->columns())); |
||
| 540 | $this->assertEquals(['column1', 'column2', 'column3'], array_keys($this->crudPanel->columns())); |
||
| 541 | $this->assertNotContains($this->otherOneColumnArray, $this->crudPanel->columns()); |
||
| 542 | } |
||
| 543 | |||
| 544 | public function testSetColumnDetails() |
||
| 545 | { |
||
| 546 | $this->markTestIncomplete('Not correctly implemented'); |
||
| 547 | |||
| 548 | // TODO: refactor crud panel sync method |
||
| 549 | } |
||
| 550 | |||
| 551 | public function testSetColumnsDetails() |
||
| 552 | { |
||
| 553 | $this->markTestIncomplete('Not correctly implemented'); |
||
| 554 | |||
| 555 | // TODO: refactor crud panel sync method |
||
| 556 | } |
||
| 557 | |||
| 558 | public function testOrderColumns() |
||
| 559 | { |
||
| 560 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
| 561 | |||
| 562 | $this->crudPanel->orderColumns(['column2', 'column1', 'column3']); |
||
| 563 | |||
| 564 | $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
||
| 565 | } |
||
| 566 | |||
| 567 | public function testOrderColumnsIncompleteList() |
||
| 568 | { |
||
| 569 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
| 570 | |||
| 571 | $this->crudPanel->orderColumns(['column2', 'column3']); |
||
| 572 | |||
| 573 | $this->assertEquals(['column2', 'column3', 'column1'], array_keys($this->crudPanel->columns())); |
||
| 574 | } |
||
| 575 | |||
| 576 | public function testOrderColumnsEmptyList() |
||
| 577 | { |
||
| 578 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
| 579 | |||
| 580 | $this->crudPanel->orderColumns([]); |
||
| 581 | |||
| 582 | $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
| 583 | } |
||
| 584 | |||
| 585 | public function testOrderColumnsUnknownList() |
||
| 586 | { |
||
| 587 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
| 588 | |||
| 589 | $this->crudPanel->orderColumns(['column4', 'column5', 'column6']); |
||
| 590 | |||
| 591 | $this->assertEquals($this->expectedThreeColumnsArray, $this->crudPanel->columns()); |
||
| 592 | } |
||
| 593 | |||
| 594 | public function testOrderColumnsMixedList() |
||
| 595 | { |
||
| 596 | $this->crudPanel->addColumns($this->threeColumnsArray); |
||
| 597 | |||
| 598 | $this->crudPanel->orderColumns(['column2', 'column5', 'column6']); |
||
| 599 | |||
| 600 | $this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns())); |
||
| 601 | } |
||
| 602 | |||
| 603 | public function testMakeFirstColumnReturnFalseWhenNoColumnsExist() |
||
| 604 | { |
||
| 605 | $this->assertEmpty($this->crudPanel->columns()); |
||
| 606 | $column = $this->crudPanel->makeFirstColumn(); |
||
| 607 | $this->assertFalse($column); |
||
| 608 | } |
||
| 609 | |||
| 610 | public function testItCanAddADefaultTypeToTheColumn() |
||
| 611 | { |
||
| 612 | $column = $this->crudPanel->addDefaultTypeToColumn(['name' => 'name']); |
||
| 613 | |||
| 614 | $this->assertEquals('text', $column['type']); |
||
| 615 | } |
||
| 616 | |||
| 617 | public function testItReturnFalseWhenTryingToAddTypeToAColumnWithoutName() |
||
| 618 | { |
||
| 619 | $column = $this->crudPanel->addDefaultTypeToColumn(['attribute' => 'name']); |
||
| 620 | |||
| 621 | $this->assertFalse($column); |
||
| 622 | } |
||
| 623 | |||
| 624 | public function testItCanChangeTheColumnKey() |
||
| 625 | { |
||
| 626 | $this->crudPanel->column('test'); |
||
| 627 | |||
| 628 | $this->assertEquals('test', $this->crudPanel->columns()['test']['key']); |
||
| 629 | |||
| 630 | $this->crudPanel->column('test')->key('new_key'); |
||
| 631 | |||
| 632 | $this->assertEquals('new_key', $this->crudPanel->columns()['new_key']['key']); |
||
| 633 | } |
||
| 634 | |||
| 635 | public function testItCanAddAFluentColumn() |
||
| 636 | { |
||
| 637 | $this->crudPanel->setModel(User::class); |
||
| 638 | |||
| 639 | $this->crudPanel->column('my_column')->label('my_column'); |
||
| 640 | |||
| 641 | $this->assertCount(1, $this->crudPanel->columns()); |
||
| 642 | |||
| 643 | $this->assertEquals([ |
||
| 644 | 'name' => 'my_column', |
||
| 645 | 'type' => 'text', |
||
| 646 | 'label' => 'my_column', |
||
| 647 | 'key' => 'my_column', |
||
| 648 | 'priority' => 0, |
||
| 649 | 'tableColumn' => false, |
||
| 650 | 'orderable' => false, |
||
| 651 | 'searchLogic' => false, |
||
| 652 | ], $this->crudPanel->columns()['my_column']); |
||
| 653 | } |
||
| 654 | |||
| 655 | public function testItCanMakeAColumnFirstFluently() |
||
| 656 | { |
||
| 657 | $this->crudPanel->column('test1'); |
||
| 658 | $this->crudPanel->column('test2')->makeFirst(); |
||
| 659 | $crudColumns = $this->crudPanel->columns(); |
||
| 660 | $firstColumn = reset($crudColumns); |
||
| 661 | $this->assertEquals($firstColumn['name'], 'test2'); |
||
| 662 | } |
||
| 663 | |||
| 664 | public function testItCanMakeAColumnLastFluently() |
||
| 665 | { |
||
| 666 | $this->crudPanel->column('test1'); |
||
| 667 | $this->crudPanel->column('test2'); |
||
| 668 | $this->crudPanel->column('test1')->makeLast(); |
||
| 669 | $crudColumns = $this->crudPanel->columns(); |
||
| 670 | $firstColumn = reset($crudColumns); |
||
| 671 | $this->assertEquals($firstColumn['name'], 'test2'); |
||
| 672 | } |
||
| 673 | |||
| 674 | public function testItCanPlaceColumnsFluently() |
||
| 675 | { |
||
| 676 | $this->crudPanel->column('test1'); |
||
| 677 | $this->crudPanel->column('test2'); |
||
| 678 | $this->crudPanel->column('test3')->after('test1'); |
||
| 679 | |||
| 680 | $crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
||
| 681 | $this->assertEquals($crudColumnsNames, ['test1', 'test3', 'test2']); |
||
| 682 | |||
| 683 | $this->crudPanel->column('test4')->before('test1'); |
||
| 684 | $crudColumnsNames = array_column($this->crudPanel->columns(), 'name'); |
||
| 685 | $this->assertEquals($crudColumnsNames, ['test4', 'test1', 'test3', 'test2']); |
||
| 686 | } |
||
| 687 | |||
| 688 | public function testItCanRemoveColumnAttributesFluently() |
||
| 689 | { |
||
| 690 | $this->crudPanel->column('test1')->type('test'); |
||
| 691 | $this->assertEquals($this->crudPanel->columns()['test1']['type'], 'test'); |
||
| 692 | $this->crudPanel->column('test1')->forget('type'); |
||
| 693 | $this->assertNull($this->crudPanel->columns()['test1']['type'] ?? null); |
||
| 694 | } |
||
| 695 | |||
| 696 | public function testItCanRemoveColumnFluently() |
||
| 697 | { |
||
| 698 | $this->crudPanel->column('test1')->type('test'); |
||
| 699 | $this->assertCount(1, $this->crudPanel->columns()); |
||
| 700 | $this->crudPanel->column('test1')->remove(); |
||
| 701 | $this->assertCount(0, $this->crudPanel->columns()); |
||
| 702 | } |
||
| 703 | |||
| 704 | public function testItCanAddAColumnToCrudFromClass() |
||
| 705 | { |
||
| 706 | CrudColumn::name('test'); |
||
| 707 | $this->assertCount(1, $this->crudPanel->columns()); |
||
| 708 | } |
||
| 709 | |||
| 710 | public function testItCanAddAFluentColumnUsingArray() |
||
| 711 | { |
||
| 712 | $this->crudPanel->column($this->oneColumnArray); |
||
| 713 | $this->assertCount(1, $this->crudPanel->columns()); |
||
| 714 | } |
||
| 715 | |||
| 716 | public function testItCanAddAFluentColumnUsingArrayWithoutName() |
||
| 717 | { |
||
| 718 | $this->crudPanel->column(['type' => 'text']); |
||
| 719 | $this->assertCount(1, $this->crudPanel->columns()); |
||
| 720 | } |
||
| 721 | |||
| 722 | public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled() |
||
| 723 | { |
||
| 724 | $this->expectException(\Exception::class); |
||
| 725 | $this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insufficient parameters provided in column: [articles].'); |
||
| 726 | $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']); |
||
| 727 | } |
||
| 728 | |||
| 729 | public function testItThrowsExceptionIfRouteNotFound() |
||
| 735 | } |
||
| 736 | |||
| 737 | public function testColumnLinkToWithRouteNameOnly() |
||
| 738 | { |
||
| 739 | $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show'); |
||
| 740 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 741 | $reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
||
| 742 | $arguments = $reflection->getClosureUsedVariables(); |
||
| 743 | $this->crudPanel->entry = Article::first(); |
||
| 744 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 745 | $this->assertEquals('articles.show', $arguments['route']); |
||
| 746 | $this->assertCount(1, $arguments['parameters']); |
||
| 747 | $this->assertEquals('http://localhost/admin/articles/1/show', $url); |
||
| 748 | } |
||
| 749 | |||
| 750 | public function testColumnLinkToWithRouteNameAndAdditionalParameters() |
||
| 751 | { |
||
| 752 | $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']); |
||
| 753 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 754 | $reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
||
| 755 | $arguments = $reflection->getClosureUsedVariables(); |
||
| 756 | $this->assertEquals('articles.show', $arguments['route']); |
||
| 757 | $this->assertCount(3, $arguments['parameters']); |
||
| 758 | $this->crudPanel->entry = Article::first(); |
||
| 759 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 760 | $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url); |
||
| 761 | } |
||
| 762 | |||
| 763 | public function testColumnLinkToWithCustomParameters() |
||
| 764 | { |
||
| 765 | $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']); |
||
| 766 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 767 | $this->crudPanel->entry = Article::first(); |
||
| 768 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 769 | $this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url); |
||
| 770 | } |
||
| 771 | |||
| 772 | public function testColumnLinkToWithCustomClosureParameters() |
||
| 773 | { |
||
| 774 | $this->crudPanel->column('articles') |
||
| 775 | ->entity('articles') |
||
| 776 | ->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
||
| 777 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 778 | $this->crudPanel->entry = Article::first(); |
||
| 779 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 780 | $this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url); |
||
| 781 | } |
||
| 782 | |||
| 783 | public function testColumnLinkToDontAutoInferParametersIfAllProvided() |
||
| 784 | { |
||
| 785 | $this->crudPanel->column('articles') |
||
| 786 | ->entity('articles') |
||
| 787 | ->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
||
| 788 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 789 | $this->crudPanel->entry = Article::first(); |
||
| 790 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 791 | $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
||
| 792 | } |
||
| 793 | |||
| 794 | public function testColumnLinkToAutoInferAnySingleParameter() |
||
| 803 | } |
||
| 804 | |||
| 805 | public function testColumnLinkToWithClosure() |
||
| 806 | { |
||
| 807 | $this->crudPanel->column('articles') |
||
| 808 | ->entity('articles') |
||
| 809 | ->linkTo(fn ($entry) => route('articles.show', $entry->content)); |
||
| 810 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 811 | $this->crudPanel->entry = Article::first(); |
||
| 812 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 813 | $this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url); |
||
| 814 | } |
||
| 815 | |||
| 816 | public function testColumnArrayDefinitionLinkToRouteAsClosure() |
||
| 817 | { |
||
| 818 | $this->crudPanel->setModel(User::class); |
||
| 819 | $this->crudPanel->column([ |
||
| 820 | 'name' => 'articles', |
||
| 821 | 'entity' => 'articles', |
||
| 822 | 'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']), |
||
| 823 | ]); |
||
| 824 | $columnArray = $this->crudPanel->columns()['articles']; |
||
| 825 | $this->crudPanel->entry = Article::first(); |
||
| 826 | $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
||
| 827 | $this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url); |
||
| 828 | } |
||
| 829 | |||
| 830 | public function testColumnArrayDefinitionLinkToRouteNameOnly() |
||
| 842 | } |
||
| 843 | |||
| 844 | public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters() |
||
| 845 | { |
||
| 846 | $this->crudPanel->setModel(User::class); |
||
| 847 | $this->crudPanel->column([ |
||
| 848 | 'name' => 'articles', |
||
| 849 | 'entity' => 'articles', |
||
| 850 | 'linkTo' => [ |
||
| 851 | 'route' => 'articles.show', |
||
| 852 | 'parameters' => [ |
||
| 853 | 'test' => 'testing', |
||
| 854 | 'test2' => fn ($entry) => $entry->content, |
||
| 855 | ], |
||
| 856 | ], |
||
| 857 | ]); |
||
| 866 | } |
||
| 867 | } |
||
| 868 |