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 |
||
| 8 | class MenuItemTest extends BaseTestCase |
||
|
|
|||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Menu |
||
| 12 | */ |
||
| 13 | private $menu; |
||
| 14 | |||
| 15 | public function setUp() |
||
| 20 | /** @test */ |
||
| 21 | public function it_can_make_an_empty_menu_item() |
||
| 27 | |||
| 28 | /** @test */ |
||
| 29 | public function it_can_set_properties_on_menu_item() |
||
| 46 | |||
| 47 | /** @test */ |
||
| 48 | public function it_can_fill_a_menu_item_with_allowed_properties() |
||
| 73 | |||
| 74 | /** @test */ |
||
| 75 | public function it_can_set_icon_via_attributes() |
||
| 81 | |||
| 82 | /** @test */ |
||
| 83 | public function it_can_add_a_child_menu_item() |
||
| 90 | |||
| 91 | /** @test */ |
||
| 92 | public function it_can_get_ordered_children() |
||
| 103 | |||
| 104 | /** @test */ |
||
| 105 | public function it_can_create_a_dropdown_menu_item() |
||
| 115 | |||
| 116 | /** @test */ |
||
| 117 | public function it_can_make_a_simple_route_menu_item() |
||
| 130 | |||
| 131 | /** @test */ |
||
| 132 | public function it_can_make_a_route_menu_item() |
||
| 147 | |||
| 148 | /** @test */ |
||
| 149 | public function it_can_make_a_simple_url_menu_item() |
||
| 150 | { |
||
| 151 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||
| 152 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||
| 153 | $sub->url('settings/account', 'Account'); |
||
| 154 | }); |
||
| 155 | $children = $menuItem->getChilds()[0]->getChilds(); |
||
| 156 | |||
| 157 | $this->assertCount(1, $children); |
||
| 158 | $childMenuItem = array_first($children); |
||
| 159 | $this->assertEquals('settings/account', $childMenuItem->url); |
||
| 160 | $this->assertEquals('Account', $childMenuItem->title); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** @test */ |
||
| 164 | public function it_can_make_a_url_menu_item() |
||
| 179 | |||
| 180 | /** @test */ |
||
| 181 | View Code Duplication | public function it_can_add_a_menu_item_divider() |
|
| 182 | { |
||
| 183 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||
| 184 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||
| 185 | $sub->url('settings/account', 'Account'); |
||
| 186 | $sub->divider(); |
||
| 187 | }); |
||
| 188 | |||
| 189 | $children = $menuItem->getChilds()[0]->getChilds(); |
||
| 190 | |||
| 191 | $this->assertCount(2, $children); |
||
| 192 | $dividerMenuItem = $children[1]; |
||
| 193 | $this->assertEquals('divider', $dividerMenuItem->name); |
||
| 194 | $this->assertTrue($dividerMenuItem->isDivider()); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** @test */ |
||
| 198 | View Code Duplication | public function it_can_add_a_header_menu_item() |
|
| 199 | { |
||
| 200 | $menuItem = MenuItem::make(['title' => 'Parent Item']); |
||
| 201 | $menuItem->dropdown('Dropdown item', function (MenuItem $sub) { |
||
| 202 | $sub->header('User Stuff'); |
||
| 203 | $sub->url('settings/account', 'Account'); |
||
| 204 | }); |
||
| 205 | |||
| 206 | $children = $menuItem->getChilds()[0]->getChilds(); |
||
| 207 | |||
| 208 | $this->assertCount(2, $children); |
||
| 209 | $headerItem = $children[0]; |
||
| 210 | $this->assertEquals('header', $headerItem->name); |
||
| 211 | $this->assertEquals('User Stuff', $headerItem->title); |
||
| 212 | $this->assertTrue($headerItem->isHeader()); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** @test */ |
||
| 216 | public function it_can_get_the_correct_url_for_url_type() |
||
| 222 | |||
| 223 | /** @test */ |
||
| 224 | public function it_can_get_the_correct_url_for_route_type() |
||
| 236 | |||
| 237 | /** @test */ |
||
| 238 | public function it_can_get_request_uri() |
||
| 244 | |||
| 245 | /** @test */ |
||
| 246 | public function it_can_get_the_icon_html_attribute() |
||
| 252 | |||
| 253 | /** @test */ |
||
| 254 | public function it_returns_no_icon_if_none_exist() |
||
| 260 | |||
| 261 | /** @test */ |
||
| 262 | public function it_returns_default_icon_if_none_exist() |
||
| 268 | |||
| 269 | /** @test */ |
||
| 270 | public function it_can_get_item_properties() |
||
| 276 | |||
| 277 | /** @test */ |
||
| 278 | public function it_can_get_item_html_attributes() |
||
| 284 | |||
| 285 | /** @test */ |
||
| 286 | public function it_can_check_for_a_submenu() |
||
| 287 | { |
||
| 297 | |||
| 298 | public function it_can_check_active_state_on_item() |
||
| 301 | |||
| 302 | /** @test */ |
||
| 303 | public function it_can_set_auth_via_attributes() |
||
| 309 | } |
||
| 310 |