Passed
Pull Request — master (#1257)
by René
03:23
created

OptionMapperTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
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
use League\FactoryMuffin\Faker\Facade as Faker;
30
31
use OCA\Polls\Db\Poll;
32
use OCA\Polls\Db\PollMapper;
33
use OCA\Polls\Db\Option;
34
use OCA\Polls\Db\OptionMapper;
35
36
/**
37
 * @group DB
38
 */
39
class OptionMapperTest extends MapperTestUtility {
40
41
	/** @var IDBConnection */
42
	private $con;
43
44
	/** @var OptionMapper|\PHPUnit\Framework\MockObject\MockObject */
45
	private $optionMapper;
46
47
	/** @var PollMapper|\PHPUnit\Framework\MockObject\MockObject */
48
	private $pollMapper;
49
50
	/** @var array */
51
	private $polls;
52
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, Faker::text(255), 'admin')
80
		];
81
82
		foreach ($this->polls as $poll) {
83
			$entry = $this->pollMapper->insert($poll);
84
			$entry->resetUpdatedFields();
85
			$this->pollById[$entry->getId()] = $entry;
0 ignored issues
show
Bug Best Practice introduced by
The property pollById does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
		}
87
88
		$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...
89
			$this->createOptionEntity(1, Faker::text(255), 1),
90
			$this->createOptionEntity(1, Faker::text(255), 2),
91
			$this->createOptionEntity(1, Faker::text(255), 3)
92
93
		];
94
		foreach ($this->options as $option) {
95
			$entry = $this->optionMapper->insert($option);
96
			$entry->resetUpdatedFields();
97
			$this->optionById[$entry->getId()] = $entry;
0 ignored issues
show
Bug Best Practice introduced by
The property optionById does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
		}
99
100
	}
101
102
	private function createPollEntity($type, $title, $owner) {
103
		$poll = new Poll();
104
		$poll->setType($type);
105
		$poll->setCreated(time());
106
		$poll->setOwner($owner);
107
		$poll->setTitle($title);
108
		$poll->setDescription(Faker::text(255));
0 ignored issues
show
Bug introduced by
It seems like League\FactoryMuffin\Faker\Facade::text(255) can also be of type Closure; however, parameter $value of OCA\Polls\Db\Poll::setDescription() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

108
		$poll->setDescription(/** @scrutinizer ignore-type */ Faker::text(255));
Loading history...
109
		$poll->setAccess(Poll::ACCESS_PUBLIC);
110
		$poll->setExpire(0);
111
		$poll->setAnonymous(0);
112
		$poll->setFullAnonymous(0);
113
		$poll->setAllowMaybe(0);
114
		$poll->setVoteLimit(0);
115
		$poll->setSettings('{"someJSON":0}');
116
		$poll->setOptions('["yes","no","maybe"]');
117
		$poll->setShowResults(Poll::SHOW_RESULTS_ALWAYS);
118
		$poll->setDeleted(0);
119
		$poll->setAdminAccess(0);
120
		$poll->setImportant(0);
121
		return $poll;
122
	}
123
124
	private function createOptionEntity($pollId, $pollOptionText, $order) {
125
		$option = new Option();
126
		$option->setPollId($pollId);
127
		$option->setType($pollOptionText);
0 ignored issues
show
Bug introduced by
The method setType() does not exist on OCA\Polls\Db\Option. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

127
		$option->/** @scrutinizer ignore-call */ 
128
           setType($pollOptionText);
Loading history...
128
		$option->setTimestamp(time());
129
		$option->setOrder($order);
130
		$option->setconfirmed(0);
131
		return $option;
132
	}
133
134
	/**
135
	 * Find the previously created entries from the database.
136
	 */
137
	public function testFind(array $options) {
138
		foreach ($this->options as $id => $option) {
139
			$this->assertEquals($option, $this->optionMapper->find($id));
140
		}
141
	}
142
143
	/**
144
	 * Find the previously created entries from the database.
145
	 */
146
	public function testFindByPoll(array $options) {
147
		foreach ($polls as $id => $poll) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $polls seems to be never defined.
Loading history...
148
			$this->assertTrue(count($this->optionMapper->findByPoll($id)) > 0);
149
		}
150
	}
151
152
	/**
153
	 * Update the previously created entry and persist the changes.
154
	 */
155
	public function testUpdate(array $options) {
156
		foreach ($options as $option) {
157
			$newPollOptionText = Faker::text(255);
0 ignored issues
show
Unused Code introduced by
The assignment to $newPollOptionText is dead and can be removed.
Loading history...
158
			$option->setPollOptionText(Faker::text(255));
159
			$this->assertEquals($option, $this->optionMapper->update($option));
160
		}
161
	}
162
163
	/**
164
	 * Delete the previously created entries from the database.
165
	 *
166
	 * @depends testUpdate
167
	 */
168
	public function testDelete(array $options) {
169
		foreach ($options as $option) {
170
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
171
		}
172
	}
173
174
	public function tearDown(): void {
175
		parent::tearDown();
176
		foreach ($this->polls as $poll) {
177
			$this->pollMapper->delete($poll);
178
		}
179
	}
180
}
181