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

SubscriptionMapperTest::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
c 0
b 0
f 0
dl 0
loc 4
rs 10
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\Poll;
27
use OCA\Polls\Db\PollMapper;
28
use OCA\Polls\Db\Subscription;
29
use OCA\Polls\Db\SubscriptionMapper;
30
use OCA\Polls\Tests\Unit\UnitTestCase;
31
use OCP\IDBConnection;
32
use League\FactoryMuffin\Faker\Facade as Faker;
33
34
class SubscriptionMapperTest extends UnitTestCase {
35
36
	/** @var IDBConnection */
37
	private $con;
38
39
	/** @var SubscriptionMapper */
40
	private $subscriptionMapper;
41
42
	/** @var PollMapper */
43
	private $pollMapper;
44
45
	/** @var array */
46
	private $polls;
47
48
	/** @var array */
49
	private $subscriptions;
50
51
	/** @var array */
52
	private $pollsById;
53
54
	/** @var array */
55
	private $subscriptionsById;
0 ignored issues
show
introduced by
The private property $subscriptionsById is not used, and could be removed.
Loading history...
56
57
	/** @var array */
58
	private $users;
59
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64
	protected function setUp(): void {
65
		parent::setUp();
66
		$this->con = \OC::$server->getDatabaseConnection();
67
		$this->subscriptionMapper = new SubscriptionMapper($this->con);
68
		$this->pollMapper = new PollMapper($this->con);
69
70
		$this->polls = [];
71
		$this->subscriptions = [];
72
73
		$this->polls = [
74
			$this->fm->instance('OCA\Polls\Db\Poll')
75
		];
76
77
		foreach ($this->polls as $poll) {
78
			$entry = $this->pollMapper->insert($poll);
79
			$entry->resetUpdatedFields();
80
			$this->pollsById[$entry->getId()] = $entry;
81
		}
82
		foreach ($this->pollsById as $id => $polls) {
83
			for ($count=0; $count < 2; $count++) {
84
				$subscription = $this->fm->instance('OCA\Polls\Db\Subscription');
85
				$subscription->setPollId($id);
86
				array_push($this->subscriptions, $subscription);
87
			}
88
			$this->users[$id] = $subscription->getUserId();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subscription does not seem to be defined for all execution paths leading up to this point.
Loading history...
89
		}
90
	}
91
92
	/**
93
	 * Find the previously created entries from the database.
94
	 */
95
	public function testFindByPoll() {
96
		foreach ($this->pollsById as $id => $poll) {
97
			$this->assertTrue(count($this->subscriptionMapper->findByPoll($id)) > 0);
0 ignored issues
show
Bug introduced by
The method findByPoll() does not exist on OCA\Polls\Db\SubscriptionMapper. Did you maybe mean findByUserAndPoll()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
			$this->assertTrue(count($this->subscriptionMapper->/** @scrutinizer ignore-call */ findByPoll($id)) > 0);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
		}
99
	}
100
101
	public function testFindByUserAndPoll() {
102
		foreach ($this->pollsById as $id => $poll) {
103
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->findByUserAndPoll($id, $this->users[$id]));
104
		}
105
	}
106
	/**
107
	 * Delete the previously created entries from the database.
108
	 */
109
	public function testUnsubscribe() {
110
		foreach ($this->pollsById as $id => $poll) {
111
			$this->assertInstanceOf(Subscription::class, $this->subscriptionMapper->unsubscribe($id, $this->users[$id]));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->subscriptionMappe...$id, $this->users[$id]) targeting OCA\Polls\Db\SubscriptionMapper::unsubscribe() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
112
		}
113
	}
114
115
	public function tearDown(): void {
116
		parent::tearDown();
117
		foreach ($this->polls as $poll) {
118
			$this->pollMapper->delete($poll);
119
		}
120
	}
121
122
}
123