Passed
Pull Request — master (#1257)
by René
05:39 queued 02:04
created

OptionMapperTest::testDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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