Completed
Push — master ( 4f4724...8201c5 )
by Travis
02:00
created

ContainerTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 233
Duplicated Lines 30.9 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.12%

Importance

Changes 0
Metric Value
dl 72
loc 233
ccs 101
cts 104
cp 0.9712
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A it_adds_a_menu() 0 6 1
A it_checks_if_menu_exists_returns_false() 0 4 1
A it_checks_if_menu_exists_returns_true() 0 7 1
A it_checks_if_has_links_return_true() 0 9 1
A it_checks_if_has_links_return_false() 0 7 1
A it_checks_set_active() 0 6 1
A it_checks_if_get_menu_returns_the_existing_menu() 0 9 1
A it_throws_an_exception_if_render_cant_find_the_menu() 0 4 1
A it_can_render_a_menu() 0 9 1
A it_sets_active_items() 0 13 1
A it_sets_active_items_in_dropdown() 16 16 1
A it_sets_active_items_in_dropdown_without_parentage() 18 18 1
A it_tests_insert_before_dropdown() 16 16 1
A it_tests_insert_after_link() 22 22 1
A it_tests_insert_after_dropdown() 0 22 1
A it_tests_insert_before_link() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Tests;
4
5
use JumpGate\Menu\Container;
6
use JumpGate\Menu\DropDown;
7
use JumpGate\Menu\Link;
8
use PHPUnit\Framework\TestCase;
9
10
class ContainerTest extends TestCase
11
{
12
    /**
13
     * @var \JumpGate\Menu\Container
14
     */
15
    protected $menu;
16
17 16
    public function setUp()
18
    {
19 16
        parent::setUp();
20
21 16
        $this->menu = new Container();
22 16
    }
23
24
    /** @test */
25 1
    public function it_adds_a_menu()
26
    {
27 1
        $menu = $this->menu->getMenu('menuName');
28
29 1
        $this->assertInstanceOf('\Jumpgate\Menu\Menu', $menu);
30 1
    }
31
32
    /** @test */
33 2
    public function it_checks_if_menu_exists_returns_false()
34
    {
35 1
        $this->assertFalse($this->menu->exists('menuName'));
36 2
    }
37
38
    /** @test */
39 1
    public function it_checks_if_menu_exists_returns_true()
40
    {
41 1
        $menu = $this->menu->getMenu('menuName');
42
43 1
        $this->assertInstanceOf('\Jumpgate\Menu\Menu', $menu);
44 1
        $this->assertTrue($this->menu->exists('menuName'));
45 1
    }
46
47
    /** @test */
48 1
    public function it_checks_if_has_links_return_true()
49
    {
50 1
        $this->menu->getMenu('menuName')
51
                   ->link('slug', function () {
52
                       //
53 1
                   });
54
55 1
        $this->assertTrue($this->menu->hasLinks('menuName'));
56 1
    }
57
58
    /** @test */
59 2
    public function it_checks_if_has_links_return_false()
60
    {
61 1
        $menu = $this->menu->getMenu('menuName');
62
63 1
        $this->assertInstanceOf('\Jumpgate\Menu\Menu', $menu);
64 1
        $this->assertFalse($this->menu->hasLinks('menuName'));
65 2
    }
66
67
    /** @test */
68 1
    public function it_checks_set_active()
69
    {
70 1
        $this->menu->setActive('slug');
71
72 1
        $this->assertEquals('slug', $this->menu->getActive());
73 1
    }
74
75
    /** @test */
76 1
    public function it_checks_if_get_menu_returns_the_existing_menu()
77
    {
78 1
        $this->menu->getMenu('menuName')
79
                   ->link('slug', function () {
80
                       //
81 1
                   });
82
83 1
        $this->assertCount(1, $this->menu->getMenu('menuName')->links);
84 1
    }
85
86
    /**
87
     * @test
88
     *
89
     * @expectedException Exception
90
     * @expectedExceptionMessage Menu test not found.
91
     */
92 1
    public function it_throws_an_exception_if_render_cant_find_the_menu()
93
    {
94 1
        $this->menu->render('test');
95
    }
96
97
    /** @test */
98 1
    public function it_can_render_a_menu()
99
    {
100 1
        $this->menu->getMenu('test')
101
                   ->link('slug', function () {
102
                       //
103 1
                   });
104
105 1
        $this->assertInstanceOf('\Jumpgate\Menu\Menu', $this->menu->render('test'));
106 1
    }
107
108
    /** @test */
109 1
    public function it_sets_active_items()
110
    {
111 1
        $this->menu->getMenu('test')
112
                   ->link('slug', function () {
113
                       //
114 1
                   });
115
116 1
        $this->menu->setActive('slug');
117
118 1
        $menu = $this->menu->render('test');
119
120 1
        $this->assertTrue($menu->links->first()->active);
121 1
    }
122
123
    /** @test */
124 1 View Code Duplication
    public function it_sets_active_items_in_dropdown()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126 1
        $this->menu->getMenu('test')
127
                   ->dropdown('slug', 'name', function (DropDown $dropdown) {
128
                       $dropdown->link('slug2', function () {
129
                           //
130 1
                       });
131 1
                   });
132
133 1
        $this->menu->setActive('slug2');
134
135 1
        $menu = $this->menu->render('test');
136
137 1
        $this->assertTrue($menu->links->first()->active);
138 1
        $this->assertTrue($menu->links->first()->links->first()->active);
139 1
    }
