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 |
||
| 25 | class AccountServiceTest extends TestCase { |
||
| 26 | |||
| 27 | private $user = 'herbert'; |
||
| 28 | private $mapper; |
||
| 29 | private $l10n; |
||
| 30 | private $service; |
||
| 31 | private $account1; |
||
| 32 | private $account2; |
||
| 33 | |||
| 34 | protected function setUp() { |
||
| 35 | parent::setUp(); |
||
| 36 | |||
| 37 | $this->mapper = $this->getMockBuilder('OCA\Mail\Db\MailAccountMapper') |
||
| 38 | ->disableOriginalConstructor() |
||
| 39 | ->getMock(); |
||
| 40 | $this->l10n = $this->getMockBuilder('\OCP\IL10N') |
||
| 41 | ->disableOriginalConstructor() |
||
| 42 | ->getMock(); |
||
| 43 | $this->service = new AccountService($this->mapper, $this->l10n); |
||
| 44 | $this->account1 = $this->getMockBuilder('OCA\Mail\Db\MailAccount') |
||
| 45 | ->disableOriginalConstructor() |
||
| 46 | ->getMock(); |
||
| 47 | $this->account2 = $this->getMockBuilder('OCA\Mail\Db\MailAccount') |
||
| 48 | ->disableOriginalConstructor() |
||
| 49 | ->getMock(); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testFindByUserId() { |
||
| 53 | $this->mapper->expects($this->once()) |
||
| 54 | ->method('findByUserId') |
||
| 55 | ->with($this->user) |
||
| 56 | ->will($this->returnValue([$this->account1])); |
||
| 57 | |||
| 58 | $expected = [ |
||
| 59 | new Account($this->account1) |
||
| 60 | ]; |
||
| 61 | $actual = $this->service->findByUserId($this->user); |
||
| 62 | |||
| 63 | $this->assertEquals($expected, $actual); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function testFindByUserIdUnifiedInbox() { |
||
| 67 | $this->mapper->expects($this->once()) |
||
| 68 | ->method('findByUserId') |
||
| 69 | ->with($this->user) |
||
| 70 | ->will($this->returnValue([ |
||
| 71 | $this->account1, |
||
| 72 | $this->account2, |
||
| 73 | ])); |
||
| 74 | |||
| 75 | $expected = [ |
||
| 76 | null, |
||
| 77 | new Account($this->account1), |
||
| 78 | new Account($this->account2), |
||
| 79 | ]; |
||
| 80 | $actual = $this->service->findByUserId($this->user); |
||
| 81 | |||
| 82 | $this->assertCount(3, $actual); |
||
| 83 | $this->assertEquals($expected[1], $actual[1]); |
||
| 84 | $this->assertEquals($expected[2], $actual[2]); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testFind() { |
||
| 88 | $accountId = 123; |
||
| 89 | |||
| 90 | $this->mapper->expects($this->once()) |
||
| 91 | ->method('find') |
||
| 92 | ->with($this->user, $accountId) |
||
| 93 | ->will($this->returnValue($this->account1)); |
||
| 94 | |||
| 95 | $expected = new Account($this->account1); |
||
| 96 | $actual = $this->service->find($this->user, $accountId); |
||
| 97 | |||
| 98 | $this->assertEquals($expected, $actual); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testFindNotFound() { |
||
| 102 | // TODO: implement code + write tests |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testDelete() { |
||
| 106 | $accountId = 33; |
||
| 107 | |||
| 108 | $this->mapper->expects($this->once()) |
||
| 109 | ->method('find') |
||
| 110 | ->with($this->user, $accountId) |
||
| 111 | ->will($this->returnValue($this->account1)); |
||
| 112 | $this->mapper->expects($this->once()) |
||
| 113 | ->method('delete') |
||
| 114 | ->with($this->account1); |
||
| 115 | |||
| 116 | $this->service->delete($this->user, $accountId); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testDeleteUnifiedInbox() { |
||
| 120 | $accountId = -1; |
||
| 121 | |||
| 122 | $this->mapper->expects($this->never()) |
||
| 123 | ->method('find') |
||
| 124 | ->with($this->user, $accountId) |
||
| 125 | ->will($this->returnValue($this->account1)); |
||
| 126 | $this->mapper->expects($this->never()) |
||
| 127 | ->method('delete') |
||
| 128 | ->with($this->account1); |
||
| 129 | |||
| 130 | $this->service->delete($this->user, $accountId); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testSave() { |
||
| 134 | $account = new OCA\Mail\Db\MailAccount(); |
||
| 135 | $expected = 42; |
||
| 136 | |||
| 137 | $this->mapper->expects($this->once()) |
||
| 138 | ->method('save') |
||
| 139 | ->with($account) |
||
| 140 | ->will($this->returnValue($expected)); |
||
| 141 | |||
| 142 | $actual = $this->service->save($account); |
||
| 143 | |||
| 144 | $this->assertEquals($expected, $actual); |
||
| 145 | } |
||
| 146 | |||
| 147 | } |
||
| 148 |