. */ namespace SP\Tests\Domain\Account\Search; use PHPUnit\Framework\MockObject\MockObject; use SP\DataModel\AccountSearchVData; use SP\Domain\Account\Ports\AccountAclServiceInterface; use SP\Domain\Account\Ports\AccountCacheServiceInterface; use SP\Domain\Account\Ports\AccountToFavoriteServiceInterface; use SP\Domain\Account\Ports\AccountToTagRepositoryInterface; use SP\Domain\Account\Search\AccountSearchDataBuilder; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\File\FileCacheInterface; use SP\Infrastructure\File\FileException; use SP\Tests\Generators\AccountDataGenerator; use SP\Tests\UnitaryTestCase; use function PHPUnit\Framework\exactly; use function PHPUnit\Framework\once; /** * Class AccountSearchDataBuilderTest * * @group unitary */ class AccountSearchDataBuilderTest extends UnitaryTestCase { private AccountSearchDataBuilder $accountSearchDataBuilder; private AccountAclServiceInterface|MockObject $accountAclService; private AccountCacheServiceInterface|MockObject $accountCacheService; private AccountToTagRepositoryInterface|MockObject $accountToTagRepository; private AccountToFavoriteServiceInterface|MockObject $accountToFavoriteService; private MockObject|FileCacheInterface $fileCache; /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\SPException */ public function testBuildFrom(): void { $accountSearchVData = array_map(static fn() => AccountDataGenerator::factory()->buildAccountSearchView(), range(0, 4)); $numResults = count($accountSearchVData); $queryResult = new QueryResult($accountSearchVData); $this->accountToFavoriteService ->expects(once()) ->method('getForUserId') ->with($this->context->getUserData()->getId()); $this->accountCacheService ->expects(exactly($numResults)) ->method('getCacheForAccount'); $this->accountAclService ->expects(exactly($numResults)) ->method('getAcl'); $this->accountToTagRepository ->expects(exactly($numResults)) ->method('getTagsByAccountId') ->willReturn(new QueryResult([1, 2, 3])); $this->fileCache ->expects(exactly($numResults)) ->method('save'); $this->accountSearchDataBuilder->buildFrom($queryResult); } /** * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\SPException */ public function testBuildFromWithColorCacheException(): void { $accountSearchVData = array_map(static fn() => AccountDataGenerator::factory()->buildAccountSearchView(), range(0, 4)); $numResults = count($accountSearchVData); $queryResult = new QueryResult($accountSearchVData); $this->accountToFavoriteService ->expects(once()) ->method('getForUserId') ->with($this->context->getUserData()->getId()); $this->accountCacheService ->expects(exactly($numResults)) ->method('getCacheForAccount'); $this->accountAclService ->expects(exactly($numResults)) ->method('getAcl'); $this->accountToTagRepository ->expects(exactly($numResults)) ->method('getTagsByAccountId') ->willReturn(new QueryResult([1, 2, 3])); $this->fileCache ->expects(exactly($numResults)) ->method('save') ->willThrowException(new FileException('test')); $this->accountSearchDataBuilder->buildFrom($queryResult); } public function testInitializeWithException(): void { $fileCache = $this->createMock(FileCacheInterface::class); $fileCache ->expects(once()) ->method('load') ->willThrowException(new FileException('test')); new AccountSearchDataBuilder( $this->application, $this->accountAclService, $this->accountToTagRepository, $this->accountToFavoriteService, $this->accountCacheService, $fileCache, $this->config->getConfigData() ); } protected function setUp(): void { parent::setUp(); $this->accountAclService = $this->createMock(AccountAclServiceInterface::class); $this->accountToTagRepository = $this->createMock(AccountToTagRepositoryInterface::class); $this->accountToFavoriteService = $this->createMock(AccountToFavoriteServiceInterface::class); $this->accountCacheService = $this->createMock(AccountCacheServiceInterface::class); $this->fileCache = $this->createMock(FileCacheInterface::class); $this->fileCache ->expects(self::once()) ->method('load'); $this->accountSearchDataBuilder = new AccountSearchDataBuilder( $this->application, $this->accountAclService, $this->accountToTagRepository, $this->accountToFavoriteService, $this->accountCacheService, $this->fileCache, $this->config->getConfigData() ); } }