Completed
Pull Request — master (#1276)
by Thomas
04:23
created

AddressCollectorTest::testAddAddresses()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
/**
4
 * ownCloud - Mail
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Christoph Wurst <[email protected]>
10
 * @copyright Christoph Wurst 2016
11
 */
12
13
namespace OCA\Mail\Tests\Service\Autocompletion;
14
15
use PHPUnit_Framework_TestCase;
16
use OCA\Mail\Db\CollectedAddress;
17
use OCA\Mail\Service\AutoCompletion\AddressCollector;
18
19
class AddressCollectorTest extends PHPUnit_Framework_TestCase {
20
21
	private $mapper;
22
	private $userId = 'testuser';
23
	private $logger;
24
	private $collector;
25
26
	protected function setUp() {
27
		parent::setUp();
28
29
		$this->mapper = $this->getMockBuilder('\OCA\Mail\Db\CollectedAddressMapper')
30
			->disableOriginalConstructor()
31
			->getMock();
32
		$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger')
33
			->disableOriginalConstructor()
34
			->getMock();
35
36
		$this->collector = new AddressCollector($this->mapper, $this->userId,
37
			$this->logger);
38
	}
39
40
	public function testAddAddresses() {
41
		$addresses = [
42
			'[email protected]',
43
			'[email protected]',
44
		];
45
		$data = array_map(function($address) {
46
			$ca = new CollectedAddress();
47
			$ca->setEmail($address);
48
			$ca->setUserId($this->userId);
49
			return $ca;
50
		}, $addresses);
51
52
		$this->mapper->expects($this->at(0))
53
			->method('exists')
54
			->with($this->userId, $addresses[0])
55
			->will($this->returnValue(false));
56
		$this->mapper->expects($this->at(1))
57
			->method('insert')
58
			->with($data[0]);
59
		$this->mapper->expects($this->at(2))
60
			->method('exists')
61
			->with($this->userId, $addresses[1])
62
			->will($this->returnValue(false));
63
		$this->mapper->expects($this->at(3))
64
			->method('insert')
65
			->with($data[1]);
66
67
		$this->collector->addAddresses($addresses);
68
	}
69
70
	public function testAddDuplicateAddresses() {
71
		$addresses = [
72
			'[email protected]',
73
		];
74
75
		$this->mapper->expects($this->at(0))
76
			->method('exists')
77
			->with($this->userId, $addresses[0])
78
			->will($this->returnValue(true));
79
		$this->mapper->expects($this->never())
80
			->method('insert');
81
82
		$this->collector->addAddresses($addresses);
83
	}
84
85
	public function testSearchAddress() {
86
		$term = 'john';
87
		$mapperResult = ['some', 'data'];
88
89
		$this->mapper->expects($this->once())
90
			->method('findMatching')
91
			->with($this->userId, $term)
92
			->will($this->returnValue($mapperResult));
93
94
		$result = $this->collector->searchAddress($term);
0 ignored issues
show
Documentation introduced by
$term is of type string, but the function expects a array<integer,object<Horde_Mail_Rfc822_Address>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
		
96
		$this->assertequals($mapperResult, $result);
97
	}
98
99
}
100