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 |
||
| 18 | class MenuTest extends TestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @return Menu |
||
| 22 | */ |
||
| 23 | protected function menuFactory() |
||
| 24 | { |
||
| 25 | /** @var Menu $menu */ |
||
| 26 | $menu = $this->app->makeWith(Menu::class, ['container' => $this->app]); |
||
| 27 | $menu->make('test', function(Builder $builder) { |
||
| 28 | $builder->create('one', Link::class, function(LinkFactory $factory) { |
||
| 29 | $factory->title = 'One'; |
||
| 30 | $factory->url = '/one'; |
||
| 31 | |||
| 32 | return $factory->build(); |
||
| 33 | }); |
||
| 34 | }); |
||
| 35 | |||
| 36 | return $menu; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testFacade() |
||
| 40 | { |
||
| 41 | $this->assertInstanceOf(Menu::class, \Menu::getFacadeRoot()); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testGet() |
||
| 45 | { |
||
| 46 | $menu = $this->menuFactory(); |
||
| 47 | $builder = $menu->get('test'); |
||
| 48 | $this->assertInstanceOf(Builder::class, $builder); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @expectedException \RuntimeException |
||
| 53 | */ |
||
| 54 | public function testGetException() |
||
| 55 | { |
||
| 56 | $menu = \Menu::get('test'); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testHas() |
||
| 60 | { |
||
| 61 | $menu = $this->menuFactory(); |
||
| 62 | $this->assertFalse($menu->has('another')); |
||
| 63 | $this->assertTrue($menu->has('test')); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function testForget() |
||
| 67 | { |
||
| 68 | $menu = $this->menuFactory(); |
||
| 69 | $menu->forget('test'); |
||
| 70 | $this->assertAttributeEquals([], 'menuList', $menu); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testRender() |
||
| 74 | { |
||
| 75 | $menu = $this->menuFactory(); |
||
| 76 | $file = file_get_contents(__DIR__ . '/stub/facade_render.html'); |
||
| 77 | $this->assertEquals($file, $menu->render('test')); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testFromArray() |
||
| 81 | { |
||
| 82 | /** @var Builder $builder */ |
||
| 83 | $builder = $this->app->makeWith(Builder::class, [ |
||
| 84 | 'attributes' => new Attributes(['class' => 'menu']), |
||
| 85 | 'activeAttributes' => new Attributes(['class' => 'active']), |
||
| 86 | ]); |
||
| 87 | |||
| 88 | $builder->create('index', Link::class, function(LinkFactory $factory) { |
||
| 89 | $factory->title = 'Index'; |
||
| 90 | $factory->url = '/'; |
||
| 91 | |||
| 92 | return $factory->build(); |
||
| 93 | }); |
||
| 94 | |||
| 95 | $builder->create('deliver_1', Text::class, function(TextFactory $factory) { |
||
| 96 | $factory->text = null; |
||
| 97 | $factory->attributes->put('class', 'deliver'); |
||
| 98 | |||
| 99 | return $factory->build(); |
||
| 100 | }); |
||
| 101 | |||
| 102 | $builder->create('settings', SubMenu::class, function(SubMenuFactory $factory) { |
||
| 103 | $factory->title = 'Index'; |
||
| 104 | $factory->builder->create('some', Link::class, function(LinkFactory $factory) { |
||
| 105 | $factory->title = 'Some setting'; |
||
| 106 | $factory->url = '/settings/some'; |
||
| 107 | |||
| 108 | return $factory->build(); |
||
| 109 | }); |
||
| 110 | |||
| 111 | return $factory->build(); |
||
| 112 | }); |
||
| 113 | |||
| 114 | $builder->create('deliver_2', Text::class, function(TextFactory $factory) { |
||
| 115 | $factory->text = null; |
||
| 116 | $factory->attributes->put('class', 'deliver'); |
||
| 117 | |||
| 118 | return $factory->build(); |
||
| 119 | }); |
||
| 120 | |||
| 121 | $builder->create('logout', Link::class, function(LinkFactory $factory) { |
||
| 122 | $factory->title = 'Logout'; |
||
| 123 | $factory->url = '/logout'; |
||
| 124 | |||
| 125 | return $factory->build(); |
||
| 126 | }); |
||
| 127 | |||
| 128 | /** @var Menu $menu */ |
||
| 129 | $menu = $this->app->make(Menu::class); |
||
| 130 | $menu->fromArray('from_array', $builder->toArray()); |
||
| 131 | |||
| 132 | $this->assertEquals($builder->render(), $menu->get('from_array')->render()); |
||
| 133 | $this->assertEquals($builder->toArray(), $menu->toArray('from_array')); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function testHelper() |
||
| 137 | { |
||
| 138 | $this->assertInstanceOf(Menu::class, menu()); |
||
| 139 | $this->assertInstanceOf(Builder::class, menu('test')); |
||
| 140 | } |
||
| 141 | } |