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 AppManagerTest extends \PHPUnit_Framework_TestCase { |
||
| 8 | |||
| 9 | public function testDisableApp(){ |
||
| 10 | $appId = 'anyapp'; |
||
| 11 | $appManager = new AppManager($this->getOccRunnerMock('')); |
||
| 12 | $result = $appManager->disableApp($appId); |
||
| 13 | $this->assertTrue($result); |
||
| 14 | } |
||
| 15 | |||
| 16 | public function testEnableApp(){ |
||
| 17 | $appId = 'anyapp'; |
||
| 18 | $appManager = new AppManager($this->getOccRunnerMock('')); |
||
| 19 | $result = $appManager->enableApp($appId); |
||
| 20 | $this->assertTrue($result); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function appListProvider(){ |
||
| 24 | return [ |
||
| 25 | |||
| 26 | [ |
||
| 27 | [ |
||
| 28 | 'enabled' => [ 'app1' => '1.0.1', 'app2' => '2.4.1' ], |
||
| 29 | 'disabled' => [ 'dapp1' => '0.0.1', 'dapp2' => '5.1.1' ] |
||
| 30 | ], |
||
| 31 | [ 'app1', 'app2', 'dapp1', 'dapp2' ] |
||
| 32 | ] |
||
| 33 | ]; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @dataProvider appListProvider |
||
| 38 | */ |
||
| 39 | public function testGetAllApps($apps, $expected){ |
||
| 40 | $encoded = json_encode($apps); |
||
| 41 | $appManager = new AppManager($this->getOccRunnerMock($encoded)); |
||
| 42 | $actual = $appManager->getShippedApps(); |
||
| 43 | $this->assertEquals($expected, $actual); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @dataProvider appListProvider |
||
| 48 | */ |
||
| 49 | public function testGetShippedApps($apps, $expected){ |
||
| 50 | $encoded = json_encode($apps); |
||
| 51 | $appManager = new AppManager($this->getOccRunnerMock($encoded)); |
||
| 52 | $actual = $appManager->getShippedApps(); |
||
| 53 | $this->assertEquals($expected, $actual); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testGetAppPath(){ |
||
| 57 | $expected = '/dev/null'; |
||
| 58 | $appId = 'anyapp'; |
||
| 59 | $appManager = new AppManager($this->getOccRunnerMock($expected)); |
||
| 60 | $actual = $appManager->getAppPath($appId); |
||
| 61 | $this->assertEquals($expected, $actual); |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function getOccRunnerMock($result){ |
||
| 65 | $runnerMock = $this->getMockBuilder('Owncloud\Updater\Utils\OccRunner') |
||
| 66 | ->setMethods(['run']) |
||
| 67 | ->disableOriginalConstructor() |
||
| 68 | ->getMock() |
||
| 69 | ; |
||
| 70 | $runnerMock |
||
| 71 | ->expects($this->any()) |
||
| 72 | ->method('run') |
||
| 73 | ->willReturn($result) |
||
| 74 | ; |
||
| 75 | return $runnerMock; |
||
| 76 | } |
||
| 77 | |||
| 78 | } |
||
| 79 |