Passed
Pull Request — master (#1269)
by René
03:49 queued 22s
created

VoteMapperTest::testDeleteByPollAndUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 2
nc 2
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 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
				$userName = 'voter';
83
				for ($votesCount=0; $votesCount < 2; $votesCount++) {
84
					$vote = $this->fm->instance('OCA\Polls\Db\Vote');
85
					$vote->setPollId($option->getPollId());
86
					$vote->setUserId($userName);
87
					$vote->setVoteOptionText($option->getPollOptionText());
88
					array_push($this->votes, $this->voteMapper->insert($vote));
89
				}
90
			}
91
		}
92
		unset($poll);
93
	}
94
95
96
	/**
97
	 * testFind
98
	 */
99
	public function testFind() {
100
		foreach ($this->votes as $vote) {
101
			$this->assertEquals($vote, $this->voteMapper->find($vote->getId()));
0 ignored issues
show
Bug introduced by
The method find() does not exist on OCA\Polls\Db\VoteMapper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
			$this->assertEquals($vote, $this->voteMapper->/** @scrutinizer ignore-call */ find($vote->getId()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
		}
103
	}
104
105
	/**
106
	 * testFindByPoll
107
	 */
108
	public function testFindByPoll() {
109
		foreach ($this->polls as $poll) {
110
			$this->assertTrue(count($this->voteMapper->findByPoll($poll->getId())) > 0);
111
		}
112
	}
113
114
	/**
115
	 * testFindByPollAndUser
116
	 */
117
	public function testFindByPollAndUser() {
118
		foreach ($this->polls as $poll) {
119
			$this->assertTrue(count($this->voteMapper->findByPollAndUser($poll->getId(), 'voter')) > 0);
120
		}
121
	}
122
123
	/**
124
	 * testFindSingleVote
125
	 */
126
	public function testFindSingleVote() {
127
		foreach ($this->votes as $vote) {
128
			$this->assertEquals($vote, $this->voteMapper->findSingleVote($vote->getPollId(), $vote->getVoteOptionText(), $vote->getUserId()));
129
		}
130
	}
131
132
	/**
133
	 * testParticipantsByPoll
134
	 */
135
	public function testParticipantsByPoll() {
136
		foreach ($this->polls as $poll) {
137
			$this->assertTrue(count($this->voteMapper->findParticipantsByPoll($poll->getId())) > 0);
138
		}
139
	}
140
141
	/**
142
	 * testParticipantsByPoll
143
	 */
144
	public function testFindParticipantsVotes() {
145
		foreach ($this->votes as $vote) {
146
			$this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getUserId())) > 0);
0 ignored issues
show
Bug introduced by
The call to OCA\Polls\Db\VoteMapper::findParticipantsVotes() has too few arguments starting with userId. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
			$this->assertTrue(count($this->voteMapper->/** @scrutinizer ignore-call */ findParticipantsVotes($vote->getUserId())) > 0);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
147
		}
148
	}
149
150
	/**
151
	 * Update the previously created entry and persist the changes.
152
	 *
153
	 * @depends testCreate
154
	 * @param Vote $vote
155
	 * @return Vote
156
	 */
157
	public function testUpdate(Vote $vote) {
158
		$newVoteOptionText = Faker::date('Y-m-d H:i:s');
159
		$vote->setVoteOptionText($newVoteOptionText());
160
		$this->voteMapper->update($vote);
161
162
		return $vote;
163
	}
164
165
	/**
166
	 * testDeleteByPollAndUser
167
	 */
168
	public function testDeleteByPollAndUser() {
169
		foreach ($this->polls as $poll) {
170
			$this->assertTrue($this->voteMapper->deleteByPollAndUser($poll->getId(), 'voter'));
171
		}
172
	}
173
174
	/**
175
	* tearDown
176
	*/
177
	public function tearDown(): void {
178
		parent::tearDown();
179
		foreach ($this->options as $option) {
180
			$this->optionMapper->delete($option);
181
		}
182
		foreach ($this->polls as $poll) {
183
			$this->pollMapper->delete($poll);
184
		}
185
	}
186
}
187