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

CollectedAddressMapperTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * ownCloud - 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\Db;
23
24
use Test\TestCase;
25
use OCA\Mail\Db\CollectedAddressMapper;
26
use OCA\Mail\Db\CollectedAddress;
27
28
/**
29
 * Class CollectedAddressMapperTest
30
 *
31
 * @group DB
32
 *
33
 * @package OCA\Mail\Tests\Db
34
 */
35
class CollectedAddressMapperTest extends TestCase {
36
37
	/** @var \OCP\IDBConnection */
38
	private $db;
39
	private $userId = 'testuser';
40
	/** @var CollectedAddressMapper */
41
	private $mapper;
42
	/** @var CollectedAddress */
43
	private $address1;
44
	/** @var CollectedAddress */
45
	private $address2;
46
47
	protected function setUp() {
48
		parent::setUp();
49
50
		$this->db = \OC::$server->getDatabaseConnection();
51
		$this->mapper = new CollectedAddressMapper($this->db);
52
53
		$this->address1 = new CollectedAddress();
54
		$this->address1->setEmail('[email protected]');
55
		$this->address1->setUserId($this->userId);
56
57
		$this->address2 = new CollectedAddress();
58
		$this->address2->setEmail('[email protected]');
59
		$this->address2->setUserId($this->userId);
60
61
		$sql = 'INSERT INTO *PREFIX*mail_collected_addresses (`email`, `user_id`) VALUES (?, ?)';
62
		$stmt = $this->db->prepare($sql);
63
64
		$stmt->execute([
65
			$this->address1->getEmail(),
66
			$this->address1->getUserId(),
67
		]);
68
		$this->address1->setId($this->db->lastInsertId('PREFIX*mail_collected_addresses'));
69
		$stmt->execute([
70
			$this->address2->getEmail(),
71
			$this->address2->getUserId(),
72
		]);
73
		$this->address2->setId($this->db->lastInsertId('PREFIX*mail_collected_addresses'));
74
	}
75
76
	protected function tearDown() {
77
		parent::tearDown();
78
79
		$sql = 'DELETE FROM *PREFIX*mail_collected_addresses WHERE `id` = ?';
80
		$stmt = $this->db->prepare($sql);
81
		if (!empty($this->address1)) {
82
			$stmt->execute([$this->address1->getId()]);
83
		}
84
		if (!empty($this->address2)) {
85
			$stmt->execute([$this->address2->getId()]);
86
		}
87
	}
88
89
	public function matchingData() {
90
		return [
91
			['[email protected]', ['[email protected]']],
92
			['ser', ['[email protected]', '[email protected]']],
93
		];
94
	}
95
96
	/**
97
	 * @dataProvider matchingData
98
	 */
99
	public function testFindMatching($query, $result) {
100
		$matches = $this->mapper->findMatching($this->userId, $query);
101
102
		$this->assertCount(count($result), $matches);
103
		$i = 0;
104
		foreach ($matches as $match) {
105
			$this->assertInstanceOf('\OCA\Mail\Db\CollectedAddress', $match);
106
			$this->assertTrue(in_array($match->getEmail(), $result));
107
			$this->assertEquals($this->userId, $match->getUserId());
108
			$i++;
109
		}
110
	}
111
112
	public function existsData() {
113
		return [
114
			['[email protected]', true],
115
			['[email protected]', false],
116
		];
117
	}
118
119
	/**
120
	 * @dataProvider existsData
121
	 */
122
	public function testExists($email, $expected) {
123
		$actual = $this->mapper->exists($this->userId, $email);
124
125
		$this->assertSame($expected, $actual);
126
	}
127
128
}
129