From 1fd39467fd410ea0842e491264c1eb211170c461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 7 Apr 2024 08:42:47 +0200 Subject: [PATCH] chore(tests): UT for UserProfile service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../AccessManager/IndexController.php | 10 +- .../ConfigManager/IndexController.php | 30 +- .../Controllers/Helpers/ItemPresetHelper.php | 8 +- .../web/Controllers/User/UserViewBase.php | 6 +- .../UserProfile/SearchController.php | 14 +- .../UserProfile/UserProfileSaveBase.php | 8 +- .../UserProfile/UserProfileViewBase.php | 8 +- app/modules/web/Init.php | 10 +- lib/SP/Domain/Api/Services/Api.php | 4 +- lib/SP/Domain/Auth/Services/Login.php | 4 +- .../Install/Services/InstallerService.php | 18 +- .../User/Ports/UserProfileRepository.php | 6 +- ...ceInterface.php => UserProfileService.php} | 15 +- ...UserProfileService.php => UserProfile.php} | 67 ++--- lib/SP/Providers/Acl/AclHandler.php | 8 +- tests/SPT/Domain/Api/Services/ApiTest.php | 6 +- .../Domain/Install/Services/InstallerTest.php | 6 +- .../Domain/User/Services/UserProfileTest.php | 280 ++++++++++++++++++ tests/SPT/Providers/Acl/AclHandlerTest.php | 8 +- 19 files changed, 387 insertions(+), 129 deletions(-) rename lib/SP/Domain/User/Ports/{UserProfileServiceInterface.php => UserProfileService.php} (86%) rename lib/SP/Domain/User/Services/{UserProfileService.php => UserProfile.php} (63%) create mode 100644 tests/SPT/Domain/User/Services/UserProfileTest.php diff --git a/app/modules/web/Controllers/AccessManager/IndexController.php b/app/modules/web/Controllers/AccessManager/IndexController.php index 03e60206..38e247df 100644 --- a/app/modules/web/Controllers/AccessManager/IndexController.php +++ b/app/modules/web/Controllers/AccessManager/IndexController.php @@ -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 ) { diff --git a/app/modules/web/Controllers/ConfigManager/IndexController.php b/app/modules/web/Controllers/ConfigManager/IndexController.php index ecfc58d2..2f39bd31 100644 --- a/app/modules/web/Controllers/ConfigManager/IndexController.php +++ b/app/modules/web/Controllers/ConfigManager/IndexController.php @@ -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); diff --git a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php index 437a9422..6ae40dd8 100644 --- a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php @@ -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); diff --git a/app/modules/web/Controllers/User/UserViewBase.php b/app/modules/web/Controllers/User/UserViewBase.php index b6e3ce33..3c47050e 100644 --- a/app/modules/web/Controllers/User/UserViewBase.php +++ b/app/modules/web/Controllers/User/UserViewBase.php @@ -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); diff --git a/app/modules/web/Controllers/UserProfile/SearchController.php b/app/modules/web/Controllers/UserProfile/SearchController.php index a90b02d0..f4b76f50 100644 --- a/app/modules/web/Controllers/UserProfile/SearchController.php +++ b/app/modules/web/Controllers/UserProfile/SearchController.php @@ -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); diff --git a/app/modules/web/Controllers/UserProfile/UserProfileSaveBase.php b/app/modules/web/Controllers/UserProfile/UserProfileSaveBase.php index 3a8ceeef..d8b9969d 100644 --- a/app/modules/web/Controllers/UserProfile/UserProfileSaveBase.php +++ b/app/modules/web/Controllers/UserProfile/UserProfileSaveBase.php @@ -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); diff --git a/app/modules/web/Controllers/UserProfile/UserProfileViewBase.php b/app/modules/web/Controllers/UserProfile/UserProfileViewBase.php index 80a5071e..732581f8 100644 --- a/app/modules/web/Controllers/UserProfile/UserProfileViewBase.php +++ b/app/modules/web/Controllers/UserProfile/UserProfileViewBase.php @@ -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); diff --git a/app/modules/web/Init.php b/app/modules/web/Init.php index 3aff25ab..0cd4f084 100644 --- a/app/modules/web/Init.php +++ b/app/modules/web/Init.php @@ -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( diff --git a/lib/SP/Domain/Api/Services/Api.php b/lib/SP/Domain/Api/Services/Api.php index 55970c1b..94c96c1f 100644 --- a/lib/SP/Domain/Api/Services/Api.php +++ b/lib/SP/Domain/Api/Services/Api.php @@ -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); diff --git a/lib/SP/Domain/Auth/Services/Login.php b/lib/SP/Domain/Auth/Services/Login.php index ed3ab66d..9637d653 100644 --- a/lib/SP/Domain/Auth/Services/Login.php +++ b/lib/SP/Domain/Auth/Services/Login.php @@ -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); diff --git a/lib/SP/Domain/Install/Services/InstallerService.php b/lib/SP/Domain/Install/Services/InstallerService.php index dfebb1c0..a1fa7cff 100644 --- a/lib/SP/Domain/Install/Services/InstallerService.php +++ b/lib/SP/Domain/Install/Services/InstallerService.php @@ -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; } diff --git a/lib/SP/Domain/User/Ports/UserProfileRepository.php b/lib/SP/Domain/User/Ports/UserProfileRepository.php index 4e156e83..6ff4a355 100644 --- a/lib/SP/Domain/User/Ports/UserProfileRepository.php +++ b/lib/SP/Domain/User/Ports/UserProfileRepository.php @@ -55,7 +55,7 @@ interface UserProfileRepository extends Repository * * @param int $id * - * @return QueryResult + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -90,7 +90,7 @@ interface UserProfileRepository extends Repository * * @param ItemSearchData $itemSearchData * - * @return QueryResult + * @return QueryResult */ public function search(ItemSearchData $itemSearchData): QueryResult; @@ -108,7 +108,7 @@ interface UserProfileRepository extends Repository /** * Returns all the items * - * @return QueryResult + * @return QueryResult */ public function getAll(): QueryResult; } diff --git a/lib/SP/Domain/User/Ports/UserProfileServiceInterface.php b/lib/SP/Domain/User/Ports/UserProfileService.php similarity index 86% rename from lib/SP/Domain/User/Ports/UserProfileServiceInterface.php rename to lib/SP/Domain/User/Ports/UserProfileService.php index 0347fba7..2a206543 100644 --- a/lib/SP/Domain/User/Ports/UserProfileServiceInterface.php +++ b/lib/SP/Domain/User/Ports/UserProfileService.php @@ -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 * @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 * @throws ConstraintException * @throws QueryException */ diff --git a/lib/SP/Domain/User/Services/UserProfileService.php b/lib/SP/Domain/User/Services/UserProfile.php similarity index 63% rename from lib/SP/Domain/User/Services/UserProfileService.php rename to lib/SP/Domain/User/Services/UserProfile.php index e7cc6edd..ab1481c0 100644 --- a/lib/SP/Domain/User/Services/UserProfileService.php +++ b/lib/SP/Domain/User/Services/UserProfile.php @@ -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 */ 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 */ public function getAll(): array { - return $this->userProfileRepository->getAll()->getDataAsArray(); + return $this->userProfileRepository->getAll()->getDataAsArray(UserProfileModel::class); } } diff --git a/lib/SP/Providers/Acl/AclHandler.php b/lib/SP/Providers/Acl/AclHandler.php index 00665965..9accc676 100644 --- a/lib/SP/Providers/Acl/AclHandler.php +++ b/lib/SP/Providers/Acl/AclHandler.php @@ -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); } diff --git a/tests/SPT/Domain/Api/Services/ApiTest.php b/tests/SPT/Domain/Api/Services/ApiTest.php index 0c03834c..13a659ff 100644 --- a/tests/SPT/Domain/Api/Services/ApiTest.php +++ b/tests/SPT/Domain/Api/Services/ApiTest.php @@ -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); diff --git a/tests/SPT/Domain/Install/Services/InstallerTest.php b/tests/SPT/Domain/Install/Services/InstallerTest.php index 3e70157c..d63c467b 100644 --- a/tests/SPT/Domain/Install/Services/InstallerTest.php +++ b/tests/SPT/Domain/Install/Services/InstallerTest.php @@ -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(); } diff --git a/tests/SPT/Domain/User/Services/UserProfileTest.php b/tests/SPT/Domain/User/Services/UserProfileTest.php new file mode 100644 index 00000000..9c8a1dd9 --- /dev/null +++ b/tests/SPT/Domain/User/Services/UserProfileTest.php @@ -0,0 +1,280 @@ +. + */ + +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); + } +} diff --git a/tests/SPT/Providers/Acl/AclHandlerTest.php b/tests/SPT/Providers/Acl/AclHandlerTest.php index a53cf233..844c4e67 100644 --- a/tests/SPT/Providers/Acl/AclHandlerTest.php +++ b/tests/SPT/Providers/Acl/AclHandlerTest.php @@ -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);