Files
sysPass/tests/SPT/Infrastructure/Account/Repositories/AccountToUserGroupTest.php
Rubén D f697f70df3 chore(test): Refactor test naming
Signed-off-by: Rubén D <nuxsmin@syspass.org>
2024-01-16 09:12:38 +01:00

224 lines
6.4 KiB
PHP

<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, 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 SPT\Infrastructure\Account\Repositories;
use Aura\SqlQuery\QueryFactory;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use SP\DataModel\ItemData;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Account\Repositories\AccountToUserGroup;
use SP\Infrastructure\Database\DatabaseInterface;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SPT\UnitaryTestCase;
/**
* Class AccountToUserGroupRepositoryTest
*
* @group unitary
*/
class AccountToUserGroupTest extends UnitaryTestCase
{
private MockObject|DatabaseInterface $database;
private AccountToUserGroup $accountToUserGroup;
/**
* @throws QueryException
* @throws 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->accountToUserGroup->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->accountToUserGroup->getUserGroupsByAccountId($id);
}
/**
* @throws QueryException
* @throws ConstraintException
*/
public function testDeleteByUserGroupId(): void
{
$userGroupId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setAffectedNumRows(1);
$callback = new Callback(
static function (QueryData $arg) use ($userGroupId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['userGroupId'] === $userGroupId
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertTrue($this->accountToUserGroup->deleteByUserGroupId($userGroupId));
}
/**
* @throws QueryException
* @throws 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->accountToUserGroup->addByType(
self::$faker->randomNumber(),
$userGroups,
self::$faker->boolean
);
}
/**
* @throws QueryException
* @throws 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->accountToUserGroup->deleteByAccountId($accountId));
}
public function testGetUserGroupsByUserGroupId(): void
{
$id = self::$faker->randomNumber();
$callback = new Callback(
static function (QueryData $arg) use ($id) {
$query = $arg->getQuery();
return $query->getBindValues()['userGroupId'] === $id
&& $arg->getMapClassName() === ItemData::class
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doSelect')
->with($callback)
->willReturn(new QueryResult());
$this->accountToUserGroup->getUserGroupsByUserGroupId($id);
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->accountToUserGroup = new AccountToUserGroup(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}