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

CommentMapperTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 44
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDelete() 0 4 2
A testUpdate() 0 6 2
A tearDown() 0 4 2
A testFindByPoll() 0 3 2
A setUp() 0 31 5
A testFind() 0 3 2
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
66
		$this->polls = [];
67
		$this->comments = [];
68
69
		$this->polls = [
70
			$this->fm->instance('OCA\Polls\Db\Poll')
71
		];
72
73
		foreach ($this->polls as $poll) {
74
			$entry = $this->pollMapper->insert($poll);
75
			$entry->resetUpdatedFields();
76
			$this->pollsById[$entry->getId()] = $entry;
77
		}
78
79
		foreach ($this->pollsById as $id => $polls) {
80
			for ($count=0; $count < 2; $count++) {
81
				$comment = $this->fm->instance('OCA\Polls\Db\Comment');
82
				$comment->setPollId($id);
83
				array_push($this->comments, $comment);
84
			}
85
		}
86
87
		foreach ($this->comments as $comment) {
88
			$entry = $this->commentMapper->insert($comment);
89
			$entry->resetUpdatedFields();
90
			$this->commentsById[$entry->getId()] = $entry;
91
		}
92
93
	}
94
95
	 /**
96
 	 * Find the previously created entries from the database.
97
 	 */
98
 	public function testFind() {
99
 		foreach ($this->commentsById as $id => $comment) {
100
 			$this->assertEquals($comment, $this->commentMapper->find($id));
101
 		}
102
 	}
103
104
	/**
105
	 * Find the previously created entries from the database.
106
	 */
107
	public function testFindByPoll() {
108
		foreach ($this->pollsById as $id => $poll) {
109
			$this->assertTrue(count($this->commentMapper->findByPoll($id)) > 0);
110
		}
111
	}
112
113
	/**
114
	 * Update the previously created entry and persist the changes.
115
	 */
116
	public function testUpdate() {
117
		foreach ($this->commentsById as $id => $comment) {
118
			$found = $this->commentMapper->find($id);
119
			$newComment = Faker::paragraph();
120
			$found->setComment($newComment());
121
			$this->assertEquals($found, $this->commentMapper->update($found));
122
		}
123
	}
124
125
	/**
126
	 * Delete the previously created entries from the database.
127
	 */
128
	public function testDelete() {
129
		foreach ($this->commentsById as $id => $comment) {
130
			$found = $this->commentMapper->find($id);
131
			$this->assertInstanceOf(Comment::class, $this->commentMapper->delete($found));
132
		}
133
	}
134
135
	public function tearDown(): void {
136
		parent::tearDown();
137
		foreach ($this->polls as $poll) {
138
			$this->pollMapper->delete($poll);
139
		}
140
	}
141
142
}
143