Completed
Push — master ( 45aa1e...841b62 )
by Thomas
19:35
created

AccountServiceTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 21.14 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 26
loc 123
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
use Test\TestCase;
22
use OCA\Mail\Account;
23
use OCA\Mail\Service\AccountService;
24
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