Move talk:room:* command and unit test DI to traits

Signed-off-by: Daniel Rudolf <github.com@daniel-rudolf.de>
This commit is contained in:
Daniel Rudolf 2020-05-29 19:04:04 +02:00
Родитель 63a883c6d8
Коммит 0b13f861f8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A061F02CD8DE4538
16 изменённых файлов: 77 добавлений и 121 удалений

Просмотреть файл

@ -28,7 +28,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -39,15 +38,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Add extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:add')

Просмотреть файл

@ -27,7 +27,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Manager;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@ -37,15 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Create extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:create')

Просмотреть файл

@ -27,7 +27,6 @@ namespace OCA\Talk\Command\Room;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -37,15 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Delete extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:delete')

Просмотреть файл

@ -28,7 +28,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -38,15 +37,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Demote extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:demote')

Просмотреть файл

@ -28,7 +28,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -38,15 +37,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Promote extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:promote')

Просмотреть файл

@ -28,7 +28,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -38,15 +37,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Remove extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:remove')

Просмотреть файл

@ -32,12 +32,38 @@ use OCA\Talk\Manager;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
trait TRoomCommand {
/** @var Manager */
protected $manager;
/** @var IUserManager */
protected $userManager;
/** @var IGroupManager */
protected $groupManager;
/**
* TRoomCommand constructor.
*
* @param Manager $manager
* @param IUserManager $userManager
* @param IGroupManager $groupManager
*/
public function __construct(Manager $manager, IUserManager $userManager, IGroupManager $groupManager) {
parent::__construct();
$this->manager = $manager;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
}
/**
* @param Room $room
* @param string $name
@ -169,11 +195,9 @@ trait TRoomCommand {
return;
}
$groupManager = \OC::$server->getGroupManager();
$users = [];
foreach ($groupIds as $groupId) {
$group = $groupManager->get($groupId);
$group = $this->groupManager->get($groupId);
if ($group === null) {
throw new InvalidArgumentException(sprintf("Group '%s' not found.", $groupId));
}
@ -199,11 +223,9 @@ trait TRoomCommand {
return;
}
$userManager = \OC::$server->getUserManager();
$participants = [];
foreach ($userIds as $userId) {
$user = $userManager->get($userId);
$user = $this->userManager->get($userId);
if ($user === null) {
throw new InvalidArgumentException(sprintf("User '%s' not found.", $userId));
}
@ -237,8 +259,6 @@ trait TRoomCommand {
* @throws InvalidArgumentException
*/
protected function removeRoomParticipants(Room $room, array $userIds): void {
$userManager = \OC::$server->getUserManager();
$users = [];
foreach ($userIds as $userId) {
try {
@ -247,7 +267,7 @@ trait TRoomCommand {
throw new InvalidArgumentException(sprintf("User '%s' is no participant.", $userId));
}
$users[] = $userManager->get($userId);
$users[] = $this->userManager->get($userId);
}
foreach ($users as $user) {
@ -306,25 +326,21 @@ trait TRoomCommand {
}
protected function completeTokenValues(CompletionContext $context): array {
/** @var Manager $roomManager */
$roomManager = \OC::$server->query(Manager::class);
return array_map(function (Room $room) {
return $room->getToken();
}, $roomManager->searchRoomsByToken($context->getCurrentWord()));
}, $this->manager->searchRoomsByToken($context->getCurrentWord()));
}
protected function completeUserValues(CompletionContext $context): array {
$userManager = \OC::$server->getUserManager();
return array_map(function (IUser $user) {
return $user->getUID();
}, $userManager->search($context->getCurrentWord()));
}, $this->userManager->search($context->getCurrentWord()));
}
protected function completeGroupValues(CompletionContext $context): array {
$groupManager = \OC::$server->getGroupManager();
return array_map(function (IGroup $group) {
return $group->getGID();
}, $groupManager->search($context->getCurrentWord()));
}, $this->groupManager->search($context->getCurrentWord()));
}
protected function completeParticipantValues(CompletionContext $context): array {
@ -347,11 +363,8 @@ trait TRoomCommand {
return [];
}
/** @var Manager $roomManager */
$roomManager = \OC::$server->query(Manager::class);
try {
$room = $roomManager->getRoomByToken($token);
$room = $this->manager->getRoomByToken($token);
} catch (RoomNotFoundException $e) {
return [];
}

Просмотреть файл

@ -28,7 +28,6 @@ namespace OCA\Talk\Command\Room;
use InvalidArgumentException;
use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
@ -39,15 +38,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Update extends Base {
use TRoomCommand;
/** @var Manager */
public $manager;
public function __construct(Manager $manager) {
parent::__construct();
$this->manager = $manager;
}
protected function configure(): void {
$this
->setName('talk:room:update')

Просмотреть файл

@ -50,14 +50,14 @@ class AddTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(Manager::class);
$this->command = new Add($this->manager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Add($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->createTestUserMocks();
$this->createTestGroupMocks();
}

Просмотреть файл

@ -49,14 +49,14 @@ class CreateTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(Manager::class);
$this->command = new Create($this->manager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Create($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->createTestUserMocks();
$this->createTestGroupMocks();
}

Просмотреть файл

@ -49,8 +49,11 @@ class DeleteTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Delete($this->manager);
$this->command = new Delete($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);
}

Просмотреть файл

@ -50,14 +50,14 @@ class DemoteTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(Manager::class);
$this->command = new Demote($this->manager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Demote($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->createTestUserMocks();
$this->createTestGroupMocks();
}

Просмотреть файл

@ -50,14 +50,14 @@ class PromoteTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(Manager::class);
$this->command = new Promote($this->manager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Promote($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);
$this->createTestUserMocks();
$this->createTestGroupMocks();
}

Просмотреть файл

@ -50,8 +50,11 @@ class RemoveTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Remove($this->manager);
$this->command = new Remove($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);

Просмотреть файл

@ -32,6 +32,12 @@ use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
trait TRoomCommandTest {
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IGroupManager|MockObject */
protected $groupManager;
/** @var IUser[] */
private $userMocks;
@ -39,13 +45,12 @@ trait TRoomCommandTest {
private $groupMocks;
protected function registerUserManagerMock(): void {
/** @var IUserManager|MockObject $userManager */
$userManager = $this->createMock(IUserManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$userManager->method('get')
$this->userManager->method('get')
->willReturnCallback([$this, 'getUserMock']);
$this->overwriteService(IUserManager::class, $userManager);
$this->overwriteService(IUserManager::class, $this->userManager);
}
protected function createTestUserMocks(): void {
@ -73,13 +78,12 @@ trait TRoomCommandTest {
}
protected function registerGroupManagerMock(): void {
/** @var IGroupManager|MockObject $groupManager */
$groupManager = $this->createMock(IGroupManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$groupManager->method('get')
$this->groupManager->method('get')
->willReturnCallback([$this, 'getGroupMock']);
$this->overwriteService(IGroupManager::class, $groupManager);
$this->overwriteService(IGroupManager::class, $this->groupManager);
}
protected function createTestGroupMocks(): void {

Просмотреть файл

@ -50,8 +50,11 @@ class UpdateTest extends TestCase {
public function setUp(): void {
parent::setUp();
$this->registerUserManagerMock();
$this->registerGroupManagerMock();
$this->manager = $this->createMock(Manager::class);
$this->command = new Update($this->manager);
$this->command = new Update($this->manager, $this->userManager, $this->groupManager);
$this->roomMockContainer = new RoomMockContainer($this);