Passed
Pull Request — master (#1269)
by René
03:19
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
	private $pollsById;
52
	private $optionsById;
53
	/**
54
	 * {@inheritDoc}
55
	 */
56
	protected function setUp(): void {
57
		parent::setUp();
58
		$this->con = \OC::$server->getDatabaseConnection();
59
60
		$this->optionMapper = new OptionMapper($this->con);
61
		$this->pollMapper = new PollMapper($this->con);
62
63
		$yesterdayTs = function () {
0 ignored issues
show
Unused Code introduced by
The assignment to $yesterdayTs is dead and can be removed.
Loading history...
64
			$date = new DateTime('yesterday');
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Db\DateTime was not found. Did you mean DateTime? If so, make sure to prefix the type with \.
Loading history...
65
			return $date->getTimestamp();
66
		};
67
68
		$todayTs = function () {
0 ignored issues
show
Unused Code introduced by
The assignment to $todayTs is dead and can be removed.
Loading history...
69
			$date = new DateTime('today');
70
			return $date->getTimestamp();
71
		};
72
73
		$todayTs = function () {
74
			$date = new DateTime('tomorrow');
75
			return $date->getTimestamp();
76
		};
77
78
		$this->polls = [
79
			$this->createPollEntity(Poll::TYPE_TEXT, 'Poll Title', 'admin')
80
		];
81
82
		foreach ($this->polls as $poll) {
83
			$entry = $this->pollMapper->insert($poll);
84
			$entry->resetUpdatedFields();
85
			$this->pollsById[$entry->getId()] = $entry;
86
		}
87
88
		foreach ($this->pollsById as $id => $polls) {
89
			$this->options = [
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
				$this->createOptionEntity($id, 'Option 1', 1),
91
				$this->createOptionEntity($id, 'Option 2', 2),
92
				$this->createOptionEntity($id, 'Option 3', 3)
93
			];
94
		}
95
96
		foreach ($this->options as $option) {
97
			$entry = $this->optionMapper->insert($option);
98
			$entry->resetUpdatedFields();
99
			$this->optionsById[$entry->getId()] = $entry;
100
		}
101
102
	}
103
104
	private function createPollEntity($type, $title, $owner) {
105
		$poll = new Poll();
106
		$poll->setType($type);
107
		$poll->setCreated(time());
108
		$poll->setOwner($owner);
109
		$poll->setTitle($title);
110
		$poll->setDescription('Description');
111
		$poll->setAccess(Poll::ACCESS_PUBLIC);
112
		$poll->setExpire(0);
113
		$poll->setAnonymous(0);
114
		$poll->setFullAnonymous(0);
115
		$poll->setAllowMaybe(0);
116
		$poll->setVoteLimit(0);
117
		$poll->setSettings('{"someJSON":0}');
118
		$poll->setOptions('["yes","no","maybe"]');
119
		$poll->setShowResults(Poll::SHOW_RESULTS_ALWAYS);
120
		$poll->setDeleted(0);
121
		$poll->setAdminAccess(0);
122
		$poll->setImportant(0);
123
		return $poll;
124
	}
125
126
	private function createOptionEntity($pollId, $pollOptionText, $order) {
127
		$option = new Option();
128
		$option->setPollId($pollId);
129
		$option->setPollOptionText($pollOptionText);
130
		$option->setTimestamp(0);
131
		$option->setOrder($order);
132
		$option->setconfirmed(0);
133
		return $option;
134
	}
135
136
	/**
137
	 * Find the previously created entries from the database.
138
	 */
139
	public function testFind() {
140
		foreach ($this->optionsById as $id => $option) {
141
			$this->assertEquals($option, $this->optionMapper->find($id));
142
		}
143
	}
144
145
	/**
146
	 * Find the previously created entries from the database.
147
	 */
148
	public function testFindByPoll() {
149
		foreach ($this->pollsById as $id => $poll) {
150
			$this->assertTrue(count($this->optionMapper->findByPoll($id)) > 0);
151
		}
152
	}
153
154
	/**
155
	 * Update the previously created entry and persist the changes.
156
	 */
157
	public function testUpdate() {
158
		foreach ($this->optionsById as $id => $option) {
159
			$found = $this->optionMapper->find($id);
160
			$found->setPollOptionText('Changed option');
161
			$this->assertEquals($found, $this->optionMapper->update($found));
162
		}
163
	}
164
165
	/**
166
	 * Delete the previously created entries from the database.
167
	 */
168
	public function testDelete() {
169
		foreach ($this->optionsById as $id => $option) {
170
			$found = $this->optionMapper->find($id);
171
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($found));
172
		}
173
	}
174
175
	public function tearDown(): void {
176
		parent::tearDown();
177
		foreach ($this->polls as $poll) {
178
			$this->pollMapper->delete($poll);
179
		}
180
	}
181
}
182