Files
sysPass/tests/SPT/Infrastructure/Security/Repositories/TrackTest.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

234 lines
7.2 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\Security\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\Core\Dtos\ItemSearchDto;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Database\Ports\DatabaseInterface;
use SP\Domain\Security\Models\Track as TrackModel;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SP\Infrastructure\Security\Repositories\Track;
use SPT\Generators\TrackGenerator;
use SPT\UnitaryTestCase;
/**
* Class TrackTest
*/
#[Group('unitary')]
class TrackTest extends UnitaryTestCase
{
private DatabaseInterface|MockObject $database;
private Track $track;
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testClear()
{
$callback = new Callback(
static function (QueryData $arg) {
$query = $arg->getQuery();
return count($query->getBindValues()) === 0
&& is_a($query, DeleteInterface::class)
&& !empty($query->getStatement());
}
);
$this->database->expects(self::once())->method('runQuery')->with($callback);
$this->track->clear();
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testUnlock()
{
$callbackCreate = new Callback(
static function (QueryData $arg) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['id'] === 100
&& is_a($query, UpdateInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::exactly(1))
->method('runQuery')
->with($callbackCreate)
->willReturn(new QueryResult(null, 1));
$out = $this->track->unlock(100);
$this->assertEquals(1, $out);
}
public function testGetTracksForClientFromTime()
{
$track = TrackGenerator::factory()->buildTrack();
$callback = new Callback(
static function (QueryData $arg) use ($track) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 4
&& $params['time'] == $track->getTime()
&& $params['ipv4'] == $track->getIpv4()
&& $params['ipv6'] == $track->getIpv6()
&& $params['source'] == $track->getSource()
&& $arg->getMapClassName() === TrackModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback);
$this->track->getTracksForClientFromTime($track);
}
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) === 2
&& $params['time'] === 1000
&& $params['source'] === $searchStringLike
&& $arg->getMapClassName() === TrackModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback, true);
$this->track->search($item, 1000);
}
public function testSearchWithEmptyString()
{
$item = new ItemSearchDto();
$callback = new Callback(
static function (QueryData $arg) use ($item) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['time'] === 1000
&& $arg->getMapClassName() === TrackModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('runQuery')
->with($callback, true);
$this->track->search($item, 1000);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testAdd()
{
$track = TrackGenerator::factory()->buildTrack();
$callbackCreate = new Callback(
static function (QueryData $arg) use ($track) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 5
&& $params['userId'] === $track->getUserId()
&& $params['source'] === $track->getSource()
&& $params['timeUnlock'] === $track->getTimeUnlock()
&& $params['ipv4'] === $track->getIpv4()
&& $params['ipv6'] === $track->getIpv6()
&& is_a($query, InsertInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::exactly(1))
->method('runQuery')
->with($callbackCreate)
->willReturn(new QueryResult([]));
$this->track->add($track);
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->track = new Track(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}