test(UT): Fix AccountHistory test

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-21 07:33:21 +02:00
parent 7cad0e4a07
commit 9c8001fa8a
6 changed files with 79 additions and 9 deletions

View File

@@ -60,8 +60,8 @@ final class AccountHistoryDto extends AccountDto
?array $userGroupsView = null,
?array $userGroupsEdit = null,
public readonly ?int $accountId = null,
public readonly ?int $isDelete = null,
public readonly ?int $isModify = null,
public readonly ?bool $isDeleted = null,
public readonly ?bool $isModify = null,
public readonly ?int $passDate = null,
public readonly ?string $dateAdd = null,
public readonly ?string $dateEdit = null,

View File

@@ -79,7 +79,6 @@ final class AccountHistory extends Service implements AccountHistoryService
* @param int $id
*
* @return array Con los registros con id como clave y fecha - usuario como valor
* @throws SPException
*/
public function getHistoryForAccount(int $id): array
{

View File

@@ -0,0 +1,34 @@
<?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 SP\Domain\Common\Adapters;
/**
* Enum DumpMode
*/
enum DumpMode
{
case ONLY;
case EXCLUDE;
}

View File

@@ -30,6 +30,7 @@ use Closure;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use SP\Domain\Common\Adapters\DumpMode;
use SP\Domain\Common\Attributes\DtoTransformation;
use SP\Domain\Common\Attributes\ModelBounded;
use SP\Domain\Common\Models\Model;
@@ -97,11 +98,22 @@ abstract class Dto implements DtoInterface
}
/**
* @param array|null $properties Properties to process
* @param DumpMode $mode The mode to process the properties
* @return array
*/
final public function toArray(): array
final public function toArray(?array $properties = null, DumpMode $mode = DumpMode::ONLY): array
{
return get_object_vars($this);
$instanceProperties = get_object_vars($this);
if (null !== $properties) {
return match ($mode) {
DumpMode::ONLY => array_intersect_key($instanceProperties, array_flip($properties)),
DumpMode::EXCLUDE => array_diff_key($instanceProperties, array_flip($properties))
};
}
return $instanceProperties;
}
/**

View File

@@ -33,6 +33,7 @@ use SP\Domain\Account\Dtos\EncryptedPassword;
use SP\Domain\Account\Models\AccountHistory as AccountHistoryModel;
use SP\Domain\Account\Ports\AccountHistoryRepository;
use SP\Domain\Account\Services\AccountHistory;
use SP\Domain\Common\Adapters\DumpMode;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Dtos\ItemSearchDto;
use SP\Domain\Core\Exceptions\SPException;
@@ -138,10 +139,34 @@ class AccountHistoryTest extends UnitaryTestCase
$this->accountHistoryRepository->expects(self::once())->method('getById')->with($id)->willReturn($queryResult);
$properties = [
'usersView',
'usersEdit',
'otherUserGroupsView',
'otherUserGroupsEdit',
'tags',
'userGroupsView',
'userGroupsEdit',
'isDeleted',
'isPrivate',
'isPrivateGroup',
'otherUserGroupEdit',
'otherUserEdit',
'isModify',
];
$current = $this->accountHistory->getById($id);
$this->assertEquals(
$accountHistoryData->toArray(),
$this->accountHistory->getById($id)->toArray()
$accountHistoryData->toArray(null, $properties),
$current->toArray($properties, DumpMode::EXCLUDE)
);
$this->assertEquals((bool)$accountHistoryData->getIsDeleted(), $current->isDeleted);
$this->assertEquals((bool)$accountHistoryData->getIsPrivate(), $current->isPrivate);
$this->assertEquals((bool)$accountHistoryData->getIsPrivateGroup(), $current->isPrivateGroup);
$this->assertEquals((bool)$accountHistoryData->getOtherUserGroupEdit(), $current->otherUserGroupEdit);
$this->assertEquals((bool)$accountHistoryData->getOtherUserEdit(), $current->otherUserEdit);
$this->assertEquals((bool)$accountHistoryData->getIsModify(), $current->isModify);
}
/**

View File

@@ -235,8 +235,8 @@ final class AccountDataGenerator extends DataGenerator
otherUserEdit: $this->faker->boolean(),
otherUserGroupEdit: $this->faker->boolean(),
accountId: $this->faker->randomNumber(3),
isDelete: (int)$this->faker->boolean(),
isModify: (int)$this->faker->boolean(),
isDeleted: $this->faker->boolean(),
isModify: $this->faker->boolean(),
passDate: $this->faker->unixTime(),
dateAdd: $this->faker->dateTime()->format('Y-m-d H:i:s'),
dateEdit: $this->faker->dateTime()->format('Y-m-d H:i:s'),