mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 07:04:07 +01:00
chore(tests): UT for Notification service
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
public function getForUserIdByDate(string $component, int $id): array;
|
||||
|
||||
/**
|
||||
* @return Notification[]
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
* @param int $id
|
||||
* @return array<T>
|
||||
*/
|
||||
public function getAllForUserId(int $id): array;
|
||||
|
||||
/**
|
||||
* @return Notification[]
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
* @return array<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
public function searchForUserId(ItemSearchData $itemSearchData, int $userId): QueryResult;
|
||||
}
|
||||
@@ -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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
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<T>
|
||||
*/
|
||||
public function searchForUserId(ItemSearchData $itemSearchData, int $userId): QueryResult
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
521
tests/SPT/Domain/Notification/Services/NotificationTest.php
Normal file
521
tests/SPT/Domain/Notification/Services/NotificationTest.php
Normal file
@@ -0,0 +1,521 @@
|
||||
<?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\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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user