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

AddressCollectorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0
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
22
namespace OCA\Mail\Tests\Service\Autocompletion;
23
24
use PHPUnit_Framework_TestCase;
25
use OCA\Mail\Db\CollectedAddress;
26
use OCA\Mail\Service\AutoCompletion\AddressCollector;
27
28
class AddressCollectorTest extends PHPUnit_Framework_TestCase {
29
30
	private $mapper;
31
	private $userId = 'testuser';
32
	private $logger;
33
	private $collector;
34
35
	protected function setUp() {
36
		parent::setUp();
37
38
		$this->mapper = $this->getMockBuilder('\OCA\Mail\Db\CollectedAddressMapper')
39
			->disableOriginalConstructor()
40
			->getMock();
41
		$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger')
42
			->disableOriginalConstructor()
43
			->getMock();
44
45
		$this->collector = new AddressCollector($this->mapper, $this->userId,
46
			$this->logger);
47
	}
48
49
	public function testAddAddresses() {
50
		$addresses = [
51
			'[email protected]',
52
			'[email protected]',
53
		];
54
		$data = array_map(function($address) {
55
			$ca = new CollectedAddress();
56
			$ca->setEmail($address);
57
			$ca->setUserId($this->userId);
58
			return $ca;
59
		}, $addresses);
60
61
		$this->mapper->expects($this->at(0))
62
			->method('exists')
63
			->with($this->userId, $addresses[0])
64
			->will($this->returnValue(false));
65
		$this->mapper->expects($this->at(1))
66
			->method('insert')
67
			->with($data[0]);
68
		$this->mapper->expects($this->at(2))
69
			->method('exists')
70
			->with($this->userId, $addresses[1])
71
			->will($this->returnValue(false));
72
		$this->mapper->expects($this->at(3))
73
			->method('insert')
74
			->with($data[1]);
75
76
		$this->collector->addAddresses($addresses);
77
	}
78
79
	public function testAddDuplicateAddresses() {
80
		$addresses = [
81
			'[email protected]',
82
		];
83
84
		$this->mapper->expects($this->at(0))
85
			->method('exists')
86
			->with($this->userId, $addresses[0])
87
			->will($this->returnValue(true));
88
		$this->mapper->expects($this->never())
89
			->method('insert');
90
91
		$this->collector->addAddresses($addresses);
92
	}
93
94
	public function testSearchAddress() {
95
		$term = 'john';
96
		$mapperResult = ['some', 'data'];
97
98
		$this->mapper->expects($this->once())
99
			->method('findMatching')
100
			->with($this->userId, $term)
101
			->will($this->returnValue($mapperResult));
102
103
		$result = $this->collector->searchAddress($term);
104
		
105
		$this->assertequals($mapperResult, $result);
106
	}
107
108
}
109