Passed
Pull Request — master (#1269)
by René
03:39
created

CommentMapperTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Tests\Unit\Db;
25
26
use OCA\Polls\Db\Comment;
27
use OCA\Polls\Db\CommentMapper;
28
use OCA\Polls\Db\Poll;
29
use OCA\Polls\Db\PollMapper;
30
use OCA\Polls\Tests\Unit\UnitTestCase;
31
use OCP\IDBConnection;
32
use League\FactoryMuffin\Faker\Facade as Faker;
33
34
class CommentMapperTest extends UnitTestCase {
35
36
	/** @var IDBConnection */
37
	private $con;
38
39
	/** @var CommentMapper */
40
	private $commentMapper;
41
42
	/** @var PollMapper */
43
	private $pollMapper;
44
45
	/** @var array */
46
	private $polls;
47
48
	/** @var array */
49
	private $comments;
50
51
	/** @var array */
52
	private $pollsById;
53
54
	/** @var array */
55
	private $commentsById;
56
57
	/**
58
	 * {@inheritDoc}
59
	 */
60
	protected function setUp(): void {
61
		parent::setUp();
62
		$this->con = \OC::$server->getDatabaseConnection();
63
		$this->commentMapper = new CommentMapper($this->con);
64
		$this->pollMapper = new PollMapper($this->con);
65
		$this->polls = [
66
			$this->fm->instance('OCA\Polls\Db\Poll')
67
		];
68
69
		foreach ($this->polls as $poll) {
70
			$entry = $this->pollMapper->insert($poll);
71
			$entry->resetUpdatedFields();
72
			$this->pollsById[$entry->getId()] = $entry;
73
		}
74
75
		foreach ($this->pollsById as $id => $polls) {
76
			for ($count=0; $count < 2; $count++) {
77
				$comment = $this->fm->instance('OCA\Polls\Db\Comment');
78
				$comment->setPollId($id);
79
				array_push($this->comments, $comment);
80
			}
81
		}
82
83
		foreach ($this->comments as $comment) {
84
			$entry = $this->commentMapper->insert($comment);
85
			$entry->resetUpdatedFields();
86
			$this->commentsById[$entry->getId()] = $entry;
87
		}
88
89
	}
90
91
	 /**
92
 	 * Find the previously created entries from the database.
93
 	 */
94
 	public function testFind() {
95
 		foreach ($this->commentsById as $id => $comment) {
96
 			$this->assertEquals($comment, $this->commentMapper->find($id));
97
 		}
98
 	}
99
100
	/**
101
	 * Find the previously created entries from the database.
102
	 */
103
	public function testFindByPoll() {
104
		foreach ($this->pollsById as $id => $poll) {
105
			$this->assertTrue(count($this->commentMapper->findByPoll($id)) > 0);
106
		}
107
	}
108
109
	/**
110
	 * Update the previously created entry and persist the changes.
111
	 */
112
	public function testUpdate() {
113
		foreach ($this->commentsById as $id => $comment) {
114
			$found = $this->commentMapper->find($id);
115
			$newComment = Faker::paragraph();
116
			$found->setComment($newComment());
117
			$this->assertEquals($found, $this->commentMapper->update($found));
118
		}
119
	}
120
121
	/**
122
	 * Delete the previously created entries from the database.
123
	 */
124
	public function testDelete() {
125
		foreach ($this->commentsById as $id => $comment) {
126
			$found = $this->commentMapper->find($id);
127
			$this->assertInstanceOf(Option::class, $this->commentMapper->delete($found));
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Tests\Unit\Db\Option 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...
128
		}
129
	}
130
131
	public function tearDown(): void {
132
		parent::tearDown();
133
		foreach ($this->polls as $poll) {
134
			$this->pollMapper->delete($poll);
135
		}
136
	}
137
138
}
139