AddUserTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. namespace Test\Core\Command\Group;
  24. use OC\Core\Command\Group\AddUser;
  25. use OCP\IGroup;
  26. use OCP\IGroupManager;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Test\TestCase;
  32. class AddUserTest extends TestCase {
  33. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  34. private $groupManager;
  35. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  36. private $userManager;
  37. /** @var AddUser */
  38. private $command;
  39. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  40. private $input;
  41. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  42. private $output;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->groupManager = $this->createMock(IGroupManager::class);
  46. $this->userManager = $this->createMock(IUserManager::class);
  47. $this->command = new AddUser($this->userManager, $this->groupManager);
  48. $this->input = $this->createMock(InputInterface::class);
  49. $this->input->method('getArgument')
  50. ->willReturnCallback(function ($arg) {
  51. if ($arg === 'group') {
  52. return 'myGroup';
  53. } elseif ($arg === 'user') {
  54. return 'myUser';
  55. }
  56. throw new \Exception();
  57. });
  58. $this->output = $this->createMock(OutputInterface::class);
  59. }
  60. public function testNoGroup() {
  61. $this->groupManager->method('get')
  62. ->with('myGroup')
  63. ->willReturn(null);
  64. $this->output->expects($this->once())
  65. ->method('writeln')
  66. ->with('<error>group not found</error>');
  67. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  68. }
  69. public function testNoUser() {
  70. $group = $this->createMock(IGroup::class);
  71. $this->groupManager->method('get')
  72. ->with('myGroup')
  73. ->willReturn($group);
  74. $this->userManager->method('get')
  75. ->with('myUser')
  76. ->willReturn(null);
  77. $this->output->expects($this->once())
  78. ->method('writeln')
  79. ->with('<error>user not found</error>');
  80. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  81. }
  82. public function testAdd() {
  83. $group = $this->createMock(IGroup::class);
  84. $this->groupManager->method('get')
  85. ->with('myGroup')
  86. ->willReturn($group);
  87. $user = $this->createMock(IUser::class);
  88. $this->userManager->method('get')
  89. ->with('myUser')
  90. ->willReturn($user);
  91. $group->expects($this->once())
  92. ->method('addUser')
  93. ->with($user);
  94. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  95. }
  96. }