From db73e69dba920e1738f0438b47c59eb8cfd04ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 10 Mar 2024 01:15:53 +0100 Subject: [PATCH] chore(tests): UT for Notification service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Items/NotificationsController.php | 8 +- .../Notification/NotificationSaveBase.php | 10 +- .../Notification/NotificationViewBase.php | 14 +- .../Notification/SearchController.php | 14 +- ...eInterface.php => NotificationService.php} | 60 +- ...tificationService.php => Notification.php} | 125 +++-- .../Notification/NotificationHandler.php | 4 +- .../Services/NotificationTest.php | 521 ++++++++++++++++++ 8 files changed, 638 insertions(+), 118 deletions(-) rename lib/SP/Domain/Notification/Ports/{NotificationServiceInterface.php => NotificationService.php} (72%) rename lib/SP/Domain/Notification/Services/{NotificationService.php => Notification.php} (59%) create mode 100644 tests/SPT/Domain/Notification/Services/NotificationTest.php diff --git a/app/modules/web/Controllers/Items/NotificationsController.php b/app/modules/web/Controllers/Items/NotificationsController.php index 2faaa038..86239c8b 100644 --- a/app/modules/web/Controllers/Items/NotificationsController.php +++ b/app/modules/web/Controllers/Items/NotificationsController.php @@ -29,7 +29,7 @@ use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Notification\Models\Notification; -use SP\Domain\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; use SP\Html\Html; use SP\Http\JsonMessage; use SP\Http\JsonResponse; @@ -41,12 +41,12 @@ use SP\Mvc\Controller\SimpleControllerHelper; */ final class NotificationsController extends SimpleControllerBase { - private NotificationServiceInterface $notificationService; + private NotificationService $notificationService; public function __construct( Application $application, SimpleControllerHelper $simpleControllerHelper, - NotificationServiceInterface $notificationService + NotificationService $notificationService ) { parent::__construct($application, $simpleControllerHelper); @@ -72,7 +72,7 @@ final class NotificationsController extends SimpleControllerBase Html::truncate(Html::stripTags($notification->getDescription()), 30) ); }, - $this->notificationService->getAllActiveForUserId($this->session->getUserData()->getId()) + $this->notificationService->getAllActiveForCurrentUser() ); $count = count($notifications); diff --git a/app/modules/web/Controllers/Notification/NotificationSaveBase.php b/app/modules/web/Controllers/Notification/NotificationSaveBase.php index 650ad2aa..e9555f0b 100644 --- a/app/modules/web/Controllers/Notification/NotificationSaveBase.php +++ b/app/modules/web/Controllers/Notification/NotificationSaveBase.php @@ -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,7 +26,7 @@ namespace SP\Modules\Web\Controllers\Notification; use SP\Core\Application; -use SP\Domain\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Forms\NotificationForm; use SP\Mvc\Controller\WebControllerHelper; @@ -36,13 +36,13 @@ use SP\Mvc\Controller\WebControllerHelper; */ abstract class NotificationSaveBase extends ControllerBase { - protected NotificationServiceInterface $notificationService; - protected NotificationForm $form; + protected NotificationService $notificationService; + protected NotificationForm $form; public function __construct( Application $application, WebControllerHelper $webControllerHelper, - NotificationServiceInterface $notificationService + NotificationService $notificationService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/Notification/NotificationViewBase.php b/app/modules/web/Controllers/Notification/NotificationViewBase.php index b33b6d3d..58bdb0d3 100644 --- a/app/modules/web/Controllers/Notification/NotificationViewBase.php +++ b/app/modules/web/Controllers/Notification/NotificationViewBase.php @@ -31,7 +31,7 @@ use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Notification\Models\Notification; -use SP\Domain\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Web\Controllers\ControllerBase; @@ -43,14 +43,14 @@ use SP\Mvc\View\Components\SelectItemAdapter; */ abstract class NotificationViewBase extends ControllerBase { - private NotificationServiceInterface $notificationService; - private UserServiceInterface $userService; + private NotificationService $notificationService; + private UserServiceInterface $userService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - NotificationServiceInterface $notificationService, - UserServiceInterface $userService + Application $application, + WebControllerHelper $webControllerHelper, + NotificationService $notificationService, + UserServiceInterface $userService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/Notification/SearchController.php b/app/modules/web/Controllers/Notification/SearchController.php index 56958c8a..86e7356b 100644 --- a/app/modules/web/Controllers/Notification/SearchController.php +++ b/app/modules/web/Controllers/Notification/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\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; 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 NotificationServiceInterface $notificationService; - private NotificationGrid $notificationGrid; + private NotificationService $notificationService; + private NotificationGrid $notificationGrid; public function __construct( - Application $application, + Application $application, WebControllerHelper $webControllerHelper, - NotificationServiceInterface $notificationService, - NotificationGrid $notificationGrid + NotificationService $notificationService, + NotificationGrid $notificationGrid ) { parent::__construct($application, $webControllerHelper); diff --git a/lib/SP/Domain/Notification/Ports/NotificationServiceInterface.php b/lib/SP/Domain/Notification/Ports/NotificationService.php similarity index 72% rename from lib/SP/Domain/Notification/Ports/NotificationServiceInterface.php rename to lib/SP/Domain/Notification/Ports/NotificationService.php index d4dffdb5..44d74eb2 100644 --- a/lib/SP/Domain/Notification/Ports/NotificationServiceInterface.php +++ b/lib/SP/Domain/Notification/Ports/NotificationService.php @@ -28,16 +28,16 @@ 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\Notification\Models\Notification; +use SP\Domain\Notification\Models\Notification as NotificationModel; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; /** * Class NotificationService * - * @package SP\Domain\Notification\Services + * @template T of NotificationModel */ -interface NotificationServiceInterface +interface NotificationService { /** * Creates an item @@ -45,7 +45,7 @@ interface NotificationServiceInterface * @throws ConstraintException * @throws QueryException */ - public function create(Notification $itemData): int; + public function create(NotificationModel $notification): int; /** * Updates an item @@ -53,16 +53,14 @@ interface NotificationServiceInterface * @throws ConstraintException * @throws QueryException */ - public function update(Notification $itemData): int; + public function update(NotificationModel $notification): int; /** * Devolver los elementos con los ids especificados * - * @param int[] $ids + * @param int[] $ids * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getByIdBatch(array $ids): array; @@ -73,7 +71,7 @@ interface NotificationServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): NotificationServiceInterface; + public function delete(int $id): NotificationService; /** * Deletes an item @@ -82,12 +80,12 @@ interface NotificationServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function deleteAdmin(int $id): NotificationServiceInterface; + public function deleteAdmin(int $id): NotificationService; /** * Deletes an item * - * @param int[] $ids + * @param int[] $ids * * @throws ConstraintException * @throws QueryException @@ -98,7 +96,7 @@ interface NotificationServiceInterface /** * Deletes all the items for given ids * - * @param int[] $ids + * @param int[] $ids * * @throws ConstraintException * @throws QueryException @@ -109,18 +107,16 @@ interface NotificationServiceInterface /** * Returns the item for given id * - * @throws ConstraintException - * @throws QueryException + * @param int $id + * @return NotificationModel * @throws NoSuchItemException */ - public function getById(int $id): Notification; + public function getById(int $id): NotificationModel; /** * Returns all the items * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getAll(): array; @@ -136,39 +132,37 @@ interface NotificationServiceInterface /** * Devolver las notificaciones de un usuario para una fecha y componente determinados * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @param string $component + * @param int $id + * @return array */ public function getForUserIdByDate(string $component, int $id): array; /** - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @param int $id + * @return array */ public function getAllForUserId(int $id): array; /** - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ - public function getAllActiveForUserId(int $id): array; + public function getAllActiveForCurrentUser(): array; /** * Searches for items by a given filter * - * @throws ConstraintException - * @throws QueryException + * @param ItemSearchData $itemSearchData + * @return QueryResult */ public function search(ItemSearchData $itemSearchData): QueryResult; /** * Searches for items by a given filter * - * @throws ConstraintException - * @throws QueryException + * @param ItemSearchData $itemSearchData + * @param int $userId + * @return QueryResult */ public function searchForUserId(ItemSearchData $itemSearchData, int $userId): QueryResult; } diff --git a/lib/SP/Domain/Notification/Services/NotificationService.php b/lib/SP/Domain/Notification/Services/Notification.php similarity index 59% rename from lib/SP/Domain/Notification/Services/NotificationService.php rename to lib/SP/Domain/Notification/Services/Notification.php index d8a8429a..d17f1e27 100644 --- a/lib/SP/Domain/Notification/Services/NotificationService.php +++ b/lib/SP/Domain/Notification/Services/Notification.php @@ -30,19 +30,20 @@ use SP\Domain\Common\Services\Service; 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\Notification\Models\Notification; +use SP\Domain\Notification\Models\Notification as NotificationModel; use SP\Domain\Notification\Ports\NotificationRepository; -use SP\Domain\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; +use function SP\__u; + /** * Class NotificationService * - * @package SP\Domain\Notification\Services + * @template T of NotificationModel */ -final class NotificationService extends Service implements NotificationServiceInterface +final class Notification extends Service implements NotificationService { public function __construct( @@ -58,9 +59,9 @@ final class NotificationService extends Service implements NotificationServiceIn * @throws ConstraintException * @throws QueryException */ - public function create(Notification $itemData): int + public function create(NotificationModel $notification): int { - return $this->notificationRepository->create($itemData)->getLastId(); + return $this->notificationRepository->create($notification)->getLastId(); } /** @@ -69,9 +70,9 @@ final class NotificationService extends Service implements NotificationServiceIn * @throws ConstraintException * @throws QueryException */ - public function update(Notification $itemData): int + public function update(NotificationModel $notification): int { - return $this->notificationRepository->update($itemData); + return $this->notificationRepository->update($notification); } /** @@ -79,13 +80,13 @@ final class NotificationService extends Service implements NotificationServiceIn * * @param int[] $ids * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getByIdBatch(array $ids): array { - return $this->notificationRepository->getByIdBatch($ids)->getDataAsArray(); + return $this->notificationRepository + ->getByIdBatch($ids) + ->getDataAsArray(NotificationModel::class); } /** @@ -95,10 +96,10 @@ final class NotificationService extends Service implements NotificationServiceIn * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): NotificationServiceInterface + public function delete(int $id): NotificationService { - if ($this->notificationRepository->delete($id) === 0) { - throw new NoSuchItemException(__u('Notification not found'), SPException::INFO); + if ($this->notificationRepository->delete($id)->getAffectedNumRows() === 0) { + throw NoSuchItemException::info(__u('Notification not found')); } return $this; @@ -111,10 +112,10 @@ final class NotificationService extends Service implements NotificationServiceIn * @throws QueryException * @throws NoSuchItemException */ - public function deleteAdmin(int $id): NotificationServiceInterface + public function deleteAdmin(int $id): NotificationService { - if ($this->notificationRepository->deleteAdmin($id) === 0) { - throw new NoSuchItemException(__u('Notification not found'), SPException::INFO); + if ($this->notificationRepository->deleteAdmin($id)->getAffectedNumRows() === 0) { + throw NoSuchItemException::info(__u('Notification not found')); } return $this; @@ -131,13 +132,10 @@ final class NotificationService extends Service implements NotificationServiceIn */ public function deleteAdminBatch(array $ids): int { - $count = $this->notificationRepository->deleteAdminBatch($ids); + $count = $this->notificationRepository->deleteAdminBatch($ids)->getAffectedNumRows(); if ($count !== count($ids)) { - throw new ServiceException( - __u('Error while deleting the notifications'), - SPException::WARNING - ); + throw ServiceException::warning(__u('Error while deleting the notifications')); } return $count; @@ -154,13 +152,10 @@ final class NotificationService extends Service implements NotificationServiceIn */ public function deleteByIdBatch(array $ids): int { - $count = $this->notificationRepository->deleteByIdBatch($ids); + $count = $this->notificationRepository->deleteByIdBatch($ids)->getAffectedNumRows(); if ($count !== count($ids)) { - throw new ServiceException( - __u('Error while deleting the notifications'), - SPException::WARNING - ); + throw ServiceException::warning(__u('Error while deleting the notifications')); } return $count; @@ -169,31 +164,31 @@ final class NotificationService extends Service implements NotificationServiceIn /** * Returns the item for given id * - * @throws ConstraintException - * @throws QueryException + * @param int $id + * @return NotificationModel * @throws NoSuchItemException */ - public function getById(int $id): Notification + public function getById(int $id): NotificationModel { $result = $this->notificationRepository->getById($id); if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('Notification not found'), SPException::INFO); + throw NoSuchItemException::info(__u('Notification not found')); } - return $result->getData(); + return $result->getData(NotificationModel::class); } /** * Returns all the items * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getAll(): array { - return $this->notificationRepository->getAll()->getDataAsArray(); + return $this->notificationRepository + ->getAll() + ->getDataAsArray(NotificationModel::class); } /** @@ -206,68 +201,78 @@ final class NotificationService extends Service implements NotificationServiceIn public function setCheckedById(int $id): void { if ($this->notificationRepository->setCheckedById($id) === 0) { - throw new NoSuchItemException(__u('Notification not found'), SPException::INFO); + throw NoSuchItemException::info(__u('Notification not found')); } } /** * Devolver las notificaciones de un usuario para una fecha y componente determinados * - * @return Notification[] - * @throws ConstraintException - * @throws QueryException + * @param string $component + * @param int $id + * @return array */ public function getForUserIdByDate(string $component, int $id): array { - return $this->notificationRepository->getForUserIdByDate($component, $id)->getDataAsArray(); + return $this->notificationRepository + ->getForUserIdByDate($component, $id) + ->getDataAsArray(NotificationModel::class); } /** - * @return \SP\Domain\Notification\Models\Notification[] - * @throws ConstraintException - * @throws QueryException + * @param int $id + * @return array */ public function getAllForUserId(int $id): array { - return $this->notificationRepository->getAllForUserId($id)->getDataAsArray(); + return $this->notificationRepository + ->getAllForUserId($id) + ->getDataAsArray(NotificationModel::class); } /** - * @return \SP\Domain\Notification\Models\Notification[] - * @throws ConstraintException - * @throws QueryException + * @return array */ - public function getAllActiveForUserId(int $id): array + public function getAllActiveForCurrentUser(): array { - if ($this->context->getUserData()->getIsAdminApp()) { - return $this->notificationRepository->getAllActiveForAdmin($id)->getDataAsArray(); + $userData = $this->context->getUserData(); + + if ($userData->getIsAdminApp()) { + return $this->notificationRepository + ->getAllActiveForAdmin($userData->getId()) + ->getDataAsArray(NotificationModel::class); } - return $this->notificationRepository->getAllActiveForUserId($id)->getDataAsArray(); + return $this->notificationRepository + ->getAllActiveForUserId($userData->getId()) + ->getDataAsArray(NotificationModel::class); } /** * Searches for items by a given filter * - * @throws ConstraintException - * @throws QueryException + * @param ItemSearchData $itemSearchData + * @return QueryResult */ public function search(ItemSearchData $itemSearchData): QueryResult { $userData = $this->context->getUserData(); if ($userData->getIsAdminApp()) { - return $this->notificationRepository->searchForAdmin($itemSearchData, $userData->getId()); + return $this->notificationRepository + ->searchForAdmin($itemSearchData, $userData->getId()); } - return $this->notificationRepository->searchForUserId($itemSearchData, $userData->getId()); + return $this->notificationRepository + ->searchForUserId($itemSearchData, $userData->getId()); } /** * Searches for items by a given filter * - * @throws ConstraintException - * @throws QueryException + * @param ItemSearchData $itemSearchData + * @param int $userId + * @return QueryResult */ public function searchForUserId(ItemSearchData $itemSearchData, int $userId): QueryResult { diff --git a/lib/SP/Providers/Notification/NotificationHandler.php b/lib/SP/Providers/Notification/NotificationHandler.php index db93ad70..6fe73d47 100644 --- a/lib/SP/Providers/Notification/NotificationHandler.php +++ b/lib/SP/Providers/Notification/NotificationHandler.php @@ -29,7 +29,7 @@ use SP\Core\Application; use SP\Core\Events\Event; use SP\Domain\Core\Events\EventReceiver; use SP\Domain\Notification\Models\Notification; -use SP\Domain\Notification\Ports\NotificationServiceInterface; +use SP\Domain\Notification\Ports\NotificationService; use SP\Providers\EventsTrait; use SP\Providers\Provider; @@ -54,7 +54,7 @@ final class NotificationHandler extends Provider implements EventReceiver public function __construct( Application $application, - private readonly NotificationServiceInterface $notificationService + private readonly NotificationService $notificationService ) { parent::__construct($application); } diff --git a/tests/SPT/Domain/Notification/Services/NotificationTest.php b/tests/SPT/Domain/Notification/Services/NotificationTest.php new file mode 100644 index 00000000..0306c866 --- /dev/null +++ b/tests/SPT/Domain/Notification/Services/NotificationTest.php @@ -0,0 +1,521 @@ +. + */ + +namespace SPT\Domain\Notification\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +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\Notification\Models\Notification as NotificationModel; +use SP\Domain\Notification\Ports\NotificationRepository; +use SP\Domain\Notification\Services\Notification; +use SP\Infrastructure\Common\Repositories\NoSuchItemException; +use SP\Infrastructure\Database\QueryResult; +use SPT\Generators\NotificationDataGenerator; +use SPT\UnitaryTestCase; + +/** + * Class NotificationTest + */ +#[Group('unitary')] +class NotificationTest extends UnitaryTestCase +{ + + private NotificationRepository|MockObject $notificationRepository; + private Notification $notification; + + public function testSearchForUserId() + { + $itemSearchData = new ItemSearchData(); + + $this->notificationRepository + ->expects($this->once()) + ->method('searchForUserId') + ->with($itemSearchData, 100); + + $this->notification->searchForUserId($itemSearchData, 100); + } + + /** + * @throws Exception + */ + public function testGetAll() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getAll') + ->willReturn($queryResult); + + $out = $this->notification->getAll(); + + $this->assertEquals([1], $out); + } + + public function testSearchWithAdmin() + { + $userData = $this->context->getUserData()->setIsAdminApp(true); + $this->context->setUserData($userData); + + $itemSearchData = new ItemSearchData(); + + $this->notificationRepository + ->expects($this->once()) + ->method('searchForAdmin') + ->with($itemSearchData, $userData->getId()); + + $this->notification->search($itemSearchData); + } + + public function testSearchWithNoAdmin() + { + $userData = $this->context->getUserData(); + + $itemSearchData = new ItemSearchData(); + + $this->notificationRepository + ->expects($this->once()) + ->method('searchForUserId') + ->with($itemSearchData, $userData->getId()); + + $this->notification->search($itemSearchData); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testSetCheckedById() + { + $this->notificationRepository + ->expects($this->once()) + ->method('setCheckedById') + ->with(100) + ->willReturn(1); + + $this->notification->setCheckedById(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testSetCheckedByIdWithException() + { + $this->notificationRepository + ->expects($this->once()) + ->method('setCheckedById') + ->with(100) + ->willReturn(0); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Notification not found'); + + $this->notification->setCheckedById(100); + } + + /** + * @throws Exception + */ + public function testGetAllActiveForCurrentUserWithAdmin() + { + $userData = $this->context->getUserData()->setIsAdminApp(true); + $this->context->setUserData($userData); + + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getAllActiveForAdmin') + ->with($userData->getId()) + ->willReturn($queryResult); + + $out = $this->notification->getAllActiveForCurrentUser(); + + $this->assertEquals([1], $out); + } + + /** + * @throws Exception + */ + public function testGetAllActiveForCurrentUserWithNoAdmin() + { + $userData = $this->context->getUserData(); + + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getAllActiveForUserId') + ->with($userData->getId()) + ->willReturn($queryResult); + + $out = $this->notification->getAllActiveForCurrentUser(); + + $this->assertEquals([1], $out); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testCreate() + { + $notification = NotificationDataGenerator::factory()->buildNotification(); + + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('create') + ->with($notification) + ->willReturn($queryResult->setLastId(100)); + + $out = $this->notification->create($notification); + + $this->assertEquals(100, $out); + } + + /** + * @throws Exception + */ + public function testGetAllForUserId() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getAllForUserId') + ->with(100) + ->willReturn($queryResult); + + $out = $this->notification->getAllForUserId(100); + + $this->assertEquals([1], $out); + } + + public function testGetByIdBatch() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getByIdBatch') + ->with([100, 200, 300]) + ->willReturn($queryResult); + + $out = $this->notification->getByIdBatch([100, 200, 300]); + + $this->assertEquals([1], $out); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDelete() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn($queryResult->setAffectedNumRows(1)); + + $this->notification->delete(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDeleteWithException() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn($queryResult->setAffectedNumRows(0)); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Notification not found'); + + + $this->notification->delete(100); + } + + /** + * @throws Exception + */ + public function testGetForUserIdByDate() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getDataAsArray') + ->with(NotificationModel::class) + ->willReturn([1]); + + $this->notificationRepository + ->expects($this->once()) + ->method('getForUserIdByDate') + ->with('test', 100) + ->willReturn($queryResult); + + $out = $this->notification->getForUserIdByDate('test', 100); + + $this->assertEquals([1], $out); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testUpdate() + { + $notification = NotificationDataGenerator::factory()->buildNotification(); + + $this->notificationRepository + ->expects($this->once()) + ->method('update') + ->with($notification) + ->willReturn(100); + + $out = $this->notification->update($notification); + + $this->assertEquals(100, $out); + } + + /** + * @throws NoSuchItemException + * @throws Exception + */ + public function testGetById() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getNumRows') + ->willReturn(1); + + $notification = new NotificationModel(); + + $queryResult->expects($this->once()) + ->method('getData') + ->with(NotificationModel::class) + ->willReturn($notification); + + $this->notificationRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn($queryResult); + + $out = $this->notification->getById(100); + + $this->assertEquals($notification, $out); + } + + /** + * @throws NoSuchItemException + * @throws Exception + */ + public function testGetByIdWithException() + { + $queryResult = $this->createMock(QueryResult::class); + $queryResult->expects($this->once()) + ->method('getNumRows') + ->willReturn(0); + + $queryResult->expects($this->never()) + ->method('getData'); + + $this->notificationRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn($queryResult); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Notification not found'); + + $this->notification->getById(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDeleteAdmin() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteAdmin') + ->with(100) + ->willReturn($queryResult->setAffectedNumRows(1)); + + $this->notification->deleteAdmin(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDeleteAdminWithException() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteAdmin') + ->with(100) + ->willReturn($queryResult->setAffectedNumRows(0)); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Notification not found'); + + $this->notification->deleteAdmin(100); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatch() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn($queryResult->setAffectedNumRows(3)); + + $this->notification->deleteByIdBatch([100, 200, 300]); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatchWithException() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn($queryResult->setAffectedNumRows(1)); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while deleting the notifications'); + + $this->notification->deleteByIdBatch([100, 200, 300]); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteAdminBatch() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteAdminBatch') + ->with([100, 200, 300]) + ->willReturn($queryResult->setAffectedNumRows(3)); + + $this->notification->deleteAdminBatch([100, 200, 300]); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteAdminBatchWithException() + { + $queryResult = new QueryResult([]); + + $this->notificationRepository + ->expects($this->once()) + ->method('deleteAdminBatch') + ->with([100, 200, 300]) + ->willReturn($queryResult->setAffectedNumRows(1)); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while deleting the notifications'); + + $this->notification->deleteAdminBatch([100, 200, 300]); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->notificationRepository = $this->createMock(NotificationRepository::class); + + $this->notification = new Notification($this->application, $this->notificationRepository); + } +}