Passed
Pull Request — master (#1257)
by René
05:52 queued 01:59
created

OptionMapperTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 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\Tests\Unit\Db;
25
26
use OCA\Polls\Db\Poll;
27
use OCA\Polls\Db\PollMapper;
28
use OCA\Polls\Db\Option;
29
use OCA\Polls\Db\OptionMapper;
30
use OCA\Polls\Tests\Unit\UnitTestCase;
31
use OCP\IDBConnection;
32
use League\FactoryMuffin\Faker\Facade as Faker;
33
34
class OptionMapperTest extends UnitTestCase {
35
36
	/** @var IDBConnection */
37
	private $con;
38
39
	/** @var OptionMapper */
40
	private $optionMapper;
41
42
	/** @var PollMapper */
43
	private $pollMapper;
44
45
	/** @var array */
46
	private $polls;
47
48
	/** @var array */
49
	private $options;
50
51
	/**
52
	 * {@inheritDoc}
53
	 */
54
	protected function setUp(): void {
55
		parent::setUp();
56
		$this->con = \OC::$server->getDatabaseConnection();
57
		$this->optionMapper = new OptionMapper($this->con);
58
		$this->pollMapper = new PollMapper($this->con);
59
		$this->polls = [];
60
		$this->options = [];
61
62
		$poll = $this->fm->instance('OCA\Polls\Db\Poll');
63
		$this->polls[] = $this->pollMapper->insert($poll);
64
	}
65
66
	/**
67
	 * Create some fake data and persist them to the database.
68
	 */
69
	public function testCreate() {
70
		/** @var Option $option */
71
72
		foreach ($this->polls as $poll) {
73
			$option = $this->fm->instance('OCA\Polls\Db\Option');
74
75
			$option->setPollId($poll->getId());
76
			$this->optionMapper->insert($option);
77
			$this->options[] = $option;
78
			$this->assertInstanceOf(Option::class, $option);
79
		}
80
81
	}
82
83
	/**
84
	 * Find the previously created entries from the database.
85
	 *
86
	 * @depends testCreate
87
	 */
88
	public function testFind() {
89
		foreach ($this->options as $option) {
90
			$this->assertEquals($option, $this->optionMapper->find($option->getId()));
91
		}
92
	}
93
	/**
94
	 * Find the previously created entries from the database.
95
	 *
96
	 * @depends testCreate
97
	 * @return Option
98
	 */
99
	public function testFindByPoll() {
100
		foreach ($this->options as $option) {
101
			$this->assertContains($option, $this->optionMapper->findByPoll($option->getId()));
102
		}
103
	}
104
105
	/**
106
	 * Update the previously created entry and persist the changes.
107
	 *
108
	 * @depends testCreate
109
	 */
110
	public function testUpdate() {
111
		foreach ($this->options as $option) {
112
			$newPollOptionText = Faker::text(255);
113
			$option->setPollOptionText($newPollOptionText());
114
			$this->assertEquals($option, $this->optionMapper->update($option));
115
		}
116
	}
117
118
	/**
119
	 * Delete the previously created entries from the database.
120
	 *
121
	 * @depends testUpdate
122
	 */
123
	public function testDelete() {
124
		foreach ($this->options as $option) {
125
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
126
		}
127
	}
128
129
	public function tearDown(): void {
130
		parent::tearDown();
131
		foreach ($this->polls as $poll) {
132
			$this->pollMapper->delete($poll);
133
		}
134
	}
135
}
136