OptionMapperTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 32
dl 0
loc 88
rs 10
c 0
b 0
f 0

6 Methods

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