Skip to content

Commit

Permalink
magento#28239: resize command does not process hidden images
Browse files Browse the repository at this point in the history
- Fixed Unit Test
- Removed unnecessary dependency in the constructor
  • Loading branch information
BGorbach committed Jul 12, 2021
1 parent c35cd1f commit d16ee89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage;


/**
* Resizes product images according to theme view definitions.
Expand Down Expand Up @@ -55,11 +55,6 @@ class ImagesResizeCommand extends Command
*/
private $progressBarFactory;

/**
* @var ProductImage
*/
private $productImage;

/**
* @var bool
*/
Expand All @@ -70,22 +65,19 @@ class ImagesResizeCommand extends Command
* @param ImageResize $imageResize
* @param ImageResizeScheduler $imageResizeScheduler
* @param ProgressBarFactory $progressBarFactory
* @param ProductImage $productImage
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
State $appState,
ImageResize $imageResize,
ImageResizeScheduler $imageResizeScheduler,
ProgressBarFactory $progressBarFactory,
ProductImage $productImage
ProgressBarFactory $progressBarFactory
) {
parent::__construct();
$this->appState = $appState;
$this->imageResize = $imageResize;
$this->imageResizeScheduler = $imageResizeScheduler;
$this->progressBarFactory = $progressBarFactory;
$this->productImage = $productImage;
}

/**
Expand Down
63 changes: 35 additions & 28 deletions app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class ImageResizeTest extends TestCase
*/
private $storeManager;

/**
* @var string
*/
private $testImageHiddenfilepath;

/**
* @inheritDoc
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand All @@ -139,6 +144,8 @@ protected function setUp(): void
$this->testfilename = "image.jpg";
$this->testImageHiddenFilename = "image_hidden.jpg";
$this->testfilepath = "/image.jpg";
$this->testImageHiddenfilepath = "/image_hidden.jpg";


$this->appStateMock = $this->createMock(State::class);
$this->imageConfigMock = $this->createMock(MediaConfig::class);
Expand Down Expand Up @@ -167,7 +174,7 @@ protected function setUp(): void

$this->assetImageMock->expects($this->any())
->method('getPath')
->willReturn($this->testfilepath);
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
$this->assetImageFactoryMock->expects($this->any())
->method('create')
->willReturn($this->assetImageMock);
Expand All @@ -189,16 +196,16 @@ protected function setUp(): void

$this->imageConfigMock->expects($this->any())
->method('getMediaPath')
->with($this->testfilename)
->willReturn($this->testfilepath);
->withConsecutive([$this->testfilename], [$this->testImageHiddenFilename])
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
$this->mediaDirectoryMock->expects($this->any())
->method('getAbsolutePath')
->with($this->testfilepath)
->willReturn($this->testfilepath);
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
$this->mediaDirectoryMock->expects($this->any())
->method('getRelativePath')
->with($this->testfilepath)
->willReturn($this->testfilepath);
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath)
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);

$this->viewMock->expects($this->any())
->method('getMediaEntities')
Expand Down Expand Up @@ -255,7 +262,7 @@ public function testResizeFromThemesMediaStorageDatabase()
->willReturn(false);

$imageMock = $this->createMock(Image::class);
$this->imageFactoryMock->expects($this->once())
$this->imageFactoryMock->expects($this->any())
->method('create')
->willReturn($imageMock);

Expand All @@ -275,15 +282,15 @@ function () {

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturn(true);

$this->databaseMock->expects($this->once())
$this->databaseMock->expects($this->any())
->method('saveFileToFilesystem')
->with($this->testfilepath);
$this->databaseMock->expects($this->once())
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);
$this->databaseMock->expects($this->any())
->method('saveFile')
->with($this->testfilepath);
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);

$generator = $this->service->resizeFromThemes(['test-theme'], true);
while ($generator->valid()) {
Expand All @@ -304,7 +311,7 @@ public function testResizeFromThemesHiddenImagesMediaStorageDatabase()
->willReturn(false);

$imageMock = $this->createMock(Image::class);
$this->imageFactoryMock->expects($this->once())
$this->imageFactoryMock->expects($this->any())
->method('create')
->willReturn($imageMock);

Expand Down Expand Up @@ -338,15 +345,15 @@ function () {

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturn(true);

$this->databaseMock->expects($this->once())
$this->databaseMock->expects($this->any())
->method('saveFileToFilesystem')
->with($this->testfilepath);
$this->databaseMock->expects($this->once())
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);
$this->databaseMock->expects($this->any())
->method('saveFile')
->with($this->testfilepath);
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);

$this->assertEquals(2, $this->service->getCountProductImages());
$this->assertEquals(1, $this->service->getCountProductImages(true));
Expand All @@ -370,7 +377,7 @@ public function testResizeFromThemesUnsupportedImage()
->method('fileExists')
->willReturn(false);

$this->imageFactoryMock->expects($this->once())
$this->imageFactoryMock->expects($this->any())
->method('create')
->willThrowException(new \InvalidArgumentException('Unsupported image format.'));

Expand All @@ -390,7 +397,7 @@ function () {

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturn(true);

$generator = $this->service->resizeFromThemes(['test-theme'], true);
Expand All @@ -411,13 +418,13 @@ public function testResizeFromImageNameMediaStorageDatabase()
->willReturn(false);

$imageMock = $this->createMock(Image::class);
$this->imageFactoryMock->expects($this->once())
$this->imageFactoryMock->expects($this->any())
->method('create')
->willReturn($imageMock);

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturnOnConsecutiveCalls(
$this->returnValue(false),
$this->returnValue(true)
Expand All @@ -434,12 +441,12 @@ public function testResizeFromImageNameMediaStorageDatabase()
['0' => []]
);

$this->databaseMock->expects($this->once())
$this->databaseMock->expects($this->any())
->method('saveFileToFilesystem')
->with($this->testfilepath);
$this->databaseMock->expects($this->once())
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);
$this->databaseMock->expects($this->any())
->method('saveFile')
->with($this->testfilepath);
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);

$this->service->resizeFromImageName($this->testfilename);
}
Expand Down Expand Up @@ -482,7 +489,7 @@ public function testSkipResizingAlreadyResizedImageInDatabase()

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
->willReturnOnConsecutiveCalls(
$this->returnValue(false),
$this->returnValue(true)
Expand Down

0 comments on commit d16ee89

Please sign in to comment.