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 |
||
| 24 | final class DirectoryUtilityTest extends PHPUnit_Framework_TestCase { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Tests the create() method. |
||
| 28 | * |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public function testCreate() { |
||
| 32 | |||
| 33 | $arg1 = getcwd() . "/phpunit"; |
||
| 34 | $arg2 = $arg1 . "/unittest"; |
||
| 35 | |||
| 36 | $this->assertEquals(true, DirectoryUtility::create($arg1)); |
||
| 37 | $this->assertEquals(null, DirectoryUtility::create($arg1)); |
||
| 38 | $this->assertEquals(true, DirectoryUtility::create($arg2)); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Tests the isEmpty() method. |
||
| 43 | * |
||
| 44 | * @return void |
||
| 45 | * @depends testCreate |
||
| 46 | */ |
||
| 47 | public function testIsEmpty() { |
||
| 48 | |||
| 49 | $arg1 = getcwd() . "/phpunit"; |
||
| 50 | $arg2 = $arg1 . "/unittest"; |
||
| 51 | |||
| 52 | $this->assertEquals(null, DirectoryUtility::isEmpty("exception")); |
||
| 53 | $this->assertEquals(false, DirectoryUtility::isEmpty($arg1)); |
||
| 54 | $this->assertEquals(true, DirectoryUtility::isEmpty($arg2)); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Tests the rename() method. |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | * @depends testCreate |
||
| 62 | */ |
||
| 63 | public function testRename() { |
||
| 64 | |||
| 65 | $arg1 = getcwd() . "/phpunit"; |
||
| 66 | $arg2 = $arg1 . "/unittest"; |
||
| 67 | |||
| 68 | $this->assertEquals(true, DirectoryUtility::rename($arg2, $arg2 . "2")); |
||
| 69 | $this->assertEquals(null, DirectoryUtility::rename($arg2, $arg2 . "2")); |
||
| 70 | $this->assertEquals(null, DirectoryUtility::rename($arg2 . "2", $arg1)); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Tests the delete() method. |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | * @depends testRename |
||
| 78 | */ |
||
| 79 | public function testDelete() { |
||
| 80 | |||
| 81 | $arg1 = getcwd() . "/phpunit"; |
||
| 82 | $arg2 = $arg1 . "/unittest2"; |
||
| 83 | |||
| 84 | $this->assertEquals(null, DirectoryUtility::delete($arg1)); |
||
| 85 | $this->assertEquals(true, DirectoryUtility::delete($arg2)); |
||
| 86 | $this->assertEquals(true, DirectoryUtility::delete($arg1)); |
||
| 87 | } |
||
| 88 | |||
| 89 | } |
||
| 90 |