Passed
Pull Request — master (#1269)
by René
03:42
created

SubscriptionMapperTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * @copyright Copyright (c) 2017 Kai Schröer <[email protected]>
4
 *
5
 * @author Kai Schröer <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Tests\Unit\Db;
25
26
use OCA\Polls\Db\Poll;
27
use OCA\Polls\Db\PollMapper;
28
use OCA\Polls\Db\Subscription;
29
use OCA\Polls\Db\SubscriptionMapper;
30
use OCA\Polls\Tests\Unit\UnitTestCase;
31
use OCP\IDBConnection;
32
use League\FactoryMuffin\Faker\Facade as Faker;
33
34
class SubscriptionMapperTest extends UnitTestCase {
35
36
	/** @var IDBConnection */
37
	private $con;
38
39
	/** @var SubscriptionMapper */
40
	private $subscriptionMapper;
41
42
	/** @var PollMapper */
43
	private $pollMapper;
44
45
	/** @var array */
46
	private $polls = [];
47
48
	/** @var array */
49
	private $subscriptions = [];
50
51
	/** @var array */
52
	private $pollsById = [];
53
54
	/** @var array */
55
	private $users = [];
56
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61
	protected function setUp(): void {
62
		parent::setUp();
63
		$this->con = \OC::$server->getDatabaseConnection();
64
		$this->subscriptionMapper = new SubscriptionMapper($this->con);
65
		$this->pollMapper = new PollMapper($this->con);
66
67
		$this->polls = [
68
			$this->fm->instance('OCA\Polls\Db\Poll')
69
		];
70
71
		foreach ($this->polls as $poll) {
72
			$entry = $this->pollMapper->insert($poll);
73
			$entry->resetUpdatedFields();
74
			$this->pollsById[$entry->getId()] = $entry;
75
		}
76
77
		foreach ($this->pollsById as $id => $polls) {
78
			for ($count=0; $count < 2; $count++) {
79
				$subscription = $this->fm->instance('OCA\Polls\Db\Subscription');
80
				$subscription->setPollId($id);
81
				array_push($this->subscriptions, $subscription);
82
				$this->subscriptionMapper->insert($subscription);
83
			}
84
			$this->users[$id] = $subscription->getUserId();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subscription does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
		}
86
	}
87
88
	/**
89
	 * Find the previously created entries from the database.
90
	 */
91
	public function testFindAllByPoll() {
92
		foreach ($this->polls as $poll) {
93
			$this->assertTrue(count($this->subscriptionMapper->findAllByPoll($poll->getId())) > 0);
94
		}
95
	}
96
97
	public function testFindByUserAndPoll() {
98
		foreach ($this->polls as $poll) {
99
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->findByUserAndPoll($poll->getId(), $this->users[$poll->getId()]));
100
		}
101
	}
102
	/**
103
	 * Delete the previously created entries from the database.
104
	 */
105
	public function testUnsubscribe() {
106
		foreach ($this->pollsById as $id => $poll) {
107
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->unsubscribe($id, $this->users[$id]));
108
		}
109
	}
110
111
	public function tearDown(): void {
112
		parent::tearDown();
113
		foreach ($this->polls as $poll) {
114
			$this->pollMapper->delete($poll);
115
		}
116
	}
117
118
}
119