140
141
    /** @test */
142 1 View Code Duplication
    public function it_sets_active_items_in_dropdown_without_parentage()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144 1
        $this->menu->getMenu('test')
145
                   ->dropdown('slug', 'name', function (DropDown $dropdown) {
146 1
                       $dropdown->disableActiveParentage();
147
148
                       $dropdown->link('slug2', function () {
149
                           //
150 1
                       });
151 1
                   });
152
153 1
        $this->menu->setActive('slug2');
154
155 1
        $menu = $this->menu->render('test');
156
157 1
        $this->assertFalse($menu->links->first()->active);
158 1
        $this->assertTrue($menu->links->first()->links->first()->active);
159 1
    }
160
161
    /** @test */
162 1
    public function it_tests_insert_before_link()
163
    {
164 1
        $menu = $this->menu->getMenu('test');
165
166
        $menu->link('slug', function () {
167
            //
168 1
        });
169
170
        $menu->link('slug2', function () {
171 1
            $link->insertBefore('slug');
0 ignored issues
show
Bug introduced by
The variable $link does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172 1
        });
173
174
        $this->assertEquals('slug2', $menu->links->first()->slug);
175
    }
176
177
    /** @test */
178 1 View Code Duplication
    public function it_tests_insert_before_dropdown()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180 1
        $menu = $this->menu->getMenu('test');
181
182
        $menu->dropdown('slug', 'name', function (DropDown $dropdown) {
183
            $dropdown->link('slug2', function () {
184
                //
185 1
            });
186 1
        });
187
188
        $menu->link('slug3', function ($link) {
189 1
            $link->insertBefore('slug2');
190 1
        });
191
192 1
        $this->assertEquals('slug3', $menu->links->first()->links->first()->slug);
193 1
    }
194
195
    /** @test */
196 1 View Code Duplication
    public function it_tests_insert_after_link()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198 1
        $menu = $this->menu->getMenu('test');
199
200
        $menu->link('slug', function () {
201
            //
202 1
        });
203
204
        $menu->link('slug2', function () {
205
            //
206 1
        });
207
208
        $menu->link('slug3', function () {
209
            //
210 1
        });
211
212
        $menu->link('slug4', function (Link $link) {
213 1
            $link->insertAfter('slug2');
214 1
        });
215
216 1
        $this->assertEquals('slug4', $menu->links->get(2)->slug);
217 1
    }
218
219
    /** @test */
220 1
    public function it_tests_insert_after_dropdown()
221
    {
222 1
        $menu = $this->menu->getMenu('test');
223
224
        $menu->dropdown('slug', 'name', function (DropDown $dropdown) {
225
            $dropdown->link('slug2', function () {
226
                //
227 1
            });
228
            $dropdown->link('slug3', function () {
229
                //
230 1
            });
231
            $dropdown->link('slug4', function () {
232
                //
233 1
            });
234 1
        });
235
236 1
        $menu->link('slug5', function (Link $link) {
237 1
            $link->insertAfter('slug2');
238 1
        });
239
240 1
        $this->assertEquals('slug5', $menu->links->first()->links->get(1)->slug);
241 1
    }
242
}
243