ListCommandTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 Affero 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\ListCommand;
  25. use OCP\IGroup;
  26. use OCP\IGroupManager;
  27. use OCP\IUser;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Test\TestCase;
  31. class ListCommandTest extends TestCase {
  32. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  33. private $groupManager;
  34. /** @var ListCommand|\PHPUnit_Framework_MockObject_MockObject */
  35. private $command;
  36. /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */
  37. private $input;
  38. /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */
  39. private $output;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->groupManager = $this->createMock(IGroupManager::class);
  43. $this->command = $this->getMockBuilder(ListCommand::class)
  44. ->setConstructorArgs([$this->groupManager])
  45. ->setMethods(['writeArrayInOutputFormat'])
  46. ->getMock();
  47. $this->input = $this->createMock(InputInterface::class);
  48. $this->input->method('getOption')
  49. ->willReturnCallback(function($arg) {
  50. if ($arg === 'limit') {
  51. return '100';
  52. } else if ($arg === 'offset') {
  53. return '42';
  54. }
  55. throw new \Exception();
  56. });
  57. $this->output = $this->createMock(OutputInterface::class);
  58. }
  59. public function testExecute() {
  60. $group1 = $this->createMock(IGroup::class);
  61. $group1->method('getGID')->willReturn('group1');
  62. $group2 = $this->createMock(IGroup::class);
  63. $group2->method('getGID')->willReturn('group2');
  64. $group3 = $this->createMock(IGroup::class);
  65. $group3->method('getGID')->willReturn('group3');
  66. $user = $this->createMock(IUser::class);
  67. $this->groupManager->method('search')
  68. ->with(
  69. '',
  70. 100,
  71. 42
  72. )->willReturn([$group1, $group2, $group3]);
  73. $group1->method('getUsers')
  74. ->willReturn([
  75. 'user1' => $user,
  76. 'user2' => $user,
  77. ]);
  78. $group2->method('getUsers')
  79. ->willReturn([
  80. ]);
  81. $group3->method('getUsers')
  82. ->willReturn([
  83. 'user1' => $user,
  84. 'user3' => $user,
  85. ]);
  86. $this->command->expects($this->once())
  87. ->method('writeArrayInOutputFormat')
  88. ->with(
  89. $this->equalTo($this->input),
  90. $this->equalTo($this->output),
  91. [
  92. 'group1' => [
  93. 'user1',
  94. 'user2',
  95. ],
  96. 'group2' => [
  97. ],
  98. 'group3' => [
  99. 'user1',
  100. 'user3',
  101. ]
  102. ]
  103. );
  104. $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
  105. }
  106. }