chore(tests): UT for UserToUserGroup service

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-04-11 06:36:34 +02:00
parent 465fec9935
commit 2b8b593a41
13 changed files with 562 additions and 196 deletions

View File

@@ -38,7 +38,7 @@ use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Storage\Ports\FileCacheService;
use SP\Domain\User\Dtos\UserDataDto;
use SP\Domain\User\Ports\UserToUserGroupServiceInterface;
use SP\Domain\User\Ports\UserToUserGroupService;
use SP\Infrastructure\File\FileException;
use function SP\processException;
@@ -60,10 +60,10 @@ final class AccountAcl extends Service implements AccountAclService
private UserDataDto $userData;
public function __construct(
Application $application,
private readonly AclInterface $acl,
private readonly UserToUserGroupServiceInterface $userToUserGroupService,
private readonly ?FileCacheService $fileCache = null
Application $application,
private readonly AclInterface $acl,
private readonly UserToUserGroupService $userToUserGroupService,
private readonly ?FileCacheService $fileCache = null
) {
parent::__construct($application);

View File

@@ -28,7 +28,7 @@ use SP\DataModel\ItemSearchData;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Tag\Models\Tag;
use SP\Domain\Tag\Models\Tag as TagModel;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
@@ -41,6 +41,7 @@ use SP\Infrastructure\Database\QueryResult;
interface TagService
{
/**
* @return QueryResult<TagModel>
* @throws ConstraintException
* @throws QueryException
*/
@@ -51,14 +52,14 @@ interface TagService
* @throws QueryException
* @throws NoSuchItemException
*/
public function getById(int $id): Tag;
public function getById(int $id): TagModel;
/**
* @throws NoSuchItemException
* @throws ConstraintException
* @throws QueryException
*/
public function getByName(string $name): ?Tag;
public function getByName(string $name): ?TagModel;
/**
* @throws ConstraintException
@@ -68,30 +69,30 @@ interface TagService
public function delete(int $id): TagService;
/**
* @param int[] $ids
* @param int[] $ids
*
* @throws SPException
*/
public function deleteByIdBatch(array $ids): TagService;
public function deleteByIdBatch(array $ids): void;
/**
* @throws ConstraintException
* @throws QueryException
* @throws DuplicatedItemException
*/
public function create(Tag $itemData): int;
public function create(TagModel $tag): int;
/**
* @throws SPException
* @throws ConstraintException
* @throws QueryException
*/
public function update(Tag $itemData): int;
public function update(TagModel $tag): int;
/**
* Get all items from the service's repository
*
* @return Tag[]
* @return array<TagModel>
* @throws ConstraintException
* @throws QueryException
*/

View File

@@ -42,8 +42,6 @@ use function SP\__u;
/**
* Class Tag
*
* @template T of TagModel
*/
final class Tag extends Service implements TagService
{
@@ -55,7 +53,7 @@ final class Tag extends Service implements TagService
/**
* @param ItemSearchData $itemSearchData
* @return QueryResult<T>
* @return QueryResult<TagModel>
*/
public function search(ItemSearchData $itemSearchData): QueryResult
{
@@ -113,13 +111,11 @@ final class Tag extends Service implements TagService
*
* @throws SPException
*/
public function deleteByIdBatch(array $ids): TagService
public function deleteByIdBatch(array $ids): void
{
if ($this->tagRepository->deleteByIdBatch($ids) !== count($ids)) {
if ($this->tagRepository->deleteByIdBatch($ids)->getAffectedNumRows() !== count($ids)) {
throw ServiceException::warning(__u('Error while removing the tags'));
}
return $this;
}
/**
@@ -127,9 +123,9 @@ final class Tag extends Service implements TagService
* @throws QueryException
* @throws DuplicatedItemException
*/
public function create(TagModel $itemData): int
public function create(TagModel $tag): int
{
return $this->tagRepository->create($itemData)->getLastId();
return $this->tagRepository->create($tag)->getLastId();
}
/**
@@ -137,15 +133,15 @@ final class Tag extends Service implements TagService
* @throws ConstraintException
* @throws QueryException
*/
public function update(TagModel $itemData): int
public function update(TagModel $tag): int
{
return $this->tagRepository->update($itemData);
return $this->tagRepository->update($tag);
}
/**
* Get all items from the service's repository
*
* @return array<T>
* @return array<TagModel>
*/
public function getAll(): array
{

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -26,14 +26,13 @@ namespace SP\Domain\User\Ports;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
/**
* Class UserToUserGroupService
*
* @package SP\Domain\Common\Services\UserGroup
*/
interface UserToUserGroupServiceInterface
interface UserToUserGroupService
{
/**
* @throws ConstraintException
@@ -53,21 +52,6 @@ interface UserToUserGroupServiceInterface
*/
public function getUsersByGroupId(int $id): array;
/**
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function getById(int $id): array;
/**
* Checks whether the user is included in the group
*
* @throws ConstraintException
* @throws QueryException
*/
public function checkUserInGroup(int $groupId, int $userId): bool;
/**
* Returns the groups which the user belongs to
*

View File

@@ -33,7 +33,7 @@ use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\UserGroup as UserGroupModel;
use SP\Domain\User\Ports\UserGroupRepository;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserToUserGroupServiceInterface;
use SP\Domain\User\Ports\UserToUserGroupService;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
@@ -47,9 +47,9 @@ use function SP\__u;
final class UserGroup extends Service implements UserGroupService
{
public function __construct(
Application $application,
private readonly UserGroupRepository $userGroupRepository,
private readonly UserToUserGroupServiceInterface $userToUserGroupService,
Application $application,
private readonly UserGroupRepository $userGroupRepository,
private readonly UserToUserGroupService $userToUserGroupService,
) {
parent::__construct($application);
}

View File

@@ -0,0 +1,96 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Domain\User\Services;
use SP\Core\Application;
use SP\Domain\Common\Services\Service;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\UserToUserGroup as UserToUserGroupModel;
use SP\Domain\User\Ports\UserToUserGroupRepository;
use SP\Domain\User\Ports\UserToUserGroupService;
/**
* Class UserToUserGroup
*/
final class UserToUserGroup extends Service implements UserToUserGroupService
{
public function __construct(
Application $application,
private readonly UserToUserGroupRepository $userToUserGroupRepository
) {
parent::__construct($application);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function add(int $id, array $users): int
{
return $this->userToUserGroupRepository->add($id, $users)->getLastId();
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function update(int $id, array $users): int
{
if (count($users) === 0) {
return $this->userToUserGroupRepository->delete($id)->getAffectedNumRows();
}
return $this->userToUserGroupRepository->update($id, $users)->getAffectedNumRows();
}
/**
* @param int $id
* @return array
*/
public function getUsersByGroupId(int $id): array
{
return array_map(
static fn(UserToUserGroupModel $userToUserGroup) => $userToUserGroup->getUserId(),
$this->userToUserGroupRepository
->getById($id)
->getDataAsArray(UserToUserGroupModel::class)
);
}
/**
* Returns the groups which the user belongs to
*
* @param int $userId
* @return array
*/
public function getGroupsForUser(int $userId): array
{
return $this->userToUserGroupRepository
->getGroupsForUser($userId)
->getDataAsArray(UserToUserGroupModel::class);
}
}

View File

@@ -1,130 +0,0 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Domain\User\Services;
use SP\Core\Application;
use SP\Domain\Common\Services\Service;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\User\Models\UserToUserGroup;
use SP\Domain\User\Ports\UserToUserGroupRepository;
use SP\Domain\User\Ports\UserToUserGroupServiceInterface;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
/**
* Class UserToUserGroupService
*
* @package SP\Domain\Common\Services\UserGroup
*/
final class UserToUserGroupService extends Service implements UserToUserGroupServiceInterface
{
protected UserToUserGroupRepository $userToUserGroupRepository;
public function __construct(Application $application, UserToUserGroupRepository $userToUserGroupRepository)
{
parent::__construct($application);
$this->userToUserGroupRepository = $userToUserGroupRepository;
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function add(int $id, array $users): int
{
return $this->userToUserGroupRepository->add($id, $users);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function update(int $id, array $users): int
{
if (count($users) === 0) {
return $this->userToUserGroupRepository->delete($id);
}
return $this->userToUserGroupRepository->update($id, $users);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function getUsersByGroupId(int $id): array
{
$usersId = [];
/** @var \SP\Domain\User\Models\UserToUserGroup $userToUserGroupData */
$userByGroup = $this->userToUserGroupRepository->getById($id)->getDataAsArray();
foreach ($userByGroup as $userToUserGroupData) {
$usersId[] = $userToUserGroupData->getUserId();
}
return $usersId;
}
/**
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function getById(int $id): array
{
$result = $this->userToUserGroupRepository->getById($id);
if ($result->getNumRows() === 0) {
throw new NoSuchItemException(__u('Group not found'), SPException::INFO);
}
return $result->getDataAsArray();
}
/**
* Checks whether the user is included in the group
*
* @throws ConstraintException
* @throws QueryException
*/
public function checkUserInGroup(int $groupId, int $userId): bool
{
return $this->userToUserGroupRepository->checkUserInGroup($groupId, $userId);
}
/**
* Returns the groups which the user belongs to
*
* @throws ConstraintException
* @throws QueryException
*/
public function getGroupsForUser(int $userId): array
{
return $this->userToUserGroupRepository->getGroupsForUser($userId)->getDataAsArray();
}
}

View File

@@ -69,6 +69,8 @@ final class UserToUserGroup extends BaseRepository implements UserToUserGroupRep
* @param int $userId
*
* @return QueryResult<T>
* @throws ConstraintException
* @throws QueryException
*/
public function getGroupsForUser(int $userId): QueryResult
{
@@ -158,6 +160,8 @@ final class UserToUserGroup extends BaseRepository implements UserToUserGroupRep
* @param $id int
*
* @return QueryResult<T>
* @throws ConstraintException
* @throws QueryException
*/
public function getById(int $id): QueryResult
{

View File

@@ -41,7 +41,7 @@ use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Storage\Ports\FileCacheService;
use SP\Domain\User\Dtos\UserDataDto;
use SP\Domain\User\Models\User;
use SP\Domain\User\Ports\UserToUserGroupServiceInterface;
use SP\Domain\User\Ports\UserToUserGroupService;
use SP\Infrastructure\File\FileException;
use SPT\Generators\UserDataGenerator;
use SPT\UnitaryTestCase;
@@ -67,8 +67,8 @@ class AccountAclTest extends UnitaryTestCase
AclActionsInterface::ACCOUNT_DELETE,
];
private static array $accounts;
private Acl $acl;
private UserToUserGroupServiceInterface|MockObject $userToUserGroupService;
private Acl $acl;
private UserToUserGroupService|MockObject $userToUserGroupService;
public static function setUpBeforeClass(): void
{
@@ -684,7 +684,7 @@ class AccountAclTest extends UnitaryTestCase
self::$faker->unixTime
);
$userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$userToUserGroupService->method('getGroupsForUser')->willReturn([]);
$fileCache = $this->createMock(FileCacheService::class);
$actions = $this->createMock(ActionsInterface::class);
@@ -729,7 +729,7 @@ class AccountAclTest extends UnitaryTestCase
self::$faker->unixTime
);
$userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$userToUserGroupService->method('getGroupsForUser')->willReturn([]);
$fileCache = $this->createMock(FileCacheService::class);
$actions = $this->createMock(ActionsInterface::class);
@@ -776,7 +776,7 @@ class AccountAclTest extends UnitaryTestCase
self::$faker->unixTime
);
$userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$userToUserGroupService->method('getGroupsForUser')->willReturn([]);
$fileCache = $this->createMock(FileCacheService::class);
$actions = $this->createMock(ActionsInterface::class);
@@ -812,7 +812,7 @@ class AccountAclTest extends UnitaryTestCase
self::$faker->unixTime
);
$userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$userToUserGroupService->method('getGroupsForUser')->willReturn([]);
$fileCache = $this->createMock(FileCacheService::class);
$actions = $this->createMock(ActionsInterface::class);
@@ -844,7 +844,7 @@ class AccountAclTest extends UnitaryTestCase
$actions = $this->createMock(ActionsInterface::class);
$this->acl = new Acl($this->context, $this->application->getEventDispatcher(), $actions);
$this->userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$this->userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$this->userToUserGroupService->method('getGroupsForUser')
->willReturnMap([
[1, [new Simple(['userGroupId' => 2])]],

View File

@@ -145,7 +145,7 @@ class ClientTest extends UnitaryTestCase
->expects(self::once())
->method('delete')
->with($id)
->willReturn(new QueryResult([]));
->willReturn(new QueryResult());
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Client not found');
@@ -275,11 +275,6 @@ class ClientTest extends UnitaryTestCase
$this->client->deleteByIdBatch($ids);
}
/**
* @throws ConstraintException
* @throws SPException
* @throws QueryException
*/
public function testGetAll()
{
$client = ClientGenerator::factory()->buildClient();

View File

@@ -0,0 +1,280 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SPT\Domain\Tag\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Tag\Ports\TagRepository;
use SP\Domain\Tag\Services\Tag;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
use SPT\Generators\ItemSearchDataGenerator;
use SPT\Generators\TagGenerator;
use SPT\UnitaryTestCase;
/**
* Class TagTest
*/
#[Group('unitary')]
class TagTest extends UnitaryTestCase
{
private Tag $tag;
private MockObject|TagRepository $tagRepository;
/**
* @throws ConstraintException
* @throws NoSuchItemException
* @throws QueryException
*/
public function testDelete()
{
$id = self::$faker->randomNumber();
$this->tagRepository
->expects(self::once())
->method('delete')
->with($id)
->willReturn(new QueryResult(null, 1));
$this->tag->delete($id);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testDeleteWithNotFound()
{
$id = self::$faker->randomNumber();
$this->tagRepository
->expects(self::once())
->method('delete')
->with($id)
->willReturn(new QueryResult());
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Tag not found');
$this->tag->delete($id);
}
/**
* @throws NoSuchItemException
*/
public function testGetById()
{
$id = self::$faker->randomNumber();
$tag = TagGenerator::factory()->buildTag();
$this->tagRepository
->expects(self::once())
->method('getById')
->with($id)
->willReturn(new QueryResult([$tag]));
$out = $this->tag->getById($id);
$this->assertEquals($tag, $out);
}
/**
* @throws NoSuchItemException
*/
public function testGetByIdWithUnknownId()
{
$id = self::$faker->randomNumber();
$this->tagRepository
->expects(self::once())
->method('getById')
->with($id)
->willReturn(new QueryResult([]));
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Tag not found');
$this->tag->getById($id);
}
/**
* @throws NoSuchItemException
*/
public function testGetByName()
{
$name = self::$faker->colorName();
$tag = TagGenerator::factory()->buildTag();
$this->tagRepository
->expects(self::once())
->method('getByName')
->with($name)
->willReturn(new QueryResult([$tag]));
$out = $this->tag->getByName($name);
$this->assertEquals($tag, $out);
}
/**
* @throws NoSuchItemException
*/
public function testGetByNameWithUnknownName()
{
$name = self::$faker->colorName();
$this->tagRepository
->expects(self::once())
->method('getByName')
->with($name)
->willReturn(new QueryResult([]));
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Tag not found');
$this->tag->getByName($name);
}
public function testGetAll()
{
$tag = TagGenerator::factory()->buildTag();
$this->tagRepository
->expects(self::once())
->method('getAll')
->willReturn(new QueryResult([$tag]));
$out = $this->tag->getAll();
$this->assertEquals([$tag], $out);
}
/**
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testCreate()
{
$tag = TagGenerator::factory()->buildTag();
$queryResult = new QueryResult(null, 0, self::$faker->randomNumber());
$this->tagRepository
->expects(self::once())
->method('create')
->with($tag)
->willReturn($queryResult);
$out = $this->tag->create($tag);
$this->assertEquals($queryResult->getLastId(), $out);
}
/**
* @throws SPException
*/
public function testDeleteByIdBatch()
{
$ids = array_map(fn() => self::$faker->randomNumber(), range(0, 4));
$this->tagRepository
->expects(self::once())
->method('deleteByIdBatch')
->with($ids)
->willReturn(new QueryResult(null, 5));
$this->tag->deleteByIdBatch($ids);
}
/**
* @throws ServiceException
* @throws SPException
*/
public function testDeleteByIdBatchError()
{
$ids = array_map(fn() => self::$faker->randomNumber(), range(0, 4));
$this->tagRepository
->expects(self::once())
->method('deleteByIdBatch')
->with($ids)
->willReturn(new QueryResult(null, 0));
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Error while removing the tags');
$this->tag->deleteByIdBatch($ids);
}
/**
* @throws ConstraintException
* @throws SPException
* @throws QueryException
*/
public function testUpdate()
{
$tag = TagGenerator::factory()->buildTag();
$this->tagRepository
->expects(self::once())
->method('update')
->with($tag)
->willReturn(1);
$this->tag->update($tag);
}
public function testSearch()
{
$itemSearch = ItemSearchDataGenerator::factory()->buildItemSearchData();
$this->tagRepository
->expects(self::once())
->method('search')
->with($itemSearch);
$this->tag->search($itemSearch);
}
protected function setUp(): void
{
parent::setUp();
$this->tagRepository = $this->createMock(TagRepository::class);
$this->tag = new Tag($this->application, $this->tagRepository);
}
}

View File

@@ -32,7 +32,7 @@ use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\UserGroup as UserGroupModel;
use SP\Domain\User\Ports\UserGroupRepository;
use SP\Domain\User\Ports\UserToUserGroupServiceInterface;
use SP\Domain\User\Ports\UserToUserGroupService;
use SP\Domain\User\Services\UserGroup;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
@@ -47,9 +47,9 @@ use SPT\UnitaryTestCase;
class UserGroupTest extends UnitaryTestCase
{
private MockObject|UserGroupRepository $userGroupRepository;
private UserToUserGroupServiceInterface|MockObject $userToUserGroupService;
private UserGroup $userGroup;
private MockObject|UserGroupRepository $userGroupRepository;
private UserToUserGroupService|MockObject $userToUserGroupService;
private UserGroup $userGroup;
/**
* @throws ConstraintException
@@ -372,7 +372,7 @@ class UserGroupTest extends UnitaryTestCase
UserGroupRepositoryStub::class,
$userGroupRepositoryMethods
);
$this->userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class);
$this->userToUserGroupService = $this->createMock(UserToUserGroupService::class);
$this->userGroup = new UserGroup($this->application, $this->userGroupRepository, $this->userToUserGroupService);
}

View File

@@ -0,0 +1,140 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SPT\Domain\User\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\UserToUserGroup as UserToUserGroupModel;
use SP\Domain\User\Ports\UserToUserGroupRepository;
use SP\Domain\User\Services\UserToUserGroup;
use SP\Infrastructure\Database\QueryResult;
use SPT\UnitaryTestCase;
/**
* Class UserToUserGroupTest
*/
#[Group('unitary')]
class UserToUserGroupTest extends UnitaryTestCase
{
private UserToUserGroup $userToUserGroup;
private UserToUserGroupRepository|MockObject $userToUserGroupRepository;
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testUpdate()
{
$this->userToUserGroupRepository
->expects($this->once())
->method('update')
->with(100, [1, 2, 3])
->willReturn(new QueryResult(null, 10));
$this->assertEquals(10, $this->userToUserGroup->update(100, [1, 2, 3]));
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testUpdateWithNoUsers()
{
$this->userToUserGroupRepository
->expects($this->once())
->method('delete')
->with(100)
->willReturn(new QueryResult(null, 10));
$this->assertEquals(10, $this->userToUserGroup->update(100, []));
}
public function testGetUsersByGroupId()
{
$userToUserGroup = new UserToUserGroupModel(
[
'userGroupId' => self::$faker->randomNumber(3),
'userId' => self::$faker->randomNumber(3),
'users' => [100, 200, 300]
]
);
$this->userToUserGroupRepository
->expects($this->once())
->method('getById')
->with(100)
->willReturn(new QueryResult([$userToUserGroup]));
$out = $this->userToUserGroup->getUsersByGroupId(100);
$this->assertEquals([$userToUserGroup->getUserId()], $out);
}
public function testGetGroupsForUser()
{
$userToUserGroup = new UserToUserGroupModel(
[
'userGroupId' => self::$faker->randomNumber(3),
'userId' => self::$faker->randomNumber(3),
'users' => [100, 200, 300]
]
);
$this->userToUserGroupRepository
->expects($this->once())
->method('getGroupsForUser')
->with(100)
->willReturn(new QueryResult([$userToUserGroup]));
$out = $this->userToUserGroup->getGroupsForUser(100);
$this->assertEquals([$userToUserGroup], $out);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testAdd()
{
$this->userToUserGroupRepository
->expects($this->once())
->method('add')
->with(100, [1, 2, 3])
->willReturn(new QueryResult(null, 0, 10));
$this->assertEquals(10, $this->userToUserGroup->add(100, [1, 2, 3]));
}
protected function setUp(): void
{
parent::setUp();
$this->userToUserGroupRepository = $this->createMock(UserToUserGroupRepository::class);
$this->userToUserGroup = new UserToUserGroup($this->application, $this->userToUserGroupRepository);
}
}