Skip to content

Commit 847a4d9

Browse files
enjeckblizzz
authored andcommitted
feat(occ): make it possible to remove an arbitrary number of users to a group
Signed-off-by: Enjeck C. <patrathewhiz@gmail.com>
1 parent 37bb3fb commit 847a4d9

2 files changed

Lines changed: 111 additions & 11 deletions

File tree

core/Command/Group/RemoveUser.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected function configure() {
3535
'group to remove the user from'
3636
)->addArgument(
3737
'user',
38-
InputArgument::REQUIRED,
39-
'user to remove from the group'
38+
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
39+
'users to remove from the group'
4040
);
4141
}
4242

@@ -45,15 +45,35 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4545
$group = $this->groupManager->get($input->getArgument('group'));
4646
if (is_null($group)) {
4747
$output->writeln('<error>group not found</error>');
48-
return 1;
48+
return Base::FAILURE;
49+
}
50+
51+
$allUsersFound = true;
52+
$noUserFound = true;
53+
$users = (array)$input->getArgument('user');
54+
foreach ($users as $userId) {
55+
$user = $this->userManager->get($userId);
56+
if (is_null($user)) {
57+
$output->writeln('<error>user ' . $userId . ' not found</error>');
58+
$allUsersFound = false;
59+
continue;
60+
}
61+
$noUserFound = false;
62+
$group->removeUser($user);
63+
unset($user);
64+
$output->writeln('<info>user ' . $userId . ' removed</info>');
4965
}
50-
$user = $this->userManager->get($input->getArgument('user'));
51-
if (is_null($user)) {
52-
$output->writeln('<error>user not found</error>');
53-
return 1;
66+
67+
if (!$allUsersFound && !$noUserFound) {
68+
$output->writeln('<error>Some users were not found, all others where removed from the group.</error>');
69+
return Base::FAILURE;
70+
}
71+
72+
if ($noUserFound) {
73+
return Base::FAILURE;
5474
}
55-
$group->removeUser($user);
56-
return 0;
75+
76+
return Base::SUCCESS;
5777
}
5878

5979
/**

tests/Core/Command/Group/RemoveUserTest.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public function testNoUser(): void {
7777

7878
$this->output->expects($this->once())
7979
->method('writeln')
80-
->with('<error>user not found</error>');
80+
->with('<error>user myUser not found</error>');
8181

8282
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
8383
}
8484

85-
public function testAdd(): void {
85+
public function testRemove(): void {
8686
$group = $this->createMock(IGroup::class);
8787
$this->groupManager->method('get')
8888
->with('myGroup')
@@ -99,4 +99,84 @@ public function testAdd(): void {
9999

100100
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
101101
}
102+
103+
public function testRemoveMultiple(): void {
104+
$this->input->method('getArgument')
105+
->willReturnCallback(function ($arg) {
106+
if ($arg === 'group') {
107+
return 'myGroup';
108+
}
109+
if ($arg === 'user') {
110+
return ['myUser', 'myOtherUser'];
111+
}
112+
throw new \Exception();
113+
});
114+
115+
$group = $this->createMock(IGroup::class);
116+
$this->groupManager->method('get')
117+
->with('myGroup')
118+
->willReturn($group);
119+
120+
$user1 = $this->createMock(IUser::class);
121+
$user2 = $this->createMock(IUser::class);
122+
$this->userManager->method('get')
123+
->willReturnMap([
124+
['myUser', $user1],
125+
['myOtherUser', $user2],
126+
]);
127+
128+
$group->expects($this->exactly(2))
129+
->method('removeUser')
130+
->with($this->callback(static fn(IUser $user): bool => in_array($user, [$user1, $user2], true)));
131+
132+
$this->output->expects($this->exactly(2))
133+
->method('writeln')
134+
->with($this->callback(static fn (string $message): bool => in_array($message,
135+
[
136+
'<info>user myUser removed</info>',
137+
'<info>user myOtherUser removed</info>',
138+
], true)));
139+
140+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
141+
}
142+
143+
public function testRemoveMultiplePartialSuccess(): void {
144+
$this->input->method('getArgument')
145+
->willReturnCallback(function ($arg) {
146+
if ($arg === 'group') {
147+
return 'myGroup';
148+
}
149+
if ($arg === 'user') {
150+
return ['myUser', 'myOtherUser'];
151+
}
152+
throw new \Exception();
153+
});
154+
155+
$group = $this->createMock(IGroup::class);
156+
$this->groupManager->method('get')
157+
->with('myGroup')
158+
->willReturn($group);
159+
160+
$user = $this->createMock(IUser::class);
161+
$this->userManager->method('get')
162+
->willReturnMap([
163+
['myUser', $user],
164+
['myOtherUser', null],
165+
]);
166+
167+
$group->expects($this->once())
168+
->method('removeUser')
169+
->with($user);
170+
171+
$this->output->expects($this->exactly(3))
172+
->method('writeln')
173+
->with($this->callback(static fn (string $message): bool => in_array($message,
174+
[
175+
'<info>user myUser removed</info>',
176+
'<error>user myOtherUser not found</error>',
177+
'<error>Some users were not found, all others where removed from the group.</error>',
178+
], true)));
179+
180+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
181+
}
102182
}

0 commit comments

Comments
 (0)