Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions core/Command/Group/AddUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,44 @@ protected function configure() {
'group to add the user to'
)->addArgument(
'user',
InputArgument::REQUIRED,
'user to add to the group'
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
'users to add to the group',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$group = $this->groupManager->get($input->getArgument('group'));
if (is_null($group)) {
$output->writeln('<error>group not found</error>');
return 1;
return Base::FAILURE;
}

$allUsersFound = true;
$noUserFound = true;
$users = (array)$input->getArgument('user');
foreach ($users as $userId) {
$user = $this->userManager->get($userId);
if (is_null($user)) {
$output->writeln('<error>user ' . $userId . ' not found</error>');
$allUsersFound = false;
continue;
}
$noUserFound = false;
$group->addUser($user);
unset($user);
$output->writeln('<info>user ' . $userId . ' added</info>');
}
$user = $this->userManager->get($input->getArgument('user'));
if (is_null($user)) {
$output->writeln('<error>user not found</error>');
return 1;

if (!$allUsersFound && !$noUserFound) {
$output->writeln('<error>Some users were not found, all others where added to the group.</error>');
return Base::FAILURE;
}

if ($noUserFound) {
return Base::FAILURE;
}
$group->addUser($user);
return 0;

return Base::SUCCESS;
}

/**
Expand Down
38 changes: 29 additions & 9 deletions core/Command/Group/RemoveUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,44 @@ protected function configure() {
'group to remove the user from'
)->addArgument(
'user',
InputArgument::REQUIRED,
'user to remove from the group'
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
'users to remove from the group'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$group = $this->groupManager->get($input->getArgument('group'));
if (is_null($group)) {
$output->writeln('<error>group not found</error>');
return 1;
return Base::FAILURE;
}

$allUsersFound = true;
$noUserFound = true;
$users = (array)$input->getArgument('user');
foreach ($users as $userId) {
$user = $this->userManager->get($userId);
if (is_null($user)) {
$output->writeln('<error>user ' . $userId . ' not found</error>');
$allUsersFound = false;
continue;
}
$noUserFound = false;
$group->removeUser($user);
unset($user);
$output->writeln('<info>user ' . $userId . ' removed</info>');
}
$user = $this->userManager->get($input->getArgument('user'));
if (is_null($user)) {
$output->writeln('<error>user not found</error>');
return 1;

if (!$allUsersFound && !$noUserFound) {
$output->writeln('<error>Some users were not found, all others where removed from the group.</error>');
return Base::FAILURE;
}

if ($noUserFound) {
return Base::FAILURE;
}
$group->removeUser($user);
return 0;

return Base::SUCCESS;
}

/**
Expand Down
83 changes: 77 additions & 6 deletions tests/Core/Command/Group/AddUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@ protected function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->command = new AddUser($this->userManager, $this->groupManager);
$this->output = $this->createMock(OutputInterface::class);
}

protected function configureInput(array|string $returnGroup, array|string $returnUser): void {
$this->input = $this->createMock(InputInterface::class);
$this->input->method('getArgument')
->willReturnCallback(function ($arg) {
->willReturnCallback(function ($arg) use ($returnGroup, $returnUser) {
if ($arg === 'group') {
return 'myGroup';
} elseif ($arg === 'user') {
return 'myUser';
return $returnGroup;
}
if ($arg === 'user') {
return $returnUser;
}
throw new \Exception();
});
$this->output = $this->createMock(OutputInterface::class);
}

public function testNoGroup(): void {
$this->configureInput('myGroup', 'myUser');

$this->groupManager->method('get')
->with('myGroup')
->willReturn(null);
Expand All @@ -65,6 +70,8 @@ public function testNoGroup(): void {
}

public function testNoUser(): void {
$this->configureInput('myGroup', 'myUser');

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
Expand All @@ -76,12 +83,14 @@ public function testNoUser(): void {

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

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

public function testAdd(): void {
$this->configureInput('myGroup', 'myUser');

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
Expand All @@ -98,4 +107,66 @@ public function testAdd(): void {

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

public function testAddMultiple(): void {
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
->willReturn($group);

$user1 = $this->createMock(IUser::class);
$user2 = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturnMap([
['myUser', $user1],
['myOtherUser', $user2],
]);

$group->expects($this->exactly(2))
->method('addUser')
->with($this->callback(static fn (IUser $user): bool => in_array($user, [$user1, $user2], true)));

$this->output->expects($this->exactly(2))
->method('writeln')
->with($this->callback(static fn (string $message): bool => in_array($message,
[
'<info>user myUser added</info>',
'<info>user myOtherUser added</info>',
], true)));

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

public function testAddMultiplePartialSuccess(): void {
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
->willReturn($group);

$user = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturnMap([
['myUser', $user],
['myOtherUser', null],
]);

$group->expects($this->once())
->method('addUser')
->with($user);

$this->output->expects($this->exactly(3))
->method('writeln')
->with($this->callback(static fn (string $message): bool => in_array($message,
[
'<info>user myUser added</info>',
'<error>user myOtherUser not found</error>',
'<error>Some users were not found, all others where added to the group.</error>',
], true)));

$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
}
}
85 changes: 78 additions & 7 deletions tests/Core/Command/Group/RemoveUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@ protected function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->command = new RemoveUser($this->userManager, $this->groupManager);
$this->output = $this->createMock(OutputInterface::class);
}

protected function configureInput(array|string $returnGroup, array|string $returnUser): void {
$this->input = $this->createMock(InputInterface::class);
$this->input->method('getArgument')
->willReturnCallback(function ($arg) {
->willReturnCallback(function ($arg) use ($returnGroup, $returnUser) {
if ($arg === 'group') {
return 'myGroup';
} elseif ($arg === 'user') {
return 'myUser';
return $returnGroup;
}
if ($arg === 'user') {
return $returnUser;
}
throw new \Exception();
});
$this->output = $this->createMock(OutputInterface::class);
}

public function testNoGroup(): void {
$this->configureInput('myGroup', 'myUser');

$this->groupManager->method('get')
->with('myGroup')
->willReturn(null);
Expand All @@ -65,6 +70,8 @@ public function testNoGroup(): void {
}

public function testNoUser(): void {
$this->configureInput('myGroup', 'myUser');

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
Expand All @@ -76,12 +83,14 @@ public function testNoUser(): void {

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

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

public function testAdd(): void {
public function testRemove(): void {
$this->configureInput('myGroup', 'myUser');

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
Expand All @@ -98,4 +107,66 @@ public function testAdd(): void {

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

public function testRemoveMultiple(): void {
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
->willReturn($group);

$user1 = $this->createMock(IUser::class);
$user2 = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturnMap([
['myUser', $user1],
['myOtherUser', $user2],
]);

$group->expects($this->exactly(2))
->method('removeUser')
->with($this->callback(static fn (IUser $user): bool => in_array($user, [$user1, $user2], true)));

$this->output->expects($this->exactly(2))
->method('writeln')
->with($this->callback(static fn (string $message): bool => in_array($message,
[
'<info>user myUser removed</info>',
'<info>user myOtherUser removed</info>',
], true)));

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

public function testRemoveMultiplePartialSuccess(): void {
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);

$group = $this->createMock(IGroup::class);
$this->groupManager->method('get')
->with('myGroup')
->willReturn($group);

$user = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturnMap([
['myUser', $user],
['myOtherUser', null],
]);

$group->expects($this->once())
->method('removeUser')
->with($user);

$this->output->expects($this->exactly(3))
->method('writeln')
->with($this->callback(static fn (string $message): bool => in_array($message,
[
'<info>user myUser removed</info>',
'<error>user myOtherUser not found</error>',
'<error>Some users were not found, all others where removed from the group.</error>',
], true)));

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