chore: Add tests for AccountCacheService

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-12-03 22:57:10 +01:00
parent c588734fb8
commit 973bb8ebfd
3 changed files with 116 additions and 3 deletions

View File

@@ -205,7 +205,7 @@ class StatelessContext extends ContextBase
*/
public function getAccountsCache(): ?array
{
return null;
return $this->getContextKey('accountsCache');
}
/**

View File

@@ -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);
}

View File

@@ -0,0 +1,114 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, 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 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
);
}
}