Files
sysPass/tests/SPT/Infrastructure/User/Repositories/UserProfileTest.php
Rubén D cda692d4de chore: Relocate classes
Signed-off-by: Rubén D <nuxsmin@syspass.org>
2024-04-21 21:09:32 +02:00

383 lines
12 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\User\Repositories;
use Aura\SqlQuery\Common\DeleteInterface;
use Aura\SqlQuery\Common\InsertInterface;
use Aura\SqlQuery\Common\SelectInterface;
use Aura\SqlQuery\Common\UpdateInterface;
use Aura\SqlQuery\QueryFactory;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Common\Models\Simple as SimpleModel;
use SP\Domain\Core\Dtos\ItemSearchDto;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Database\Ports\DatabaseInterface;
use SP\Domain\User\Models\UserProfile as UserProfileModel;
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SP\Infrastructure\User\Repositories\UserProfile;
use SPT\Generators\UserProfileDataGenerator;
use SPT\UnitaryTestCase;
/**
* Class UserProfileTest
*/
#[Group('unitary')]
class UserProfileTest extends UnitaryTestCase
{
private UserProfile $userProfile;
private MockObject|DatabaseInterface $database;
public function testSearch()
{
$item = new ItemSearchDto(self::$faker->name);
$callback = new Callback(
static function (QueryData $arg) use ($item) {
$query = $arg->getQuery();
$params = $query->getBindValues();
$searchStringLike = '%' . $item->getSeachString() . '%';
return count($params) === 1
&& $params['name'] === $searchStringLike
&& $arg->getMapClassName() === UserProfileModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback, true);
$this->userProfile->search($item);
}
public function testSearchWithNoString()
{
$item = new ItemSearchDto();
$callback = new Callback(
static function (QueryData $arg) use ($item) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 0
&& $arg->getMapClassName() === UserProfileModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback, true);
$this->userProfile->search($item);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testDeleteByIdBatch()
{
$ids = [self::$faker->randomNumber(), self::$faker->randomNumber(), self::$faker->randomNumber()];
$callback = new Callback(
static function (QueryData $arg) use ($ids) {
$query = $arg->getQuery();
$values = $query->getBindValues();
return count($values) === 3
&& array_shift($values) === array_shift($ids)
&& array_shift($values) === array_shift($ids)
&& array_shift($values) === array_shift($ids)
&& $arg->getMapClassName() === SimpleModel::class
&& is_a($query, DeleteInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback);
$this->userProfile->deleteByIdBatch($ids);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testDeleteByIdBatchWithNoIds(): void
{
$this->database
->expects(self::never())
->method('runQuery');
$this->userProfile->deleteByIdBatch([]);
}
public function testGetAll()
{
$callback = new Callback(
static function (QueryData $arg) {
$query = $arg->getQuery();
return $arg->getMapClassName() === UserProfileModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback);
$this->userProfile->getAll();
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testDelete()
{
$id = self::$faker->randomNumber();
$callback = new Callback(
static function (QueryData $arg) use ($id) {
$query = $arg->getQuery();
return $query->getBindValues()['id'] === $id
&& is_a($query, DeleteInterface::class)
&& !empty($query->getStatement());
}
);
$this->database->expects(self::once())->method('runQuery')->with($callback);
$this->userProfile->delete($id);
}
/**
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testCreate()
{
$userProfileData = UserProfileDataGenerator::factory()->buildUserProfileData();
$callbackDuplicate = new Callback(
static function (QueryData $arg) use ($userProfileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['name'] === $userProfileData->getName()
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$callbackCreate = new Callback(
static function (QueryData $arg) use ($userProfileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 2
&& $params['name'] === $userProfileData->getName()
&& $params['profile'] === $userProfileData->getProfile()
&& is_a($query, InsertInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::exactly(2))
->method('runQuery')
->with(...self::withConsecutive([$callbackDuplicate], [$callbackCreate]))
->willReturn(new QueryResult([]), new QueryResult([1]));
$this->userProfile->create($userProfileData);
}
/**
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testCreateWithDuplicate()
{
$userProfileData = UserProfileDataGenerator::factory()->buildUserProfileData();
$callbackDuplicate = new Callback(
static function (QueryData $arg) use ($userProfileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['name'] === $userProfileData->getName()
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callbackDuplicate)
->willReturn(new QueryResult([1]));
$this->expectException(DuplicatedItemException::class);
$this->expectExceptionMessage('Duplicated profile name');
$this->userProfile->create($userProfileData);
}
/**
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testUpdate()
{
$profileData = UserProfileDataGenerator::factory()->buildUserProfileData();
$callbackDuplicate = new Callback(
static function (QueryData $arg) use ($profileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 2
&& $params['id'] === $profileData->getId()
&& $params['name'] === $profileData->getName()
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$callbackUpdate = new Callback(
static function (QueryData $arg) use ($profileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 3
&& $params['id'] === $profileData->getId()
&& $params['name'] === $profileData->getName()
&& $params['profile'] === $profileData->getProfile()
&& is_a($query, UpdateInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::exactly(2))
->method('runQuery')
->with(...self::withConsecutive([$callbackDuplicate], [$callbackUpdate]))
->willReturn(new QueryResult([]), new QueryResult([1]));
$this->userProfile->update($profileData);
}
/**
* @throws ConstraintException
* @throws DuplicatedItemException
* @throws QueryException
*/
public function testUpdateWithDuplicate()
{
$profileData = UserProfileDataGenerator::factory()->buildUserProfileData();
$callbackDuplicate = new Callback(
static function (QueryData $arg) use ($profileData) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 2
&& $params['id'] === $profileData->getId()
&& $params['name'] === $profileData->getName()
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callbackDuplicate)
->willReturn(new QueryResult([1]));
$this->expectException(DuplicatedItemException::class);
$this->expectExceptionMessage('Duplicated profile name');
$this->userProfile->update($profileData);
}
public function testGetById()
{
$this->database
->expects($this->once())
->method('runQuery')
->with(
self::callback(static function (QueryData $queryData) {
$params = $queryData->getQuery()->getBindValues();
return count($params) === 1
&& $params['id'] === 100
&& $queryData->getMapClassName() === UserProfileModel::class;
})
);
$this->userProfile->getById(100);
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->userProfile = new UserProfile(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}