RemoveUserTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Core\Command\Group;
  7. use OC\Core\Command\Group\RemoveUser;
  8. use OCP\IGroup;
  9. use OCP\IGroupManager;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Test\TestCase;
  15. class RemoveUserTest extends TestCase {
  16. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  17. private $groupManager;
  18. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  19. private $userManager;
  20. /** @var RemoveUser */
  21. private $command;
  22. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  23. private $input;
  24. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  25. private $output;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->groupManager = $this->createMock(IGroupManager::class);
  29. $this->userManager = $this->createMock(IUserManager::class);
  30. $this->command = new RemoveUser($this->userManager, $this->groupManager);
  31. $this->input = $this->createMock(InputInterface::class);
  32. $this->input->method('getArgument')
  33. ->willReturnCallback(function ($arg) {
  34. if ($arg === 'group') {
  35. return 'myGroup';
  36. } elseif ($arg === 'user') {
  37. return 'myUser';
  38. }
  39. throw new \Exception();
  40. });
  41. $this->output = $this->createMock(OutputInterface::class);
  42. }
  43. public function testNoGroup() {
  44. $this->groupManager->method('get')
  45. ->with('myGroup')
  46. ->willReturn(null);
  47. $this->output->expects($this->once())
  48. ->method('writeln')
  49. ->with('<error>group not found</error>');
  50. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  51. }
  52. public function testNoUser() {
  53. $group = $this->createMock(IGroup::class);
  54. $this->groupManager->method('get')
  55. ->with('myGroup')
  56. ->willReturn($group);
  57. $this->userManager->method('get')
  58. ->with('myUser')
  59. ->willReturn(null);
  60. $this->output->expects($this->once())
  61. ->method('writeln')
  62. ->with('<error>user not found</error>');
  63. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  64. }
  65. public function testAdd() {
  66. $group = $this->createMock(IGroup::class);
  67. $this->groupManager->method('get')
  68. ->with('myGroup')
  69. ->willReturn($group);
  70. $user = $this->createMock(IUser::class);
  71. $this->userManager->method('get')
  72. ->with('myUser')
  73. ->willReturn($user);
  74. $group->expects($this->once())
  75. ->method('removeUser')
  76. ->with($user);
  77. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  78. }
  79. }