Passed
Pull Request — master (#1516)
by Daniel
03:43
created

RemoveTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 90
c 3
b 0
f 0
dl 0
loc 148
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testPollNotFound() 0 23 1
A setUp() 0 4 1
A validProvider() 0 46 1
A testValid() 0 43 4
A testMissingArguments() 0 18 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 Daniel Rudolf <[email protected]>
4
 *
5
 * @author Daniel Rudolf <[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\Integration\Command\Share;
25
26
use OCA\Polls\Command\Share\Remove;
27
use OCA\Polls\Db\Poll;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use PHPUnit\Framework\TestCase;
30
use Symfony\Component\Console\Exception\RuntimeException as ConsoleRuntimeException;
31
use Symfony\Component\Console\Tester\CommandTester;
32
33
class RemoveTest extends TestCase {
34
	use TShareCommandTest;
35
36
	public function setUp(): void {
37
		parent::setUp();
38
39
		$this->setUpMocks();
40
	}
41
42
	public function testMissingArguments(): void {
43
		$this->pollMapper
44
			->expects($this->never())
45
			->method('find');
46
47
		$this->expectException(ConsoleRuntimeException::class);
48
		$this->expectExceptionMessage('Not enough arguments (missing: "id").');
49
50
		$command = new Remove(
51
			$this->pollMapper,
52
			$this->shareMapper,
53
			$this->shareService,
54
			$this->userManager,
55
			$this->groupManager
56
		);
57
58
		$tester = new CommandTester($command);
59
		$tester->execute([]);
60
	}
61
62
	public function testPollNotFound(): void {
63
		$pollId = 123;
64
65
		$this->pollMapper
66
			->expects($this->once())
67
			->method('find')
68
			->with($pollId)
69
			->willReturnCallback(static function (int $id): Poll {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

69
			->willReturnCallback(static function (/** @scrutinizer ignore-unused */ int $id): Poll {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
				throw new DoesNotExistException('');
71
			});
72
73
		$command = new Remove(
74
			$this->pollMapper,
75
			$this->shareMapper,
76
			$this->shareService,
77
			$this->userManager,
78
			$this->groupManager
79
		);
80
81
		$tester = new CommandTester($command);
82
		$tester->execute(['id' => $pollId]);
83
84
		$this->assertEquals("Poll not found.\n", $tester->getDisplay());
85
	}
86
87
	/**
88
	 * @dataProvider validProvider
89
	 */
90
	public function testValid(array $input, array $pollData): void {
91
		$initialShares = [];
92
		$expectedShareCount = 0;
93
		$expectedShareTokens = [];
94
		foreach ($pollData['initialShares'] ?? [] as $type => $shares) {
95
			foreach ($shares as $userId) {
96
				$initialShares[] = $this->createShareMock($pollData['pollId'], $type, $userId);
97
98
				if (in_array($userId, $pollData['expectedShares'][$type] ?? [])) {
99
					$expectedShareTokens[] = $this->getShareToken($pollData['pollId'], $type, $userId);
100
					$expectedShareCount++;
101
				}
102
			}
103
		}
104
105
		$this->pollMapper
106
			->expects($this->once())
107
			->method('find')
108
			->with($pollData['pollId'])
109
			->willReturnCallback([$this, 'createPollMock']);
110
111
		$this->shareMapper
112
			->method('findByPoll')
0 ignored issues
show
Bug introduced by
The method method() does not exist on OCA\Polls\Db\ShareMapper. ( Ignorable by Annotation )

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

112
			->/** @scrutinizer ignore-call */ 
113
     method('findByPoll')

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...
113
			->with($pollData['pollId'])
114
			->willReturn($initialShares);
115
116
		$this->shareService
117
			->expects($this->exactly($expectedShareCount))
118
			->method('delete')
119
			->with($this->logicalOr(...$expectedShareTokens));
120
121
		$command = new Remove(
122
			$this->pollMapper,
123
			$this->shareMapper,
124
			$this->shareService,
125
			$this->userManager,
126
			$this->groupManager
127
		);
128
129
		$tester = new CommandTester($command);
130
		$tester->execute($input);
131
132
		$this->assertEquals("Poll invitations successfully revoked.\n", $tester->getDisplay());
133
	}
134
135
	public function validProvider(): array {
136
		return [
137
			[
138
				[
139
					'id' => 1,
140
				],
141
				[
142
					'pollId' => 1,
143
				],
144
			],
145
			[
146
				[
147
					'id' => 123,
148
					'--user' => ['user1', 'user2'],
149
					'--group' => ['group1'],
150
					'--email' => ['[email protected]'],
151
				],
152
				[
153
					'pollId' => 123,
154
					'initialShares' => [
155
						'user' => ['user1', 'user2', 'user3'],
156
						'group' => ['group1'],
157
						'email' => ['[email protected]', '[email protected]'],
158
					],
159
					'expectedShares' => [
160
						'user' => ['user1', 'user2'],
161
						'group' => ['group1'],
162
						'email' => ['[email protected]'],
163
					],
164
				],
165
			],
166
			[
167
				[
168
					'id' => 456,
169
					'--user' => ['user1', 'user2', 'user3', 'user4'],
170
					'--email' => ['[email protected]', '[email protected]'],
171
				],
172
				[
173
					'pollId' => 456,
174
					'initialShares' => [
175
						'user' => ['user2', 'user3'],
176
						'email' => ['[email protected]', '[email protected]', '[email protected]'],
177
					],
178
					'expectedShares' => [
179
						'user' => ['user2', 'user3'],
180
						'email' => ['[email protected]', '[email protected]'],
181
					],
182
				]
183
			],
184
		];
185
	}
186
}
187