Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductDataMapperTest.php
Go to the documentation of this file.
1 <?php
7 
19 use Magento\AdvancedSearch\Model\ResourceModel\Index as AdvancedSearchIndex;
21 
27 class ProductDataMapperTest extends \PHPUnit\Framework\TestCase
28 {
32  protected $model;
33 
37  private $builderMock;
38 
42  private $attributeContainerMock;
43 
47  private $attribute;
48 
52  private $resourceIndex;
53 
57  private $advancedSearchIndex;
58 
62  private $fieldMapperMock;
63 
67  private $dateTimeMock;
68 
72  private $localeDateMock;
73 
77  private $scopeConfigMock;
78 
82  private $storeManagerMock;
83 
87  private $storeInterface;
88 
92  protected function setUp()
93  {
94  $this->builderMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\Document\Builder::class)
95  ->setMethods(['addField', 'addFields', 'build'])
96  ->disableOriginalConstructor()
97  ->getMock();
98 
99  $this->attributeContainerMock = $this->getMockBuilder(
100  \Magento\Elasticsearch\Model\Adapter\Container\Attribute::class
101  )->setMethods(['getAttribute', 'setStoreId', 'getBackendType', 'getFrontendInput'])
102  ->disableOriginalConstructor()
103  ->getMock();
104 
105  $this->resourceIndex = $this->getMockBuilder(\Magento\Elasticsearch\Model\ResourceModel\Index::class)
106  ->disableOriginalConstructor()
107  ->setMethods([
108  'getPriceIndexData',
109  'getFullCategoryProductIndexData',
110  'getFullProductIndexData',
111  ])
112  ->getMock();
113 
114  $this->fieldMapperMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class)
115  ->setMethods(['getFieldName', 'getAllAttributesTypes'])
116  ->disableOriginalConstructor()
117  ->getMock();
118 
119  $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
120  ->setMethods(['isEmptyDate', 'setTimezone', 'format'])
121  ->disableOriginalConstructor()
122  ->getMock();
123 
124  $this->localeDateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127 
128  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
129  ->disableOriginalConstructor()
130  ->getMock();
131 
132  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
133  ->disableOriginalConstructor()
134  ->getMock();
135 
136  $this->advancedSearchIndex = $this->getMockBuilder(\Magento\AdvancedSearch\Model\ResourceModel\Index::class)
137  ->disableOriginalConstructor()
138  ->getMock();
139 
140  $this->attribute = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
141  ->disableOriginalConstructor()
142  ->getMock();
143 
144  $this->storeInterface = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
145  ->disableOriginalConstructor()
146  ->getMock();
147 
148  $objectManager = new ObjectManagerHelper($this);
149  $this->model = $objectManager->getObject(
150  \Magento\Elasticsearch\Model\Adapter\DataMapper\ProductDataMapper::class,
151  [
152  'builder' => $this->builderMock,
153  'attributeContainer' => $this->attributeContainerMock,
154  'resourceIndex' => $this->resourceIndex,
155  'fieldMapper' => $this->fieldMapperMock,
156  'dateTime' => $this->dateTimeMock,
157  'localeDate' => $this->localeDateMock,
158  'scopeConfig' => $this->scopeConfigMock,
159  'storeManager' => $this->storeManagerMock
160  ]
161  );
162  }
163 
177  public function testGetMap($productId, $productData, $storeId, $emptyDate, $type, $frontendInput)
178  {
179  $this->attributeContainerMock->expects($this->any())->method('getAttribute')->will(
180  $this->returnValue($this->attribute)
181  );
182  $this->resourceIndex->expects($this->any())
183  ->method('getPriceIndexData')
184  ->with([1, ], 1)
185  ->willReturn([
186  1 => [1]
187  ]);
188  $this->resourceIndex->expects($this->any())
189  ->method('getFullCategoryProductIndexData')
190  ->willReturn([
191  1 => [
192  0 => [
193  'id' => 2,
194  'name' => 'Default Category',
195  'position' => '1',
196  ],
197  1 => [
198  'id' => 3,
199  'name' => 'Gear',
200  'position' => '1',
201  ],
202  2 => [
203  'id' => 4,
204  'name' => 'Bags',
205  'position' => '1',
206  ],
207  ],
208  ]);
209  $this->storeManagerMock->expects($this->any())
210  ->method('getStore')
211  ->willReturn($this->storeInterface);
212  $this->storeInterface->expects($this->any())
213  ->method('getWebsiteId')
214  ->willReturn(1);
215  $this->attributeContainerMock->expects($this->any())->method('setStoreId')->will(
216  $this->returnValue($this->attributeContainerMock)
217  );
218  $this->attribute->expects($this->any())->method('getBackendType')->will(
219  $this->returnValue($type)
220  );
221  $this->attribute->expects($this->any())->method('getFrontendInput')->will(
222  $this->returnValue($frontendInput)
223  );
224  $this->dateTimeMock->expects($this->any())->method('isEmptyDate')->will(
225  $this->returnValue($emptyDate)
226  );
227  $this->scopeConfigMock->expects($this->any())->method('getValue')->will(
228  $this->returnValue('Europe/London')
229  );
230  $this->builderMock->expects($this->any())->method('addField')->will(
231  $this->returnValue([])
232  );
233  $this->builderMock->expects($this->any())->method('addFields')->will(
234  $this->returnValue([])
235  );
236  $this->builderMock->expects($this->any())->method('build')->will(
237  $this->returnValue([])
238  );
239 
240  $this->resourceIndex->expects($this->once())
241  ->method('getFullProductIndexData')
242  ->willReturn($productData);
243 
244  $this->assertInternalType(
245  'array',
246  $this->model->map($productId, $productData, $storeId)
247  );
248  }
249 
254  public static function mapProvider()
255  {
256  return [
257  [
258  '1',
259  ['price'=>'11','created_at'=>'00-00-00 00:00:00', 'color_value'=>'11'],
260  '1',
261  false,
262  'datetime',
263  'select',
264  ],
265  [
266  '1',
267  ['price'=>'11','created_at'=>'00-00-00 00:00:00', 'color_value'=>'11'],
268  '1',
269  false,
270  'time',
271  'multiselect',
272  ],
273  [
274  '1',
275  ['price'=>'11','created_at'=>null,'color_value'=>'11', ],
276  '1',
277  true,
278  'datetime',
279  'select',
280  ],
281  [
282  '1',
283  [
284  'tier_price'=>
285  [[
286  'price_id'=>'1',
287  'website_id'=>'1',
288  'all_groups'=>'1',
289  'cust_group'=>'1',
290  'price_qty'=>'1',
291  'website_price'=>'1',
292  'price'=>'1'
293  ]],
294  'created_at'=>'00-00-00 00:00:00'
295  ],
296  '1',
297  false,
298  'string',
299  'select',
300  ],
301  [
302  '1',
303  ['image'=>'11','created_at'=>'00-00-00 00:00:00'],
304  '1',
305  false,
306  'string',
307  'select',
308  ],
309  [
310  '1',
311  [
312  'image' => '1',
313  'small_image' => '1',
314  'thumbnail' => '1',
315  'swatch_image' => '1',
316  'media_gallery'=>
317  [
318  'images' =>
319  [[
320  'file'=>'1',
321  'media_type'=>'image',
322  'position'=>'1',
323  'disabled'=>'1',
324  'label'=>'1',
325  'title'=>'1',
326  'base_image'=>'1',
327  'small_image'=>'1',
328  'thumbnail'=>'1',
329  'swatch_image'=>'1'
330  ]]
331  ],
332  'created_at'=>'00-00-00 00:00:00'
333  ],
334  '1',
335  false,
336  'string',
337  'select',
338  ],
339  [
340  '1',
341  [
342  'image' => '1',
343  'small_image' => '1',
344  'thumbnail' => '1',
345  'swatch_image' => '1',
346  'media_gallery'=>
347  [
348  'images' =>
349  [[
350  'file'=>'1',
351  'media_type'=>'video',
352  'position'=>'1',
353  'disabled'=>'1',
354  'label'=>'1',
355  'title'=>'1',
356  'base_image'=>'1',
357  'small_image'=>'1',
358  'thumbnail'=>'1',
359  'swatch_image'=>'1',
360  'video_title'=>'1',
361  'video_url'=>'1',
362  'video_description'=>'1',
363  'video_metadata'=>'1',
364  'video_provider'=>'1'
365  ]]
366  ],
367  'created_at'=>'00-00-00 00:00:00'
368  ],
369  '1',
370  false,
371  'string',
372  'select',
373  ],
374  [
375  '1',
376  ['quantity_and_stock_status'=>'11','created_at'=>'00-00-00 00:00:00'],
377  '1',
378  false,
379  'string',
380  'select',
381  ],
382  [
383  '1',
384  ['quantity_and_stock_status'=>['is_in_stock' => '1', 'qty' => '12'],'created_at'=>'00-00-00 00:00:00'],
385  '1',
386  false,
387  'string',
388  'select',
389  ],
390  [
391  '1',
392  ['price'=>'11','created_at'=>'1995-12-31 23:59:59','options'=>['value1','value2']],
393  '1',
394  false,
395  'string',
396  'select',
397  ],
398  ];
399  }
400 }
$objectManager
Definition: bootstrap.php:17
testGetMap($productId, $productData, $storeId, $emptyDate, $type, $frontendInput)
$type
Definition: item.phtml:13
$productData