|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ConfigurableProduct\Test\Unit\Controller\Adminhtml\Product\Initialization\Helper\Plugin; |
| 7 | + |
| 8 | +use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper as ProductInitializationHelper; |
| 9 | +use Magento\Catalog\Model\Product; |
| 10 | +use Magento\Catalog\Model\Product\Media\Config as MediaConfig; |
| 11 | +use Magento\ConfigurableProduct\Controller\Adminhtml\Product\Initialization\Helper\Plugin\CleanConfigurationTmpImages; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Directory\Write; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 16 | +use Magento\MediaStorage\Helper\File\Storage\Database as FileStorage; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class CleanConfigurationTmpImagesTest |
| 20 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 21 | + * @package Magento\ConfigurableProduct\Test\Unit\Controller\Adminhtml\Product\Initialization\Helper\Plugin |
| 22 | + */ |
| 23 | +class CleanConfigurationTmpImagesTest extends \PHPUnit\Framework\TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var CleanConfigurationTmpImages |
| 27 | + */ |
| 28 | + private $cleanConfigurationTmpImages; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ObjectManagerHelper |
| 32 | + */ |
| 33 | + private $objectManagerHelper; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $requestMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var FileStorage|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $fileStorageDb; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var MediaConfig|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $mediaConfig; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject |
| 52 | + */ |
| 53 | + private $filesystem; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var Write|\PHPUnit_Framework_MockObject_MockObject |
| 57 | + */ |
| 58 | + private $writeFolder; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var ProductInitializationHelper|\PHPUnit_Framework_MockObject_MockObject |
| 62 | + */ |
| 63 | + private $subjectMock; |
| 64 | + |
| 65 | + protected function setUp() |
| 66 | + { |
| 67 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 68 | + ->getMockForAbstractClass(); |
| 69 | + $this->fileStorageDb = $this->getMockBuilder(FileStorage::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $this->mediaConfig = $this->getMockBuilder(MediaConfig::class) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->getMock(); |
| 75 | + $this->filesystem = $this->getMockBuilder(Filesystem::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $this->writeFolder = $this->getMockBuilder(Write::class) |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->getMock(); |
| 81 | + $this->subjectMock = $this->getMockBuilder(ProductInitializationHelper::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->getMock(); |
| 84 | + |
| 85 | + $this->filesystem->expects($this->once()) |
| 86 | + ->method('getDirectoryWrite') |
| 87 | + ->willReturn($this->writeFolder); |
| 88 | + |
| 89 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 90 | + $this->cleanConfigurationTmpImages = $this->objectManagerHelper->getObject( |
| 91 | + CleanConfigurationTmpImages::class, |
| 92 | + [ |
| 93 | + 'request' => $this->requestMock, |
| 94 | + 'fileStorageDb' => $this->fileStorageDb, |
| 95 | + 'mediaConfig' => $this->mediaConfig, |
| 96 | + 'filesystem' => $this->filesystem |
| 97 | + ] |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Prepare configurable matrix |
| 103 | + * |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + private function getConfigurableMatrix() |
| 107 | + { |
| 108 | + return [ |
| 109 | + [ |
| 110 | + 'newProduct' => true, |
| 111 | + 'id' => 'product1' |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'newProduct' => false, |
| 115 | + 'id' => 'product2', |
| 116 | + 'status' => 'simple2_status', |
| 117 | + 'sku' => 'simple2_sku', |
| 118 | + 'name' => 'simple2_name', |
| 119 | + 'price' => '3.33', |
| 120 | + 'configurable_attribute' => 'simple2_configurable_attribute', |
| 121 | + 'weight' => '5.55', |
| 122 | + 'media_gallery' => [ |
| 123 | + 'images' => [ |
| 124 | + ['file' => 'test'] |
| 125 | + ], |
| 126 | + ], |
| 127 | + 'swatch_image' => 'simple2_swatch_image', |
| 128 | + 'small_image' => 'simple2_small_image', |
| 129 | + 'thumbnail' => 'simple2_thumbnail', |
| 130 | + 'image' => 'simple2_image', |
| 131 | + 'was_changed' => true, |
| 132 | + ], |
| 133 | + [ |
| 134 | + 'newProduct' => false, |
| 135 | + 'id' => 'product3', |
| 136 | + 'qty' => '3', |
| 137 | + 'was_changed' => true, |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'newProduct' => false, |
| 141 | + 'id' => 'product4', |
| 142 | + 'status' => 'simple4_status', |
| 143 | + 'sku' => 'simple2_sku', |
| 144 | + 'name' => 'simple2_name', |
| 145 | + 'price' => '3.33', |
| 146 | + 'weight' => '5.55', |
| 147 | + ], |
| 148 | + ]; |
| 149 | + } |
| 150 | + |
| 151 | + public function testAfterInitialize() |
| 152 | + { |
| 153 | + $productMock = $this->getProductMock(); |
| 154 | + $configurableMatrix = $this->getConfigurableMatrix(); |
| 155 | + |
| 156 | + $this->requestMock->expects(static::any()) |
| 157 | + ->method('getParam') |
| 158 | + ->willReturnMap( |
| 159 | + [ |
| 160 | + ['store', 0, 0], |
| 161 | + ['configurable-matrix-serialized', "[]", json_encode($configurableMatrix)] |
| 162 | + ] |
| 163 | + ); |
| 164 | + |
| 165 | + $this->assertSame($productMock, $this->cleanConfigurationTmpImages->afterInitialize($this->subjectMock, $productMock)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get product mock |
| 170 | + * |
| 171 | + * @param array $expectedData |
| 172 | + * @param bool $hasDataChanges |
| 173 | + * @param bool $wasChanged |
| 174 | + * @return Product|\PHPUnit_Framework_MockObject_MockObject |
| 175 | + */ |
| 176 | + protected function getProductMock(array $expectedData = null, $hasDataChanges = false, $wasChanged = false) |
| 177 | + { |
| 178 | + $productMock = $this->getMockBuilder(Product::class) |
| 179 | + ->disableOriginalConstructor() |
| 180 | + ->getMock(); |
| 181 | + |
| 182 | + if ($wasChanged !== false) { |
| 183 | + if ($expectedData !== null) { |
| 184 | + $productMock->expects(static::once()) |
| 185 | + ->method('addData') |
| 186 | + ->with($expectedData) |
| 187 | + ->willReturnSelf(); |
| 188 | + } |
| 189 | + |
| 190 | + $productMock->expects(static::any()) |
| 191 | + ->method('hasDataChanges') |
| 192 | + ->willReturn($hasDataChanges); |
| 193 | + $productMock->expects($hasDataChanges ? static::once() : static::never()) |
| 194 | + ->method('save') |
| 195 | + ->willReturnSelf(); |
| 196 | + } |
| 197 | + return $productMock; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Test for no exceptions if configurable matrix is empty string. |
| 202 | + */ |
| 203 | + public function testAfterInitializeEmptyMatrix() |
| 204 | + { |
| 205 | + $productMock = $this->getProductMock(); |
| 206 | + |
| 207 | + $this->requestMock->expects(static::any()) |
| 208 | + ->method('getParam') |
| 209 | + ->willReturnMap( |
| 210 | + [ |
| 211 | + ['store', 0, 0], |
| 212 | + ['configurable-matrix-serialized', null, ''], |
| 213 | + ] |
| 214 | + ); |
| 215 | + |
| 216 | + $this->cleanConfigurationTmpImages->afterInitialize($this->subjectMock, $productMock); |
| 217 | + |
| 218 | + $this->assertEmpty($productMock->getData()); |
| 219 | + } |
| 220 | +} |
0 commit comments