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

OptionMapperTest::testCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 2
nc 2
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\Db;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\IDBConnection;
28
use Test\AppFramework\Db\MapperTestUtility;
0 ignored issues
show
Bug introduced by
The type Test\AppFramework\Db\MapperTestUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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