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, null, '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 testSaveToFlexiBee |
||
| 162 | * @depends testInsertToFlexiBee |
||
| 163 | * @depends testIdExists |
||
| 164 | * @depends testRecordExists |
||
| 165 | * @depends testGetColumnsFromFlexibee |
||
| 166 | * @depends testSearchString |
||
| 167 | */ |
||
| 168 | public function testDisconnect() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @covers FlexiPeeHP\FlexiBee::__destruct |
||
| 176 | * @depends testDisconnect |
||
| 177 | */ |
||
| 178 | public function test__destruct() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @covers FlexiPeeHP\FlexiBee::loadFlexiData |
||
| 185 | * @todo Implement testLoadFlexiData(). |
||
| 186 | */ |
||
| 187 | public function testLoadFlexiData() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @covers FlexiPeeHP\FlexiBee::getFlexiRow |
||
| 197 | */ |
||
| 198 | public function testGetFlexiRow() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @covers FlexiPeeHP\FlexiBee::getFlexiData |
||
| 206 | */ |
||
| 207 | public function testGetFlexiData() |
||
| 208 | { |
||
| 209 | if (is_null($this->object->evidence) || ($this->object->evidence == 'test')) { |
||
| 210 | $this->object->evidence = 'c'; |
||
| 211 | $this->object->prefix = ''; |
||
| 212 | $this->object->company = ''; |
||
| 213 | $this->object->nameSpace = 'companies'; |
||
| 214 | $flexidata = $this->object->getFlexiData(); |
||
| 215 | $this->assertArrayHasKey('company', $flexidata); |
||
| 216 | View Code Duplication | } else { |
|
| 217 | $flexidata = $this->object->getFlexiData(); |
||
| 218 | $this->assertArrayHasKey(0, $flexidata); |
||
| 219 | $this->assertArrayHasKey('id', $flexidata[0]); |
||
| 220 | $filtrered = $this->object->getFlexiData(null, |
||
| 221 | key($flexidata[0])." = ".current($flexidata[0])); |
||
| 222 | $this->assertArrayHasKey(0, $filtrered); |
||
| 223 | $this->assertArrayHasKey('id', $filtrered[0]); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @covers FlexiPeeHP\FlexiBee::loadFromFlexiBee |
||
| 229 | */ |
||
| 230 | public function testLoadFromFlexiBee() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @covers FlexiPeeHP\FlexiBee::saveToFlexiBee |
||
| 238 | * @todo Implement testSaveToFlexiBee(). |
||
| 239 | */ |
||
| 240 | public function testSaveToFlexiBee() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @covers FlexiPeeHP\FlexiBee::jsonizeData |
||
| 250 | */ |
||
| 251 | public function testJsonizeData() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @covers FlexiPeeHP\FlexiBee::insertToFlexiBee |
||
| 259 | * @todo Implement testInsertToFlexiBee(). |
||
| 260 | */ |
||
| 261 | public function testInsertToFlexiBee() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @covers FlexiPeeHP\FlexiBee::idExists |
||
| 271 | * @todo Implement testIdExists(). |
||
| 272 | */ |
||
| 273 | public function testIdExists() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @covers FlexiPeeHP\FlexiBee::recordExists |
||
| 283 | */ |
||
| 284 | public function testRecordExists() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @covers FlexiPeeHP\FlexiBee::getColumnsFromFlexibee |
||
| 292 | * @todo Implement testGetColumnsFromFlexibee(). |
||
| 293 | */ |
||
| 294 | public function testGetColumnsFromFlexibee() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @covers FlexiPeeHP\FlexiBee::getKod |
||
| 304 | */ |
||
| 305 | public function testGetKod() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @covers FlexiPeeHP\FlexiBee::searchString |
||
| 335 | * @todo Implement testSearchString(). |
||
| 336 | */ |
||
| 337 | public function testSearchString() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @covers FlexiPeeHP\FlexiBee::logResult |
||
| 347 | */ |
||
| 348 | public function testLogResult() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @covers FlexiPeeHP\FlexiBee::flexiUrl |
||
| 371 | */ |
||
| 372 | public function testFlexiUrl() |
||
| 379 | } |
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.