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

OptionMapperTest::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\Db;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\IDBConnection;
28
use Test\AppFramework\Db\MapperTestUtility;
0 ignored issues
show
Bug introduced by
The type Test\AppFramework\Db\MapperTestUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
use OCA\Polls\Db\Poll;
31
use OCA\Polls\Db\PollMapper;
32
use OCA\Polls\Db\Option;
33
use OCA\Polls\Db\OptionMapper;
34
35
/**
36
 * @group DB
37
 */
38
class OptionMapperTest extends MapperTestUtility {
39
40
	/** @var IDBConnection */
41
	private $con;
42
43
	/** @var OptionMapper|\PHPUnit\Framework\MockObject\MockObject */
44
	private $optionMapper;
45
46
	/** @var PollMapper|\PHPUnit\Framework\MockObject\MockObject */
47
	private $pollMapper;
48
49
	/** @var array */
50
	private $polls;
51
52
	/** @var array */
53
	private $options;
54
55
	/** @var array */
56
	private $pollsById;
57
58
	/** @var array */
59
	private $optionsById;
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64
	protected function setUp(): void {
65
		parent::setUp();
66
		$this->con = \OC::$server->getDatabaseConnection();
67
68
		$this->optionMapper = new OptionMapper($this->con);
69
		$this->pollMapper = new PollMapper($this->con);
70
71
		$this->polls = [];
72
		$this->comments = [];
0 ignored issues
show
Bug Best Practice introduced by
The property comments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
74
		$this->polls = [
75
			$this->createPollEntity(Poll::TYPE_TEXT, 'Poll Title', 'admin')
76
		];
77
78
		foreach ($this->polls as $poll) {
79
			$entry = $this->pollMapper->insert($poll);
80
			$entry->resetUpdatedFields();
81
			$this->pollsById[$entry->getId()] = $entry;
82
		}
83
84
		foreach ($this->pollsById as $id => $polls) {
85
			$this->options = [
86
				$this->createOptionEntity($id, 'Option 1', 1),
87
				$this->createOptionEntity($id, 'Option 2', 2),
88
				$this->createOptionEntity($id, 'Option 3', 3)
89
			];
90
		}
91
92
		foreach ($this->options as $option) {
93
			$entry = $this->optionMapper->insert($option);
94
			$entry->resetUpdatedFields();
95
			$this->optionsById[$entry->getId()] = $entry;
96
		}
97
98
	}
99
100
	private function createPollEntity($type, $title, $owner) {
101
		$poll = new Poll();
102
		$poll->setType($type);
103
		$poll->setCreated(time());
104
		$poll->setOwner($owner);
105
		$poll->setTitle($title);
106
		$poll->setDescription('Description');
107
		$poll->setAccess(Poll::ACCESS_PUBLIC);
108
		$poll->setExpire(0);
109
		$poll->setAnonymous(0);
110
		$poll->setFullAnonymous(0);
111
		$poll->setAllowMaybe(0);
112
		$poll->setVoteLimit(0);
113
		$poll->setSettings('{"someJSON":0}');
114
		$poll->setOptions('["yes","no","maybe"]');
115
		$poll->setShowResults(Poll::SHOW_RESULTS_ALWAYS);
116
		$poll->setDeleted(0);
117
		$poll->setAdminAccess(0);
118
		$poll->setImportant(0);
119
		return $poll;
120
	}
121
122
	private function createOptionEntity($pollId, $pollOptionText, $order) {
123
		$option = new Option();
124
		$option->setPollId($pollId);
125
		$option->setPollOptionText($pollOptionText);
126
		$option->setTimestamp(0);
127
		$option->setOrder($order);
128
		$option->setconfirmed(0);
129
		return $option;
130
	}
131
132
	/**
133
	 * Find the previously created entries from the database.
134
	 */
135
	public function testFind() {
136
		foreach ($this->optionsById as $id => $option) {
137
			$this->assertEquals($option, $this->optionMapper->find($id));
138
		}
139
	}
140
141
	/**
142
	 * Find the previously created entries from the database.
143
	 */
144
	public function testFindByPoll() {
145
		foreach ($this->pollsById as $id => $poll) {
146
			$this->assertTrue(count($this->optionMapper->findByPoll($id)) > 0);
147
		}
148
	}
149
150
	/**
151
	 * Update the previously created entry and persist the changes.
152
	 */
153
	public function testUpdate() {
154
		foreach ($this->optionsById as $id => $option) {
155
			$found = $this->optionMapper->find($id);
156
			$found->setPollOptionText('Changed option');
157
			$this->assertEquals($found, $this->optionMapper->update($found));
158
		}
159
	}
160
161
	/**
162
	 * Delete the previously created entries from the database.
163
	 */
164
	public function testDelete() {
165
		foreach ($this->optionsById as $id => $option) {
166
			$found = $this->optionMapper->find($id);
167
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($found));
168
		}
169
	}
170
171
	public function tearDown(): void {
172
		parent::tearDown();
173
		foreach ($this->polls as $poll) {
174
			$this->pollMapper->delete($poll);
175
		}
176
	}
177
}
178