Passed
Pull Request — master (#1270)
by René
03:41
created

VoteMapperTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
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 League\FactoryMuffin\Faker\Facade as Faker;
27
use OCP\IDBConnection;
28
use OCA\Polls\Tests\Unit\UnitTestCase;
29
30
use OCA\Polls\Db\Poll;
31
use OCA\Polls\Db\PollMapper;
32
use OCA\Polls\Db\Vote;
33
use OCA\Polls\Db\VoteMapper;
34
use OCA\Polls\Db\Option;
35
use OCA\Polls\Db\OptionMapper;
36
37
class VoteMapperTest extends UnitTestCase {
38
39
	/** @var IDBConnection */
40
	private $con;
41
	/** @var VoteMapper */
42
	private $voteMapper;
43
	/** @var PollMapper */
44
	private $pollMapper;
45
	/** @var OptionMapper */
46
	private $optionMapper;
47
48
49
	/** @var array */
50
	private $polls = [];
51
52
	/** @var array */
53
	private $options = [];
54
55
	/** @var array */
56
	private $votes = [];
57
58
	/** @var array */
59
	private $users = [];
0 ignored issues
show
introduced by
The private property $users is not used, and could be removed.
Loading history...
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64
	protected function setUp(): void {
65
		parent::setUp();
66
		$this->con = \OC::$server->getDatabaseConnection();
67
		$this->voteMapper = new VoteMapper($this->con);
68
		$this->pollMapper = new PollMapper($this->con);
69
		$this->optionMapper = new OptionMapper($this->con);
70
71
		$this->polls = [
72
			$this->fm->instance('OCA\Polls\Db\Poll')
73
		];
74
75
		foreach ($this->polls as &$poll) {
76
			$poll = $this->pollMapper->insert($poll);
77
78
			for ($optionsCount=0; $optionsCount < 2; $optionsCount++) {
79
				$option = $this->fm->instance('OCA\Polls\Db\Option');
80
				$option->setPollId($poll->getId());
81
				array_push($this->options, $this->optionMapper->insert($option));
82
				$vote = $this->fm->instance('OCA\Polls\Db\Vote');
83
				$vote->setPollId($option->getPollId());
84
				$vote->setUserId('voter');
85
				$vote->setVoteOptionText($option->getPollOptionText());
86
				array_push($this->votes, $this->voteMapper->insert($vote));
87
			}
88
		}
89
		unset($poll);
90
	}
91
92
93
	/**
94
	 * testFindByPoll
95
	 */
96
	public function testFindByPoll() {
97
		foreach ($this->polls as $poll) {
98
			$this->assertTrue(count($this->voteMapper->findByPoll($poll->getId())) > 0);
99
		}
100
	}
101
102
	/**
103
	 * testFindByPollAndUser
104
	 */
105
	public function testFindByPollAndUser() {
106
		foreach ($this->polls as $poll) {
107
			$this->assertTrue(count($this->voteMapper->findByPollAndUser($poll->getId(), 'voter')) > 0);
108
		}
109
	}
110
111
	/**
112
	 * testFindSingleVote
113
	 */
114
	public function testFindSingleVote() {
115
		foreach ($this->votes as $vote) {
116
			$this->assertInstanceOf(Vote::class, $this->voteMapper->findSingleVote($vote->getPollId(), $vote->getVoteOptionText(), $vote->getUserId()));
117
		}
118
	}
119
120
	/**
121
	 * testParticipantsByPoll
122
	 */
123
	public function testParticipantsByPoll() {
124
		foreach ($this->polls as $poll) {
125
			$this->assertTrue(count($this->voteMapper->findParticipantsByPoll($poll->getId())) > 0);
126
		}
127
	}
128
129
	/**
130
	 * testParticipantsByPoll
131
	 */
132
	public function testFindParticipantsVotes() {
133
		foreach ($this->votes as $vote) {
134
			$this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getPollId(), $vote->getUserId())) > 0);
135
		}
136
	}
137
138
	/**
139
	* testUpdate
140
	 */
141
	public function testUpdate() {
142
		foreach ($this->votes as &$vote) {
143
			$vote->setVoteAnswer('no');
144
			$this->assertInstanceOf(Vote::class, $this->voteMapper->update($vote));
145
		}
146
		unset($vote);
147
	}
148
149
	/**
150
	 * testDeleteByPollAndUser
151
	 */
152
	public function testDeleteByPollAndUser() {
153
		foreach ($this->polls as $poll) {
154
			$this->assertTrue($this->voteMapper->deleteByPollAndUser($poll->getId(), 'voter'));
155
		}
156
	}
157
158
	/**
159
	* tearDown
160
	*/
161
	public function tearDown(): void {
162
		parent::tearDown();
163
		foreach ($this->options as $option) {
164
			$this->optionMapper->delete($option);
165
		}
166
		foreach ($this->polls as $poll) {
167
			$this->pollMapper->delete($poll);
168
		}
169
	}
170
}
171