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

OptionMapperTest::createOptionEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
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
	private $faker;
54
	/**
55
	 * {@inheritDoc}
56
	 */
57
	protected function setUp(): void {
58
		parent::setUp();
59
		$this->faker = Faker\Factory::create();
0 ignored issues
show
Bug introduced by
The type League\FactoryMuffin\Faker\Facade\Factory 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...
60
		$this->con = \OC::$server->getDatabaseConnection();
61
62
		$this->optionMapper = new OptionMapper($this->con);
63
		$this->pollMapper = new PollMapper($this->con);
64
65
		$yesterdayTs = function () {
0 ignored issues
show
Unused Code introduced by
The assignment to $yesterdayTs is dead and can be removed.
Loading history...
66
			$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...
67
			return $date->getTimestamp();
68
		};
69
70
		$todayTs = function () {
0 ignored issues
show
Unused Code introduced by
The assignment to $todayTs is dead and can be removed.
Loading history...
71
			$date = new DateTime('today');
72
			return $date->getTimestamp();
73
		};
74
75
		$todayTs = function () {
76
			$date = new DateTime('tomorrow');
77
			return $date->getTimestamp();
78
		};
79
80
		$this->polls = [
81
			$this->createPollEntity(Poll::TYPE_TEXT, $this->faker->words(8), $this->faker->firstNameMale())
82
		];
83
84
		foreach ($this->polls as $poll) {
85
			$entry = $this->pollMapper->insert($poll);
86
			$entry->resetUpdatedFields();
87
			$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...
88
		}
89
90
		$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...
91
			$this->createOptionEntity(1, $this->faker->words(4), 1),
92
			$this->createOptionEntity(1, $this->faker->words(4), 2),
93
			$this->createOptionEntity(1, $this->faker->words(4), 3)
94
95
		];
96
		foreach ($this->options as $option) {
97
			$entry = $this->optionMapper->insert($option);
98
			$entry->resetUpdatedFields();
99
			$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...
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($this->faker->words(12));
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->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

129
		$option->/** @scrutinizer ignore-call */ 
130
           setType($pollOptionText);
Loading history...
130
		$option->setTimestamp(time());
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(array $options) {
140
		foreach ($this->options 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(array $options) {
149
		foreach ($polls as $id => $poll) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $polls seems to be never defined.
Loading history...
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(array $options) {
158
		foreach ($options as $option) {
159
			$newPollOptionText = $this->faker->words(2);
160
			$option->setPollOptionText($newPollOptionText);
161
			$this->assertEquals($option, $this->optionMapper->update($option));
162
		}
163
	}
164
165
	/**
166
	 * Delete the previously created entries from the database.
167
	 *
168
	 * @depends testUpdate
169
	 */
170
	public function testDelete(array $options) {
171
		foreach ($options as $option) {
172
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
173
		}
174
	}
175
176
	public function tearDown(): void {
177
		parent::tearDown();
178
		foreach ($this->polls as $poll) {
179
			$this->pollMapper->delete($poll);
180
		}
181
	}
182
}
183