diff --git a/lib/SP/Core/Context/StatelessContext.php b/lib/SP/Core/Context/StatelessContext.php index 4b8e634b..cf6ca883 100644 --- a/lib/SP/Core/Context/StatelessContext.php +++ b/lib/SP/Core/Context/StatelessContext.php @@ -205,7 +205,7 @@ class StatelessContext extends ContextBase */ public function getAccountsCache(): ?array { - return null; + return $this->getContextKey('accountsCache'); } /** diff --git a/lib/SP/Domain/Account/Services/AccountCacheService.php b/lib/SP/Domain/Account/Services/AccountCacheService.php index ee505543..bb38d46b 100644 --- a/lib/SP/Domain/Account/Services/AccountCacheService.php +++ b/lib/SP/Domain/Account/Services/AccountCacheService.php @@ -39,8 +39,7 @@ final class AccountCacheService extends Service implements AccountCacheServiceIn public function __construct( Application $application, private AccountToUserRepositoryInterface $accountToUserRepository, - private AccountToUserGroupRepositoryInterface $accountToUserGroupRepository, - + private AccountToUserGroupRepositoryInterface $accountToUserGroupRepository ) { parent::__construct($application); } diff --git a/tests/SP/Domain/Account/Services/AccountCacheServiceTest.php b/tests/SP/Domain/Account/Services/AccountCacheServiceTest.php new file mode 100644 index 00000000..ab6e439a --- /dev/null +++ b/tests/SP/Domain/Account/Services/AccountCacheServiceTest.php @@ -0,0 +1,114 @@ +. + */ + +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 + */ +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 + ); + } + +}