AddTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 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\Add;
  8. use OCP\IGroup;
  9. use OCP\IGroupManager;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class AddTest extends TestCase {
  14. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  15. private $groupManager;
  16. /** @var Add */
  17. private $command;
  18. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  19. private $input;
  20. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  21. private $output;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->groupManager = $this->createMock(IGroupManager::class);
  25. $this->command = new Add($this->groupManager);
  26. $this->input = $this->createMock(InputInterface::class);
  27. $this->input->method('getArgument')
  28. ->willReturnCallback(function ($arg) {
  29. if ($arg === 'groupid') {
  30. return 'myGroup';
  31. }
  32. throw new \Exception();
  33. });
  34. $this->output = $this->createMock(OutputInterface::class);
  35. }
  36. public function testGroupExists(): void {
  37. $gid = 'myGroup';
  38. $group = $this->createMock(IGroup::class);
  39. $this->groupManager->method('get')
  40. ->with($gid)
  41. ->willReturn($group);
  42. $this->groupManager->expects($this->never())
  43. ->method('createGroup');
  44. $this->output->expects($this->once())
  45. ->method('writeln')
  46. ->with($this->equalTo('<error>Group "' . $gid . '" already exists.</error>'));
  47. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  48. }
  49. public function testAdd(): void {
  50. $gid = 'myGroup';
  51. $group = $this->createMock(IGroup::class);
  52. $group->method('getGID')
  53. ->willReturn($gid);
  54. $this->groupManager->method('createGroup')
  55. ->willReturn($group);
  56. $this->groupManager->expects($this->once())
  57. ->method('createGroup')
  58. ->with($this->equalTo($gid));
  59. $this->output->expects($this->once())
  60. ->method('writeln')
  61. ->with($this->equalTo('Created group "' . $group->getGID() . '"'));
  62. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  63. }
  64. }