mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-24 17:17:15 +01:00
chore(tests): UT for Config service & repository
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
185
tests/SPT/Infrastructure/Config/Repositories/ConfigTest.php
Normal file
185
tests/SPT/Infrastructure/Config/Repositories/ConfigTest.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?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\Config\Repositories;
|
||||
|
||||
use Aura\SqlQuery\Common\InsertInterface;
|
||||
use Aura\SqlQuery\Common\SelectInterface;
|
||||
use Aura\SqlQuery\Common\UpdateInterface;
|
||||
use Aura\SqlQuery\QueryFactory;
|
||||
use PHPUnit\Framework\Constraint\Callback;
|
||||
use SP\Domain\Common\Models\Simple;
|
||||
use SP\Domain\Config\Models\Config as ConfigModel;
|
||||
use SP\Domain\Core\Exceptions\ConstraintException;
|
||||
use SP\Domain\Core\Exceptions\QueryException;
|
||||
use SP\Infrastructure\Config\Repositories\Config;
|
||||
use SP\Infrastructure\Database\DatabaseInterface;
|
||||
use SP\Infrastructure\Database\QueryData;
|
||||
use SP\Infrastructure\Database\QueryResult;
|
||||
use SPT\Generators\ConfigGenerator;
|
||||
use SPT\UnitaryTestCase;
|
||||
|
||||
/**
|
||||
* Class ConfigTest
|
||||
*
|
||||
* @group unitary
|
||||
*/
|
||||
class ConfigTest extends UnitaryTestCase
|
||||
{
|
||||
private Config $configRepository;
|
||||
|
||||
public function testGetByParam()
|
||||
{
|
||||
$param = self::$faker->colorName();
|
||||
|
||||
$callback = new Callback(
|
||||
static function (QueryData $arg) use ($param) {
|
||||
$query = $arg->getQuery();
|
||||
$params = $query->getBindValues();
|
||||
|
||||
return count($params) === 1
|
||||
&& $params['parameter'] === $param
|
||||
&& $arg->getMapClassName() === ConfigModel::class
|
||||
&& is_a($query, SelectInterface::class)
|
||||
&& !empty($query->getStatement());
|
||||
}
|
||||
);
|
||||
|
||||
$this->database
|
||||
->expects(self::once())
|
||||
->method('doSelect')
|
||||
->with($callback);
|
||||
|
||||
$this->configRepository->getByParam($param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$config = ConfigGenerator::factory()->buildConfig();
|
||||
|
||||
$callback = new Callback(
|
||||
static function (QueryData $arg) use ($config) {
|
||||
$query = $arg->getQuery();
|
||||
$params = $query->getBindValues();
|
||||
|
||||
return count($params) === 2
|
||||
&& $params['parameter'] === $config->getParameter()
|
||||
&& $params['value'] === $config->getValue()
|
||||
&& is_a($query, InsertInterface::class)
|
||||
&& !empty($query->getStatement());
|
||||
}
|
||||
);
|
||||
|
||||
$queryResult = new QueryResult([1]);
|
||||
|
||||
$this->database
|
||||
->expects(self::exactly(1))
|
||||
->method('doQuery')
|
||||
->with($callback)
|
||||
->willReturn($queryResult);
|
||||
|
||||
$out = $this->configRepository->create($config);
|
||||
|
||||
$this->assertEquals($queryResult, $out);
|
||||
}
|
||||
|
||||
public function testHas()
|
||||
{
|
||||
$param = self::$faker->colorName();
|
||||
|
||||
$callback = new Callback(
|
||||
static function (QueryData $arg) use ($param) {
|
||||
$query = $arg->getQuery();
|
||||
$params = $query->getBindValues();
|
||||
|
||||
return count($params) === 1
|
||||
&& $params['parameter'] === $param
|
||||
&& $arg->getMapClassName() === Simple::class
|
||||
&& is_a($query, SelectInterface::class)
|
||||
&& !empty($query->getStatement());
|
||||
}
|
||||
);
|
||||
|
||||
$this->database
|
||||
->expects(self::once())
|
||||
->method('doSelect')
|
||||
->with($callback)
|
||||
->willReturn(new QueryResult([1]));
|
||||
|
||||
$this->assertTrue($this->configRepository->has($param));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$config = ConfigGenerator::factory()->buildConfig();
|
||||
|
||||
$callback = new Callback(
|
||||
static function (QueryData $arg) use ($config) {
|
||||
$query = $arg->getQuery();
|
||||
$params = $query->getBindValues();
|
||||
|
||||
return count($params) === 2
|
||||
&& $params['parameter'] === $config->getParameter()
|
||||
&& $params['value'] === $config->getValue()
|
||||
&& is_a($query, UpdateInterface::class)
|
||||
&& !empty($query->getStatement());
|
||||
}
|
||||
);
|
||||
|
||||
$queryResult = new QueryResult([1]);
|
||||
|
||||
$this->database
|
||||
->expects(self::exactly(1))
|
||||
->method('doQuery')
|
||||
->with($callback)
|
||||
->willReturn($queryResult);
|
||||
|
||||
$out = $this->configRepository->update($config);
|
||||
|
||||
$this->assertEquals($queryResult, $out);
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->database = $this->createMock(DatabaseInterface::class);
|
||||
$queryFactory = new QueryFactory('mysql');
|
||||
|
||||
$this->configRepository = new Config(
|
||||
$this->database,
|
||||
$this->context,
|
||||
$this->application->getEventDispatcher(),
|
||||
$queryFactory,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user