mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-06 08:34:16 +01:00
chore: Add tests for AccountCacheService
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -205,7 +205,7 @@ class StatelessContext extends ContextBase
|
||||
*/
|
||||
public function getAccountsCache(): ?array
|
||||
{
|
||||
return null;
|
||||
return $this->getContextKey('accountsCache');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
114
tests/SP/Domain/Account/Services/AccountCacheServiceTest.php
Normal file
114
tests/SP/Domain/Account/Services/AccountCacheServiceTest.php
Normal 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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user