. */ declare(strict_types=1); namespace SP\Tests\Domain\Account\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; use SP\Domain\Account\Ports\AccountToTagRepository; use SP\Domain\Account\Ports\AccountToUserGroupRepository; use SP\Domain\Account\Ports\AccountToUserRepository; use SP\Domain\Account\Services\AccountItems; 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\Tests\Generators\AccountDataGenerator; use SP\Tests\UnitaryTestCase; /** * Class AccountItemsTest */ #[Group('unitary')] class AccountItemsTest extends UnitaryTestCase { private MockObject|AccountToUserGroupRepository $accountToUserGroupRepository; private AccountItems $accountItems; private AccountToUserRepository|MockObject $accountToUserRepository; private MockObject|AccountToTagRepository $accountToTagRepository; /** * @throws ConstraintException * @throws ServiceException * @throws QueryException */ public function testUpdateItems() { $accountUpdateDto = AccountDataGenerator::factory()->buildAccountUpdateDto(); $this->accountToUserGroupRepository ->expects($this->exactly(2)) ->method('transactionAware') ->with($this->withResolveCallableCallback()); $this->accountToUserGroupRepository ->expects($this->exactly(2)) ->method('deleteTypeByAccountId') ->with(...self::withConsecutive([100, false], [100, true])); $this->accountToUserGroupRepository ->expects($this->exactly(2)) ->method('addByType') ->with( ... self::withConsecutive([100, $accountUpdateDto->userGroupsView, false], [100, $accountUpdateDto->userGroupsEdit, true]) ); $this->accountToUserRepository ->expects($this->exactly(2)) ->method('transactionAware') ->with($this->withResolveCallableCallback()); $this->accountToUserRepository ->expects($this->exactly(2)) ->method('deleteTypeByAccountId') ->with(...self::withConsecutive([100, false], [100, true])); $this->accountToUserRepository ->expects($this->exactly(2)) ->method('addByType') ->with( ... self::withConsecutive([100, $accountUpdateDto->usersView, false], [100, $accountUpdateDto->usersEdit, true]) ); $this->accountToTagRepository ->expects($this->once()) ->method('transactionAware') ->with($this->withResolveCallableCallback()); $this->accountToTagRepository ->expects($this->once()) ->method('deleteByAccountId') ->with(100); $this->accountToTagRepository ->expects($this->once()) ->method('add') ->with(100, $accountUpdateDto->tags); $this->accountItems->updateItems(true, 100, $accountUpdateDto); } /** * @throws ConstraintException * @throws QueryException * @throws SPException * @throws ServiceException */ public function testUpdateItemsWithNoItems() { $accountUpdateDto = AccountDataGenerator::factory() ->buildAccountUpdateDto() ->mutate( [ 'usersView' => null, 'usersEdit' => null, 'userGroupsView' => null, 'userGroupsEdit' => null, 'tags' => null ] ); $this->accountToUserGroupRepository ->expects($this->never()) ->method('transactionAware'); $this->accountToUserGroupRepository ->expects($this->exactly(2)) ->method('deleteTypeByAccountId') ->with(...self::withConsecutive([100, false], [100, true])); $this->accountToUserGroupRepository ->expects($this->never()) ->method('addByType'); $this->accountToUserRepository ->expects($this->never()) ->method('transactionAware'); $this->accountToUserRepository ->expects($this->exactly(2)) ->method('deleteTypeByAccountId') ->with(...self::withConsecutive([100, false], [100, true])); $this->accountToUserRepository ->expects($this->never()) ->method('addByType'); $this->accountToTagRepository ->expects($this->never()) ->method('transactionAware'); $this->accountToTagRepository ->expects($this->once()) ->method('deleteByAccountId') ->with(100); $this->accountToTagRepository ->expects($this->never()) ->method('add'); $this->accountItems->updateItems(true, 100, $accountUpdateDto); } /** * @throws ConstraintException * @throws ServiceException * @throws QueryException */ public function testUpdateItemsWithNoPermission() { $accountUpdateDto = AccountDataGenerator::factory()->buildAccountUpdateDto(); $this->accountToUserGroupRepository ->expects($this->never()) ->method('transactionAware'); $this->accountToUserGroupRepository ->expects($this->never()) ->method('deleteTypeByAccountId'); $this->accountToUserGroupRepository ->expects($this->never()) ->method('addByType'); $this->accountToUserRepository ->expects($this->never()) ->method('transactionAware'); $this->accountToUserRepository ->expects($this->never()) ->method('deleteTypeByAccountId'); $this->accountToUserRepository ->expects($this->never()) ->method('addByType'); $this->accountToTagRepository ->expects($this->once()) ->method('transactionAware') ->with($this->withResolveCallableCallback()); $this->accountToTagRepository ->expects($this->once()) ->method('deleteByAccountId') ->with(100); $this->accountToTagRepository ->expects($this->once()) ->method('add') ->with(100, $accountUpdateDto->tags); $this->accountItems->updateItems(false, 100, $accountUpdateDto); } public function testAddItems() { $accountCreateDto = AccountDataGenerator::factory()->buildAccountCreateDto(); $this->accountToUserGroupRepository ->expects($this->exactly(2)) ->method('addByType') ->with( ... self::withConsecutive( [100, $accountCreateDto->userGroupsView, false], [100, $accountCreateDto->userGroupsEdit, true] ) ); $this->accountToUserRepository ->expects($this->exactly(2)) ->method('addByType') ->with( ... self::withConsecutive([100, $accountCreateDto->usersView, false], [100, $accountCreateDto->usersEdit, true]) ); $this->accountToTagRepository ->expects($this->once()) ->method('add') ->with(100, $accountCreateDto->tags); $this->accountItems->addItems(true, 100, $accountCreateDto); } public function testAddItemsWithNoPermission() { $accountCreateDto = AccountDataGenerator::factory()->buildAccountCreateDto(); $this->accountToUserGroupRepository ->expects($this->never()) ->method('addByType'); $this->accountToUserRepository ->expects($this->never()) ->method('addByType'); $this->accountToTagRepository ->expects($this->once()) ->method('add') ->with(100, $accountCreateDto->tags); $this->accountItems->addItems(false, 100, $accountCreateDto); } public function testAddItemsWithException() { $accountCreateDto = AccountDataGenerator::factory()->buildAccountCreateDto(); $this->accountToUserGroupRepository ->expects($this->never()) ->method('addByType'); $this->accountToUserRepository ->expects($this->never()) ->method('addByType'); $this->accountToTagRepository ->expects($this->once()) ->method('add') ->willThrowException(SPException::error('test')); $this->accountItems->addItems(false, 100, $accountCreateDto); } protected function setUp(): void { parent::setUp(); $this->accountToUserGroupRepository = $this->createMock(AccountToUserGroupRepository::class); $this->accountToUserRepository = $this->createMock(AccountToUserRepository::class); $this->accountToTagRepository = $this->createMock(AccountToTagRepository::class); $this->accountItems = new AccountItems( $this->application, $this->accountToUserGroupRepository, $this->accountToUserRepository, $this->accountToTagRepository ); } }