Passed
Pull Request — master (#1516)
by Daniel
03:46
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
{
40
	use TShareCommandTest;
41
42
	public function setUp(): void {
43
		parent::setUp();
44
45
		$this->setUpMocks();
46
	}
47
48
	public function testMissingArguments(): void {
49
		$this->pollMapper
50
			->expects($this->never())
51
			->method('find');
52
53
		$this->expectException(ConsoleRuntimeException::class);
54
		$this->expectExceptionMessage('Not enough arguments (missing: "id").');
55
56
		$command = new Add(
57
			$this->pollMapper,
58
			$this->shareMapper,
59
			$this->shareService,
60
			$this->userManager,
61
			$this->groupManager
62
		);
63
64
		$tester = new CommandTester($command);
65
		$tester->execute([]);
66
	}
67
68
	public function testPollNotFound(): void {
69
		$pollId = 123;
70
71
		$this->pollMapper
72
			->expects($this->once())
73
			->method('find')
74
			->with($pollId)
75
			->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

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