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 |
||
38 | class FlexiBeeTest extends \Test\Ease\BrickTest |
||
|
|||
39 | { |
||
40 | /** |
||
41 | * @var FlexiBee |
||
42 | */ |
||
43 | protected $object; |
||
44 | |||
45 | /** |
||
46 | * Sets up the fixture, for example, opens a network connection. |
||
47 | * This method is called before a test is executed. |
||
48 | * @covers FlexiPeeHP\FlexiBee::__construct |
||
49 | */ |
||
50 | protected function setUp() |
||
54 | |||
55 | /** |
||
56 | * Tears down the fixture, for example, closes a network connection. |
||
57 | * This method is called after a test is executed. |
||
58 | */ |
||
59 | protected function tearDown() |
||
63 | |||
64 | public function testCurlInit() |
||
69 | |||
70 | /** |
||
71 | * @covers FlexiPeeHP\FlexiBee::setEvidence |
||
72 | */ |
||
73 | public function testSetEvidence() |
||
74 | { |
||
75 | $this->object->setEvidence('nastaveni'); |
||
76 | $this->assertEquals('nastaveni', $this->object->evidence); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @covers FlexiPeeHP\FlexiBee::object2array |
||
81 | */ |
||
82 | public function testObject2array() |
||
92 | |||
93 | /** |
||
94 | * @covers FlexiPeeHP\FlexiBee::performRequest |
||
95 | */ |
||
96 | public function testPerformRequest() |
||
97 | { |
||
98 | |||
99 | if (!is_null($this->object->evidence) && $this->object->evidence != 'test') { |
||
100 | $json = $this->object->performRequest($this->object->evidence.'.json'); |
||
101 | if (array_key_exists('message', $json)) { |
||
102 | $this->assertArrayHasKey('@version', $json); |
||
103 | } else { |
||
104 | $this->assertArrayHasKey($this->object->evidence, $json); |
||
105 | } |
||
106 | } else { |
||
107 | $this->object->evidence = 'c'; |
||
108 | $this->object->prefix = ''; |
||
109 | $this->object->company = ''; |
||
110 | $this->object->nameSpace = 'companies'; |
||
111 | $json = $this->object->performRequest(); |
||
112 | $this->assertArrayHasKey('company', $json); |
||
113 | |||
114 | $xml = $this->object->performRequest(null, 'GET', 'xml'); |
||
115 | $this->assertArrayHasKey('company', $xml); |
||
116 | } |
||
117 | |||
118 | $err = $this->object->performRequest('error.json'); |
||
119 | $this->assertArrayHasKey('success', $err); |
||
120 | $this->assertEquals('false', $err['success']); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @covers FlexiPeeHP\FlexiBee::getLastInsertedId |
||
125 | * @depends testInsertToFlexiBee |
||
126 | */ |
||
127 | public function testGetLastInsertedId() |
||
131 | |||
132 | /** |
||
133 | * @covers FlexiPeeHP\FlexiBee::xml2array |
||
134 | */ |
||
135 | public function testXml2array() |
||
152 | |||
153 | /** |
||
154 | * @covers FlexiPeeHP\FlexiBee::disconnect |
||
155 | * |
||
156 | * @depends testPerformRequest |
||
157 | * @depends testLoadFlexiData |
||
158 | * @depends testGetFlexiRow |
||
159 | * @depends testGetFlexiData |
||
160 | * @depends testLoadFromFlexiBee |
||
161 | * @depends testInsertToFlexiBee |
||
162 | * @depends testIdExists |
||
163 | * @depends testRecordExists |
||
164 | * @depends testGetColumnsFromFlexibee |
||
165 | * @depends testSearchString |
||
166 | */ |
||
167 | public function testDisconnect() |
||
172 | |||
173 | /** |
||
174 | * @covers FlexiPeeHP\FlexiBee::__destruct |
||
175 | * @depends testDisconnect |
||
176 | */ |
||
177 | public function test__destruct() |
||
181 | |||
182 | /** |
||
183 | * @covers FlexiPeeHP\FlexiBee::loadFlexiData |
||
184 | * @todo Implement testLoadFlexiData(). |
||
185 | */ |
||
186 | public function testLoadFlexiData() |
||
193 | |||
194 | /** |
||
195 | * @covers FlexiPeeHP\FlexiBee::getFlexiRow |
||
196 | */ |
||
197 | public function testGetFlexiRow() |
||
202 | |||
203 | /** |
||
204 | * @covers FlexiPeeHP\FlexiBee::getFlexiData |
||
205 | */ |
||
206 | public function testGetFlexiData() |
||
225 | |||
226 | /** |
||
227 | * @covers FlexiPeeHP\FlexiBee::loadFromFlexiBee |
||
228 | */ |
||
229 | public function testLoadFromFlexiBee() |
||
234 | |||
235 | /** |
||
236 | * @covers FlexiPeeHP\FlexiBee::jsonizeData |
||
237 | */ |
||
238 | public function testJsonizeData() |
||
243 | |||
244 | /** |
||
245 | * @covers FlexiPeeHP\FlexiBee::insertToFlexiBee |
||
246 | * @todo Implement testInsertToFlexiBee(). |
||
247 | */ |
||
248 | public function testInsertToFlexiBee() |
||
255 | |||
256 | /** |
||
257 | * @covers FlexiPeeHP\FlexiBee::idExists |
||
258 | * @todo Implement testIdExists(). |
||
259 | */ |
||
260 | public function testIdExists() |
||
267 | |||
268 | /** |
||
269 | * @covers FlexiPeeHP\FlexiBee::recordExists |
||
270 | */ |
||
271 | public function testRecordExists() |
||
276 | |||
277 | /** |
||
278 | * @covers FlexiPeeHP\FlexiBee::getColumnsFromFlexibee |
||
279 | * @todo Implement testGetColumnsFromFlexibee(). |
||
280 | */ |
||
281 | public function testGetColumnsFromFlexibee() |
||
288 | |||
289 | /** |
||
290 | * @covers FlexiPeeHP\FlexiBee::getKod |
||
291 | */ |
||
292 | public function testGetKod() |
||
319 | |||
320 | /** |
||
321 | * @covers FlexiPeeHP\FlexiBee::searchString |
||
322 | * @todo Implement testSearchString(). |
||
323 | */ |
||
324 | public function testSearchString() |
||
331 | |||
332 | /** |
||
333 | * @covers FlexiPeeHP\FlexiBee::logResult |
||
334 | */ |
||
335 | public function testLogResult() |
||
355 | |||
356 | /** |
||
357 | * @covers FlexiPeeHP\FlexiBee::flexiUrl |
||
358 | */ |
||
359 | public function testFlexiUrl() |
||
366 | } |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.