We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 |
| Total Lines | 432 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 2 |
Complex classes like CrudPanelButtonsTest 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 CrudPanelButtonsTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CrudPanelButtonsTest extends BaseCrudPanel |
||
| 13 | { |
||
| 14 | private $defaultButtonNames = []; |
||
| 15 | |||
| 16 | private $topViewButton; |
||
| 17 | |||
| 18 | private $lineViewButton; |
||
| 19 | |||
| 20 | private $bottomViewButton; |
||
| 21 | |||
| 22 | private $topModelFunctionButton; |
||
| 23 | |||
| 24 | protected function setUp(): void |
||
| 25 | { |
||
| 26 | parent::setUp(); |
||
| 27 | |||
| 28 | $this->crudPanel->setOperation('list'); |
||
| 29 | |||
| 30 | $this->topViewButton = [ |
||
| 31 | 'name' => 'topViewButton', |
||
| 32 | 'stack' => 'top', |
||
| 33 | 'type' => 'view', |
||
| 34 | 'content' => 'crud::buttons.show', |
||
| 35 | 'meta' => [], |
||
| 36 | ]; |
||
| 37 | $this->lineViewButton = [ |
||
| 38 | 'name' => 'lineViewButton', |
||
| 39 | 'stack' => 'line', |
||
| 40 | 'type' => 'view', |
||
| 41 | 'content' => 'crud::buttons.show', |
||
| 42 | 'position' => null, |
||
| 43 | 'meta' => [], |
||
| 44 | ]; |
||
| 45 | $this->bottomViewButton = [ |
||
| 46 | 'name' => 'bottomViewButton', |
||
| 47 | 'stack' => 'bottom', |
||
| 48 | 'type' => 'view', |
||
| 49 | 'content' => 'crud::buttons.show', |
||
| 50 | 'position' => null, |
||
| 51 | 'meta' => [], |
||
| 52 | ]; |
||
| 53 | $this->topModelFunctionButton = [ |
||
| 54 | 'name' => 'topModelFunctionButton', |
||
| 55 | 'stack' => 'top', |
||
| 56 | 'type' => 'model_function', |
||
| 57 | 'content' => 'crud::buttons.show', |
||
| 58 | 'position' => null, |
||
| 59 | 'meta' => [], |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testItCanAddMultipleButtons() |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testCanAddButtonToSpecificStack() |
||
| 71 | { |
||
| 72 | $this->addDefaultButtons(); |
||
| 73 | $expectedButton = $this->topViewButton; |
||
| 74 | $expectedButton['name'] = 'topViewButtonCustomName'; |
||
| 75 | $expectedButton['stack'] = 'top'; |
||
| 76 | $expectedButton['position'] = null; |
||
| 77 | |||
| 78 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']); |
||
| 79 | |||
| 80 | $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray()); |
||
| 81 | $this->assertCount(3, $this->crudPanel->getButtonsForStack($expectedButton['stack'])); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testAddButtonsWithSameName() |
||
| 85 | { |
||
| 86 | $expectedButton = $this->topViewButton; |
||
| 87 | |||
| 88 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']); |
||
| 89 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']); |
||
| 90 | |||
| 91 | $this->assertCount(1, $this->crudPanel->buttons()); |
||
| 92 | |||
| 93 | $expectedButton2 = $this->bottomViewButton; |
||
| 94 | CrudButton::name($expectedButton2); |
||
| 95 | CrudButton::name($expectedButton2); |
||
| 96 | |||
| 97 | $this->assertCount(2, $this->crudPanel->buttons()); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testAddButtonBeginning() |
||
| 101 | { |
||
| 102 | $this->addTestButton('topViewButton'); |
||
| 103 | |||
| 104 | $expectedButton = $this->bottomViewButton; |
||
| 105 | |||
| 106 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'beginning'); |
||
| 107 | |||
| 108 | $this->assertEquals($expectedButton, $this->crudPanel->buttons()->first()->toArray()); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function testAddButtonEnd() |
||
| 112 | { |
||
| 113 | $this->addTestButton('lineViewButton'); |
||
| 114 | |||
| 115 | $expectedButton = $this->lineViewButton; |
||
| 116 | |||
| 117 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content'], 'end'); |
||
| 118 | |||
| 119 | $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray()); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testAddButtonFromModelFunction() |
||
| 123 | { |
||
| 124 | $expectedButton = $this->topModelFunctionButton; |
||
| 125 | |||
| 126 | $this->crudPanel->addButton($expectedButton['stack'], $expectedButton['name'], $expectedButton['type'], $expectedButton['content']); |
||
| 127 | |||
| 128 | $this->assertEquals($expectedButton, $this->crudPanel->buttons()->last()->toArray()); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function testAddButtonFromView() |
||
| 132 | { |
||
| 133 | $expectedButton = $this->topViewButton; |
||
| 134 | $viewName = 'someViewName'; |
||
| 135 | |||
| 136 | $this->crudPanel->addButtonFromView($expectedButton['stack'], $expectedButton['name'], $viewName); |
||
| 137 | |||
| 138 | $backpackButtonViewPackage = 'crud::buttons.'; |
||
| 139 | $actualButton = $this->crudPanel->buttons()->last(); |
||
| 140 | |||
| 141 | $this->assertEquals($expectedButton['stack'], $actualButton->stack); |
||
| 142 | $this->assertEquals($expectedButton['name'], $actualButton->name); |
||
| 143 | $this->assertEquals($backpackButtonViewPackage.$viewName, $actualButton->content); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testRemoveButton() |
||
| 147 | { |
||
| 148 | $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
||
| 149 | $this->crudPanel->removeButton('update'); |
||
| 150 | |||
| 151 | $this->assertCount(0, $this->crudPanel->buttons()); |
||
| 152 | $this->assertNull($this->getButtonByName('update')); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testRemoveButtons() |
||
| 156 | { |
||
| 157 | $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
||
| 158 | $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end'); |
||
| 159 | $this->crudPanel->removeButtons(['show', 'update']); |
||
| 160 | |||
| 161 | $this->assertCount(0, $this->crudPanel->buttons()); |
||
| 162 | $this->assertNull($this->getButtonByName('show')); |
||
| 163 | $this->assertNull($this->getButtonByName('update')); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testRemoveUnknownButtons() |
||
| 167 | { |
||
| 168 | $buttonNames = [ |
||
| 169 | 'someButtonName', |
||
| 170 | 'someOtherButtonName', |
||
| 171 | ]; |
||
| 172 | |||
| 173 | $this->addDefaultButtons(); |
||
| 174 | $this->crudPanel->removeButtons($buttonNames); |
||
| 175 | |||
| 176 | $this->assertCount(4, $this->crudPanel->buttons()); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function testRemoveUnknownButton() |
||
| 180 | { |
||
| 181 | $this->addTestButton('topViewButton'); |
||
| 182 | |||
| 183 | $this->crudPanel->removeButton('someButtonName'); |
||
| 184 | |||
| 185 | $this->assertCount(1, $this->crudPanel->buttons()); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function testRemoveAllButtons() |
||
| 189 | { |
||
| 190 | $this->addDefaultButtons(); |
||
| 191 | $this->crudPanel->removeAllButtons(); |
||
| 192 | |||
| 193 | $this->assertEmpty($this->crudPanel->buttons()); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function testRemoveButtonFromStack() |
||
| 197 | { |
||
| 198 | $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
||
| 199 | |||
| 200 | $button = $this->crudPanel->buttons()->first(); |
||
| 201 | |||
| 202 | $this->crudPanel->removeButtonFromStack($button->name, $button->stack); |
||
| 203 | |||
| 204 | $this->assertCount(0, $this->crudPanel->buttons()); |
||
| 205 | $this->assertNull($this->getButtonByName($button->name)); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testRemoveUnknownButtonFromStack() |
||
| 209 | { |
||
| 210 | $this->addTestButton('lineViewButton'); |
||
| 211 | $this->crudPanel->removeButtonFromStack('someButtonName', 'line'); |
||
| 212 | |||
| 213 | $this->assertCount(1, $this->crudPanel->buttons()); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function testRemoveButtonFromUnknownStack() |
||
| 226 | } |
||
| 227 | |||
| 228 | public function testRemoveAllButtonsFromStack() |
||
| 229 | { |
||
| 230 | $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
||
| 231 | $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end'); |
||
| 232 | |||
| 233 | $this->crudPanel->removeAllButtonsFromStack('line'); |
||
| 234 | |||
| 235 | $this->assertCount(0, $this->crudPanel->buttons()); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function testRemoveAllButtonsFromUnknownStack() |
||
| 245 | } |
||
| 246 | |||
| 247 | public function testOrderButtons() |
||
| 248 | { |
||
| 249 | $this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
||
| 250 | $this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end'); |
||
| 251 | $this->crudPanel->addButton('line', 'test', 'view', 'crud::buttons.test', 'end'); |
||
| 252 | |||
| 253 | $this->crudPanel->orderButtons('line', ['show', 'test']); |
||
| 256 | } |
||
| 257 | |||
| 258 | public function testOrderButtonsInStack() |
||
| 259 | { |
||
| 260 | $this->addDefaultButtons(); |
||
| 261 | |||
| 262 | $this->crudPanel->orderButtons('top', ['topModelFunctionButton']); |
||
| 263 | |||
| 264 | $this->assertEquals(['topModelFunctionButton', 'topViewButton'], $this->crudPanel->getButtonsForStack('top')->pluck('name')->toArray()); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function testOrderThrowExceptionIfButtonDoesNotExist() |
||
| 268 | { |
||
| 269 | $this->addDefaultButtons(); |
||
| 270 | |||
| 271 | $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class); |
||
| 272 | |||
| 273 | $this->crudPanel->orderButtons('top', ['unknownButton']); |
||
| 274 | } |
||
| 275 | |||
| 276 | public function testAddButtonFluently() |
||
| 285 | } |
||
| 286 | |||
| 287 | public function testItThrowsExceptionWhenModifyingUnknownButton() |
||
| 288 | { |
||
| 289 | $this->addDefaultButtons(); |
||
| 290 | |||
| 291 | $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class); |
||
| 292 | |||
| 293 | $this->crudPanel->modifyButton('unknownButton', function ($button) { |
||
| 294 | $button->name = 'newName'; |
||
| 295 | }); |
||
| 296 | } |
||
| 297 | |||
| 298 | public function testItCanAddAButtonFromAModelFunction() |
||
| 299 | { |
||
| 300 | $this->crudPanel->addButtonFromModelFunction('line', 'buttonModelFunction', 'buttonModelFunction'); |
||
| 301 | $this->assertEquals('buttonModelFunction', $this->crudPanel->buttons()->first()->content); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function testItDoesNotMoveFieldWhenTargetIsUnknown() |
||
| 305 | { |
||
| 306 | $this->addDefaultButtons(); |
||
| 307 | |||
| 308 | $firstButtonName = $this->crudPanel->buttons()->first()->name; |
||
| 309 | |||
| 310 | $this->crudPanel->moveButton('unknownButton', 'before', 'topViewButton'); |
||
| 311 | |||
| 312 | $this->assertCount(4, $this->crudPanel->buttons()); |
||
| 313 | $this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function testItDoesNotMoveButtonWhenDestinationIsUnknown() |
||
| 317 | { |
||
| 318 | $this->addDefaultButtons(); |
||
| 319 | |||
| 320 | $firstButtonName = $this->crudPanel->buttons()->first()->name; |
||
| 321 | |||
| 322 | $this->crudPanel->moveButton('topViewButton', 'before', 'unknownButton'); |
||
| 323 | |||
| 324 | $this->assertCount(4, $this->crudPanel->buttons()); |
||
| 325 | $this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function testItCanCreateANewCrudButtonInstance() |
||
| 329 | { |
||
| 330 | $button = $this->crudPanel->button(['name' => 'testButton', 'stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']); |
||
| 331 | $this->assertEquals($button->toArray(), $this->crudPanel->buttons()->last()->toArray()); |
||
| 332 | $this->assertInstanceOf(\Backpack\CRUD\app\Library\CrudPanel\CrudButton::class, $button); |
||
| 333 | } |
||
| 334 | |||
| 335 | public function testItCanCheckIfAnyOfTheButtonsHasTheDeterminedKayValuePair() |
||
| 341 | } |
||
| 342 | |||
| 343 | public function testItGenerateARandomButtonNameIfOneNotProvided() |
||
| 344 | { |
||
| 345 | $button = $this->crudPanel->button(['stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']); |
||
| 346 | $this->assertTrue(str_starts_with($button->name, 'button_')); |
||
| 347 | } |
||
| 348 | |||
| 349 | public function testMovingTheButtonUsingPosition() |
||
| 350 | { |
||
| 351 | $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view'); |
||
| 352 | $button2 = CrudButton::name('lineTest2')->to('line')->view('crud::buttons.test')->type('view')->position('beginning'); |
||
| 353 | $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray()); |
||
| 354 | $button2->position('end'); |
||
| 355 | $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->first()->toArray()); |
||
| 356 | } |
||
| 357 | |||
| 358 | public function testThrowsErrorInUnknownPosition() |
||
| 359 | { |
||
| 360 | try { |
||
| 361 | $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view')->position('unknown'); |
||
| 362 | } catch (\Throwable $e) { |
||
| 363 | } |
||
| 364 | $this->assertEquals( |
||
| 365 | new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Unknown button position - please use \'beginning\' or \'end\'.'), |
||
| 366 | $e |
||
| 367 | ); |
||
| 368 | } |
||
| 369 | |||
| 370 | public function testItCanGetButtonKeyInTheArray() |
||
| 371 | { |
||
| 372 | $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view'); |
||
| 373 | $this->assertEquals(0, CrudButton::make('lineTest')->getKey()); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function testItCanRemoveButtonFromButtonList() |
||
| 377 | { |
||
| 378 | $this->addDefaultButtons(); |
||
| 379 | |||
| 380 | CrudButton::make('topViewButton')->remove(); |
||
| 381 | |||
| 382 | $this->assertCount(3, $this->crudPanel->buttons()); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function testItCanAddButtonsToAnHiddenStack() |
||
| 386 | { |
||
| 387 | $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view'); |
||
| 388 | $this->assertCount(1, $this->crudPanel->getButtonsForStack('hidden')); |
||
| 389 | } |
||
| 390 | |||
| 391 | public function testItCanAddMetadataToAButton() |
||
| 395 | } |
||
| 396 | |||
| 397 | public function testItCanForgetAPropertyFromAButton() |
||
| 398 | { |
||
| 399 | $button = CrudButton::make('lineTest')->content('crud::buttons.test')->type('view')->meta(['key' => 'value'])->group('line'); |
||
| 402 | } |
||
| 403 | |||
| 404 | public function testItCanGetTheButtonHtmlToRender() |
||
| 412 | } |
||
| 413 | |||
| 414 | public function testItThrowsErrorWhenAttemptingToRenderUnknowButtonView() |
||
| 415 | { |
||
| 416 | $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class); |
||
| 417 | $this->crudPanel->button('dontexist')->type('view')->content('unknown_view')->getHtml(); |
||
| 418 | } |
||
| 419 | |||
| 420 | public function testItThrowsErrorWhenAttemptingToRenderUnknowButtonType() |
||
| 421 | { |
||
| 422 | $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class); |
||
| 423 | $this->crudPanel->button('dontexist')->type('unknown')->getHtml(); |
||
| 424 | } |
||
| 425 | |||
| 426 | private function getButtonByName($name) |
||
| 427 | { |
||
| 428 | return $this->crudPanel->buttons()->first(function ($value) use ($name) { |
||
| 429 | return $value->name == $name; |
||
| 430 | }); |
||
| 431 | } |
||
| 432 | |||
| 433 | private function addDefaultButtons() |
||
| 439 | } |
||
| 440 | |||
| 441 | private function addTestButton($buttonName) |
||
| 444 | } |
||
| 445 | } |
||
| 446 |