|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright 2018, Denis Mosolov <[email protected]> |
| 4 | + * |
| 5 | + * @author Denis Mosolov <[email protected]> |
| 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 | + |
| 25 | +use OC\Core\Command\Group\Add; |
| 26 | +use OCP\IGroup; |
| 27 | +use OCP\IGroupManager; |
| 28 | +use Symfony\Component\Console\Input\InputInterface; |
| 29 | +use Symfony\Component\Console\Output\OutputInterface; |
| 30 | +use Test\TestCase; |
| 31 | + |
| 32 | +class AddTest extends TestCase { |
| 33 | + |
| 34 | + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + private $groupManager; |
| 36 | + |
| 37 | + /** @var Add */ |
| 38 | + private $command; |
| 39 | + |
| 40 | + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 41 | + private $input; |
| 42 | + |
| 43 | + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 44 | + private $output; |
| 45 | + |
| 46 | + public function setUp() { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->groupManager = $this->createMock(IGroupManager::class); |
| 50 | + $this->command = new Add($this->groupManager); |
| 51 | + |
| 52 | + $this->input = $this->createMock(InputInterface::class); |
| 53 | + $this->input->method('getArgument') |
| 54 | + ->willReturnCallback(function($arg) { |
| 55 | + if ($arg === 'groupid') { |
| 56 | + return 'myGroup'; |
| 57 | + } |
| 58 | + throw new \Exception(); |
| 59 | + }); |
| 60 | + $this->output = $this->createMock(OutputInterface::class); |
| 61 | + } |
| 62 | + |
| 63 | + public function testGroupExists() { |
| 64 | + $gid = 'myGroup'; |
| 65 | + $group = $this->createMock(IGroup::class); |
| 66 | + $this->groupManager->method('get') |
| 67 | + ->with($gid) |
| 68 | + ->willReturn($group); |
| 69 | + |
| 70 | + $this->groupManager->expects($this->never()) |
| 71 | + ->method('createGroup'); |
| 72 | + $this->output->expects($this->once()) |
| 73 | + ->method('writeln') |
| 74 | + ->with($this->equalTo('<error>Group "' . $gid . '" already exists.</error>')); |
| 75 | + |
| 76 | + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); |
| 77 | + } |
| 78 | + |
| 79 | + public function testAdd() { |
| 80 | + $gid = 'myGroup'; |
| 81 | + $group = $this->createMock(IGroup::class); |
| 82 | + $group->method('getGID') |
| 83 | + ->willReturn($gid); |
| 84 | + $this->groupManager->method('createGroup') |
| 85 | + ->willReturn($group); |
| 86 | + |
| 87 | + $this->groupManager->expects($this->once()) |
| 88 | + ->method('createGroup') |
| 89 | + ->with($this->equalTo($gid)); |
| 90 | + $this->output->expects($this->once()) |
| 91 | + ->method('writeln') |
| 92 | + ->with($this->equalTo('Created group "' . $group->getGID() . '"')); |
| 93 | + |
| 94 | + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +} |
0 commit comments