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

OptionMapperTest::testFindByPoll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 4
c 5
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
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
	/**
49
	 * {@inheritDoc}
50
	 */
51
	protected function setUp(): void {
52
		parent::setUp();
53
		$this->con = \OC::$server->getDatabaseConnection();
54
		$this->optionMapper = new OptionMapper($this->con);
55
		$this->pollMapper = new PollMapper($this->con);
56
		$this->polls = [];
57
58
		for ($pollCount=0; $pollCount < 2; $pollCount++) {
59
			$poll = $this->fm->instance('OCA\Polls\Db\Poll');
60
			array_push($this->polls, $this->pollMapper->insert($poll));
61
			print 'added poll ';
62
			var_dump($poll->getId());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($poll->getId()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
63
		}
64
	}
65
66
	/**
67
	 * Create some fake data and persist them to the database.
68
	 */
69
	public function testCreate() {
70
		$options = [];
71
72
		foreach ($this->polls as $poll) {
73
			/** @var Option $option */
74
			$option = $this->fm->instance('OCA\Polls\Db\Option');
75
76
			$option->setPollId($poll->getId());
77
			$option = $this->optionMapper->insert($option);
78
			array_push($options, $option);
79
			$this->assertInstanceOf(Option::class, $option);
80
			print 'added option ';
81
			var_dump($option->getId());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($option->getId()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
		}
83
		return $options;
84
	}
85
86
	/**
87
	 * Find the previously created entries from the database.
88
	 *
89
	 * @depends testCreate
90
	 * @return Option[]
91
	 */
92
	public function testFind(array $options) {
93
		foreach ($options as $option) {
94
			print 'try find option ';
95
			var_dump($option->getId());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($option->getId()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
96
			$this->assertInstanceOf(Option::class, $this->optionMapper->find($option->getId()));
97
		}
98
	}
99
100
	/**
101
	 * Find the previously created entries from the database.
102
	 *
103
	 * @depends testCreate
104
	 */
105
	public function testFindByPoll(array $options) {
106
		foreach ($options as $option) {
107
			print 'try find options of poll ';
108
			var_dump($option->getPollId());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($option->getPollId()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
109
110
			$this->assertTrue(count($this->optionMapper->findByPoll($option->getPollId())) > 0);
111
		}
112
	}
113
114
	/**
115
	 * Update the previously created entry and persist the changes.
116
	 *
117
	 * @depends testCreate
118
	 * @return Option[]
119
	 */
120
	public function testUpdate(array $options) {
121
		foreach ($options as $option) {
122
			$newPollOptionText = Faker::text(255);
123
			$option->setPollOptionText($newPollOptionText());
124
			$this->assertEquals($option, $this->optionMapper->update($option));
125
		}
126
		return $options;
127
	}
128
129
	/**
130
	 * Delete the previously created entries from the database.
131
	 *
132
	 * @depends testUpdate
133
	 */
134
	public function testDelete(array $options) {
135
		foreach ($options as $option) {
136
			$this->assertInstanceOf(Option::class, $this->optionMapper->delete($option));
137
		}
138
	}
139
140
	public function tearDown(): void {
141
		parent::tearDown();
142
		foreach ($this->polls as $poll) {
143
			$this->pollMapper->delete($poll);
144
		}
145
	}
146
}
147