. */ namespace SP\Tests\Domain\Account\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; use SP\Domain\Account\Dtos\AccountCacheDto; use SP\Domain\Account\Ports\AccountToUserGroupRepository; use SP\Domain\Account\Ports\AccountToUserRepository; use SP\Domain\Account\Services\AccountCache; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Infrastructure\Account\Repositories\AccountToUserGroup; use SP\Infrastructure\Database\QueryResult; use SP\Tests\UnitaryTestCase; /** * Class AccountCacheServiceTest * */ #[Group('unitary')] class AccountCacheTest extends UnitaryTestCase { private AccountToUserRepository|MockObject $accountToUserRepository; private AccountToUserGroup|MockObject $accountToUserGroupRepository; private AccountCache $accountCache; /** * @throws QueryException * @throws ConstraintException * @throws 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->accountCache->getCacheForAccount($accountId, $dateEdit); $this->assertEquals($accountCacheDto, $out); } /** * @throws QueryException * @throws ConstraintException * @throws 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->accountCache->getCacheForAccount($accountId, $accountCacheDto->getTime()); $this->assertEquals($accountCacheDto, $out); } protected function setUp(): void { parent::setUp(); $this->accountToUserRepository = $this->createMock(AccountToUserRepository::class); $this->accountToUserGroupRepository = $this->createMock(AccountToUserGroupRepository::class); $this->accountCache = new AccountCache( $this->application, $this->accountToUserRepository, $this->accountToUserGroupRepository ); } }