AddTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Add;
  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 AddTest extends TestCase {
  31. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  32. private $groupManager;
  33. /** @var Add */
  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 Add($this->groupManager);
  43. $this->input = $this->createMock(InputInterface::class);
  44. $this->input->method('getArgument')
  45. ->willReturnCallback(function ($arg) {
  46. if ($arg === 'groupid') {
  47. return 'myGroup';
  48. }
  49. throw new \Exception();
  50. });
  51. $this->output = $this->createMock(OutputInterface::class);
  52. }
  53. public function testGroupExists() {
  54. $gid = 'myGroup';
  55. $group = $this->createMock(IGroup::class);
  56. $this->groupManager->method('get')
  57. ->with($gid)
  58. ->willReturn($group);
  59. $this->groupManager->expects($this->never())
  60. ->method('createGroup');
  61. $this->output->expects($this->once())
  62. ->method('writeln')
  63. ->with($this->equalTo('<error>Group "' . $gid . '" already exists.</error>'));
  64. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  65. }
  66. public function testAdd() {
  67. $gid = 'myGroup';
  68. $group = $this->createMock(IGroup::class);
  69. $group->method('getGID')
  70. ->willReturn($gid);
  71. $this->groupManager->method('createGroup')
  72. ->willReturn($group);
  73. $this->groupManager->expects($this->once())
  74. ->method('createGroup')
  75. ->with($this->equalTo($gid));
  76. $this->output->expects($this->once())
  77. ->method('writeln')
  78. ->with($this->equalTo('Created group "' . $group->getGID() . '"'));
  79. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  80. }
  81. }