DeleteTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Delete;
  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 DeleteTest extends TestCase {
  14. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  15. private $groupManager;
  16. /** @var Delete */
  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 Delete($this->groupManager);
  26. $this->input = $this->createMock(InputInterface::class);
  27. $this->output = $this->createMock(OutputInterface::class);
  28. }
  29. public function testDoesNotExists() {
  30. $gid = 'myGroup';
  31. $this->input->method('getArgument')
  32. ->willReturnCallback(function ($arg) use ($gid) {
  33. if ($arg === 'groupid') {
  34. return $gid;
  35. }
  36. throw new \Exception();
  37. });
  38. $this->groupManager->method('groupExists')
  39. ->with($gid)
  40. ->willReturn(false);
  41. $this->groupManager->expects($this->never())
  42. ->method('get');
  43. $this->output->expects($this->once())
  44. ->method('writeln')
  45. ->with($this->equalTo('<error>Group "' . $gid . '" does not exist.</error>'));
  46. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  47. }
  48. public function testDeleteAdmin() {
  49. $gid = 'admin';
  50. $this->input->method('getArgument')
  51. ->willReturnCallback(function ($arg) use ($gid) {
  52. if ($arg === 'groupid') {
  53. return $gid;
  54. }
  55. throw new \Exception();
  56. });
  57. $this->groupManager->expects($this->never())
  58. ->method($this->anything());
  59. $this->output->expects($this->once())
  60. ->method('writeln')
  61. ->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted.</error>'));
  62. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  63. }
  64. public function testDeleteFailed() {
  65. $gid = 'myGroup';
  66. $this->input->method('getArgument')
  67. ->willReturnCallback(function ($arg) use ($gid) {
  68. if ($arg === 'groupid') {
  69. return $gid;
  70. }
  71. throw new \Exception();
  72. });
  73. $group = $this->createMock(IGroup::class);
  74. $group->method('delete')
  75. ->willReturn(false);
  76. $this->groupManager->method('groupExists')
  77. ->with($gid)
  78. ->willReturn(true);
  79. $this->groupManager->method('get')
  80. ->with($gid)
  81. ->willReturn($group);
  82. $this->output->expects($this->once())
  83. ->method('writeln')
  84. ->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>'));
  85. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  86. }
  87. public function testDelete() {
  88. $gid = 'myGroup';
  89. $this->input->method('getArgument')
  90. ->willReturnCallback(function ($arg) use ($gid) {
  91. if ($arg === 'groupid') {
  92. return $gid;
  93. }
  94. throw new \Exception();
  95. });
  96. $group = $this->createMock(IGroup::class);
  97. $group->method('delete')
  98. ->willReturn(true);
  99. $this->groupManager->method('groupExists')
  100. ->with($gid)
  101. ->willReturn(true);
  102. $this->groupManager->method('get')
  103. ->with($gid)
  104. ->willReturn($group);
  105. $this->output->expects($this->once())
  106. ->method('writeln')
  107. ->with($this->equalTo('Group "' . $gid . '" was removed'));
  108. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  109. }
  110. }