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

LogMapperTest::setUp()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 30
rs 9.2728
cc 5
nc 12
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\Log;
27
use OCA\Polls\Db\LogMapper;
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 LogMapperTest extends UnitTestCase {
35
36
	/** @var IDBConnection */
37
	private $con;
38
39
	/** @var LogMapper */
40
	private $logMapper;
41
42
	/** @var PollMapper */
43
	private $pollMapper;
44
45
	/** @var array */
46
	private $polls;
47
48
	/** @var array */
49
	private $logs;
50
51
	/** @var array */
52
	private $pollsById;
53
54
	/** @var array */
55
	private $logsById;
56
57
	/**
58
	 * {@inheritDoc}
59
	 */
60
	protected function setUp(): void {
61
		parent::setUp();
62
		$this->con = \OC::$server->getDatabaseConnection();
63
		$this->logMapper = new LogMapper($this->con);
64
		$this->pollMapper = new PollMapper($this->con);
65
66
		$this->polls = [];
67
		$this->logs = [];
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
		foreach ($this->pollsById as $id => $polls) {
79
			for ($count=0; $count < 2; $count++) {
80
				$log = $this->fm->instance('OCA\Polls\Db\Log');
81
				$log->setPollId($id);
82
				array_push($this->logs, $log);
83
			}
84
		}
85
86
		foreach ($this->logs as $log) {
87
			$entry = $this->logMapper->insert($log);
88
			$entry->resetUpdatedFields();
89
			$this->logsById[$entry->getId()] = $entry;
90
		}
91
92
93
	}
94
95
	/**
96
	 * Find the previously created entries from the database.
97
	 */
98
	public function testFindByPollId() {
99
		foreach ($this->pollsById as $id => $poll) {
100
			$this->assertTrue(count($this->logMapper->findByPollId($id)) > 0);
101
		}
102
	}
103
104
	/**
105
	 * Find the previously created entries from the database.
106
	 */
107
	public function testFindUnprocessed() {
108
		$this->assertTrue(count($this->logMapper->findUnprocessed()) > 0);
109
	}
110
111
	/**
112
	 * Find the previously created entries from the database.
113
	 */
114
	public function testFindUnprocessedPolls() {
115
		$this->assertTrue(count($this->logMapper->findUnprocessedPolls()) > 0);
116
	}
117
118
	/**
119
	 * Find the previously created entries from the database.
120
	 */
121
	public function testGetLastRecord() {
122
		foreach ($this->pollsById as $id => $poll) {
123
			$this->assertInstanceOf(Log::class, $this->logMapper->getLastRecord($id));
124
		}
125
	}
126
127
	/**
128
	 * Delete the previously created entries from the database.
129
	 */
130
	public function testDelete() {
131
		foreach ($this->logsById as $id => $log) {
132
			$found = $this->logMapper->find($id);
133
			$this->assertInstanceOf(Log::class, $this->logMapper->delete($found));
134
		}
135
	}
136
137
	public function tearDown(): void {
138
		parent::tearDown();
139
		foreach ($this->polls as $poll) {
140
			$this->pollMapper->delete($poll);
141
		}
142
	}
143
144
}
145