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 |
||
| 7 | class ViewTest extends \PHPUnit_Framework_TestCase { |
||
| 8 | |||
| 9 | public function testViewNotFound() { |
||
| 10 | $view = new View('this-view-doesnt-exist'); |
||
| 11 | $this->assertFalse($view->exists()); |
||
| 12 | } |
||
| 13 | |||
| 14 | public function testViewIndex() { |
||
| 15 | $view = new View('index'); |
||
| 16 | $this->assertTrue($view->exists()); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testToString() { |
||
| 20 | $viewEmpty = new View('this-file-doent-exist'); |
||
| 21 | $this->assertEquals(0, strlen($viewEmpty->toString())); |
||
| 22 | |||
| 23 | $viewNotEmpty = new View('index'); |
||
| 24 | $this->assertNotEquals(0, strlen($viewNotEmpty->toString())); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testParamData() { |
||
| 28 | $view = new View('index', ['a' => 5]); |
||
| 29 | $this->assertEquals($view->getData('a'), 5); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testAddData() { |
||
| 33 | $view = new View('index'); |
||
| 34 | $view->addData('a', 10); |
||
| 35 | $this->assertEquals($view->getData('a'), 10); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function testMergeData() { |
||
| 39 | $view = new View('index'); |
||
| 40 | $view->mergeData(['a' => 10, 'b' => 20]); |
||
| 41 | |||
| 42 | $this->assertEquals($view->getData('a'), 10); |
||
| 43 | $this->assertEquals($view->getData('b'), 20); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function testAddDataAndMergeData() { |
||
| 47 | $view = new View('index'); |
||
| 48 | $view->addData('a', 10); |
||
| 49 | $view->mergeData(['a' => 15, 'b' => 20]); |
||
| 50 | |||
| 51 | $this->assertEquals($view->getData('a'), 15); |
||
| 52 | $this->assertEquals($view->getData('b'), 20); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testParamAndMergeDataAndAddData() { |
||
| 56 | $view = new View('index', ['a' => 5, 'c' => 30]); |
||
| 57 | $view->mergeData(['a' => 15, 'b' => 20]); |
||
| 58 | $view->addData('a', 10); |
||
| 59 | |||
| 60 | $this->assertEquals($view->getData('a'), 10); |
||
| 61 | $this->assertEquals($view->getData('b'), 20); |
||
| 62 | $this->assertEquals($view->getData('c'), 30); |
||
| 63 | } |
||
| 64 | |||
| 65 | } |
||
| 66 |