Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductImageTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ProductImageTest extends \PHPUnit\Framework\TestCase
12 {
15 
18 
20  protected $eavConfigMock;
21 
23  protected $attributeMock;
24 
26  protected $requestMock;
27 
29  protected $productMock;
30 
32  protected $pluginModel;
33 
34  protected function setUp()
35  {
36  $this->swatchesHelperMock = $this->createPartialMock(
37  \Magento\Swatches\Helper\Data::class,
38  ['loadVariationByFallback', 'isSwatchAttribute', 'isProductHasSwatch']
39  );
40 
41  $this->attributeFactoryMock = $this->createPartialMock(
42  \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory::class,
43  ['create']
44  );
45 
46  $this->eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
47 
48  $this->attributeMock = $this->createPartialMock(
49  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class,
50  ['loadByCode', 'getId', 'getUsedInProductListing', 'getIsFilterable', 'getData']
51  );
52 
53  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParams']);
54  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
55 
56  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57 
58  $this->pluginModel = $objectManager->getObject(
59  \Magento\Swatches\Model\Plugin\ProductImage::class,
60  [
61  'swatchesHelperData' => $this->swatchesHelperMock,
62  'eavConfig' => $this->eavConfigMock,
63  'request' => $this->requestMock,
64  ]
65  );
66  }
67 
71  public function testBeforeGetImage($expected)
72  {
73  $this->productMock->expects($this->once())->method('getTypeId')->willReturn('configurable');
74 
75  $this->requestMock
76  ->expects($this->once())
77  ->method('getParams')
78  ->willReturn($expected['getParams']);
79 
80  $this->eavConfigMock
81  ->method('getEntityAttributes')
82  ->with('catalog_product')
83  ->willReturn(['color' => $this->attributeMock]);
84 
85  $this->canReplaceImageWithSwatch($expected);
86  $this->swatchesHelperMock
87  ->expects($this->exactly($expected['loadVariationByFallback_count']))
88  ->method('loadVariationByFallback')
89  ->willReturn($expected['product']);
90  $this->swatchesHelperMock
91  ->method('isProductHasSwatch')
92  ->with($this->productMock)
93  ->willReturn(false);
94 
95  $productImageMock = $this->createMock(\Magento\Catalog\Block\Product\AbstractProduct::class);
96 
97  $result = $this->pluginModel->beforeGetImage($productImageMock, $this->productMock, $expected['page_handle']);
98  $this->assertEquals([$this->productMock, $expected['page_handle'], []], $result);
99  }
100 
104  protected function getFilterArray($expected)
105  {
106  $this->eavConfigMock
107  ->method('getEntityAttributeCodes')
108  ->with('catalog_product')
109  ->willReturn($expected['attribute_codes_array']);
110 
111  $this->eavConfigMock
112  ->method('getAttribute')
113  ->with('catalog_product', $expected['attribute_code'])
114  ->willReturn($this->attributeMock);
115 
116  $this->attributeMock
117  ->expects($this->exactly($expected['getId_count']))
118  ->method('getId')
119  ->willReturn($expected['getId']);
120  }
121 
125  protected function canReplaceImageWithSwatch($expected)
126  {
127  $this->swatchesHelperMock
128  ->expects($this->once())
129  ->method('isSwatchAttribute')
130  ->with($this->attributeMock)
131  ->willReturn($expected['isSwatchAttribute']);
132 
133  $this->attributeMock
134  ->expects($this->exactly($expected['getUsedInProductListing_count']))
135  ->method('getUsedInProductListing')
136  ->willReturn($expected['getUsedInProductListing']);
137 
138  $this->attributeMock
139  ->expects($this->exactly($expected['getIsFilterable_count']))
140  ->method('getIsFilterable')
141  ->willReturn($expected['getIsFilterable']);
142 
143  if ($expected['update_product_preview_image__count'] == 1) {
144  $this->attributeMock
145  ->method('getData')
146  ->with('update_product_preview_image')
147  ->willReturn($expected['update_product_preview_image']);
148  }
149  }
150 
155  public function dataForTest()
156  {
157  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
158  $productMock->expects($this->any())->method('getImage')->willReturn(false);
159 
160  return [
161  [
162  [
163  'page_handle' => 'category_page_grid',
164  'getParams' => ['color' => 31],
165  'attribute_code' => 'color',
166  'getId_count' => 1,
167  'getId' => 332,
168  'isSwatchAttribute' => false,
169  'getUsedInProductListing' => true,
170  'getUsedInProductListing_count' => 1,
171  'getIsFilterable' => true,
172  'getIsFilterable_count' => 1,
173  'update_product_preview_image' =>true,
174  'update_product_preview_image__count' => 1,
175  'loadVariationByFallback_count' => 0,
176  'product' => $productMock,
177  ],
178  ],
179  [
180  [
181  'page_handle' => 'category_page_grid',
182  'getParams' => ['color' => 31],
183  'attribute_code' => 'color',
184  'getId_count' => 1,
185  'getId' => 332,
186  'isSwatchAttribute' => true,
187  'getUsedInProductListing' => true,
188  'getUsedInProductListing_count' => 1,
189  'getIsFilterable' => true,
190  'getIsFilterable_count' => 1,
191  'update_product_preview_image' =>true,
192  'update_product_preview_image__count' => 1,
193  'loadVariationByFallback_count' => 1,
194  'product' => $productMock,
195  ],
196  ],
197  [
198  [
199  'page_handle' => 'category_page_grid',
200  'getParams' => ['color' => 31],
201  'attribute_code' => 'color',
202  'getId_count' => 1,
203  'getId' => 332,
204  'isSwatchAttribute' => true,
205  'getUsedInProductListing' => true,
206  'getUsedInProductListing_count' => 1,
207  'getIsFilterable' => true,
208  'getIsFilterable_count' => 1,
209  'update_product_preview_image' =>false,
210  'update_product_preview_image__count' => 1,
211  'loadVariationByFallback_count' => 0,
212  'product' => $productMock,
213  ],
214  ],
215  ];
216  }
217 }
$objectManager
Definition: bootstrap.php:17