Passed
Pull Request — master (#1257)
by René
03:41
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;
29
use League\FactoryMuffin\Faker\Facade as Faker;
30
31
use OCP\AppFramework\Db\DoesNotExistException;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use OCP\AppFramework\Db\DoesNotExistException as DoesNotExistException because the name is already in use
Loading history...
32
use OCP\IDBConnection;
33
use Test\AppFramework\Db\MapperTestUtility;
34
use League\FactoryMuffin\Faker\Facade as Faker;
35
36
use OCA\Polls\Db\Poll;
37
use OCA\Polls\Db\PollMapper;
38
use OCA\Polls\Db\Option;
39
use OCA\Polls\Db\OptionMapper;
40
41
class OptionMapperTest extends MapperTestUtility {
42
43
	/** @var IDBConnection */
44
	private $con;
45
46
	/** @var OptionMapper */
47
	private $optionMapper;
48
49
	/** @var PollMapper */
50
	private $pollMapper;
51
52
	/** @var array */
53
	private $polls;
54
55
	/**
56
	 * {@inheritDoc}
57
	 */
58
	protected function setUp(): void {
59
		parent::setUp();
60
		$this->con = \OC::$server->getDatabaseConnection();
61
62
		$this->optionMapper = new OptionMapper($this->con);
63
		$this->pollMapper = new PollMapper($this->con);
64
		$this->polls = [];
65
66
		for ($pollCount=0; $pollCount < 2; $pollCount++) {
67
			$poll = $this->pollMapper->insert($this->fm->instance('OCA\Polls\Db\Poll'));
68
			array_push($this->polls, $poll);
69
			print 'added poll ';
70
			var_dump($poll->getId());
71
		}
72
	}
73
74
	/**
75
	 * Create some fake data and persist them to the database.
76
	 */
77
	public function testCreate() {
78
		$options = [];
79
80
		foreach ($this->polls as $poll) {
81
			/** @var Option $option */
82
			$option = $this->fm->instance('OCA\Polls\Db\Option');
83
84
			$option->setPollId($poll->getId());
85
			$option = $this->optionMapper->insert($option);
86
			array_push($options, $option);
87
			$this->assertInstanceOf(Option::class, $option);
88
			print 'added option ';
89
			var_dump($option->getId());
90
		}
91
		return $options;
92
	}
93
94
	/**
95
	 * Find the previously created entries from the database.
96
	 *
97
	 * @depends testCreate
98
	 * @return Option[]
99
	 */
100
	public function testFind(array $options) {
101
		foreach ($options as $option) {
102
			print 'try find option ';
103
			var_dump($option->getId());
104
			$this->assertInstanceOf(Option::class, $this->optionMapper->find($option->getId()));
105
		}
106
	}
107
108
	/**
109
	 * Find the previously created entries from the database.
110
	 *
111
	 * @depends testCreate
112
	 */
113
	public function testFindByPoll(array $options) {
114
		foreach ($options as $option) {
115
			print 'try find options of poll ';
116
			var_dump($option->getPollId());
117
118
			$this->assertTrue(count($this->optionMapper->findByPoll($option->getPollId())) > 0);
119
		}
120
	}
121
122
	/**
123
	 * Update the previously created entry and persist the changes.
124
	 *
125
	 * @depends testCreate
126
	 * @return Option[]
127
	 */
128
	public function testUpdate(array $options) {
129
		foreach ($options as $option) {
130
			$newPollOptionText = Faker::text(255);
131
			$option->setPollOptionText($newPollOptionText());
132
			$this->assertEquals($option, $this->optionMapper->update($option));
133
		}
134
		return $options;
135
	}
136
137
	/**
138
	 * Delete the previously created entries from the database.
139
	 *
140
	 * @depends testUpdate
141
	 */
142
	public function testDelete(array $options) {
143
		foreach ($options as $option) {
144
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
145
		}
146
	}
147
148
	public function tearDown(): void {
149
		parent::tearDown();
150
		foreach ($this->polls as $poll) {
151
			$this->pollMapper->delete($poll);
152
		}
153
	}
154
}
155