DeleteTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright 2018, Denis Mosolov <denismosolov@gmail.com>
  4. *
  5. * @author Denis Mosolov <denismosolov@gmail.com>
  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 Afferoq 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\Delete;
  25. use OCP\IGroup;
  26. use OCP\IGroupManager;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. use Test\TestCase;
  30. class DeleteTest extends TestCase {
  31. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  32. private $groupManager;
  33. /** @var Delete */
  34. private $command;
  35. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  36. private $input;
  37. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  38. private $output;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->groupManager = $this->createMock(IGroupManager::class);
  42. $this->command = new Delete($this->groupManager);
  43. $this->input = $this->createMock(InputInterface::class);
  44. $this->output = $this->createMock(OutputInterface::class);
  45. }
  46. public function testDoesNotExists() {
  47. $gid = 'myGroup';
  48. $this->input->method('getArgument')
  49. ->willReturnCallback(function ($arg) use ($gid) {
  50. if ($arg === 'groupid') {
  51. return $gid;
  52. }
  53. throw new \Exception();
  54. });
  55. $this->groupManager->method('groupExists')
  56. ->with($gid)
  57. ->willReturn(false);
  58. $this->groupManager->expects($this->never())
  59. ->method('get');
  60. $this->output->expects($this->once())
  61. ->method('writeln')
  62. ->with($this->equalTo('<error>Group "' . $gid . '" does not exist.</error>'));
  63. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  64. }
  65. public function testDeleteAdmin() {
  66. $gid = 'admin';
  67. $this->input->method('getArgument')
  68. ->willReturnCallback(function ($arg) use ($gid) {
  69. if ($arg === 'groupid') {
  70. return $gid;
  71. }
  72. throw new \Exception();
  73. });
  74. $this->groupManager->expects($this->never())
  75. ->method($this->anything());
  76. $this->output->expects($this->once())
  77. ->method('writeln')
  78. ->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted.</error>'));
  79. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  80. }
  81. public function testDeleteFailed() {
  82. $gid = 'myGroup';
  83. $this->input->method('getArgument')
  84. ->willReturnCallback(function ($arg) use ($gid) {
  85. if ($arg === 'groupid') {
  86. return $gid;
  87. }
  88. throw new \Exception();
  89. });
  90. $group = $this->createMock(IGroup::class);
  91. $group->method('delete')
  92. ->willReturn(false);
  93. $this->groupManager->method('groupExists')
  94. ->with($gid)
  95. ->willReturn(true);
  96. $this->groupManager->method('get')
  97. ->with($gid)
  98. ->willReturn($group);
  99. $this->output->expects($this->once())
  100. ->method('writeln')
  101. ->with($this->equalTo('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>'));
  102. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  103. }
  104. public function testDelete() {
  105. $gid = 'myGroup';
  106. $this->input->method('getArgument')
  107. ->willReturnCallback(function ($arg) use ($gid) {
  108. if ($arg === 'groupid') {
  109. return $gid;
  110. }
  111. throw new \Exception();
  112. });
  113. $group = $this->createMock(IGroup::class);
  114. $group->method('delete')
  115. ->willReturn(true);
  116. $this->groupManager->method('groupExists')
  117. ->with($gid)
  118. ->willReturn(true);
  119. $this->groupManager->method('get')
  120. ->with($gid)
  121. ->willReturn($group);
  122. $this->output->expects($this->once())
  123. ->method('writeln')
  124. ->with($this->equalTo('Group "' . $gid . '" was removed'));
  125. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  126. }
  127. }