. */ namespace SP\Tests\Domain\Account\Services; use PHPUnit\Framework\MockObject\MockObject; use SP\Domain\Account\Dtos\AccountCacheDto; use SP\Domain\Account\Ports\AccountToUserGroupRepositoryInterface; use SP\Domain\Account\Ports\AccountToUserRepositoryInterface; use SP\Domain\Account\Services\AccountCacheService; use SP\Infrastructure\Database\QueryResult; use SP\Tests\UnitaryTestCase; /** * Class AccountCacheServiceTest * * @group unitary */ class AccountCacheServiceTest extends UnitaryTestCase { private AccountToUserRepositoryInterface|MockObject $accountToUserRepository; private AccountToUserGroupRepositoryInterface|MockObject $accountToUserGroupRepository; private AccountCacheService $accountCacheService; /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\SPException */ public function testGetCacheForAccount() { $accountId = self::$faker->randomNumber(); $dateEdit = self::$faker->unixTime; $accountCacheDto = new AccountCacheDto($accountId, [1, 2, 3], [1, 2, 3]); $this->accountToUserRepository ->expects(self::once()) ->method('getUsersByAccountId') ->with($accountId) ->willReturn(new QueryResult([1, 2, 3])); $this->accountToUserGroupRepository ->expects(self::once()) ->method('getUserGroupsByAccountId') ->with($accountId) ->willReturn(new QueryResult([1, 2, 3])); $out = $this->accountCacheService->getCacheForAccount($accountId, $dateEdit); $this->assertEquals($accountCacheDto, $out); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\SPException */ public function testGetCacheForAccountWithCacheHit() { $accountId = self::$faker->randomNumber(); $accountCacheDto = new AccountCacheDto($accountId, [1, 2, 3], [1, 2, 3]); $this->context->setAccountsCache([$accountId => $accountCacheDto]); $this->accountToUserRepository ->expects(self::never()) ->method('getUsersByAccountId'); $this->accountToUserGroupRepository ->expects(self::never()) ->method('getUserGroupsByAccountId'); $out = $this->accountCacheService->getCacheForAccount($accountId, $accountCacheDto->getTime()); $this->assertEquals($accountCacheDto, $out); } protected function setUp(): void { parent::setUp(); $this->accountToUserRepository = $this->createMock(AccountToUserRepositoryInterface::class); $this->accountToUserGroupRepository = $this->createMock(AccountToUserGroupRepositoryInterface::class); $this->accountCacheService = new AccountCacheService( $this->application, $this->accountToUserRepository, $this->accountToUserGroupRepository ); } }