Completed
Branch master (0d9475)
by WEBEWEB
02:41 queued 01:18
created

NavigationItemTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 160
Duplicated Lines 13.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 22
loc 160
rs 10
c 3
b 0
f 0

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
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Tests\Navigation\Item;
13
14
use PHPUnit_Framework_TestCase;
15
use WBW\Library\Core\Navigation\Item\NavigationItem;
16
17
/**
18
 * Navigation item test.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Tests\Navigation\Item
22
 * @final
23
 */
24
final class NavigationItemTest extends PHPUnit_Framework_TestCase {
25
26
	/**
27
	 * Tests the __construct() method.
28
	 *
29
	 * @return void
30
	 */
31
	public function testConstructor() {
32
33
		$obj = new NavigationItem("id");
34
35
		$this->assertEquals("id", $obj->getId());
36
37
		$this->assertEquals(null, $obj->getFirstNode());
38
		$this->assertEquals(null, $obj->getLastNode());
39
		$this->assertEquals([], $obj->getNodes());
40
		$this->assertEquals(null, $obj->getParent());
41
42
		$this->assertEquals("id", $obj->getSpan());
43
		$this->assertEquals("javascript: void(0);", $obj->getHref());
44
		$this->assertEquals(null, $obj->getIcon());
45
	}
46
47
	/**
48
	 * Tests the addNode() method.
49
	 *
50
	 * @return void.
51
	 */
52
	public function testAddNode() {
53
54
		$obj = new NavigationItem("id");
55
		$add = new NavigationItem("id2");
56
57
		$obj->addNode($add);
58
		$this->assertEquals([$add], $obj->getNodes());
59
	}
60
61
	/**
62
	 * Tests the clearNode() method.
63
	 *
64
	 * @return void.
65
	 */
66
	public function testClearNode() {
67
68
		$obj = new NavigationItem("id");
69
		$add = new NavigationItem("id2");
70
71
		$obj->addNode($add);
72
		$this->assertEquals([$add], $obj->getNodes());
73
74
		$obj->clearNodes();
75
		$this->assertEquals([], $obj->getNodes());
76
	}
77
78
	/**
79
	 * Tests the getFirstNode() method.
80
	 *
81
	 * @return void.
82
	 */
83
	public function testGetFirstNode() {
84
85
		$obj = new NavigationItem("id");
86
		$add = new NavigationItem("id2");
87
88
		$obj->addNode($add);
89
		$this->assertEquals($add, $obj->getFirstNode());
90
	}
91
92
	/**
93
	 * Tests the getLastNode() method.
94
	 *
95
	 * @return void.
96
	 */
97
	public function testGetLastNode() {
98
99
		$obj = new NavigationItem("id");
100
		$add = new NavigationItem("id2");
101
102
		$obj->addNode($add);
103
		$this->assertEquals($add, $obj->getLastNode());
104
	}
105
106
	/**
107
	 * Tests the getNodeAt() method.
108
	 *
109
	 * @return void.
110
	 */
111
	public function testGetNodeAt() {
112
113
		$obj = new NavigationItem("id");
114
		$add = new NavigationItem("id2");
115
116
		$obj->addNode($add);
117
		$this->assertEquals(null, $obj->getNodeAt(-1));
118
		$this->assertEquals($add, $obj->getNodeAt(0));
119
		$this->assertEquals(null, $obj->getNodeAt(1));
120
	}
121
122
	/**
123
	 * Tests the getNodeById() method.
124
	 *
125
	 * @return void.
126
	 */
127
	public function testGetNodeById() {
128
129
		$obj	 = new NavigationItem("id");
130
		$add1	 = new NavigationItem("id2");
131
		$add2	 = new NavigationItem("id3");
132
133
		$obj->addNode($add1);
134
		$add1->addNode($add2);
135
		$this->assertEquals(null, $obj->getNodeById("exception"));
136
		$this->assertEquals($add1, $obj->getNodeById("id2"));
137
		$this->assertEquals(null, $obj->getNodeById("id3"));
138
		$this->assertEquals($add2, $obj->getNodeById("id3", true));
139
	}
140
141
	/**
142
	 * Tests the removeNode() method.
143
	 *
144
	 * @return void.
145
	 */
146
	public function testRemoveNode() {
147
148
		$obj = new NavigationItem("id");
149
		$add = new NavigationItem("id2");
150
151
		$obj->addNode($add);
152
		$this->assertEquals([$add], $obj->getNodes());
153
154
		$obj->removeNode($add);
155
		$this->assertEquals([], $obj->getNodes());
156
	}
157
158
	/**
159
	 * Tests the toArray() method.
160
	 *
161
	 * @return void.
162
	 */
163
	public function testToArray() {
164
165
		$obj = new NavigationItem("id");
166
167
		$res1 = ["span" => "id", "href" => "javascript: void(0);"];
168
		$this->assertEquals($res1, $obj->toArray());
169
170
		$obj->setHref("href");
171
		$res2 = ["span" => "id", "href" => "href"];
172
		$this->assertEquals($res2, $obj->toArray());
173
174
		$obj->setIcon("icon");
175
		$res3 = ["span" => "id", "href" => "href", "icon" => "icon"];
176
		$this->assertEquals($res3, $obj->toArray());
177
178
		$obj->addNode(new NavigationItem("id2"));
179
		$res4 = ["span" => "id", "href" => "href", "icon" => "icon", "subitems" => [["span" => "id2", "href" => "javascript: void(0);"]]];
180
		$this->assertEquals($res4, $obj->toArray());
181
	}
182
183
}
184