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

OptionMapperTest::testFind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
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\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->createPollEntity(Poll::TYPE_TEXT, 'Poll Title', 'admin')
73
		];
74
75
		foreach ($this->polls as $poll) {
76
			$entry = $this->pollMapper->insert($poll);
77
			$entry->resetUpdatedFields();
78
			$this->pollsById[$entry->getId()] = $entry;
79
		}
80
81
		foreach ($this->pollsById as $id => $polls) {
82
			$this->options = [
83
				$this->createOptionEntity($id, 'Option 1', 1),
84
				$this->createOptionEntity($id, 'Option 2', 2),
85
				$this->createOptionEntity($id, 'Option 3', 3)
86
			];
87
		}
88
89
		foreach ($this->options as $option) {
90
			$entry = $this->optionMapper->insert($option);
91
			$entry->resetUpdatedFields();
92
			$this->optionsById[$entry->getId()] = $entry;
93
		}
94
95
	}
96
97
	private function createPollEntity($type, $title, $owner) {
98
		$poll = new Poll();
99
		$poll->setType($type);
100
		$poll->setCreated(time());
101
		$poll->setOwner($owner);
102
		$poll->setTitle($title);
103
		$poll->setDescription('Description');
104
		$poll->setAccess(Poll::ACCESS_PUBLIC);
105
		$poll->setExpire(0);
106
		$poll->setAnonymous(0);
107
		$poll->setFullAnonymous(0);
108
		$poll->setAllowMaybe(0);
109
		$poll->setVoteLimit(0);
110
		$poll->setSettings('{"someJSON":0}');
111
		$poll->setOptions('["yes","no","maybe"]');
112
		$poll->setShowResults(Poll::SHOW_RESULTS_ALWAYS);
113
		$poll->setDeleted(0);
114
		$poll->setAdminAccess(0);
115
		$poll->setImportant(0);
116
		return $poll;
117
	}
118
119
	private function createOptionEntity($pollId, $pollOptionText, $order) {
120
		$option = new Option();
121
		$option->setPollId($pollId);
122
		$option->setPollOptionText($pollOptionText);
123
		$option->setTimestamp(0);
124
		$option->setOrder($order);
125
		$option->setconfirmed(0);
126
		return $option;
127
	}
128
129
	/**
130
	 * Find the previously created entries from the database.
131
	 */
132
	public function testFind() {
133
		foreach ($this->optionsById as $id => $option) {
134
			$this->assertEquals($option, $this->optionMapper->find($id));
135
		}
136
	}
137
138
	/**
139
	 * Find the previously created entries from the database.
140
	 */
141
	public function testFindByPoll() {
142
		foreach ($this->pollsById as $id => $poll) {
143
			$this->assertTrue(count($this->optionMapper->findByPoll($id)) > 0);
144
		}
145
	}
146
147
	/**
148
	 * Update the previously created entry and persist the changes.
149
	 */
150
	public function testUpdate() {
151
		foreach ($this->optionsById as $id => $option) {
152
			$found = $this->optionMapper->find($id);
153
			$found->setPollOptionText('Changed option');
154
			$this->assertEquals($found, $this->optionMapper->update($found));
155
		}
156
	}
157
158
	/**
159
	 * Delete the previously created entries from the database.
160
	 */
161
	public function testDelete() {
162
		foreach ($this->optionsById as $id => $option) {
163
			$found = $this->optionMapper->find($id);
164
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($found));
165
		}
166
	}
167
168
	public function tearDown(): void {
169
		parent::tearDown();
170
		foreach ($this->polls as $poll) {
171
			$this->pollMapper->delete($poll);
172
		}
173
	}
174
}
175