chore: Create AccountToFavoriteService tests.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2023-05-13 11:03:42 +02:00
parent a8939dfddf
commit b17cc55bfb
3 changed files with 135 additions and 25 deletions

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -54,6 +54,7 @@ final class AccountToFavoriteService extends Service implements AccountToFavorit
*
* @throws ConstraintException
* @throws QueryException
* @throws \SP\Core\Exceptions\SPException
*/
public function getForUserId(int $id): array
{

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -35,6 +35,9 @@ use SP\Core\Events\EventMessage;
use SP\Core\Exceptions\ConstraintException;
use SP\Core\Exceptions\QueryException;
use SP\Core\Exceptions\SPException;
use function SP\__u;
use function SP\logger;
use function SP\processException;
/**
* Class Database
@@ -90,6 +93,8 @@ final class Database implements DatabaseInterface
}
/**
* Perform a SELECT type query
*
* @throws ConstraintException
* @throws QueryException
*/
@@ -125,7 +130,7 @@ final class Database implements DatabaseInterface
}
/**
* Realizar una consulta a la BBDD.
* Perform any type of query
*
* @throws QueryException
* @throws ConstraintException
@@ -213,26 +218,8 @@ final class Database implements DatabaseInterface
}
}
/**
* Strips out the unused params from the query count
*
* TODO: remove??
*/
private function getParamsForCount(QueryData $queryData): array
{
$countSelect = substr_count($queryData->getSelect(), '?');
$countFrom = substr_count($queryData->getFrom(), '?');
$countWhere = substr_count($queryData->getWhere(), '?');
return array_slice($queryData->getParams(), $countSelect, $countFrom + $countWhere);
}
private function fetch(QueryData $queryData, PDOStatement $stmt): array
{
if ($queryData->isUseKeyPair()) {
return $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
}
if ($queryData->getMapClassName()) {
return $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, $queryData->getMapClassName());
}
@@ -284,7 +271,7 @@ final class Database implements DatabaseInterface
}
/**
* Iniciar una transacción
* Start a transaction
*/
public function beginTransaction(): bool
{
@@ -310,7 +297,7 @@ final class Database implements DatabaseInterface
}
/**
* Finalizar una transacción
* Finish a transaction
*/
public function endTransaction(): bool
{
@@ -330,7 +317,7 @@ final class Database implements DatabaseInterface
}
/**
* Rollback de una transacción
* Rollback a transaction
*/
public function rollbackTransaction(): bool
{
@@ -349,6 +336,13 @@ final class Database implements DatabaseInterface
return $result;
}
/**
* Get the columns of a table
*
* @param string $table
*
* @return array
*/
public function getColumnsForTable(string $table): array
{
$conn = $this->dbHandler->getConnection()->query("SELECT * FROM `$table` LIMIT 0");
@@ -360,4 +354,4 @@ final class Database implements DatabaseInterface
return $columns;
}
}
}

View File

@@ -0,0 +1,115 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, 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\Tests\Domain\Account\Services;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Account\Ports\AccountToFavoriteRepositoryInterface;
use SP\Domain\Account\Services\AccountToFavoriteService;
use SP\Infrastructure\Database\QueryResult;
use SP\Tests\UnitaryTestCase;
/**
* Class AccountToFavoriteServiceTest
*
* @group unitary
*/
class AccountToFavoriteServiceTest extends UnitaryTestCase
{
private AccountToFavoriteRepositoryInterface|MockObject $accountToFavoriteRepository;
private AccountToFavoriteService $accountToFavoriteService;
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\SPException
*/
public function testGetForUserId()
{
$userId = self::$faker->randomNumber();
$result = new QueryResult([['userId' => $userId, 'accountId' => self::$faker->randomNumber()]]);
$this->accountToFavoriteRepository
->expects(self::once())
->method('getForUserId')
->with($userId)
->willReturn($result);
$actual = $this->accountToFavoriteService->getForUserId($userId);
$this->assertEquals($result->getDataAsArray(), $actual);
}
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testDelete()
{
$accountId = self::$faker->randomNumber();
$userId = self::$faker->randomNumber();
$out = self::$faker->boolean;
$this->accountToFavoriteRepository->expects(self::once())
->method('delete')
->with($accountId, $userId)
->willReturn($out);
$actual = $this->accountToFavoriteService->delete($accountId, $userId);
$this->assertEquals($out, $actual);
}
/**
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\ConstraintException
*/
public function testAdd()
{
$accountId = self::$faker->randomNumber();
$userId = self::$faker->randomNumber();
$out = self::$faker->randomNumber();
$this->accountToFavoriteRepository->expects(self::once())
->method('add')
->with($accountId, $userId)
->willReturn($out);
$actual = $this->accountToFavoriteService->add($accountId, $userId);
$this->assertEquals($out, $actual);
}
protected function setUp(): void
{
parent::setUp();
$this->accountToFavoriteRepository = $this->createMock(AccountToFavoriteRepositoryInterface::class);
$this->accountToFavoriteService =
new AccountToFavoriteService($this->application, $this->accountToFavoriteRepository);
}
}