|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Model\Product\Image; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product\Image\ConvertImageMiscParamsToReadableFormat; |
| 11 | +use Magento\Catalog\Model\Product\Image\ParamsBuilder; |
| 12 | +use Magento\Catalog\Model\Product\Image\RemoveDeletedImagesFromCache; |
| 13 | +use Magento\Catalog\Model\Product\Media\Config; |
| 14 | +use Magento\Framework\Config\View; |
| 15 | +use Magento\Framework\Encryption\EncryptorInterface; |
| 16 | +use Magento\Framework\Filesystem; |
| 17 | +use Magento\Framework\Filesystem\Directory\Write; |
| 18 | +use Magento\Framework\View\ConfigInterface; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + */ |
| 25 | +class RemoveDeletedImagesFromCacheTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var RemoveDeletedImagesFromCache |
| 29 | + */ |
| 30 | + protected $model; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ConfigInterface|MockObject |
| 34 | + */ |
| 35 | + protected $presentationConfig; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var EncryptorInterface|MockObject |
| 39 | + */ |
| 40 | + protected $encryptor; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Config|MockObject |
| 44 | + */ |
| 45 | + protected $mediaConfig; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Write|MockObject |
| 49 | + */ |
| 50 | + protected $mediaDirectory; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var ParamsBuilder|MockObject |
| 54 | + */ |
| 55 | + protected $imageParamsBuilder; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var ConvertImageMiscParamsToReadableFormat|MockObject |
| 59 | + */ |
| 60 | + protected $convertImageMiscParamsToReadableFormat; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var View|MockObject |
| 64 | + */ |
| 65 | + protected $viewMock; |
| 66 | + |
| 67 | + protected function setUp(): void |
| 68 | + { |
| 69 | + $this->presentationConfig = $this->getMockBuilder(ConfigInterface::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + |
| 73 | + $this->encryptor = $this->getMockBuilder(EncryptorInterface::class) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->getMock(); |
| 76 | + |
| 77 | + $this->mediaConfig = $this->getMockBuilder(Config::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $this->mediaDirectory = $this->createMock(Write::class); |
| 82 | + |
| 83 | + $filesystem = $this->createMock(Filesystem::class); |
| 84 | + $filesystem->expects($this->once()) |
| 85 | + ->method('getDirectoryWrite') |
| 86 | + ->willReturn($this->mediaDirectory); |
| 87 | + |
| 88 | + $this->imageParamsBuilder = $this->getMockBuilder(ParamsBuilder::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->convertImageMiscParamsToReadableFormat = $this |
| 93 | + ->getMockBuilder(ConvertImageMiscParamsToReadableFormat::class) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + |
| 97 | + $this->model = new RemoveDeletedImagesFromCache( |
| 98 | + $this->presentationConfig, |
| 99 | + $this->encryptor, |
| 100 | + $this->mediaConfig, |
| 101 | + $filesystem, |
| 102 | + $this->imageParamsBuilder, |
| 103 | + $this->convertImageMiscParamsToReadableFormat |
| 104 | + ); |
| 105 | + |
| 106 | + $this->viewMock = $this->getMockBuilder(View::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->getMock(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param array $data |
| 113 | + * @dataProvider createDataProvider |
| 114 | + */ |
| 115 | + public function testRemoveDeletedImagesFromCache($data) |
| 116 | + { |
| 117 | + $this->presentationConfig->expects($this->once()) |
| 118 | + ->method('getViewConfig') |
| 119 | + ->with(['area' => \Magento\Framework\App\Area::AREA_FRONTEND]) |
| 120 | + ->willReturn($this->viewMock); |
| 121 | + |
| 122 | + $this->viewMock->expects($this->once()) |
| 123 | + ->method('getMediaAttributes') |
| 124 | + ->willReturn($data['viewImageConfig']); |
| 125 | + |
| 126 | + $this->imageParamsBuilder->expects($this->once()) |
| 127 | + ->method('build') |
| 128 | + ->willReturn($data['imageParamsBuilder']); |
| 129 | + |
| 130 | + $this->convertImageMiscParamsToReadableFormat->expects($this->once()) |
| 131 | + ->method('convertImageMiscParamsToReadableFormat') |
| 132 | + ->willReturn($data['convertImageParamsToReadableFormat']); |
| 133 | + |
| 134 | + $this->encryptor->expects($this->once()) |
| 135 | + ->method('hash') |
| 136 | + ->willReturn('85b0304775df23c13f08dd2c1f9c4c28'); |
| 137 | + |
| 138 | + $this->mediaConfig->expects($this->once()) |
| 139 | + ->method('getBaseMediaPath') |
| 140 | + ->willReturn('catalog/product'); |
| 141 | + |
| 142 | + $this->mediaDirectory->expects($this->once()) |
| 143 | + ->method('delete') |
| 144 | + ->willReturn(true); |
| 145 | + |
| 146 | + $this->model->removeDeletedImagesFromCache(['i/m/image.jpg']); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @return array |
| 151 | + */ |
| 152 | + public function createDataProvider(): array |
| 153 | + { |
| 154 | + return [ |
| 155 | + $this->getTestDataWithAttributes() |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @return array |
| 161 | + */ |
| 162 | + private function getTestDataWithAttributes(): array |
| 163 | + { |
| 164 | + return [ |
| 165 | + 'data' => [ |
| 166 | + 'viewImageConfig' => [ |
| 167 | + 'width' => 100, |
| 168 | + 'height' => 50, // <=== |
| 169 | + 'constrain_only' => false, |
| 170 | + 'aspect_ratio' => false, |
| 171 | + 'frame' => true, // <=== |
| 172 | + 'transparency' => false, |
| 173 | + 'background' => '255,255,255', |
| 174 | + 'type' => 'thumbnail' //thumbnail,small_image,image,swatch_image,swatch_thumb |
| 175 | + ], |
| 176 | + 'imageParamsBuilder' => [ |
| 177 | + 'image_width' => 100, |
| 178 | + 'image_height' => 50, |
| 179 | + 'constrain_only' => false, |
| 180 | + 'keep_aspect_ratio' => false, |
| 181 | + 'keep_frame' => true, |
| 182 | + 'keep_transparency' => false, |
| 183 | + 'background' => '255,255,255', |
| 184 | + 'image_type' => 'thumbnail', //thumbnail,small_image,image,swatch_image,swatch_thumb |
| 185 | + 'quality' => 80, |
| 186 | + 'angle' => null |
| 187 | + ], |
| 188 | + 'convertImageParamsToReadableFormat' => [ |
| 189 | + 'image_height' => 'h: 50', |
| 190 | + 'image_width' => 'w: 100', |
| 191 | + 'quality' => 'q: 80', |
| 192 | + 'angle' => 'r: ', |
| 193 | + 'keep_aspect_ratio' => 'non proportional', |
| 194 | + 'keep_frame' => 'no frame', |
| 195 | + 'keep_transparency' => 'no transparency', |
| 196 | + 'constrain_only' => 'not constrainonly', |
| 197 | + 'background' => 'rgb 255,255,255' |
| 198 | + ] |
| 199 | + ] |
| 200 | + ]; |
| 201 | + } |
| 202 | +} |
0 commit comments