Skip to content

Commit

Permalink
Reuse generateUniqueId from ShopAdapterInterface
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Jan 4, 2024
1 parent 7068cf1 commit e1e7fc2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
16 changes: 4 additions & 12 deletions src/Service/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

use Doctrine\DBAL\Connection;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsObject;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSize;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactoredInterface;
use OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface;
use OxidEsales\MediaLibrary\Media\DataType\Media as MediaDataType;
Expand All @@ -37,7 +36,8 @@ public function __construct(
protected NamingServiceInterface $namingService,
protected MediaRepositoryInterface $mediaRepository,
private FileSystemServiceInterface $fileSystemService,
protected FolderServiceInterface $folderService
protected FolderServiceInterface $folderService,
protected ShopAdapterInterface $shopAdapter,
) {
$this->connection = $connectionProvider->get();
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public function uploadMedia($sSourcePath, $sDestPath, $sFileSize, $sFileType, $b
'thumbnail' => $sThumbName,
];

$sId = $this->generateUId();
$sId = $this->shopAdapter->generateUniqueId();
$sThumbName = $aFile['thumbnail'];
$sFileName = $aFile['filename'];

Expand Down Expand Up @@ -314,14 +314,6 @@ public function delete($aIds)
}
}

/**
* @return string
*/
protected function generateUId(): string
{
return $this->utilsObject->generateUId();
}

/**
* @param $sSourcePath
* @param array|string $sDestPath
Expand Down
8 changes: 7 additions & 1 deletion tests/Integration/Service/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsObject;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResource;
use OxidEsales\MediaLibrary\Media\Repository\MediaRepositoryInterface;
use OxidEsales\MediaLibrary\Service\FileSystemServiceInterface;
Expand Down Expand Up @@ -224,6 +225,10 @@ protected function getSut(
$thumbnailGenerator,
$connectionProvider,
);

$shopAdapterStub = $this->createStub(ShopAdapterInterface::class);
$shopAdapterStub->method('generateUniqueId')->willReturn(uniqid());

return new MediaMock(
$moduleSettings ?: $this->containerFactory->get(ModuleSettings::class),
$shopConfig ?: Registry::getConfig(),
Expand All @@ -234,7 +239,8 @@ protected function getSut(
namingService: $namingService ?: $this->containerFactory->get(NamingServiceInterface::class),
mediaRepository: $this->containerFactory->get(MediaRepositoryInterface::class),
fileSystemService: $this->containerFactory->get(FileSystemServiceInterface::class),
folderService: $this->containerFactory->get(FolderServiceInterface::class)
folderService: $this->containerFactory->get(FolderServiceInterface::class),
shopAdapter: $shopAdapterStub
);
}

Expand Down
12 changes: 5 additions & 7 deletions tests/Unit/Service/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OxidEsales\Eshop\Core\UtilsObject;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResource;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface;
use OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorInterface;
Expand Down Expand Up @@ -256,15 +257,10 @@ public function testUploadMedia()
);

$sId = md5(self::FIXTURE_FILE);
$utilsObjectMock = $this->createPartialMock(UtilsObject::class, ['generateUId']);
$utilsObjectMock->expects($this->once())
->method('generateUId')
->willReturn($sId);

$sut = $this->getSut(
shopConfig: $shopConfigMock,
connectionProvider: $connectionProviderStub,
utilsObject: $utilsObjectMock,
namingService: $this->createPartialMock(NamingService::class, [])
);

Expand Down Expand Up @@ -312,7 +308,8 @@ public function getRenameDataProvider(): array
namingService: ContainerFactory::getInstance()->getContainer()->get(NamingServiceInterface::class),
mediaRepository: $this->createStub(MediaRepositoryInterface::class),
fileSystemService: $this->createStub(FileSystemServiceInterface::class),
folderService: $this->createStub(FolderServiceInterface::class)
folderService: $this->createStub(FolderServiceInterface::class),
shopAdapter: $this->createStub(ShopAdapterInterface::class)
);

$defaultThumbnailSize = $oMedia->imageResource->getDefaultThumbnailSize();
Expand Down Expand Up @@ -579,7 +576,8 @@ protected function getSut(
namingService: $namingService ?? $this->createStub(NamingServiceInterface::class),
mediaRepository: $mediaRepository ?? $this->createStub(MediaRepositoryInterface::class),
fileSystemService: $fileSystemService ?? $this->createPartialMock(FileSystemService::class, []),
folderService: $this->createStub(FolderServiceInterface::class)
folderService: $this->createStub(FolderServiceInterface::class),
shopAdapter: $this->createStub(ShopAdapterInterface::class)
);
}

Expand Down

0 comments on commit e1e7fc2

Please sign in to comment.