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

CollectedAddressMapperTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 94
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * Mail
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Mail\Tests\Db;
24
25
use OC\AppFramework\Db\Db;
26
use Test\TestCase;
27
use OCA\Mail\Db\CollectedAddressMapper;
28
use OCA\Mail\Db\CollectedAddress;
29
30
/**
31
 * Class CollectedAddressMapperTest
32
 *
33
 * @group DB
34
 *
35
 * @package OCA\Mail\Tests\Db
36
 */
37
class CollectedAddressMapperTest extends TestCase {
38
39
	/** @var \OCP\IDBConnection */
40
	private $db;
41
	private $userId = 'testuser';
42
	/** @var CollectedAddressMapper */
43
	private $mapper;
44
	/** @var CollectedAddress */
45
	private $address1;
46
	/** @var CollectedAddress */
47
	private $address2;
48
49
	protected function setUp() {
50
		parent::setUp();
51
52
		$this->db = \OC::$server->getDatabaseConnection();
53
		$this->mapper = new CollectedAddressMapper(new Db($this->db));
54
55
		$this->address1 = new CollectedAddress();
56
		$this->address1->setEmail('[email protected]');
57
		$this->address1->setUserId($this->userId);
58
59
		$this->address2 = new CollectedAddress();
60
		$this->address2->setEmail('[email protected]');
61
		$this->address2->setUserId($this->userId);
62
63
		$sql = 'INSERT INTO *PREFIX*mail_collected_addresses (`email`, `user_id`) VALUES (?, ?)';
64
		$stmt = $this->db->prepare($sql);
65
66
		$stmt->execute([
67
			$this->address1->getEmail(),
68
			$this->address1->getUserId(),
69
		]);
70
		$this->address1->setId($this->db->lastInsertId('PREFIX*mail_collected_addresses'));
71
		$stmt->execute([
72
			$this->address2->getEmail(),
73
			$this->address2->getUserId(),
74
		]);
75
		$this->address2->setId($this->db->lastInsertId('PREFIX*mail_collected_addresses'));
76
	}
77
78
	protected function tearDown() {
79
		parent::tearDown();
80
81
		$sql = 'DELETE FROM *PREFIX*mail_collected_addresses WHERE `id` = ?';
82
		$stmt = $this->db->prepare($sql);
83
		if (!empty($this->address1)) {
84
			$stmt->execute([$this->address1->getId()]);
85
		}
86
		if (!empty($this->address2)) {
87
			$stmt->execute([$this->address2->getId()]);
88
		}
89
	}
90
91
	public function matchingData() {
92
		return [
93
			['[email protected]', ['[email protected]']],
94
			['ser', ['[email protected]', '[email protected]']],
95
		];
96
	}
97
98
	/**
99
	 * @dataProvider matchingData
100
	 */
101
	public function testFindMatching($query, $result) {
102
		$matches = $this->mapper->findMatching($this->userId, $query);
103
104
		$this->assertCount(count($result), $matches);
105
		$i = 0;
106
		foreach ($matches as $match) {
107
			$this->assertInstanceOf('\OCA\Mail\Db\CollectedAddress', $match);
108
			$this->assertTrue(in_array($match->getEmail(), $result));
109
			$this->assertEquals($this->userId, $match->getUserId());
110
			$i++;
111
		}
112
	}
113
114
	public function existsData() {
115
		return [
116
			['[email protected]', true],
117
			['[email protected]', false],
118
		];
119
	}
120
121
	/**
122
	 * @dataProvider existsData
123
	 */
124
	public function testExists($email, $expected) {
125
		$actual = $this->mapper->exists($this->userId, $email);
126
127
		$this->assertSame($expected, $actual);
128
	}
129
130
}
131