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

OptionMapperTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 13
eloc 34
dl 0
loc 92
rs 10
c 2
b 1
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 2
A testFindByPoll() 0 3 2
A testDelete() 0 3 2
A testUpdate() 0 9 2
A testFind() 0 3 2
A setUp() 0 20 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 League\FactoryMuffin\Faker\Facade as Faker;
27
use OCP\IDBConnection;
28
use OCA\Polls\Tests\Unit\UnitTestCase;
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
class OptionMapperTest extends UnitTestCase {
36
37
	/** @var IDBConnection */
38
	private $con;
39
40
	/** @var OptionMapper */
41
	private $optionMapper;
42
43
	/** @var PollMapper */
44
	private $pollMapper;
45
46
	/** @var array */
47
	private $polls = [];
48
49
	/** @var array */
50
	private $options = [];
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	protected function setUp(): void {
56
		parent::setUp();
57
		$this->con = \OC::$server->getDatabaseConnection();
58
		$this->optionMapper = new OptionMapper($this->con);
59
		$this->pollMapper = new PollMapper($this->con);
60
61
		$this->polls = [
62
			$this->fm->instance('OCA\Polls\Db\Poll')
63
		];
64
65
		foreach ($this->polls as &$poll) {
66
			$poll = $this->pollMapper->insert($poll);
67
68
			for ($count=0; $count < 2; $count++) {
69
				$option = $this->fm->instance('OCA\Polls\Db\Option');
70
				$option->setPollId($poll->getId());
71
				array_push($this->options, $this->optionMapper->insert($option));
72
			}
73
		}
74
		unset($poll);
75
	}
76
77
	/**
78
	 * testFind
79
	 */
80
	public function testFind() {
81
		foreach ($this->options as $option) {
82
			$this->assertEquals($option, $this->optionMapper->find($option->getId()));
83
		}
84
	}
85
86
	/**
87
	 * testFindByPoll
88
	 */
89
	public function testFindByPoll() {
90
		foreach ($this->polls as $poll) {
91
			$this->assertTrue(count($this->optionMapper->findByPoll($poll->getId())) > 0);
92
		}
93
	}
94
95
	/**
96
	 * testUpdate
97
	 * includes testFind
98
	 */
99
	public function testUpdate() {
100
		foreach ($this->options as &$option) {
101
			$before = $this->optionMapper->find($option->getId());
102
			$this->assertEquals($option, $before);
103
104
			$option->setPollOptionText('Changed option');
105
106
			$this->assertEquals($option, $this->optionMapper->update($option));
107
			$this->assertNotEquals($before, $this->optionMapper->find($option->getId()));
108
		}
109
	}
110
111
	/**
112
	 * testDelete
113
	 */
114
	public function testDelete() {
115
		foreach ($this->options as $option) {
116
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
117
		}
118
	}
119
120
	/**
121
	 * tearDown
122
	 */
123
	public function tearDown(): void {
124
		parent::tearDown();
125
		foreach ($this->polls as $poll) {
126
			$this->pollMapper->delete($poll);
127
		}
128
	}
129
}
130