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

SubscriptionMapperTest::setUp()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
dl 0
loc 28
rs 9.6
c 2
b 1
f 0
cc 4
nc 6
nop 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 $subscriptionsById;
0 ignored issues
show
introduced by
The private property $subscriptionsById is not used, and could be removed.
Loading history...
56
57
	/** @var array */
58
	private $users;
59
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64
	protected function setUp(): void {
65
		parent::setUp();
66
		$this->con = \OC::$server->getDatabaseConnection();
67
		$this->subscriptionMapper = new SubscriptionMapper($this->con);
68
		$this->pollMapper = new PollMapper($this->con);
69
70
		$this->polls = [];
71
		$this->subscriptions = [];
72
73
		$this->polls = [
74
			$this->fm->instance('OCA\Polls\Db\Poll')
75
		];
76
77
		foreach ($this->polls as $poll) {
78
			$entry = $this->pollMapper->insert($poll);
79
			$entry->resetUpdatedFields();
80
			$this->pollsById[$entry->getId()] = $entry;
81
		}
82
		foreach ($this->pollsById as $id => $polls) {
83
			for ($count=0; $count < 2; $count++) {
84
				$subscription = $this->fm->instance('OCA\Polls\Db\Subscription');
85
				$subscription->setPollId($id);
86
				array_push($this->subscriptions, $subscription);
87
			}
88
			var_dump($subscription);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($subscription) looks like debug code. Are you sure you do not want to remove it?
Loading history...
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...
89
			$this->users[$id] = $subscription->getUserId();
90
		}
91
		var_dump($this->users);
92
	}
93
94
	/**
95
	 * Find the previously created entries from the database.
96
	 */
97
	public function testFindAllByPoll() {
98
		foreach ($this->pollsById as $id => $poll) {
99
			$this->assertTrue(count($this->subscriptionMapper->findAllByPoll($id)) > 0);
100
		}
101
	}
102
103
	public function testFindByUserAndPoll() {
104
		foreach ($this->pollsById as $id => $poll) {
105
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->findByUserAndPoll($id, $this->users[$id]));
106
		}
107
	}
108
	/**
109
	 * Delete the previously created entries from the database.
110
	 */
111
	public function testUnsubscribe() {
112
		foreach ($this->pollsById as $id => $poll) {
113
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->unsubscribe($id, $this->users[$id]));
114
		}
115
	}
116
117
	public function tearDown(): void {
118
		parent::tearDown();
119
		foreach ($this->polls as $poll) {
120
			$this->pollMapper->delete($poll);
121
		}
122
	}
123
124
}
125