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

AddTest::validProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 50
rs 9.456
cc 1
nc 1
nop 0
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\Add;
27
use OCA\Polls\Db\Poll;
28
use OCA\Polls\Db\Share;
29
use OCA\Polls\Exceptions\ShareAlreadyExistsException;
30
use OCA\Polls\Model\Email;
31
use OCA\Polls\Model\Group;
32
use OCA\Polls\Model\User;
33
use OCP\AppFramework\Db\DoesNotExistException;
34
use PHPUnit\Framework\TestCase;
35
use Symfony\Component\Console\Exception\RuntimeException as ConsoleRuntimeException;
36
use Symfony\Component\Console\Tester\CommandTester;
37
38
class AddTest extends TestCase {
39
	use TShareCommandTest;
40
41
	public function setUp(): void {
42
		parent::setUp();
43
44
		$this->setUpMocks();
45
	}
46
47
	public function testMissingArguments(): void {
48
		$this->pollMapper
49
			->expects($this->never())
50
			->method('find');
51
52
		$this->expectException(ConsoleRuntimeException::class);
53
		$this->expectExceptionMessage('Not enough arguments (missing: "id").');
54
55
		$command = new Add(
56
			$this->pollMapper,
57
			$this->shareMapper,
58
			$this->shareService,
59
			$this->userManager,
60
			$this->groupManager
61
		);
62
63
		$tester = new CommandTester($command);
64
		$tester->execute([]);
65
	}
66
67
	public function testPollNotFound(): void {
68
		$pollId = 123;
69
70
		$this->pollMapper
71
			->expects($this->once())
72
			->method('find')
73
			->with($pollId)
74
			->willReturnCallback(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

74
			->willReturnCallback(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...
75
				throw new DoesNotExistException('');
76
			});
77
78
		$command = new Add(
79
			$this->pollMapper,
80
			$this->shareMapper,
81
			$this->shareService,
82
			$this->userManager,
83
			$this->groupManager
84
		);
85
86
		$tester = new CommandTester($command);
87
		$tester->execute(['id' => $pollId]);
88
89
		$this->assertEquals("Poll not found.\n", $tester->getDisplay());
90
	}
91
92
	/**
93
	 * @dataProvider validProvider
94
	 */
95
	public function testValid(array $input, array $pollData): void {
96
		$expectedShareCount = count($pollData['expectedShares']['user'] ?? [])
97
			+ count($pollData['expectedShares']['group'] ?? [])
98
			+ count($pollData['expectedShares']['email'] ?? []);
99
		$expectedInvitationCount = count($pollData['expectedInvitations']['user'] ?? [])
100
			+ count($pollData['expectedInvitations']['group'] ?? [])
101
			+ count($pollData['expectedInvitations']['email'] ?? []);
102
103
		$expectedInvitationShareTokens = [];
104
		foreach ($pollData['expectedInvitations'] ?? [] as $type => $shares) {
105
			foreach ($shares as $userId) {
106
				$expectedInvitationShareTokens[] = $this->getShareToken($pollData['pollId'], $type, $userId);
107
			}
108
		}
109
110
		$this->pollMapper
111
			->expects($this->once())
112
			->method('find')
113
			->with($pollData['pollId'])
114
			->willReturnCallback([$this, 'createPollMock']);
115
116
		$this->shareService
117
			->expects($this->exactly($expectedShareCount))
118
			->method('add')
119
			->with($pollData['pollId'], $this->logicalOr(User::TYPE, Group::TYPE, Email::TYPE), $this->anything())
120
			->willReturnCallback(function (int $pollId, string $type, string $userId = '') use ($pollData): Share {
121
				$userIdConstraint = $this->logicalOr(...$pollData['expectedShares'][$type] ?? []);
122
				$userIdConstraint->evaluate($userId);
123
124
				if (in_array($userId, $pollData['initialShares'][$type] ?? [])) {
125
					throw new ShareAlreadyExistsException();
126
				}
127
128
				return $this->createShareMock($pollId, $type, $userId);
129
			});
130
131
		$this->shareService
132
			->expects($this->exactly($expectedInvitationCount))
133
			->method('sendInvitation')
134
			->with($this->logicalOr(...$expectedInvitationShareTokens));
135
136
		$command = new Add(
137
			$this->pollMapper,
138
			$this->shareMapper,
139
			$this->shareService,
140
			$this->userManager,
141
			$this->groupManager
142
		);
143
144
		$tester = new CommandTester($command);
145
		$tester->execute($input);
146
147
		$this->assertEquals("Users successfully invited to poll.\n", $tester->getDisplay());
148
	}
149
150
	public function validProvider(): array {
151
		return [
152
			[
153
				[
154
					'id' => 1,
155
				],
156
				[
157
					'pollId' => 1,
158
				],
159
			],
160
			[
161
				[
162
					'id' => 123,
163
					'--user' => ['user1', 'user2'],
164
					'--group' => ['group1'],
165
					'--email' => ['[email protected]', '[email protected]'],
166
				],
167
				[
168
					'pollId' => 123,
169
					'expectedShares' => [
170
						'user' => ['user1', 'user2'],
171
						'group' => ['group1'],
172
						'email' => ['[email protected]', '[email protected]'],
173
					],
174
					'expectedInvitations' => [
175
						'user' => ['user1', 'user2'],
176
						'group' => ['group1'],
177
						'email' => ['[email protected]', '[email protected]'],
178
					],
179
				],
180
			],
181
			[
182
				[
183
					'id' => 456,
184
					'--user' => ['user2', 'user3', 'user4'],
185
					'--email' => ['[email protected]', '[email protected]'],
186
				],
187
				[
188
					'pollId' => 456,
189
					'initialShares' => [
190
						'user' => ['user1', 'user2'],
191
						'email' => ['[email protected]'],
192
					],
193
					'expectedShares' => [
194
						'user' => ['user2', 'user3', 'user4'],
195
						'email' => ['[email protected]', '[email protected]'],
196
					],
197
					'expectedInvitations' => [
198
						'user' => ['user3', 'user4'],
199
						'email' => ['[email protected]'],
200
					],
201
				],
202
			],
203
		];
204
	}
205
}
206