Files
sysPass/tests/SPT/Infrastructure/Account/Repositories/AccountToFavoriteTest.php
Rubén D f697f70df3 chore(test): Refactor test naming
Signed-off-by: Rubén D <nuxsmin@syspass.org>
2024-01-16 09:12:38 +01:00

145 lines
4.3 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\Account\Repositories;
use Aura\SqlQuery\QueryFactory;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Common\Models\Simple;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Account\Repositories\AccountToFavorite;
use SP\Infrastructure\Database\DatabaseInterface;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SPT\UnitaryTestCase;
/**
* Class AccountToFavoriteRepositoryTest
*
* @group unitary
*/
class AccountToFavoriteTest extends UnitaryTestCase
{
private MockObject|DatabaseInterface $database;
private AccountToFavorite $accountToFavorite;
public function testGetForUserId()
{
$id = self::$faker->randomNumber();
$callback = new Callback(
static function (QueryData $arg) use ($id) {
$query = $arg->getQuery();
return $query->getBindValues()['userId'] === $id
&& $arg->getMapClassName() === Simple::class
&& !empty($query->getStatement());
}
);
$this->database->expects(self::once())
->method('doSelect')
->with($callback)
->willReturn(new QueryResult());
$this->accountToFavorite->getForUserId($id);
}
public function testDelete()
{
$accountId = self::$faker->randomNumber();
$userId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setAffectedNumRows(1);
$callback = new Callback(
static function (QueryData $arg) use ($accountId, $userId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['accountId'] === $accountId
&& $params['userId'] === $userId
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertTrue($this->accountToFavorite->delete($accountId, $userId));
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testAdd()
{
$accountId = self::$faker->randomNumber();
$userId = self::$faker->randomNumber();
$expected = new QueryResult();
$expected->setLastId(1);
$callback = new Callback(
static function (QueryData $arg) use ($accountId, $userId) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return $params['accountId'] === $accountId
&& $params['userId'] === $userId
&& !empty($query->getStatement());
}
);
$this->database->expects(self::once())
->method('doQuery')
->with($callback)
->willReturn($expected);
$this->assertEquals($expected->getLastId(), $this->accountToFavorite->add($accountId, $userId));
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->accountToFavorite = new AccountToFavorite(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}