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 |
||
9 | class HtmlElementTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | private $textInDiv = 'my text in div'; |
||
12 | private $textInSpan = 'my text in span'; |
||
13 | private $textInTitle = 'my text in title'; |
||
14 | private $divAttr = 'div-attr'; |
||
15 | private $spanAttr = 'span-attr'; |
||
16 | private $titleAttr = 'title-attr'; |
||
17 | |||
18 | public function testInterfaceInstance() |
||
19 | { |
||
20 | $this->assertInstanceOf(HtmlElementInterface::class, new HtmlElement()); |
||
21 | } |
||
22 | |||
23 | public function testInterfaceElementInstance() |
||
24 | { |
||
25 | $html = new HtmlElement(array('myDiv' => array('type' => 'div'))); |
||
26 | |||
27 | $this->assertInstanceOf(ElementInterface::class, $html->load('myDiv')); |
||
28 | } |
||
29 | |||
30 | public function testDynamicDiv() |
||
31 | { |
||
32 | $map = array( |
||
33 | 'myDiv' => array( |
||
34 | 'type' => 'div', |
||
35 | 'text' => $this->textInDiv |
||
36 | ) |
||
37 | ); |
||
38 | |||
39 | $htmlElement = new HtmlElement($map); |
||
40 | |||
41 | $this->assertEquals($htmlElement->myDiv(), $this->renderDiv()); |
||
42 | } |
||
43 | |||
44 | public function testMapDiv() |
||
45 | { |
||
46 | $map = array( |
||
47 | 'myDiv' => array( |
||
48 | 'type' => 'div', |
||
49 | 'text' => $this->textInDiv |
||
50 | ) |
||
51 | ); |
||
52 | |||
53 | $htmlElement = new HtmlElement($map); |
||
54 | |||
55 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDiv()); |
||
56 | } |
||
57 | |||
58 | public function testMapDivWithAttribute() |
||
59 | { |
||
60 | $map = array( |
||
61 | 'myDiv' => array( |
||
62 | 'type' => 'div', |
||
63 | 'text' => $this->textInDiv, |
||
64 | 'attr' => array( |
||
65 | $this->divAttr => 'value' |
||
66 | ) |
||
67 | ) |
||
68 | ); |
||
69 | |||
70 | $htmlElement = new HtmlElement($map); |
||
71 | |||
72 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDivWithAttribute()); |
||
73 | } |
||
74 | |||
75 | public function testMapDivWithChild() |
||
76 | { |
||
77 | $map = array( |
||
78 | 'myDiv' => array( |
||
79 | 'type' => 'div', |
||
80 | 'children' => array( |
||
81 | 'mySpan' |
||
82 | ) |
||
83 | ), |
||
84 | 'mySpan' => array( |
||
85 | 'type' => 'span', |
||
86 | 'text' => $this->textInSpan |
||
87 | ) |
||
88 | ); |
||
89 | |||
90 | $htmlElement = new HtmlElement($map); |
||
91 | |||
92 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDivWithChild()); |
||
93 | } |
||
94 | |||
95 | public function testMapDivWithAttributeAndChildren() |
||
96 | { |
||
97 | $map = array( |
||
98 | 'myDiv' => array( |
||
99 | 'type' => 'div', |
||
100 | 'attr' => array( |
||
101 | $this->divAttr => 'value' |
||
102 | ), |
||
103 | 'children' => array( |
||
104 | 'myTitle', |
||
105 | 'mySpan' |
||
106 | ) |
||
107 | ), |
||
108 | 'mySpan' => array( |
||
109 | 'type' => 'span', |
||
110 | 'text' => $this->textInSpan, |
||
111 | 'attr' => array( |
||
112 | $this->spanAttr => 'value' |
||
113 | ) |
||
114 | ), |
||
115 | 'myTitle' => array( |
||
116 | 'type' => 'h1', |
||
117 | 'text' => $this->textInTitle, |
||
118 | 'attr' => array( |
||
119 | $this->titleAttr => 'value' |
||
120 | ) |
||
121 | ) |
||
122 | ); |
||
123 | |||
124 | $htmlElement = new HtmlElement($map); |
||
125 | |||
126 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDivWithAttributeAndChildren()); |
||
127 | } |
||
128 | |||
129 | public function testMapSpanWithAttributeAndParent() |
||
130 | { |
||
131 | $map = array( |
||
132 | 'myDiv' => array( |
||
133 | 'type' => 'div', |
||
134 | 'attr' => array( |
||
135 | $this->divAttr => 'value' |
||
136 | ), |
||
137 | 'children' => array( |
||
138 | 'myTitle' |
||
139 | ) |
||
140 | ), |
||
141 | 'mySpan' => array( |
||
142 | 'type' => 'span', |
||
143 | 'parent' => 'myDiv', |
||
144 | 'text' => $this->textInSpan, |
||
145 | 'attr' => array( |
||
146 | $this->spanAttr => 'value' |
||
147 | ) |
||
148 | ), |
||
149 | 'myTitle' => array( |
||
150 | 'type' => 'h1', |
||
151 | 'text' => $this->textInTitle, |
||
152 | 'attr' => array( |
||
153 | $this->titleAttr => 'value' |
||
154 | ) |
||
155 | ) |
||
156 | ); |
||
157 | |||
158 | $htmlElement = new HtmlElement($map); |
||
159 | |||
160 | $this->assertEquals($htmlElement->load('mySpan'), $this->renderDivWithAttributeAndChildren()); |
||
161 | } |
||
162 | |||
163 | public function testMapDivWithAttributeAndDynamicChildren() |
||
164 | { |
||
165 | $map = array( |
||
166 | 'myDiv' => array( |
||
167 | 'type' => 'div', |
||
168 | 'attr' => array( |
||
169 | $this->divAttr => 'value' |
||
170 | ), |
||
171 | 'children' => array( |
||
172 | 'myTitle', |
||
173 | array( |
||
174 | 'name' => 'mySpan', |
||
175 | 'type' => 'span', |
||
176 | 'text' => $this->textInSpan, |
||
177 | 'attr' => array( |
||
178 | $this->spanAttr => 'value' |
||
179 | ) |
||
180 | ) |
||
181 | ) |
||
182 | ), |
||
183 | 'myTitle' => array( |
||
184 | 'type' => 'h1', |
||
185 | 'text' => $this->textInTitle, |
||
186 | 'attr' => array( |
||
187 | $this->titleAttr => 'value' |
||
188 | ) |
||
189 | ) |
||
190 | ); |
||
191 | |||
192 | $htmlElement = new HtmlElement($map); |
||
193 | |||
194 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDivWithAttributeAndChildren()); |
||
195 | } |
||
196 | |||
197 | public function testMapDivWithParameter() |
||
198 | { |
||
199 | $map = array( |
||
200 | 'myDiv' => array( |
||
201 | 'type' => 'div', |
||
202 | 'text' => '%myDivText%' |
||
203 | ) |
||
204 | ); |
||
205 | |||
206 | $htmlElement = new HtmlElement($map); |
||
207 | |||
208 | $this->assertEquals($htmlElement->load('myDiv', array('myDivText' => $this->textInDiv)), $this->renderDiv()); |
||
209 | } |
||
210 | |||
211 | public function testMapDivWithExtend() |
||
212 | { |
||
213 | $map = array( |
||
214 | 'baseDiv' => array( |
||
215 | 'attr' => array( |
||
216 | $this->divAttr => 'value' |
||
217 | ) |
||
218 | ), |
||
219 | 'myDiv' => array( |
||
220 | 'type' => 'div', |
||
221 | 'text' => $this->textInDiv, |
||
222 | 'extends' => 'baseDiv' |
||
223 | ) |
||
224 | ); |
||
225 | |||
226 | $htmlElement = new HtmlElement($map); |
||
227 | |||
228 | $this->assertEquals($htmlElement->load('myDiv'), $this->renderDivWithAttribute()); |
||
229 | } |
||
230 | |||
231 | private function renderDiv() |
||
232 | { |
||
233 | return sprintf('<div>%s</div>', $this->textInDiv); |
||
234 | } |
||
235 | |||
236 | private function renderDivWithAttribute() |
||
237 | { |
||
238 | return sprintf('<div %s="value">%s</div>', $this->divAttr, $this->textInDiv); |
||
239 | } |
||
240 | |||
241 | private function renderDivWithChild() |
||
242 | { |
||
243 | return sprintf('<div><span>%s</span></div>', $this->textInSpan); |
||
244 | } |
||
245 | |||
246 | private function renderDivWithAttributeAndChildren() |
||
247 | { |
||
248 | return sprintf('<div %s="value"><h1 %s="value">%s</h1><span %s="value">%s</span></div>', |
||
249 | $this->divAttr, |
||
250 | $this->titleAttr, |
||
251 | $this->textInTitle, |
||
252 | $this->spanAttr, |
||
253 | $this->textInSpan |
||
254 | ); |
||
255 | } |
||
256 | } |
||
257 |