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 
17 
23 class ProductDataMapperTest extends \PHPUnit\Framework\TestCase
24 {
28  private $model;
29 
33  private $builderMock;
34 
38  private $attribute;
39 
43  private $fieldMapperMock;
44 
48  private $dateFieldTypeMock;
49 
53  private $additionalFieldsProvider;
54 
58  private $dataProvider;
59 
63  protected function setUp()
64  {
65  $this->builderMock = $this->createTestProxy(Builder::class);
66  $this->fieldMapperMock = $this->createMock(FieldMapperInterface::class);
67  $this->dataProvider = $this->createMock(DataProvider::class);
68  $this->attribute = $this->createMock(Attribute::class);
69  $this->additionalFieldsProvider = $this->createMock(AdditionalFieldsProviderInterface::class);
70  $this->dateFieldTypeMock = $this->createMock(Date::class);
71 
72  $objectManager = new ObjectManagerHelper($this);
73  $this->model = $objectManager->getObject(
74  ProductDataMapper::class,
75  [
76  'builder' => $this->builderMock,
77  'fieldMapper' => $this->fieldMapperMock,
78  'dateFieldType' => $this->dateFieldTypeMock,
79  'dataProvider' => $this->dataProvider,
80  'additionalFieldsProvider' => $this->additionalFieldsProvider,
81  ]
82  );
83  }
84 
89  {
90  $storeId = 1;
91  $productId = 42;
92  $additionalFields = ['some data'];
93  $this->builderMock->expects($this->once())
94  ->method('addField')
95  ->with('store_id', $storeId);
96 
97  $this->builderMock->expects($this->any())
98  ->method('addFields')
99  ->withConsecutive([$additionalFields])
100  ->will($this->returnSelf());
101  $this->builderMock->expects($this->any())
102  ->method('build')
103  ->will($this->returnValue([]));
104  $this->additionalFieldsProvider->expects($this->once())
105  ->method('getFields')
106  ->with([$productId], $storeId)
107  ->willReturn([$productId => $additionalFields]);
108 
109  $documents = $this->model->map([$productId => []], $storeId, []);
110  $this->assertEquals([$productId], array_keys($documents));
111  }
112 
116  public function testGetMapEmptyData()
117  {
118  $storeId = 1;
119 
120  $this->builderMock->expects($this->never())->method('addField');
121  $this->builderMock->expects($this->never())->method('build');
122  $this->additionalFieldsProvider->expects($this->once())
123  ->method('getFields')
124  ->with([], $storeId)
125  ->willReturn([]);
126 
127  $documents = $this->model->map([], $storeId, []);
128  $this->assertEquals([], $documents);
129  }
130 
138  public function testGetMap(int $productId, array $attributeData, $attributeValue, array $returnAttributeData)
139  {
140  $storeId = 1;
141  $attributeId = 5;
142  $context = [];
143 
144  $this->dataProvider->method('getSearchableAttribute')
145  ->with($attributeId)
146  ->willReturn($this->getAttribute($attributeData));
147  $this->fieldMapperMock->method('getFieldName')
148  ->willReturnArgument(0);
149  $this->dateFieldTypeMock->method('formatDate')
150  ->willReturnArgument(1);
151  $this->additionalFieldsProvider->expects($this->once())
152  ->method('getFields')
153  ->willReturn([]);
154 
155  $documentData = [
156  $productId => [$attributeId => $attributeValue],
157  ];
158  $documents = $this->model->map($documentData, $storeId, $context);
159  $returnAttributeData['store_id'] = $storeId;
160  $this->assertEquals($returnAttributeData, $documents[$productId]);
161  }
162 
166  public function testGetMapWithOptions()
167  {
168  $storeId = 1;
169  $productId = 10;
170  $context = [];
171  $attributeValue = ['o1', 'o2'];
172  $returnAttributeData = [
173  'store_id' => $storeId,
174  'options' => $attributeValue,
175  ];
176 
177  $this->dataProvider->expects($this->never())
178  ->method('getSearchableAttribute');
179  $this->fieldMapperMock->method('getFieldName')
180  ->willReturnArgument(0);
181  $this->additionalFieldsProvider->expects($this->once())
182  ->method('getFields')
183  ->willReturn([]);
184 
185  $documentData = [
186  $productId => ['options' => $attributeValue],
187  ];
188  $documents = $this->model->map($documentData, $storeId, $context);
189  $this->assertEquals($returnAttributeData, $documents[$productId]);
190  }
191 
198  private function getAttribute(array $attributeData): \PHPUnit_Framework_MockObject_MockObject
199  {
200  $attributeMock = $this->createMock(Attribute::class);
201  $attributeMock->method('getAttributeCode')->willReturn($attributeData['code']);
202  $attributeMock->method('getBackendType')->willReturn($attributeData['backendType']);
203  $attributeMock->method('getFrontendInput')->willReturn($attributeData['frontendInput']);
204  $attributeMock->method('getIsSearchable')->willReturn($attributeData['is_searchable']);
205  $options = [];
206  foreach ($attributeData['options'] as $option) {
207  $optionMock = $this->createMock(AttributeOptionInterface::class);
208  $optionMock->method('getValue')->willReturn($option['value']);
209  $optionMock->method('getLabel')->willReturn($option['label']);
210  $options[] = $optionMock;
211  }
212  $attributeMock->method('getOptions')->willReturn($options);
213 
214  return $attributeMock;
215  }
216 
221  public static function mapProvider(): array
222  {
223  return [
224  'text' => [
225  10,
226  [
227  'code' => 'description',
228  'backendType' => 'text',
229  'frontendInput' => 'text',
230  'is_searchable' => false,
231  'options' => [],
232  ],
233  'some text',
234  ['description' => 'some text'],
235  ],
236  'datetime' => [
237  10,
238  [
239  'code' => 'created_at',
240  'backendType' => 'datetime',
241  'frontendInput' => 'date',
242  'is_searchable' => false,
243  'options' => [],
244  ],
245  '00-00-00 00:00:00',
246  ['created_at' => '00-00-00 00:00:00'],
247 
248  ],
249  'array single value' => [
250  10,
251  [
252  'code' => 'attribute_array',
253  'backendType' => 'text',
254  'frontendInput' => 'text',
255  'is_searchable' => false,
256  'options' => [],
257  ],
258  [10 => 'one'],
259  ['attribute_array' => 'one'],
260  ],
261  'array multiple value' => [
262  10,
263  [
264  'code' => 'attribute_array',
265  'backendType' => 'text',
266  'frontendInput' => 'text',
267  'is_searchable' => false,
268  'options' => [],
269  ],
270  [10 => 'one', 11 => 'two', 12 => 'three'],
271  ['attribute_array' => ['one', 'two', 'three']],
272  ],
273  'array multiple decimal value' => [
274  10,
275  [
276  'code' => 'decimal_array',
277  'backendType' => 'decimal',
278  'frontendInput' => 'text',
279  'is_searchable' => false,
280  'options' => [],
281  ],
282  [10 => '0.1', 11 => '0.2', 12 => '0.3'],
283  ['decimal_array' => ['0.1', '0.2', '0.3']],
284  ],
285  'array excluded from merge' => [
286  10,
287  [
288  'code' => 'status',
289  'backendType' => 'int',
290  'frontendInput' => 'select',
291  'is_searchable' => false,
292  'options' => [
293  ['value' => '1', 'label' => 'Enabled'],
294  ['value' => '2', 'label' => 'Disabled'],
295  ],
296  ],
297  [10 => '1', 11 => '2'],
298  ['status' => '1'],
299  ],
300  'select without options' => [
301  10,
302  [
303  'code' => 'color',
304  'backendType' => 'text',
305  'frontendInput' => 'select',
306  'is_searchable' => false,
307  'options' => [],
308  ],
309  '44',
310  ['color' => '44'],
311  ],
312  'unsearchable select with options' => [
313  10,
314  [
315  'code' => 'color',
316  'backendType' => 'text',
317  'frontendInput' => 'select',
318  'is_searchable' => false,
319  'options' => [
320  ['value' => '44', 'label' => 'red'],
321  ['value' => '45', 'label' => 'black'],
322  ],
323  ],
324  '44',
325  ['color' => '44'],
326  ],
327  'searchable select with options' => [
328  10,
329  [
330  'code' => 'color',
331  'backendType' => 'text',
332  'frontendInput' => 'select',
333  'is_searchable' => true,
334  'options' => [
335  ['value' => '44', 'label' => 'red'],
336  ['value' => '45', 'label' => 'black'],
337  ],
338  ],
339  '44',
340  ['color' => '44', 'color_value' => 'red'],
341  ],
342  'composite select with options' => [
343  10,
344  [
345  'code' => 'color',
346  'backendType' => 'text',
347  'frontendInput' => 'select',
348  'is_searchable' => true,
349  'options' => [
350  ['value' => '44', 'label' => 'red'],
351  ['value' => '45', 'label' => 'black'],
352  ],
353  ],
354  [10 => '44', 11 => '45'],
355  ['color' => ['44', '45'], 'color_value' => ['red', 'black']],
356  ],
357  'multiselect without options' => [
358  10,
359  [
360  'code' => 'multicolor',
361  'backendType' => 'text',
362  'frontendInput' => 'multiselect',
363  'is_searchable' => false,
364  'options' => [],
365  ],
366  '44,45',
367  ['multicolor' => [44, 45]],
368  ],
369  'unsearchable multiselect with options' => [
370  10,
371  [
372  'code' => 'multicolor',
373  'backendType' => 'text',
374  'frontendInput' => 'multiselect',
375  'is_searchable' => false,
376  'options' => [
377  ['value' => '44', 'label' => 'red'],
378  ['value' => '45', 'label' => 'black'],
379  ],
380  ],
381  '44,45',
382  ['multicolor' => [44, 45]],
383  ],
384  'searchable multiselect with options' => [
385  10,
386  [
387  'code' => 'multicolor',
388  'backendType' => 'text',
389  'frontendInput' => 'multiselect',
390  'is_searchable' => true,
391  'options' => [
392  ['value' => '44', 'label' => 'red'],
393  ['value' => '45', 'label' => 'black'],
394  ],
395  ],
396  '44,45',
397  ['multicolor' => [44, 45], 'multicolor_value' => ['red', 'black']],
398  ],
399  'composite multiselect with options' => [
400  10,
401  [
402  'code' => 'multicolor',
403  'backendType' => 'text',
404  'frontendInput' => 'multiselect',
405  'is_searchable' => true,
406  'options' => [
407  ['value' => '44', 'label' => 'red'],
408  ['value' => '45', 'label' => 'black'],
409  ['value' => '46', 'label' => 'green'],
410  ],
411  ],
412  [10 => '44,45', 11 => '45,46'],
413  ['multicolor' => [44, 45, 46], 'multicolor_value' => ['red', 'black', 'green']],
414  ],
415  'excluded attribute' => [
416  10,
417  [
418  'code' => 'price',
419  'backendType' => 'int',
420  'frontendInput' => 'int',
421  'is_searchable' => false,
422  'options' => []
423  ],
424  15,
425  []
426  ],
427  ];
428  }
429 }
$objectManager
Definition: bootstrap.php:17
testGetMap(int $productId, array $attributeData, $attributeValue, array $returnAttributeData)