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

RemoveTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 90
c 1
b 0
f 0
dl 0
loc 149
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
{
35
	use TShareCommandTest;
36
37
	public function setUp(): void {
38
		parent::setUp();
39
40
		$this->setUpMocks();
41
	}
42
43
	public function testMissingArguments(): void {
44
		$this->pollMapper
45
			->expects($this->never())
46
			->method('find');
47
48
		$this->expectException(ConsoleRuntimeException::class);
49
		$this->expectExceptionMessage('Not enough arguments (missing: "id").');
50
51
		$command = new Remove(
52
			$this->pollMapper,
53
			$this->shareMapper,
54
			$this->shareService,
55
			$this->userManager,
56
			$this->groupManager
57
		);
58
59
		$tester = new CommandTester($command);
60
		$tester->execute([]);
61
	}
62
63
	public function testPollNotFound(): void {
64
		$pollId = 123;
65
66
		$this->pollMapper
67
			->expects($this->once())
68
			->method('find')
69
			->with($pollId)
70
			->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

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

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