diff --git a/app/modules/api/Controllers/Tag/SearchController.php b/app/modules/api/Controllers/Tag/SearchController.php index a3f47201..004ede17 100644 --- a/app/modules/api/Controllers/Tag/SearchController.php +++ b/app/modules/api/Controllers/Tag/SearchController.php @@ -65,10 +65,10 @@ final class SearchController extends TagBase */ private function buildSearchData(): ItemSearchData { - $itemSearchData = new ItemSearchData(); - $itemSearchData->setSeachString($this->apiService->getParamString('text')); - $itemSearchData->setLimitCount($this->apiService->getParamInt('count', false, self::SEARCH_COUNT_ITEMS)); - - return $itemSearchData; + return new ItemSearchData( + $this->apiService->getParamString('text'), + 0, + $this->apiService->getParamInt('count', false, self::SEARCH_COUNT_ITEMS) + ); } -} \ No newline at end of file +} diff --git a/app/modules/web/Controllers/Account/ViewLinkController.php b/app/modules/web/Controllers/Account/ViewLinkController.php index 764a7b88..2da88e4a 100644 --- a/app/modules/web/Controllers/Account/ViewLinkController.php +++ b/app/modules/web/Controllers/Account/ViewLinkController.php @@ -40,6 +40,7 @@ use SP\Http\Uri; use SP\Mvc\Controller\WebControllerHelper; use SP\Util\ErrorUtil; use SP\Util\ImageUtil; +use SP\Util\ImageUtilInterface; use SP\Util\Util; /** @@ -57,7 +58,7 @@ final class ViewLinkController extends AccountControllerBase WebControllerHelper $webControllerHelper, \SP\Domain\Account\Ports\AccountServiceInterface $accountService, \SP\Domain\Account\Ports\PublicLinkServiceInterface $publicLinkService, - ImageUtil $imageUtil + ImageUtilInterface $imageUtil ) { parent::__construct( $application, diff --git a/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php index b04d1a53..814e9348 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php @@ -36,6 +36,7 @@ use SP\Modules\Web\Controllers\Helpers\HelperBase; use SP\Modules\Web\Controllers\Helpers\HelperException; use SP\Mvc\View\TemplateInterface; use SP\Util\ImageUtil; +use SP\Util\ImageUtilInterface; /** * Class AccountPasswordHelper @@ -53,7 +54,7 @@ final class AccountPasswordHelper extends HelperBase TemplateInterface $template, RequestInterface $request, Acl $acl, - ImageUtil $imageUtil, + ImageUtilInterface $imageUtil, MasterPassServiceInterface $masterPassService ) { parent::__construct($application, $template, $request); diff --git a/app/modules/web/Controllers/ItemManager/IndexController.php b/app/modules/web/Controllers/ItemManager/IndexController.php index db39c9c4..095ff00a 100644 --- a/app/modules/web/Controllers/ItemManager/IndexController.php +++ b/app/modules/web/Controllers/ItemManager/IndexController.php @@ -140,8 +140,7 @@ final class IndexController extends ControllerBase */ protected function getGridTabs(): void { - $this->itemSearchData = new ItemSearchData(); - $this->itemSearchData->setLimitCount($this->configData->getAccountCount()); + $this->itemSearchData = new ItemSearchData(null, 0, $this->configData->getAccountCount()); if ($this->checkAccess(ActionsInterface::CATEGORY)) { $this->tabsGridHelper->addTab($this->getCategoriesList()); diff --git a/lib/SP/DataModel/FileData.php b/lib/SP/DataModel/FileData.php index 7ea02846..238a93db 100644 --- a/lib/SP/DataModel/FileData.php +++ b/lib/SP/DataModel/FileData.php @@ -27,8 +27,6 @@ namespace SP\DataModel; use SP\Domain\Common\Adapters\DataModel; use SP\Domain\Common\Adapters\DataModelInterface; -defined('APP_ROOT') || die(); - /** * Class FileData * @@ -36,166 +34,69 @@ defined('APP_ROOT') || die(); */ class FileData extends DataModel implements DataModelInterface { - /** - * @var int - */ - public $id; - /** - * @var int - */ - public $accountId; - /** - * @var string - */ - public $name; - /** - * @var int - */ - public $type; - /** - * @var string - */ - public $content; - /** - * @var string - */ - public $extension; - /** - * @var string - */ - public $thumb; - /** - * @var int - */ - public $size; + protected ?int $id = null; + protected ?int $accountId = null; + protected ?string $name = null; + protected ?string $type = null; + protected ?string $content = null; + protected ?string $extension = null; + protected ?string $thumb = null; + protected ?int $size; public function getId(): ?int { - return (int)$this->id; + return $this->id; } - /** - * @param int $id - */ - public function setId($id) - { - $this->id = (int)$id; - } - - /** - * @return int - */ - public function getAccountId() + public function getAccountId(): ?int { return $this->accountId; } - /** - * @param int $accountId - */ - public function setAccountId($accountId) - { - $this->accountId = $accountId; - } - public function getName(): ?string { return $this->name; } - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - /** - * @return int - */ - public function getType() + public function getType(): ?string { return $this->type; } - /** - * @param int $type - */ - public function setType($type) - { - $this->type = $type; - } - - /** - * @return string - */ - public function getContent() + public function getContent(): ?string { return $this->content; } - /** - * @param string $content - */ - public function setContent($content) - { - $this->content = $content; - } - - /** - * @return string - */ - public function getExtension() + public function getExtension(): ?string { return $this->extension; } - /** - * @param string $extension - */ - public function setExtension($extension) - { - $this->extension = $extension; - } - - /** - * @return string - */ - public function getThumb() + public function getThumb(): ?string { return $this->thumb; } /** - * @param string $thumb + * @param string $thumb */ - public function setThumb($thumb) + public function setThumb(string $thumb): void { $this->thumb = $thumb; } - /** - * @return int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * @param int $size - */ - public function setSize($size) + public function getRoundSize(): float { - $this->size = $size; - } + if (null === $this->size) { + return 0.0; + } - /** - * @return float - */ - public function getRoundSize() - { return round($this->size / 1000, 2); } } diff --git a/lib/SP/DataModel/FileExtData.php b/lib/SP/DataModel/FileExtData.php index 58960297..f0022815 100644 --- a/lib/SP/DataModel/FileExtData.php +++ b/lib/SP/DataModel/FileExtData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -31,44 +31,16 @@ namespace SP\DataModel; */ class FileExtData extends FileData { - /** - * @var string - */ - public $clientName = ''; - /** - * @var string - */ - public $accountName = ''; + protected ?string $clientName = null; + protected ?string $accountName = null; - /** - * @return string - */ - public function getClientName() + public function getClientName(): ?string { return $this->clientName; } - /** - * @param string $clientName - */ - public function setClientName($clientName) - { - $this->clientName = $clientName; - } - - /** - * @return string - */ - public function getAccountName() + public function getAccountName(): ?string { return $this->accountName; } - - /** - * @param string $accountName - */ - public function setAccountName($accountName) - { - $this->accountName = $accountName; - } -} \ No newline at end of file +} diff --git a/lib/SP/DataModel/ItemSearchData.php b/lib/SP/DataModel/ItemSearchData.php index 4e941ef5..fb448d8c 100644 --- a/lib/SP/DataModel/ItemSearchData.php +++ b/lib/SP/DataModel/ItemSearchData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -26,7 +26,6 @@ namespace SP\DataModel; use SP\Util\Filter; - /** * Class ItemSearchData * @@ -34,55 +33,33 @@ use SP\Util\Filter; */ class ItemSearchData { - private const ORDER_ASC = 'ASC'; - private const ORDER_DESC = 'DESC'; - - public ?string $seachString = null; - public int $limitStart = 0; - public int $limitCount = 0; - public string $order = self::ORDER_ASC; + /** + * @param string|null $seachString + * @param int|null $limitStart + * @param int|null $limitCount + */ + public function __construct( + private ?string $seachString = null, + private ?int $limitStart = 0, + private ?int $limitCount = 0, + ) { + if (!empty($seachString)) { + $this->seachString = Filter::safeSearchString($seachString); + } + } public function getSeachString(): ?string { return $this->seachString; } - public function setSeachString(?string $seachString): void - { - if ($seachString) { - $this->seachString = Filter::safeSearchString($seachString); - } else { - $this->seachString = null; - } - } - public function getLimitStart(): int { return $this->limitStart; } - public function setLimitStart(int $limitStart): void - { - $this->limitStart = $limitStart; - } - public function getLimitCount(): int { return $this->limitCount; } - - public function setLimitCount(int $limitCount): void - { - $this->limitCount = $limitCount; - } - - public function getOrder(): string - { - return $this->order; - } - - public function setOrder(string $order): void - { - $this->order = $order; - } -} \ No newline at end of file +} diff --git a/lib/SP/Domain/Account/Ports/AccountFileRepositoryInterface.php b/lib/SP/Domain/Account/Ports/AccountFileRepositoryInterface.php index 40805409..193a0555 100644 --- a/lib/SP/Domain/Account/Ports/AccountFileRepositoryInterface.php +++ b/lib/SP/Domain/Account/Ports/AccountFileRepositoryInterface.php @@ -49,15 +49,6 @@ interface AccountFileRepositoryInterface extends RepositoryInterface */ public function create(FileData $fileData): int; - /** - * @param int $id - * - * @return QueryResult - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function getInfoById(int $id): QueryResult; - /** * Returns the item for given id * @@ -89,24 +80,6 @@ interface AccountFileRepositoryInterface extends RepositoryInterface */ public function getById(int $id): QueryResult; - /** - * Returns all the items - * - * @return QueryResult - */ - public function getAll(): QueryResult; - - /** - * Returns all the items for given ids - * - * @param array $ids - * - * @return QueryResult - * @throws ConstraintException - * @throws QueryException - */ - public function getByIdBatch(array $ids): QueryResult; - /** * Deletes all the items for given ids * diff --git a/lib/SP/Domain/Account/Ports/AccountFileServiceInterface.php b/lib/SP/Domain/Account/Ports/AccountFileServiceInterface.php index a6920471..c75877cb 100644 --- a/lib/SP/Domain/Account/Ports/AccountFileServiceInterface.php +++ b/lib/SP/Domain/Account/Ports/AccountFileServiceInterface.php @@ -48,40 +48,16 @@ interface AccountFileServiceInterface */ public function create(FileData $itemData): int; - /** - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function getInfoById(int $id): ?FileExtData; - /** * Returns the item for given id * - * @return mixed|null + * @param int $id + * + * @return FileExtData|null * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ - public function getById(int $id): mixed; - - /** - * Returns all the items - * - * @return FileExtData[] - * @throws ConstraintException - * @throws QueryException - */ - public function getAll(): array; - - /** - * Returns all the items for given ids - * - * @param int[] $ids - * - * @return FileExtData[] - * @throws ConstraintException - * @throws QueryException - */ - public function getByIdBatch(array $ids): array; + public function getById(int $id): ?FileExtData; /** * Deletes all the items for given ids diff --git a/lib/SP/Domain/Account/Services/AccountFileService.php b/lib/SP/Domain/Account/Services/AccountFileService.php index b7c39f5a..8bfb8cec 100644 --- a/lib/SP/Domain/Account/Services/AccountFileService.php +++ b/lib/SP/Domain/Account/Services/AccountFileService.php @@ -38,7 +38,7 @@ use SP\Domain\Common\Services\ServiceException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; use SP\Util\FileUtil; -use SP\Util\ImageUtil; +use SP\Util\ImageUtilInterface; use function SP\__u; /** @@ -49,12 +49,12 @@ use function SP\__u; final class AccountFileService extends Service implements AccountFileServiceInterface { private AccountFileRepositoryInterface $accountFileRepository; - private ImageUtil $imageUtil; + private ImageUtilInterface $imageUtil; public function __construct( Application $application, AccountFileRepositoryInterface $accountFileRepository, - ImageUtil $imageUtil + ImageUtilInterface $imageUtil ) { $this->accountFileRepository = $accountFileRepository; $this->imageUtil = $imageUtil; @@ -84,50 +84,17 @@ final class AccountFileService extends Service implements AccountFileServiceInte } /** - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function getInfoById(int $id): ?FileExtData - { - return $this->accountFileRepository->getInfoById($id)->getData(); - } - - /** - * Returns the item for given id + * Returns the file with its content * * @param int $id * - * @return mixed + * @return \SP\DataModel\FileExtData|null */ - public function getById(int $id): mixed + public function getById(int $id): ?FileExtData { return $this->accountFileRepository->getById($id)->getData(); } - /** - * Returns all the items - * - * @return FileExtData[] - */ - public function getAll(): array - { - return $this->accountFileRepository->getAll()->getDataAsArray(); - } - - /** - * Returns all the items for given ids - * - * @param int[] $ids - * - * @return FileExtData[] - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function getByIdBatch(array $ids): array - { - return $this->accountFileRepository->getByIdBatch($ids)->getDataAsArray(); - } - /** * Deletes all the items for given ids * diff --git a/lib/SP/Infrastructure/Account/Repositories/AccountFileRepository.php b/lib/SP/Infrastructure/Account/Repositories/AccountFileRepository.php index 779ba7dd..329ada9a 100644 --- a/lib/SP/Infrastructure/Account/Repositories/AccountFileRepository.php +++ b/lib/SP/Infrastructure/Account/Repositories/AccountFileRepository.php @@ -72,34 +72,6 @@ final class AccountFileRepository extends Repository implements AccountFileRepos return $this->db->doQuery($queryData)->getLastId(); } - /** - * @param int $id - * - * @return QueryResult - */ - public function getInfoById(int $id): QueryResult - { - $query = $this->queryFactory - ->newSelect() - ->cols([ - 'AccountFile.name', - 'AccountFile.size', - 'AccountFile.type', - 'AccountFile.accountId', - 'AccountFile.extension', - 'Account.name AS accountName', - 'Client.name AS clientName', - ]) - ->from('AccountFile') - ->join('INNER', 'Account', 'Account.id = AccountFile.accountId') - ->join('INNER', 'Client', 'Client.id = Account.clientId') - ->where('AccountFile.id = :id') - ->bindValues(['id' => $id]) - ->limit(1); - - return $this->db->doSelect(QueryData::build($query)); - } - /** * Returns the item for given id * @@ -163,72 +135,6 @@ final class AccountFileRepository extends Repository implements AccountFileRepos return $this->db->doSelect(QueryData::build($query)); } - /** - * Returns all the items - * - * @return QueryResult - */ - public function getAll(): QueryResult - { - $query = $this->queryFactory - ->newSelect() - ->cols([ - 'AccountFile.id', - 'AccountFile.name', - 'AccountFile.size', - 'AccountFile.type', - 'AccountFile.accountId', - 'AccountFile.extension', - 'AccountFile.content', - 'AccountFile.thumb', - 'Account.name AS accountName', - 'Client.name AS clientName', - ]) - ->from('AccountFile') - ->join('INNER', 'Account', 'Account.id = AccountFile.accountId') - ->join('INNER', 'Client', 'Client.id = Account.clientId') - ->orderBy(['AccountFile.name ASC']); - - return $this->db->doSelect(QueryData::build($query)); - } - - /** - * Returns all the items for given ids - * - * @param array $ids - * - * @return QueryResult - * @throws ConstraintException - * @throws QueryException - */ - public function getByIdBatch(array $ids): QueryResult - { - if (count($ids) === 0) { - return new QueryResult(); - } - - $query = $this->queryFactory - ->newSelect() - ->cols([ - 'AccountFile.id', - 'AccountFile.name', - 'AccountFile.size', - 'AccountFile.type', - 'AccountFile.accountId', - 'AccountFile.extension', - 'AccountFile.content', - 'AccountFile.thumb', - 'Account.name AS accountName', - 'Client.name AS clientName', - ]) - ->from('AccountFile') - ->join('INNER', 'Account', 'Account.id = AccountFile.accountId') - ->join('INNER', 'Client', 'Client.id = Account.clientId') - ->where(sprintf('AccountFile.id IN (%s)', $this->buildParamsFromArray($ids)), ...$ids); - - return $this->db->doQuery(QueryData::build($query)); - } - /** * Deletes an item * diff --git a/lib/SP/Mvc/Controller/ItemTrait.php b/lib/SP/Mvc/Controller/ItemTrait.php index 82ca9eb0..0a96b16b 100644 --- a/lib/SP/Mvc/Controller/ItemTrait.php +++ b/lib/SP/Mvc/Controller/ItemTrait.php @@ -208,12 +208,11 @@ trait ItemTrait */ protected function getSearchData(int $limitCount, RequestInterface $request): ItemSearchData { - $itemSearchData = new ItemSearchData(); - $itemSearchData->setSeachString($request->analyzeString('search')); - $itemSearchData->setLimitStart($request->analyzeInt('start', 0)); - $itemSearchData->setLimitCount($request->analyzeInt('count', $limitCount)); - - return $itemSearchData; + return new ItemSearchData( + $request->analyzeString('search'), + $request->analyzeInt('start', 0), + $request->analyzeInt('count', $limitCount) + ); } protected function getItemsIdFromRequest(RequestInterface $request): ?array diff --git a/lib/SP/Util/ImageUtil.php b/lib/SP/Util/ImageUtil.php index 8fc659d4..65115da2 100644 --- a/lib/SP/Util/ImageUtil.php +++ b/lib/SP/Util/ImageUtil.php @@ -36,7 +36,7 @@ defined('APP_ROOT') || die(); * * @package SP */ -final class ImageUtil +final class ImageUtil implements ImageUtilInterface { /** * @throws CheckException @@ -98,9 +98,11 @@ final class ImageUtil /** * Convertir un texto a imagen * + * @param string $text + * * @return bool|string */ - public function convertText(string $text) + public function convertText(string $text): bool|string { $width = strlen($text) * 10; @@ -135,4 +137,4 @@ final class ImageUtil return base64_encode($image); } -} \ No newline at end of file +} diff --git a/lib/SP/Util/ImageUtilInterface.php b/lib/SP/Util/ImageUtilInterface.php new file mode 100644 index 00000000..4603141e --- /dev/null +++ b/lib/SP/Util/ImageUtilInterface.php @@ -0,0 +1,51 @@ +. + */ + +namespace SP\Util; + +use SP\Core\Exceptions\InvalidImageException; + +/** + * Class ImageUtil para la manipulación de imágenes + * + * @package SP + */ +interface ImageUtilInterface +{ + /** + * Crear miniatura de una imagen + * + * @throws InvalidImageException + */ + public function createThumbnail(string $image): string; + + /** + * Convertir un texto a imagen + * + * @param string $text + * + * @return bool|string + */ + public function convertText(string $text): bool|string; +} diff --git a/tests/SP/Domain/Account/Services/AccountFileServiceTest.php b/tests/SP/Domain/Account/Services/AccountFileServiceTest.php new file mode 100644 index 00000000..dd59874d --- /dev/null +++ b/tests/SP/Domain/Account/Services/AccountFileServiceTest.php @@ -0,0 +1,214 @@ +. + */ + +namespace SP\Tests\Domain\Account\Services; + +use PHPUnit\Framework\MockObject\MockObject; +use SP\DataModel\FileData; +use SP\DataModel\FileExtData; +use SP\Domain\Account\Ports\AccountFileRepositoryInterface; +use SP\Domain\Account\Services\AccountFileService; +use SP\Domain\Common\Services\ServiceException; +use SP\Infrastructure\Common\Repositories\NoSuchItemException; +use SP\Infrastructure\Database\QueryResult; +use SP\Tests\Generators\FileDataGenerator; +use SP\Tests\Generators\ItemSearchDataGenerator; +use SP\Tests\UnitaryTestCase; +use SP\Util\ImageUtilInterface; + +/** + * Class AccountFileServiceTest + */ +class AccountFileServiceTest extends UnitaryTestCase +{ + + private MockObject|AccountFileRepositoryInterface $accountFileRepository; + private ImageUtilInterface|MockObject $imageUtil; + private AccountFileService $accountFileService; + + /** + * @throws \SP\Core\Exceptions\InvalidImageException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\ConstraintException + */ + public function testCreate(): void + { + $fileData = FileData::buildFromSimpleModel(FileDataGenerator::factory()->buildFileData()); + + $this->accountFileRepository + ->expects(self::once()) + ->method('create') + ->with($fileData); + + $this->accountFileService->create($fileData); + } + + public function testGetById(): void + { + $fileData = FileExtData::buildFromSimpleModel(FileDataGenerator::factory()->buildFileExtData()); + + $queryResult = new QueryResult([$fileData]); + + $this->accountFileRepository + ->expects(self::once()) + ->method('getById') + ->with($fileData->getId()) + ->willReturn($queryResult); + + $out = $this->accountFileService->getById($fileData->getId()); + + $this->assertEquals($fileData, $out); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Domain\Common\Services\ServiceException + */ + public function testDeleteByIdBatch(): void + { + $ids = array_map(static fn() => self::$faker->randomNumber(), range(0, 9)); + + $this->accountFileRepository + ->expects(self::once()) + ->method('deleteByIdBatch') + ->with($ids) + ->willReturn(count($ids)); + + $out = $this->accountFileService->deleteByIdBatch($ids); + + $this->assertEquals(count($ids), $out); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Domain\Common\Services\ServiceException + */ + public function testDeleteByIdBatchWithMissingUpdates(): void + { + $ids = array_map(static fn() => self::$faker->randomNumber(), range(0, 9)); + + $this->accountFileRepository + ->expects(self::once()) + ->method('deleteByIdBatch') + ->with($ids) + ->willReturn(5); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while deleting the files'); + + $this->accountFileService->deleteByIdBatch($ids); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDelete(): void + { + $id = self::$faker->randomNumber(); + + $this->accountFileRepository + ->expects(self::once()) + ->method('delete') + ->with($id) + ->willReturn(true); + + $this->accountFileService->delete($id); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDeleteWithMissingFile(): void + { + $id = self::$faker->randomNumber(); + + $this->accountFileRepository + ->expects(self::once()) + ->method('delete') + ->with($id) + ->willReturn(false); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('File not found'); + + $this->accountFileService->delete($id); + } + + public function testSearch(): void + { + $files = array_map( + static fn() => FileExtData::buildFromSimpleModel(FileDataGenerator::factory()->buildFileExtData()), + range(0, 4) + ); + $itemSearchData = ItemSearchDataGenerator::factory()->buildItemSearchData(); + + $this->accountFileRepository + ->expects(self::once()) + ->method('search') + ->with($itemSearchData) + ->willReturn(new QueryResult($files)); + + $out = $this->accountFileService->search($itemSearchData); + + $this->assertEquals($files, $out->getDataAsArray()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetByAccountId(): void + { + $fileData = FileData::buildFromSimpleModel(FileDataGenerator::factory()->buildFileData()); + + $queryResult = new QueryResult([$fileData]); + + $this->accountFileRepository + ->expects(self::once()) + ->method('getByAccountId') + ->with($fileData->getId()) + ->willReturn($queryResult); + + $out = $this->accountFileService->getByAccountId($fileData->getId()); + + $this->assertEquals([$fileData], $out); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->accountFileRepository = $this->createMock(AccountFileRepositoryInterface::class); + $this->imageUtil = $this->createMock(ImageUtilInterface::class); + + $this->accountFileService = + new AccountFileService($this->application, $this->accountFileRepository, $this->imageUtil); + } +} diff --git a/tests/SP/Generators/FileDataGenerator.php b/tests/SP/Generators/FileDataGenerator.php new file mode 100644 index 00000000..5a8337d4 --- /dev/null +++ b/tests/SP/Generators/FileDataGenerator.php @@ -0,0 +1,57 @@ +. + */ + +namespace SP\Tests\Generators; + +use SP\Domain\Common\Adapters\SimpleModel; + +/** + * Class FileDataGenerator + */ +final class FileDataGenerator extends DataGenerator +{ + public function buildFileExtData(): SimpleModel + { + return new SimpleModel( + array_merge( + $this->buildFileData()->toArray(), + ['clientName' => $this->faker->name, 'accountName' => $this->faker->name] + ) + ); + } + + public function buildFileData(): SimpleModel + { + return new SimpleModel([ + 'id' => $this->faker->randomNumber(), + 'accountId' => $this->faker->randomNumber(), + 'name' => $this->faker->colorName, + 'type' => $this->faker->mimeType, + 'content' => $this->faker->image(null, 32, 32), + 'extension' => $this->faker->fileExtension, + 'thumb' => $this->faker->image(null, 32, 32), + 'size' => $this->faker->randomNumber(), + ]); + } +} diff --git a/tests/SP/Generators/ItemSearchDataGenerator.php b/tests/SP/Generators/ItemSearchDataGenerator.php new file mode 100644 index 00000000..cdeeea8c --- /dev/null +++ b/tests/SP/Generators/ItemSearchDataGenerator.php @@ -0,0 +1,42 @@ +. + */ + +namespace SP\Tests\Generators; + +use SP\DataModel\ItemSearchData; + +/** + * Class ItemSearchDataGenerator + */ +final class ItemSearchDataGenerator extends DataGenerator +{ + public function buildItemSearchData(): ItemSearchData + { + return new ItemSearchData( + $this->faker->name, + $this->faker->randomNumber(), + $this->faker->randomNumber() + ); + } +}