chore(tests): Refactor TransactionAware callback

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-03-17 17:27:35 +01:00
parent 9dafac29b5
commit a766de876f
10 changed files with 80 additions and 55 deletions

View File

@@ -94,9 +94,8 @@ final class AuthToken extends Service implements AuthTokenService
}
/**
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @param int $id
* @return AuthTokenModel
*/
public function getById(int $id): AuthTokenModel
{
@@ -278,10 +277,10 @@ final class AuthToken extends Service implements AuthTokenService
/**
* Devolver los datos de un token
*
* @throws ConstraintException
* @param int $actionId
* @param string $token
* @return AuthTokenModel
* @throws NoSuchItemException
* @throws QueryException
* @throws SPException
*/
public function getTokenByToken(int $actionId, string $token): AuthTokenModel
{
@@ -296,9 +295,6 @@ final class AuthToken extends Service implements AuthTokenService
/**
* @return array<T>
* @throws ConstraintException
* @throws QueryException
* @throws SPException
*/
public function getAll(): array
{

View File

@@ -59,7 +59,7 @@ class AuthTokenTest extends UnitaryTestCase
private AuthTokenRepository|MockObject $authTokenRepository;
private CryptInterface|MockObject $crypt;
private AuthToken $authToken;
private AuthToken $authToken;
public static function secureActionDataProvider(): array
{
@@ -567,12 +567,7 @@ class AuthTokenTest extends UnitaryTestCase
$this->authTokenRepository
->expects(self::once())
->method('transactionAware')
->with(
new Callback(function (callable $callable) use ($authToken) {
$callable();
return true;
})
)
->with(self::withResolveCallableCallback())
->willReturn(true);
$this->authTokenRepository

View File

@@ -25,7 +25,6 @@
namespace SPT\Domain\Config\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use RuntimeException;
use SP\DataModel\Dto\ConfigRequest;
@@ -216,13 +215,7 @@ class ConfigTest extends UnitaryTestCase
$this->configRepository
->expects(self::once())
->method('transactionAware')
->with(
new Callback(function (callable $closure) {
$closure();
return true;
})
);
->with(self::withResolveCallableCallback());
$this->configRepository
->expects(self::exactly(3))

View File

@@ -27,7 +27,6 @@ namespace SPT\Domain\Crypt\Services;
use Dotenv\Repository\RepositoryInterface;
use Exception;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Account\Ports\AccountCryptService;
use SP\Domain\Common\Ports\Repository;
@@ -49,11 +48,11 @@ use SPT\UnitaryTestCase;
class MasterPassTest extends UnitaryTestCase
{
private ConfigService|MockObject $configService;
private ConfigService|MockObject $configService;
private AccountCryptService|MockObject $accountCryptService;
private CustomFieldCryptService|MockObject $customFieldCryptService;
private MockObject|RepositoryInterface $repository;
private MasterPass $masterPass;
private MasterPass $masterPass;
public function testCheckUserUpdateMPassWithFutureTime()
{
@@ -166,13 +165,7 @@ class MasterPassTest extends UnitaryTestCase
$this->repository
->expects(self::once())
->method('transactionAware')
->with(
new Callback(static function (callable $callable) {
$callable();
return true;
})
);
->with(self::withResolveCallableCallback());
$request = new UpdateMasterPassRequest('123', '456', $hash);
@@ -236,5 +229,4 @@ class MasterPassTest extends UnitaryTestCase
$this->repository
);
}
}

View File

@@ -62,17 +62,19 @@ class PluginUpgraderTest extends UnitaryTestCase
->method('getByName')
->willReturn($pluginModel);
$version = $pluginModel->getVersionLevel() + 100;
[$version, $build] = explode('.', $pluginModel->getVersionLevel());
$upgradeVersion = sprintf('%d.%d', (int)$version + 100, (int)$build);
$plugin->expects($this->once())
->method('onUpgrade')
->with($version);
->with($upgradeVersion);
$pluginManagerService->expects($this->once())
->method('update')
->with($pluginModel->mutate(['data' => null, 'versionLevel' => $version]));
->with($pluginModel->mutate(['data' => null, 'versionLevel' => $upgradeVersion]));
$pluginUpgrader->upgradeFor($plugin, $version);
$pluginUpgrader->upgradeFor($plugin, $upgradeVersion);
}
/**
@@ -95,7 +97,9 @@ class PluginUpgraderTest extends UnitaryTestCase
->method('getByName')
->willReturn($pluginModel);
$version = $pluginModel->getVersionLevel() - 100;
[$version, $build] = explode('.', $pluginModel->getVersionLevel());
$upgradeVersion = sprintf('%d.%d', (int)$version - 100, (int)$build);
$plugin->expects($this->never())
->method('onUpgrade');
@@ -103,7 +107,7 @@ class PluginUpgraderTest extends UnitaryTestCase
$pluginManagerService->expects($this->never())
->method('update');
$pluginUpgrader->upgradeFor($plugin, $version);
$pluginUpgrader->upgradeFor($plugin, $upgradeVersion);
}
/**

View File

@@ -47,5 +47,4 @@ final class PluginGenerator extends DataGenerator
'versionLevel' => sprintf('%d.%d', $this->faker->randomNumber(3, true), $this->faker->randomNumber(6, true))
];
}
}

View File

@@ -104,4 +104,20 @@ trait PHPUnitHelper
}
});
}
public static function withResolveCallableCallback(): Callback
{
return new Callback(function (callable $callable) {
$callable();
return true;
});
}
public static function getRepositoryStubMethods(string $class): array
{
return array_filter(
get_class_methods($class),
static fn(string $method) => $method != 'transactionAware'
);
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,7 +24,6 @@
namespace SPT\Stubs;
use Closure;
use SP\Domain\Account\Ports\AccountRepository;
/**
@@ -32,8 +31,5 @@ use SP\Domain\Account\Ports\AccountRepository;
*/
abstract class AccountRepositoryStub implements AccountRepository
{
public function transactionAware(Closure $closure, object $newThis): mixed
{
return $closure->call($newThis);
}
use TransactionAwareTrait;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,16 +24,12 @@
namespace SPT\Stubs;
use Closure;
use SP\Domain\Account\Ports\PublicLinkRepository;
/**
* Class AbstractPublicLinkRepositoryStub
* Class PublicLinkRepositoryStub
*/
abstract class PublicLinkRepositoryStub implements PublicLinkRepository
{
public function transactionAware(Closure $closure, object $newThis): mixed
{
return $closure->call($newThis);
}
use TransactionAwareTrait;
}

View File

@@ -0,0 +1,38 @@
<?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\Stubs;
use Closure;
/**
* Trait TransactionAwareTrait
*/
trait TransactionAwareTrait
{
public function transactionAware(Closure $closure, object $newThis): mixed
{
return $closure->call($newThis);
}
}