chore(tests): UT for UserProfile service

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-04-07 08:42:47 +02:00
parent f8ce2f1821
commit 1fd39467fd
19 changed files with 387 additions and 129 deletions

View File

@@ -34,7 +34,7 @@ use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Html\DataGrid\DataGridTab;
use SP\Modules\Web\Controllers\ControllerBase;
@@ -61,9 +61,9 @@ final class IndexController extends ControllerBase
private AuthTokenGrid $authTokenGrid;
private PublicLinkGrid $publicLinkGrid;
private UserServiceInterface $userService;
private UserGroupService $userGroupService;
private UserProfileServiceInterface $userProfileService;
private AuthTokenService $authTokenService;
private UserGroupService $userGroupService;
private UserProfileService $userProfileService;
private AuthTokenService $authTokenService;
private PublicLinkService $publicLinkService;
public function __construct(
@@ -77,7 +77,7 @@ final class IndexController extends ControllerBase
PublicLinkGrid $publicLinkGrid,
UserServiceInterface $userService,
UserGroupService $userGroupService,
UserProfileServiceInterface $userProfileService,
UserProfileService $userProfileService,
AuthTokenService $authTokenService,
PublicLinkService $publicLinkService
) {

View File

@@ -49,7 +49,7 @@ use SP\Domain\Export\Services\BackupFileHelper;
use SP\Domain\Export\Services\XmlExport;
use SP\Domain\Task\Services\Task;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\DatabaseUtil;
@@ -75,26 +75,26 @@ final class IndexController extends ControllerBase
{
protected TabsHelper $tabsHelper;
private UserServiceInterface $userService;
private UserGroupService $userGroupService;
private UserProfileServiceInterface $userProfileService;
private MimeTypesService $mimeTypes;
private UserGroupService $userGroupService;
private UserProfileService $userProfileService;
private MimeTypesService $mimeTypes;
private DatabaseUtil $databaseUtil;
private ConfigService $configService;
private AccountService $accountService;
private PluginManager $pluginManager;
public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
TabsHelper $tabsHelper,
UserServiceInterface $userService,
UserGroupService $userGroupService,
UserProfileServiceInterface $userProfileService,
MimeTypesService $mimeTypes,
DatabaseUtil $databaseUtil,
ConfigService $configService,
AccountService $accountService,
PluginManager $pluginManager
Application $application,
WebControllerHelper $webControllerHelper,
TabsHelper $tabsHelper,
UserServiceInterface $userService,
UserGroupService $userGroupService,
UserProfileService $userProfileService,
MimeTypesService $mimeTypes,
DatabaseUtil $databaseUtil,
ConfigService $configService,
AccountService $accountService,
PluginManager $pluginManager
) {
parent::__construct($application, $webControllerHelper);

View File

@@ -36,7 +36,7 @@ use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Http\RequestInterface;
use SP\Domain\ItemPreset\Models\ItemPreset;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Mvc\View\Components\SelectItemAdapter;
use SP\Mvc\View\TemplateInterface;
@@ -51,8 +51,8 @@ final class ItemPresetHelper extends HelperBase
private ?SelectItemAdapter $users = null;
private ?SelectItemAdapter $userGroups = null;
private UserServiceInterface $userService;
private UserGroupService $userGroupService;
private UserProfileServiceInterface $userProfileService;
private UserGroupService $userGroupService;
private UserProfileService $userProfileService;
public function __construct(
Application $application,
@@ -60,7 +60,7 @@ final class ItemPresetHelper extends HelperBase
RequestInterface $request,
UserServiceInterface $userService,
UserGroupService $userGroupService,
UserProfileServiceInterface $userProfileService
UserProfileService $userProfileService
) {
parent::__construct($application, $template, $request);

View File

@@ -35,7 +35,7 @@ use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SP\Domain\User\Models\User;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Modules\Web\Controllers\ControllerBase;
use SP\Mvc\Controller\ItemTrait;
@@ -51,7 +51,7 @@ abstract class UserViewBase extends ControllerBase
protected UserServiceInterface $userService;
private UserGroupService $userGroupService;
private UserProfileServiceInterface $userProfileService;
private UserProfileService $userProfileService;
private CustomFieldDataService $customFieldService;
public function __construct(
@@ -59,7 +59,7 @@ abstract class UserViewBase extends ControllerBase
WebControllerHelper $webControllerHelper,
UserServiceInterface $userService,
UserGroupService $userGroupService,
UserProfileServiceInterface $userProfileService,
UserProfileService $userProfileService,
CustomFieldDataService $customFieldService
) {
parent::__construct($application, $webControllerHelper);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -30,7 +30,7 @@ use SP\Core\Application;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Html\DataGrid\DataGridInterface;
use SP\Http\JsonMessage;
use SP\Modules\Web\Controllers\ControllerBase;
@@ -47,14 +47,14 @@ final class SearchController extends ControllerBase
use ItemTrait;
use JsonTrait;
private UserProfileServiceInterface $userProfileService;
private UserProfileGrid $userProfileGrid;
private UserProfileService $userProfileService;
private UserProfileGrid $userProfileGrid;
public function __construct(
Application $application,
Application $application,
WebControllerHelper $webControllerHelper,
UserProfileServiceInterface $userProfileService,
UserProfileGrid $userProfileGrid
UserProfileService $userProfileService,
UserProfileGrid $userProfileGrid
) {
parent::__construct($application, $webControllerHelper);

View File

@@ -27,7 +27,7 @@ namespace SP\Modules\Web\Controllers\UserProfile;
use SP\Core\Application;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Modules\Web\Controllers\ControllerBase;
use SP\Modules\Web\Forms\UserProfileForm;
use SP\Mvc\Controller\WebControllerHelper;
@@ -37,14 +37,14 @@ use SP\Mvc\Controller\WebControllerHelper;
*/
abstract class UserProfileSaveBase extends ControllerBase
{
protected UserProfileServiceInterface $userProfileService;
protected UserProfileService $userProfileService;
protected CustomFieldDataService $customFieldService;
protected UserProfileForm $form;
public function __construct(
Application $application,
Application $application,
WebControllerHelper $webControllerHelper,
UserProfileServiceInterface $userProfileService,
UserProfileService $userProfileService,
CustomFieldDataService $customFieldService
) {
parent::__construct($application, $webControllerHelper);

View File

@@ -35,7 +35,7 @@ use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SP\Domain\User\Models\UserProfile;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Web\Controllers\ControllerBase;
use SP\Mvc\Controller\ItemTrait;
@@ -48,13 +48,13 @@ abstract class UserProfileViewBase extends ControllerBase
{
use ItemTrait;
private UserProfileServiceInterface $userProfileService;
private UserProfileService $userProfileService;
private CustomFieldDataService $customFieldService;
public function __construct(
Application $application,
Application $application,
WebControllerHelper $webControllerHelper,
UserProfileServiceInterface $userProfileService,
UserProfileService $userProfileService,
CustomFieldDataService $customFieldService
) {
parent::__construct($application, $webControllerHelper);

View File

@@ -57,8 +57,8 @@ use SP\Domain\ItemPreset\Services\ItemPreset;
use SP\Domain\Upgrade\Services\UpgradeAppService;
use SP\Domain\Upgrade\Services\UpgradeDatabaseService;
use SP\Domain\Upgrade\Services\UpgradeUtil;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Services\UserProfileService;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Services\UserProfile;
use SP\Http\Address;
use SP\Http\Uri;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
@@ -142,8 +142,8 @@ final class Init extends HttpModuleBase
private PluginManager $pluginManager;
private ItemPreset $itemPresetService;
private DatabaseUtil $databaseUtil;
private UserProfileService $userProfileService;
private bool $isIndex = false;
private UserProfile $userProfileService;
private bool $isIndex = false;
public function __construct(
Application $application,
@@ -156,7 +156,7 @@ final class Init extends HttpModuleBase
PluginManager $pluginManager,
ItemPreset $itemPresetService,
DatabaseUtil $databaseUtil,
UserProfileServiceInterface $userProfileService,
UserProfileService $userProfileService,
private readonly UriContextInterface $uriContext
) {
parent::__construct(

View File

@@ -47,7 +47,7 @@ use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Security\Dtos\TrackRequest;
use SP\Domain\Security\Ports\TrackService;
use SP\Domain\User\Dtos\UserDataDto;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Api\Controllers\Help\HelpInterface;
@@ -76,7 +76,7 @@ final class Api extends Service implements ApiService
private readonly ApiRequestService $apiRequest,
private readonly AuthTokenService $authTokenService,
private readonly UserServiceInterface $userService,
private readonly UserProfileServiceInterface $userProfileService
private readonly UserProfileService $userProfileService
) {
parent::__construct($application);

View File

@@ -49,7 +49,7 @@ use SP\Domain\Security\Dtos\TrackRequest;
use SP\Domain\Security\Ports\TrackService;
use SP\Domain\User\Ports\UserMasterPassService;
use SP\Domain\User\Ports\UserPassRecoverService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Domain\User\Services\UserLoginRequest;
use SP\Domain\User\Services\UserMasterPassStatus;
@@ -97,7 +97,7 @@ final class Login extends Service implements LoginService
private readonly UserPassRecoverService $userPassRecoverService,
private readonly TemporaryMasterPassService $temporaryMasterPassService,
private readonly UserMasterPassService $userMasterPassService,
private readonly UserProfileServiceInterface $userProfileService
private readonly UserProfileService $userProfileService
) {
parent::__construct($application);

View File

@@ -43,7 +43,7 @@ use SP\Domain\User\Models\User;
use SP\Domain\User\Models\UserGroup;
use SP\Domain\User\Models\UserProfile;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Infrastructure\Database\DatabaseConnectionData;
use SP\Infrastructure\File\FileException;
@@ -68,14 +68,14 @@ final class InstallerService implements InstallerServiceInterface
private ?InstallData $installData = null;
public function __construct(
RequestInterface $request,
private readonly ConfigFileService $config,
private readonly UserServiceInterface $userService,
private readonly UserGroupService $userGroupService,
private readonly UserProfileServiceInterface $userProfileService,
private readonly ConfigService $configService,
private readonly DatabaseConnectionData $databaseConnectionData,
private readonly DatabaseSetupInterface $databaseSetup
RequestInterface $request,
private readonly ConfigFileService $config,
private readonly UserServiceInterface $userService,
private readonly UserGroupService $userGroupService,
private readonly UserProfileService $userProfileService,
private readonly ConfigService $configService,
private readonly DatabaseConnectionData $databaseConnectionData,
private readonly DatabaseSetupInterface $databaseSetup
) {
$this->request = $request;
}

View File

@@ -55,7 +55,7 @@ interface UserProfileRepository extends Repository
*
* @param int $id
*
* @return QueryResult<T>
* @return QueryResult<UserProfileModel>
* @throws ConstraintException
* @throws QueryException
*/
@@ -90,7 +90,7 @@ interface UserProfileRepository extends Repository
*
* @param ItemSearchData $itemSearchData
*
* @return QueryResult<T>
* @return QueryResult<UserProfileModel>
*/
public function search(ItemSearchData $itemSearchData): QueryResult;
@@ -108,7 +108,7 @@ interface UserProfileRepository extends Repository
/**
* Returns all the items
*
* @return QueryResult<T>
* @return QueryResult<UserProfileModel>
*/
public function getAll(): QueryResult;
}

View File

@@ -28,7 +28,7 @@ use SP\DataModel\ItemSearchData;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\UserProfile;
use SP\Domain\User\Models\UserProfile as UserProfileModel;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
@@ -38,16 +38,17 @@ use SP\Infrastructure\Database\QueryResult;
*
* @package SP\Domain\Common\Services\UserProfile
*/
interface UserProfileServiceInterface
interface UserProfileService
{
/**
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function getById(int $id): UserProfile;
public function getById(int $id): UserProfileModel;
/**
* @return QueryResult<UserProfileModel>
* @throws ConstraintException
* @throws QueryException
*/
@@ -58,7 +59,7 @@ interface UserProfileServiceInterface
* @throws QueryException
* @throws NoSuchItemException
*/
public function delete(int $id): UserProfileServiceInterface;
public function delete(int $id): void;
/**
* @param int[] $ids
@@ -74,7 +75,7 @@ interface UserProfileServiceInterface
* @throws QueryException
* @throws DuplicatedItemException
*/
public function create(UserProfile $itemData): int;
public function create(UserProfileModel $userProfile): int;
/**
* @throws ConstraintException
@@ -82,7 +83,7 @@ interface UserProfileServiceInterface
* @throws DuplicatedItemException
* @throws ServiceException
*/
public function update(UserProfile $itemData): void;
public function update(UserProfileModel $userProfile): void;
/**
* @throws ConstraintException
@@ -93,7 +94,7 @@ interface UserProfileServiceInterface
/**
* Get all items from the service's repository
*
* @return UserProfile[]
* @return array<UserProfileModel>
* @throws ConstraintException
* @throws QueryException
*/

View File

@@ -26,40 +26,29 @@ namespace SP\Domain\User\Services;
use SP\Core\Application;
use SP\DataModel\ItemSearchData;
use SP\DataModel\ProfileData;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Common\Services\ServiceItemTrait;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\User\Models\User as UserModel;
use SP\Domain\User\Models\UserProfile;
use SP\Domain\User\Models\UserProfile as UserProfileModel;
use SP\Domain\User\Ports\UserProfileRepository;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
use SP\Util\Util;
use function SP\__u;
/**
* Class UserProfileService
*
* @package SP\Domain\Common\Services\UserProfile
* Class UserProfile
*/
final class UserProfileService extends Service implements UserProfileServiceInterface
final class UserProfile extends Service implements UserProfileService
{
use ServiceItemTrait;
protected UserProfileRepository $userProfileRepository;
public function __construct(Application $application, UserProfileRepository $userProfileRepository)
public function __construct(Application $application, private readonly UserProfileRepository $userProfileRepository)
{
parent::__construct($application);
$this->userProfileRepository = $userProfileRepository;
}
/**
@@ -67,23 +56,20 @@ final class UserProfileService extends Service implements UserProfileServiceInte
* @throws QueryException
* @throws NoSuchItemException
*/
public function getById(int $id): UserProfile
public function getById(int $id): UserProfileModel
{
$result = $this->userProfileRepository->getById($id);
if ($result->getNumRows() === 0) {
throw new NoSuchItemException(__u('Profile not found'));
throw NoSuchItemException::info(__u('Profile not found'));
}
$userProfileData = $result->getData();
$userProfileData->setProfile(Util::unserialize(ProfileData::class, $userProfileData->getProfile()));
return $userProfileData;
return $result->getData(UserProfileModel::class);
}
/**
* @throws ConstraintException
* @throws QueryException
* @param ItemSearchData $itemSearchData
* @return QueryResult<UserProfileModel>
*/
public function search(ItemSearchData $itemSearchData): QueryResult
{
@@ -95,13 +81,11 @@ final class UserProfileService extends Service implements UserProfileServiceInte
* @throws QueryException
* @throws NoSuchItemException
*/
public function delete(int $id): UserProfileServiceInterface
public function delete(int $id): void
{
if ($this->userProfileRepository->delete($id) === 0) {
throw new NoSuchItemException(__u('Profile not found'), SPException::INFO);
if ($this->userProfileRepository->delete($id)->getAffectedNumRows() === 0) {
throw NoSuchItemException::info(__u('Profile not found'));
}
return $this;
}
/**
@@ -113,13 +97,10 @@ final class UserProfileService extends Service implements UserProfileServiceInte
*/
public function deleteByIdBatch(array $ids): int
{
$count = $this->userProfileRepository->deleteByIdBatch($ids);
$count = $this->userProfileRepository->deleteByIdBatch($ids)->getAffectedNumRows();
if ($count !== count($ids)) {
throw new ServiceException(
__u('Error while removing the profiles'),
SPException::WARNING
);
throw ServiceException::warning(__u('Error while removing the profiles'));
}
return $count;
@@ -130,9 +111,9 @@ final class UserProfileService extends Service implements UserProfileServiceInte
* @throws QueryException
* @throws DuplicatedItemException
*/
public function create(UserProfile $itemData): int
public function create(UserProfileModel $userProfile): int
{
return $this->userProfileRepository->create($itemData);
return $this->userProfileRepository->create($userProfile)->getLastId();
}
/**
@@ -141,12 +122,10 @@ final class UserProfileService extends Service implements UserProfileServiceInte
* @throws DuplicatedItemException
* @throws ServiceException
*/
public function update(UserProfile $itemData): void
public function update(UserProfileModel $userProfile): void
{
$update = $this->userProfileRepository->update($itemData);
if ($update === 0) {
throw new ServiceException(__u('Error while updating the profile'));
if ($this->userProfileRepository->update($userProfile) === 0) {
throw ServiceException::error(__u('Error while updating the profile'));
}
}
@@ -171,12 +150,10 @@ final class UserProfileService extends Service implements UserProfileServiceInte
/**
* Get all items from the service's repository
*
* @return UserProfile[]
* @throws ConstraintException
* @throws QueryException
* @return array<UserProfileModel>
*/
public function getAll(): array
{
return $this->userProfileRepository->getAll()->getDataAsArray();
return $this->userProfileRepository->getAll()->getDataAsArray(UserProfileModel::class);
}
}

View File

@@ -32,7 +32,7 @@ use SP\Domain\Core\Events\EventReceiver;
use SP\Domain\Core\Exceptions\FileNotFoundException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Providers\EventsTrait;
use SP\Providers\Provider;
use SP\Util\FileUtil;
@@ -61,9 +61,9 @@ final class AclHandler extends Provider implements EventReceiver
private string $events;
public function __construct(
Application $application,
private readonly UserProfileServiceInterface $userProfileService,
private readonly UserGroupService $userGroupService
Application $application,
private readonly UserProfileService $userProfileService,
private readonly UserGroupService $userGroupService
) {
parent::__construct($application);
}

View File

@@ -47,7 +47,7 @@ use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Security\Dtos\TrackRequest;
use SP\Domain\Security\Ports\TrackService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Api\Controllers\Help\AccountHelp;
@@ -70,7 +70,7 @@ class ApiTest extends UnitaryTestCase
private ApiRequestService|MockObject $apiRequest;
private AuthTokenService|MockObject $authTokenService;
private UserServiceInterface|MockObject $userService;
private MockObject|UserProfileServiceInterface $userProfileService;
private MockObject|UserProfileService $userProfileService;
private Api $apiService;
private TrackRequest $trackRequest;
@@ -304,7 +304,7 @@ class ApiTest extends UnitaryTestCase
$this->apiRequest = $this->createMock(ApiRequestService::class);
$this->authTokenService = $this->createMock(AuthTokenService::class);
$this->userService = $this->createMock(UserServiceInterface::class);
$this->userProfileService = $this->createMock(UserProfileServiceInterface::class);
$this->userProfileService = $this->createMock(UserProfileService::class);
$this->trackRequest = new TrackRequest(time(), __CLASS__, self::$faker->ipv4());
$this->trackService->method('buildTrackRequest')->willReturn($this->trackRequest);

View File

@@ -37,7 +37,7 @@ use SP\Domain\Install\Ports\InstallerServiceInterface;
use SP\Domain\Install\Services\DatabaseSetupInterface;
use SP\Domain\Install\Services\InstallerService;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Domain\User\Ports\UserServiceInterface;
use SP\Domain\User\Services\UserService;
use SP\Infrastructure\Database\DatabaseConnectionData;
@@ -72,7 +72,7 @@ class InstallerTest extends UnitaryTestCase
*/
private $userGroupService;
/**
* @var MockObject|UserProfileServiceInterface
* @var MockObject|UserProfileService
*/
private $userProfileService;
@@ -443,7 +443,7 @@ class InstallerTest extends UnitaryTestCase
$this->request = $this->createStub(RequestInterface::class);
$this->configService = $this->createMock(ConfigService::class);
$this->userGroupService = $this->createMock(UserGroupService::class);
$this->userProfileService = $this->createMock(UserProfileServiceInterface::class);
$this->userProfileService = $this->createMock(UserProfileService::class);
parent::setUp();
}

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\User\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use SP\DataModel\ItemSearchData;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\User\Models\User;
use SP\Domain\User\Ports\UserProfileRepository;
use SP\Domain\User\Services\UserProfile;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
use SPT\Generators\UserProfileDataGenerator;
use SPT\UnitaryTestCase;
/**
* Class UserProfileTest
*/
#[Group('unitary')]
class UserProfileTest extends UnitaryTestCase
{
private MockObject|UserProfileRepository $userProfileRepository;
private UserProfile $userProfile;
/**
* @throws ConstraintException
* @throws NoSuchItemException
* @throws QueryException
*/
public function testGetById()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('getById')
->with(100)
->willReturn(new QueryResult([$userProfile]));
$out = $this->userProfile->getById(100);
$this->assertEquals($userProfile, $out);
}
/**
* @throws ConstraintException
* @throws NoSuchItemException
* @throws QueryException
*/
public function testGetByIdWithNoItems()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('getById')
->with(100)
->willReturn(new QueryResult([]));
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Profile not found');
$this->userProfile->getById(100);
}
/**
* @throws DuplicatedItemException
* @throws ConstraintException
* @throws QueryException
*/
public function testCreate()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('create')
->with($userProfile)
->willReturn(new QueryResult(null, 0, 100));
$this->assertEquals(100, $this->userProfile->create($userProfile));
}
public function testGetAll()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('getAll')
->willReturn(new QueryResult([$userProfile]));
$out = $this->userProfile->getAll();
$this->assertEquals([$userProfile], $out);
}
/**
* @throws ServiceException
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testUpdate()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('update')
->with($userProfile)
->willReturn(10);
$this->userProfile->update($userProfile);
}
/**
* @throws ServiceException
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testUpdateWithException()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('update')
->with($userProfile)
->willReturn(0);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Error while updating the profile');
$this->userProfile->update($userProfile);
}
/**
* @throws ConstraintException
* @throws ServiceException
* @throws QueryException
*/
public function testDeleteByIdBatch()
{
$this->userProfileRepository
->expects($this->once())
->method('deleteByIdBatch')
->with([100, 200, 300])
->willReturn(new QueryResult(null, 3));
$this->userProfile->deleteByIdBatch([100, 200, 300]);
}
/**
* @throws ConstraintException
* @throws ServiceException
* @throws QueryException
*/
public function testDeleteByIdBatchWithException()
{
$this->userProfileRepository
->expects($this->once())
->method('deleteByIdBatch')
->with([100, 200, 300])
->willReturn(new QueryResult(null, 1));
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Error while removing the profiles');
$this->userProfile->deleteByIdBatch([100, 200, 300]);
}
public function testSearch()
{
$itemSearchData = new ItemSearchData(
self::$faker->userName(),
self::$faker->randomNumber(2),
self::$faker->randomNumber(2)
);
$queryResult = new QueryResult([1]);
$this->userProfileRepository
->expects($this->once())
->method('search')
->with($itemSearchData)
->willReturn($queryResult);
$out = $this->userProfile->search($itemSearchData);
self::assertEquals($queryResult, $out);
}
/**
* @throws ConstraintException
* @throws NoSuchItemException
* @throws QueryException
*/
public function testDelete()
{
$this->userProfileRepository
->expects($this->once())
->method('delete')
->with(100)
->willReturn(new QueryResult(null, 1));
$this->userProfile->delete(100);
}
/**
* @throws ConstraintException
* @throws NoSuchItemException
* @throws QueryException
*/
public function testDeleteWithException()
{
$this->userProfileRepository
->expects($this->once())
->method('delete')
->with(100)
->willReturn(new QueryResult());
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage('Profile not found');
$this->userProfile->delete(100);
}
public function testGetUsersForProfile()
{
$userProfile = UserProfileDataGenerator::factory()->buildUserProfileData();
$this->userProfileRepository
->expects($this->once())
->method('getAny')
->with(['id', 'login'], User::TABLE, 'userProfileId = :userProfileId', ['userProfileId' => 100])
->willReturn(new QueryResult([$userProfile]));
$out = $this->userProfile->getUsersForProfile(100);
$this->assertEquals([$userProfile], $out);
}
protected function setUp(): void
{
parent::setUp();
$this->userProfileRepository = $this->createMock(UserProfileRepository::class);
$this->userProfile = new UserProfile($this->application, $this->userProfileRepository);
}
}

View File

@@ -32,7 +32,7 @@ use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\User\Ports\UserGroupService;
use SP\Domain\User\Ports\UserProfileServiceInterface;
use SP\Domain\User\Ports\UserProfileService;
use SP\Providers\Acl\AclHandler;
use SPT\UnitaryTestCase;
@@ -43,8 +43,8 @@ use SPT\UnitaryTestCase;
#[Group('unitary')]
class AclHandlerTest extends UnitaryTestCase
{
private MockObject|UserProfileServiceInterface $userProfileService;
private UserGroupService|MockObject $userGroupService;
private MockObject|UserProfileService $userProfileService;
private UserGroupService|MockObject $userGroupService;
private AclHandler $aclHandler;
public static function userEventProvider(): array
@@ -238,7 +238,7 @@ class AclHandlerTest extends UnitaryTestCase
{
parent::setUp();
$this->userProfileService = $this->createMock(UserProfileServiceInterface::class);
$this->userProfileService = $this->createMock(UserProfileService::class);
$this->userGroupService = $this->createMock(UserGroupService::class);
$this->aclHandler = new AclHandler($this->application, $this->userProfileService, $this->userGroupService);