. */ 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\AccountToUserRepository; use SP\Infrastructure\Database\DatabaseInterface; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; use SP\Tests\UnitaryTestCase; /** * Class AccountToUserRepositoryTest */ class AccountToUserRepositoryTest extends UnitaryTestCase { private MockObject|DatabaseInterface $database; private AccountToUserRepository $accountToUserRepository; /** * @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->accountToUserRepository->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->accountToUserRepository->getUsersByAccountId($id); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ public function testDeleteByUserGroupId(): 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->accountToUserRepository->deleteByAccountId($accountId)); } /** * @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->accountToUserRepository->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->accountToUserRepository->deleteByAccountId($accountId)); } protected function setUp(): void { parent::setUp(); $this->database = $this->createMock(DatabaseInterface::class); $queryFactory = new QueryFactory('mysql'); $this->accountToUserRepository = new AccountToUserRepository( $this->database, $this->context, $this->application->getEventDispatcher(), $queryFactory, ); } }