InfoTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 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\Info;
  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 InfoTest extends TestCase {
  14. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  15. private $groupManager;
  16. /** @var Info|\PHPUnit\Framework\MockObject\MockObject */
  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 = $this->getMockBuilder(Info::class)
  26. ->setConstructorArgs([$this->groupManager])
  27. ->setMethods(['writeArrayInOutputFormat'])
  28. ->getMock();
  29. $this->input = $this->createMock(InputInterface::class);
  30. $this->output = $this->createMock(OutputInterface::class);
  31. }
  32. public function testDoesNotExists() {
  33. $gid = 'myGroup';
  34. $this->input->method('getArgument')
  35. ->willReturnCallback(function ($arg) use ($gid) {
  36. if ($arg === 'groupid') {
  37. return $gid;
  38. }
  39. throw new \Exception();
  40. });
  41. $this->groupManager->method('get')
  42. ->with($gid)
  43. ->willReturn(null);
  44. $this->output->expects($this->once())
  45. ->method('writeln')
  46. ->with($this->equalTo('<error>Group "' . $gid . '" does not exist.</error>'));
  47. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  48. }
  49. public function testInfo() {
  50. $gid = 'myGroup';
  51. $this->input->method('getArgument')
  52. ->willReturnCallback(function ($arg) use ($gid) {
  53. if ($arg === 'groupid') {
  54. return $gid;
  55. }
  56. throw new \Exception();
  57. });
  58. $group = $this->createMock(IGroup::class);
  59. $group->method('getGID')->willReturn($gid);
  60. $group->method('getDisplayName')
  61. ->willReturn('My Group');
  62. $group->method('getBackendNames')
  63. ->willReturn(['Database']);
  64. $this->groupManager->method('get')
  65. ->with($gid)
  66. ->willReturn($group);
  67. $this->command->expects($this->once())
  68. ->method('writeArrayInOutputFormat')
  69. ->with(
  70. $this->equalTo($this->input),
  71. $this->equalTo($this->output),
  72. [
  73. 'groupID' => 'myGroup',
  74. 'displayName' => 'My Group',
  75. 'backends' => ['Database'],
  76. ]
  77. );
  78. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  79. }
  80. }