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

CollectedAddressMapperTest::existsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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