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

SubscriptionMapperTest::testUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
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 $users = [];
53
54
55
	/**
56
	 * {@inheritDoc}
57
	 */
58
	protected function setUp(): void {
59
		parent::setUp();
60
		$this->con = \OC::$server->getDatabaseConnection();
61
		$this->subscriptionMapper = new SubscriptionMapper($this->con);
62
		$this->pollMapper = new PollMapper($this->con);
63
64
		$this->polls = [
65
			$this->fm->instance('OCA\Polls\Db\Poll')
66
		];
67
68
		// insert test data
69
		foreach ($this->polls as $poll) {
70
			//insert test polls
71
			$this->pollMapper->insert($poll);
72
73
			// insert 3 subscriptions per poll
74
			for ($count=0; $count < 2; $count++) {
75
				$subscription = $this->fm->instance('OCA\Polls\Db\Subscription');
76
				$subscription->setPollId($poll->getId());
77
				array_push($this->subscriptions, $subscription);
78
				$this->subscriptionMapper->insert($subscription);
79
			}
80
			// save the last inserted userId
81
			$this->users[$poll->getId()] = $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...
82
83
		}
84
	}
85
86
	/**
87
	 * Find the previously created entries from the database.
88
	 */
89
	public function testFindAllByPoll() {
90
		foreach ($this->polls as $poll) {
91
			$this->assertTrue(count($this->subscriptionMapper->findAllByPoll($poll->getId())) > 0);
92
		}
93
	}
94
95
	public function testFindByUserAndPoll() {
96
		foreach ($this->polls as $poll) {
97
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->findByUserAndPoll($poll->getId(), $this->users[$poll->getId()]));
98
		}
99
	}
100
	/**
101
	 * Delete the previously created entries from the database.
102
	 */
103
	public function testUnsubscribe() {
104
		foreach ($this->polls as $poll) {
105
			$this->assertTrue($this->subscriptionMapper->unsubscribe($poll->getId(), $this->users[$poll->getId()]));
106
		}
107
	}
108
109
	public function tearDown(): void {
110
		parent::tearDown();
111
		foreach ($this->polls as $poll) {
112
			$this->pollMapper->delete($poll);
113
		}
114
	}
115
116
}
117