Files
sysPass/tests/SP/Infrastructure/Account/Repositories/AccountToUserRepositoryTest.php
2022-11-20 17:43:49 +01:00

198 lines
5.8 KiB
PHP

<?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\Infrastructure\Account\Repositories;
use Aura\SqlQuery\QueryFactory;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use SP\DataModel\ItemData;
use SP\Infrastructure\Account\Repositories\AccountToUserRepository;
use SP\Infrastructure\Database\DatabaseInterface;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SP\Tests\UnitaryTestCase;
/**
* Class AccountToUserRepositoryTest
*/
class AccountToUserRepositoryTest extends UnitaryTestCase
{
private MockObject|DatabaseInterface $database;
private AccountToUserRepository $accountToUserRepository;
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testDeleteTypeByAccountId(): void
{
$accountId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setAffectedNumRows(1);
$callback = new Callback(
static function (QueryData $arg) use ($accountId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['accountId'] === $accountId
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertTrue($this->accountToUserRepository->deleteByAccountId($accountId));
}
public function testGetUserGroupsByAccountId(): void
{
$id = self::$faker->randomNumber();
$callback = new Callback(
static function (QueryData $arg) use ($id) {
$query = $arg->getQuery();
return $query->getBindValues()['accountId'] === $id
&& $arg->getMapClassName() === ItemData::class
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doSelect')
->with($callback)
->willReturn(new QueryResult());
$this->accountToUserRepository->getUsersByAccountId($id);
}
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testDeleteByUserGroupId(): void
{
$accountId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setAffectedNumRows(1);
$callback = new Callback(
static function (QueryData $arg) use ($accountId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['accountId'] === $accountId
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertTrue($this->accountToUserRepository->deleteByAccountId($accountId));
}
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testAddByType(): void
{
$userGroups = self::getRandomNumbers(10);
$callback = new Callback(
static fn(QueryData $arg) => !empty($arg->getQuery()->getStatement())
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback);
$this->accountToUserRepository->addByType(
self::$faker->randomNumber(),
$userGroups,
self::$faker->boolean
);
}
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testDeleteByAccountId(): void
{
$accountId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setAffectedNumRows(1);
$callback = new Callback(
static function (QueryData $arg) use ($accountId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['accountId'] === $accountId
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertTrue($this->accountToUserRepository->deleteByAccountId($accountId));
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->accountToUserRepository = new AccountToUserRepository(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}