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

LogMapperTest::setUp()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.8333
c 2
b 0
f 0
cc 3
nc 3
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
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	protected function setUp(): void {
56
		parent::setUp();
57
		$this->con = \OC::$server->getDatabaseConnection();
58
		$this->logMapper = new LogMapper($this->con);
59
		$this->pollMapper = new PollMapper($this->con);
60
61
		$this->polls = [
62
			$this->fm->instance('OCA\Polls\Db\Poll')
63
		];
64
65
		foreach ($this->polls as &$poll) {
66
			$poll = $this->pollMapper->insert($poll);
67
68
			for ($count=0; $count < 2; $count++) {
69
				$log = $this->fm->instance('OCA\Polls\Db\Log');
70
				$log->setPollId($poll->getId());
71
				array_push($this->logs, $this->logMapper->insert($log));
72
			}
73
		}
74
		unset($poll);
75
	}
76
77
	/**
78
	 * testFindByPollId
79
	 */
80
	public function testFindByPollId() {
81
		foreach ($this->polls as $poll) {
82
			$this->assertTrue(count($this->logMapper->findByPollId($poll->getId())) > 0);
83
		}
84
	}
85
86
	/**
87
	 * testFindUnprocessed
88
	 */
89
	public function testFindUnprocessed() {
90
		$this->assertTrue(count($this->logMapper->findUnprocessed()) > 0);
91
	}
92
93
	/**
94
	 * testFindUnprocessedPolls
95
	 */
96
	public function testFindUnprocessedPolls() {
97
		$this->assertTrue(count($this->logMapper->findUnprocessedPolls()) > 0);
98
	}
99
100
	/**
101
	 * testGetLastRecord
102
	 */
103
	public function testGetLastRecord() {
104
		foreach ($this->polls as $poll) {
105
			$this->assertInstanceOf(Log::class, $this->logMapper->getLastRecord($poll->getId()));
106
		}
107
	}
108
109
	/**
110
	 * testUpdate
111
	 * includes testFind
112
	 */
113
	public function testUpdate() {
114
		foreach ($this->logs as &$log) {
115
			$before = $this->logMapper->find($log->getId());
116
			$this->assertEquals($log, $before);
117
118
			$log->setMessageId(Log::MSG_ID_UPDATEPOLL);
119
120
			$this->assertEquals($log, $this->logMapper->update($log));
121
			$this->assertNotEquals($before, $this->logMapper->find($log->getId()));
122
		}
123
		unset($log);
124
	}
125
126
127
	/**
128
	 * testDelete
129
	 */
130
	public function testDelete() {
131
		foreach ($this->logs as $log) {
132
			$before = $this->logMapper->find($log->getId());
133
			$this->assertInstanceOf(Log::class, $this->logMapper->delete($before));
134
		}
135
	}
136
137
	/**
138
	 * tearDown
139
	 */
140
	public function tearDown(): void {
141
		parent::tearDown();
142
		foreach ($this->polls as $poll) {
143
			$this->pollMapper->delete($poll);
144
		}
145
	}
146
147
}
148