. */ namespace SP\Tests\Infrastructure\Account\Repositories; use Aura\SqlQuery\QueryFactory; use PHPUnit\Framework\Constraint\Callback; use PHPUnit\Framework\MockObject\MockObject; use SP\DataModel\ItemData; use SP\Infrastructure\Account\Repositories\AccountToUserGroupRepository; use SP\Infrastructure\Database\DatabaseInterface; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; use SP\Tests\UnitaryTestCase; /** * Class AccountToUserGroupRepositoryTest * * @group unitary */ class AccountToUserGroupRepositoryTest extends UnitaryTestCase { private MockObject|DatabaseInterface $database; private AccountToUserGroupRepository $accountToUserGroupRepository; /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ public function testDeleteTypeByAccountId(): void { $accountId = self::$faker->randomNumber(); $expected = new QueryResult(); $expected->setAffectedNumRows(1); $callback = new Callback( static function (QueryData $arg) use ($accountId) { $query = $arg->getQuery(); $params = $query->getBindValues(); return $params['accountId'] === $accountId && !empty($query->getStatement()); } ); $this->database ->expects(self::once()) ->method('doQuery') ->with($callback) ->willReturn($expected); $this->assertTrue($this->accountToUserGroupRepository->deleteByAccountId($accountId)); } public function testGetUserGroupsByAccountId(): void { $id = self::$faker->randomNumber(); $callback = new Callback( static function (QueryData $arg) use ($id) { $query = $arg->getQuery(); return $query->getBindValues()['accountId'] === $id && $arg->getMapClassName() === ItemData::class && !empty($query->getStatement()); } ); $this->database ->expects(self::once()) ->method('doSelect') ->with($callback) ->willReturn(new QueryResult()); $this->accountToUserGroupRepository->getUserGroupsByAccountId($id); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ public function testDeleteByUserGroupId(): void { $userGroupId = self::$faker->randomNumber(); $expected = new QueryResult(); $expected->setAffectedNumRows(1); $callback = new Callback( static function (QueryData $arg) use ($userGroupId) { $query = $arg->getQuery(); $params = $query->getBindValues(); return $params['userGroupId'] === $userGroupId && !empty($query->getStatement()); } ); $this->database ->expects(self::once()) ->method('doQuery') ->with($callback) ->willReturn($expected); $this->assertTrue($this->accountToUserGroupRepository->deleteByUserGroupId($userGroupId)); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ public function testAddByType(): void { $userGroups = self::getRandomNumbers(10); $callback = new Callback( static fn(QueryData $arg) => !empty($arg->getQuery()->getStatement()) ); $this->database ->expects(self::once()) ->method('doQuery') ->with($callback); $this->accountToUserGroupRepository->addByType( self::$faker->randomNumber(), $userGroups, self::$faker->boolean ); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ public function testDeleteByAccountId(): void { $accountId = self::$faker->randomNumber(); $expected = new QueryResult(); $expected->setAffectedNumRows(1); $callback = new Callback( static function (QueryData $arg) use ($accountId) { $query = $arg->getQuery(); $params = $query->getBindValues(); return $params['accountId'] === $accountId && !empty($query->getStatement()); } ); $this->database ->expects(self::once()) ->method('doQuery') ->with($callback) ->willReturn($expected); $this->assertTrue($this->accountToUserGroupRepository->deleteByAccountId($accountId)); } public function testGetUserGroupsByUserGroupId(): void { $id = self::$faker->randomNumber(); $callback = new Callback( static function (QueryData $arg) use ($id) { $query = $arg->getQuery(); return $query->getBindValues()['userGroupId'] === $id && $arg->getMapClassName() === ItemData::class && !empty($query->getStatement()); } ); $this->database ->expects(self::once()) ->method('doSelect') ->with($callback) ->willReturn(new QueryResult()); $this->accountToUserGroupRepository->getUserGroupsByUserGroupId($id); } protected function setUp(): void { parent::setUp(); $this->database = $this->createMock(DatabaseInterface::class); $queryFactory = new QueryFactory('mysql'); $this->accountToUserGroupRepository = new AccountToUserGroupRepository( $this->database, $this->context, $this->application->getEventDispatcher(), $queryFactory, ); } }