|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\MediaStorage\Test\Unit\Service; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\Product\Image\ParamsBuilder; |
| 9 | +use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory; |
| 10 | +use Magento\Catalog\Model\View\Asset\Image as AssetImage; |
| 11 | +use Magento\Framework\DataObject; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Image\Factory as ImageFactory; |
| 14 | +use Magento\Framework\Image; |
| 15 | +use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig; |
| 16 | +use Magento\Framework\App\State; |
| 17 | +use Magento\Framework\View\ConfigInterface as ViewConfig; |
| 18 | +use Magento\Framework\Config\View; |
| 19 | +use Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage; |
| 20 | +use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig; |
| 21 | +use Magento\Theme\Model\ResourceModel\Theme\Collection; |
| 22 | +use Magento\MediaStorage\Helper\File\Storage\Database; |
| 23 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 24 | + |
| 25 | +/** |
| 26 | + * Class ImageResizeTest |
| 27 | + * |
| 28 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class ImageResizeTest extends \PHPUnit\Framework\TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var \Magento\MediaStorage\Service\ImageResize |
| 35 | + */ |
| 36 | + protected $service; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var State|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $appStateMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var MediaConfig|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + protected $imageConfigMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ProductImage|\PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + protected $productImageMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var ImageFactory|\PHPUnit_Framework_MockObject_MockObject |
| 55 | + */ |
| 56 | + protected $imageFactoryMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Image|\PHPUnit_Framework_MockObject_MockObject |
| 60 | + */ |
| 61 | + protected $imageMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var ParamsBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 65 | + */ |
| 66 | + protected $paramsBuilderMock; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var ViewConfig|\PHPUnit_Framework_MockObject_MockObject |
| 70 | + */ |
| 71 | + protected $viewConfigMock; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var View|\PHPUnit_Framework_MockObject_MockObject |
| 75 | + */ |
| 76 | + protected $viewMock; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var AssetImage|\PHPUnit_Framework_MockObject_MockObject |
| 80 | + */ |
| 81 | + protected $assetImageMock; |
| 82 | + |
| 83 | + /** |
| 84 | + * @var AssetImageFactory|\PHPUnit_Framework_MockObject_MockObject |
| 85 | + */ |
| 86 | + protected $assetImageFactoryMock; |
| 87 | + |
| 88 | + /** |
| 89 | + * @var ThemeCustomizationConfig|\PHPUnit_Framework_MockObject_MockObject |
| 90 | + */ |
| 91 | + protected $themeCustomizationConfigMock; |
| 92 | + |
| 93 | + /** |
| 94 | + * @var Collection|\PHPUnit_Framework_MockObject_MockObject |
| 95 | + */ |
| 96 | + protected $themeCollectionMock; |
| 97 | + |
| 98 | + /** |
| 99 | + * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject |
| 100 | + */ |
| 101 | + protected $filesystemMock; |
| 102 | + |
| 103 | + /** |
| 104 | + * @var Database|\PHPUnit_Framework_MockObject_MockObject |
| 105 | + */ |
| 106 | + protected $databaseMock; |
| 107 | + |
| 108 | + /** |
| 109 | + * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject |
| 110 | + */ |
| 111 | + private $mediaDirectoryMock; |
| 112 | + |
| 113 | + /** |
| 114 | + * @var string |
| 115 | + */ |
| 116 | + private $testfilename; |
| 117 | + |
| 118 | + /** |
| 119 | + * @var string |
| 120 | + */ |
| 121 | + private $testfilepath; |
| 122 | + |
| 123 | + protected function setUp() |
| 124 | + { |
| 125 | + $this->testfilename = "image.jpg"; |
| 126 | + $this->testfilepath = "/image.jpg"; |
| 127 | + |
| 128 | + $this->appStateMock = $this->createMock(State::class); |
| 129 | + $this->imageConfigMock = $this->createMock(MediaConfig::class); |
| 130 | + $this->productImageMock = $this->createMock(ProductImage::class); |
| 131 | + $this->imageMock = $this->createMock(Image::class); |
| 132 | + $this->imageFactoryMock = $this->createMock(ImageFactory::class); |
| 133 | + $this->paramsBuilderMock = $this->createMock(ParamsBuilder::class); |
| 134 | + $this->viewMock = $this->createMock(View::class); |
| 135 | + $this->viewConfigMock = $this->createMock(ViewConfig::class); |
| 136 | + $this->assetImageMock = $this->createMock(AssetImage::class); |
| 137 | + $this->assetImageFactoryMock = $this->createMock(AssetImageFactory::class); |
| 138 | + $this->themeCustomizationConfigMock = $this->createMock(ThemeCustomizationConfig::class); |
| 139 | + $this->themeCollectionMock = $this->createMock(Collection::class); |
| 140 | + $this->filesystemMock = $this->createMock(Filesystem::class); |
| 141 | + $this->databaseMock = $this->createMock(Database::class); |
| 142 | + |
| 143 | + $this->mediaDirectoryMock = $this->getMockBuilder(Filesystem::class) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->setMethods(['getAbsolutePath','isFile','getRelativePath']) |
| 146 | + ->getMock(); |
| 147 | + |
| 148 | + $this->filesystemMock->expects($this->any()) |
| 149 | + ->method('getDirectoryWrite') |
| 150 | + ->with(DirectoryList::MEDIA) |
| 151 | + ->willReturn($this->mediaDirectoryMock); |
| 152 | + |
| 153 | + $this->imageFactoryMock->expects($this->any()) |
| 154 | + ->method('create') |
| 155 | + ->willReturn($this->imageMock); |
| 156 | + $this->assetImageMock->expects($this->any()) |
| 157 | + ->method('getPath') |
| 158 | + ->will($this->returnValue($this->testfilepath)); |
| 159 | + $this->assetImageFactoryMock->expects($this->any()) |
| 160 | + ->method('create') |
| 161 | + ->willReturn($this->assetImageMock); |
| 162 | + |
| 163 | + $this->paramsBuilderMock->expects($this->any()) |
| 164 | + ->method('build') |
| 165 | + ->willReturn( |
| 166 | + [ |
| 167 | + 'keep_aspect_ratio' => null, |
| 168 | + 'keep_frame' => null, |
| 169 | + 'keep_transparency' => null, |
| 170 | + 'constrain_only' => null, |
| 171 | + 'background' => null, |
| 172 | + 'quality' => null, |
| 173 | + 'image_width' => null, |
| 174 | + 'image_height' => null |
| 175 | + ] |
| 176 | + ); |
| 177 | + |
| 178 | + $this->imageConfigMock->expects($this->any()) |
| 179 | + ->method('getMediaPath') |
| 180 | + ->with($this->testfilename) |
| 181 | + ->willReturn($this->testfilepath); |
| 182 | + $this->mediaDirectoryMock->expects($this->any()) |
| 183 | + ->method('getAbsolutePath') |
| 184 | + ->with($this->testfilepath) |
| 185 | + ->willReturn($this->testfilepath); |
| 186 | + $this->mediaDirectoryMock->expects($this->any()) |
| 187 | + ->method('getRelativePath') |
| 188 | + ->with($this->testfilepath) |
| 189 | + ->willReturn($this->testfilepath); |
| 190 | + |
| 191 | + $this->viewMock->expects($this->any()) |
| 192 | + ->method('getMediaEntities') |
| 193 | + ->willReturn( |
| 194 | + ['product_small_image' => |
| 195 | + [ |
| 196 | + 'type' => 'small_image', |
| 197 | + 'width' => 75, |
| 198 | + 'height' => 75 |
| 199 | + ] |
| 200 | + ] |
| 201 | + ); |
| 202 | + $this->viewConfigMock->expects($this->any()) |
| 203 | + ->method('getViewConfig') |
| 204 | + ->willReturn($this->viewMock); |
| 205 | + |
| 206 | + $this->service = new \Magento\MediaStorage\Service\ImageResize( |
| 207 | + $this->appStateMock, |
| 208 | + $this->imageConfigMock, |
| 209 | + $this->productImageMock, |
| 210 | + $this->imageFactoryMock, |
| 211 | + $this->paramsBuilderMock, |
| 212 | + $this->viewConfigMock, |
| 213 | + $this->assetImageFactoryMock, |
| 214 | + $this->themeCustomizationConfigMock, |
| 215 | + $this->themeCollectionMock, |
| 216 | + $this->filesystemMock, |
| 217 | + $this->databaseMock |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + protected function tearDown() |
| 222 | + { |
| 223 | + unset($this->service); |
| 224 | + } |
| 225 | + |
| 226 | + public function testResizeFromThemesMediaStorageDatabase() |
| 227 | + { |
| 228 | + $this->databaseMock->expects($this->any()) |
| 229 | + ->method('checkDbUsage') |
| 230 | + ->will($this->returnValue(true)); |
| 231 | + |
| 232 | + $this->productImageMock->expects($this->any()) |
| 233 | + ->method('getCountUsedProductImages') |
| 234 | + ->willReturn(1); |
| 235 | + $this->productImageMock->expects($this->any()) |
| 236 | + ->method('getUsedProductImages') |
| 237 | + ->will( |
| 238 | + $this->returnCallback( |
| 239 | + function () { |
| 240 | + $data = [[ 'filepath' => $this->testfilename ]]; |
| 241 | + foreach ($data as $e) { |
| 242 | + yield $e; |
| 243 | + } |
| 244 | + } |
| 245 | + ) |
| 246 | + ); |
| 247 | + |
| 248 | + $this->mediaDirectoryMock->expects($this->any()) |
| 249 | + ->method('isFile') |
| 250 | + ->with($this->testfilepath) |
| 251 | + ->will($this->returnValue(false)); |
| 252 | + |
| 253 | + $this->databaseMock->expects($this->once()) |
| 254 | + ->method('saveFileToFilesystem') |
| 255 | + ->with($this->testfilepath); |
| 256 | + $this->databaseMock->expects($this->once()) |
| 257 | + ->method('saveFile') |
| 258 | + ->with($this->testfilepath); |
| 259 | + |
| 260 | + $generator = $this->service->resizeFromThemes(['test-theme']); |
| 261 | + while ($generator->valid()) { |
| 262 | + $generator->next(); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + public function testResizeFromImageNameMediaStorageDatabase() |
| 267 | + { |
| 268 | + $this->databaseMock->expects($this->any()) |
| 269 | + ->method('checkDbUsage') |
| 270 | + ->will($this->returnValue(true)); |
| 271 | + |
| 272 | + $this->mediaDirectoryMock->expects($this->any()) |
| 273 | + ->method('isFile') |
| 274 | + ->with($this->testfilepath) |
| 275 | + ->willReturnOnConsecutiveCalls( |
| 276 | + $this->returnValue(false), |
| 277 | + $this->returnValue(true) |
| 278 | + ); |
| 279 | + |
| 280 | + $this->themeCollectionMock->expects($this->any()) |
| 281 | + ->method('loadRegisteredThemes') |
| 282 | + ->willReturn( |
| 283 | + [ new DataObject(['id' => '0']) ] |
| 284 | + ); |
| 285 | + $this->themeCustomizationConfigMock->expects($this->any()) |
| 286 | + ->method('getStoresByThemes') |
| 287 | + ->willReturn( |
| 288 | + ['0' => []] |
| 289 | + ); |
| 290 | + |
| 291 | + $this->databaseMock->expects($this->once()) |
| 292 | + ->method('saveFileToFilesystem') |
| 293 | + ->with($this->testfilepath); |
| 294 | + $this->databaseMock->expects($this->once()) |
| 295 | + ->method('saveFile') |
| 296 | + ->with($this->testfilepath); |
| 297 | + |
| 298 | + $this->service->resizeFromImageName($this->testfilename); |
| 299 | + } |
| 300 | +} |
0 commit comments