22 private $eavAttribute;
28 private $swatchFactory;
31 private $collectionFactory;
34 private $swatchHelper;
37 private $abstractSource;
49 private $optionIds = [];
52 private $allOptions = [];
55 private $dependencyArray = [];
60 $this->swatchFactory = $this->createPartialMock(\
Magento\Swatches\Model\SwatchFactory::class, [
'create']);
61 $this->swatchHelper = $this->createMock(\
Magento\Swatches\Helper\Data::class);
62 $this->swatch = $this->createMock(\
Magento\Swatches\Model\Swatch::class);
66 $this->collectionFactory = $this->createPartialMock(
70 $this->abstractSource = $this->createMock(\
Magento\Eav\Model\Entity\
Attribute\Source\AbstractSource::class);
73 \
Magento\Framework\Serialize\Serializer\Json::class,
74 [
'serialize',
'unserialize']
78 ->method(
'serialize')->willReturnCallback(
function ($parameter) {
79 return json_encode($parameter);
83 ->method(
'unserialize')->willReturnCallback(
function ($parameter) {
84 return json_decode($parameter,
true);
87 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
89 \
Magento\Swatches\Model\Plugin\EavAttribute::class,
91 'collectionFactory' => $this->collectionFactory,
92 'swatchFactory' => $this->swatchFactory,
93 'swatchHelper' => $this->swatchHelper,
99 'value' => [
'option 89' =>
'test 1',
'option 114' =>
'test 2',
'option 170' =>
'test 3'],
100 'delete' => [
'option 89' => 0,
'option 114' => 1,
'option 170' => 0],
102 $this->allOptions = [
null, [
'value' =>
'option 12'], [
'value' =>
'option 154']];
103 $this->dependencyArray = [
'option 89',
'option 170'];
113 $this->attribute->expects($this->exactly(6))->method(
'getData')->withConsecutive(
119 )->will($this->onConsecutiveCalls(
120 self::ATTRIBUTE_DEFAULT_VALUE,
121 self::ATTRIBUTE_OPTION_VALUE,
122 self::ATTRIBUTE_SWATCH_VALUE,
127 $this->attribute->expects($this->exactly(3))->method(
'setData')
129 [
'option', self::ATTRIBUTE_OPTION_VALUE],
130 [
'default', self::ATTRIBUTE_DEFAULT_VALUE],
131 [
'swatch', self::ATTRIBUTE_SWATCH_VALUE]
134 $this->swatchHelper->expects($this->once())->method(
'assembleAdditionalDataEavAttribute')
135 ->with($this->attribute);
136 $this->swatchHelper->expects($this->atLeastOnce())->method(
'isVisualSwatch')
137 ->with($this->attribute)
139 $this->swatchHelper->expects($this->once())->method(
'isSwatchAttribute')
140 ->with($this->attribute)
142 $this->swatchHelper->expects($this->never())->method(
'isTextSwatch');
144 $this->eavAttribute->beforeBeforeSave($this->attribute);
154 $this->attribute->expects($this->exactly(6))->method(
'getData')->withConsecutive(
161 $this->onConsecutiveCalls(
162 self::ATTRIBUTE_DEFAULT_VALUE,
163 self::ATTRIBUTE_OPTION_VALUE,
164 self::ATTRIBUTE_SWATCH_VALUE,
170 $this->attribute->expects($this->exactly(3))->method(
'setData')
172 [
'option', self::ATTRIBUTE_OPTION_VALUE],
173 [
'default', self::ATTRIBUTE_DEFAULT_VALUE],
174 [
'swatch', self::ATTRIBUTE_SWATCH_VALUE]
177 $this->swatchHelper->expects($this->once())->method(
'assembleAdditionalDataEavAttribute')
178 ->with($this->attribute);
179 $this->swatchHelper->expects($this->atLeastOnce())->method(
'isVisualSwatch')
180 ->with($this->attribute)
182 $this->swatchHelper->expects($this->atLeastOnce())->method(
'isTextSwatch')
183 ->with($this->attribute)
185 $this->swatchHelper->expects($this->once())->method(
'isSwatchAttribute')
186 ->with($this->attribute)
189 $this->eavAttribute->beforeBeforeSave($this->attribute);
203 $this->swatchHelper->expects($this->once())
204 ->method(
'isSwatchAttribute')
205 ->with($this->attribute)
208 $this->swatchHelper->expects($this->atLeastOnce())
209 ->method(
'isVisualSwatch')
211 $this->attribute->expects($this->exactly(5))
221 $this->onConsecutiveCalls(
222 self::ATTRIBUTE_DEFAULT_VALUE,
223 self::ATTRIBUTE_OPTION_VALUE,
224 self::ATTRIBUTE_SWATCH_VALUE,
230 $this->eavAttribute->beforeBeforeSave($this->attribute);
244 $this->swatchHelper->expects($this->once())
245 ->method(
'isSwatchAttribute')
246 ->with($this->attribute)
249 $this->swatchHelper->expects($this->atLeastOnce())
250 ->method(
'isVisualSwatch')
252 $this->attribute->expects($this->exactly(6))
260 [
'swatch_input_type']
263 $this->onConsecutiveCalls(
264 self::ATTRIBUTE_DEFAULT_VALUE,
265 self::ATTRIBUTE_OPTION_VALUE,
266 self::ATTRIBUTE_SWATCH_VALUE,
272 $this->eavAttribute->beforeBeforeSave($this->attribute);
278 'swatch_input_type' =>
'visual',
279 'update_product_preview_image' => 1,
280 'use_product_image_for_swatch' => 0
283 $shortAdditionalData = [
284 'update_product_preview_image' => 1,
285 'use_product_image_for_swatch' => 0
288 $this->attribute->expects($this->exactly(2))->method(
'getData')->withConsecutive(
291 )->willReturnOnConsecutiveCalls(
293 json_encode($additionalData)
297 ->expects($this->once())
299 ->with(
'additional_data', json_encode($shortAdditionalData))
300 ->will($this->returnSelf());
302 $this->swatchHelper->expects($this->never())->method(
'assembleAdditionalDataEavAttribute');
303 $this->swatchHelper->expects($this->never())->method(
'isVisualSwatch');
304 $this->swatchHelper->expects($this->never())->method(
'isTextSwatch');
306 $this->swatchHelper->expects($this->once())->method(
'isSwatchAttribute')
307 ->with($this->attribute)
310 $this->eavAttribute->beforeBeforeSave($this->attribute);
333 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
334 ->willReturn($this->allOptions);
335 $this->resource->expects($this->once())->method(
'saveDefaultSwatchOption')
336 ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
338 $this->swatch->expects($this->once())->method(
'getResource')
339 ->willReturn($this->resource);
340 $this->swatch->expects($this->once())->method(
'getId')
342 $this->swatch->expects($this->once())->method(
'save');
343 $this->swatch->expects($this->exactly(4))->method(
'setData')
345 [
'option_id', self::OPTION_ID],
347 [
'type', $swatchType],
348 [
'value', $swatchValue]
351 $this->collection->expects($this->exactly(2))->method(
'addFieldToFilter')
353 [
'option_id', self::OPTION_ID],
357 $this->collection->expects($this->once())->method(
'getFirstItem')
358 ->willReturn($this->swatch);
359 $this->collectionFactory->expects($this->once())->method(
'create')
360 ->willReturn($this->collection);
362 $this->attribute->expects($this->at(0))->method(
'getData')
363 ->willReturn($this->optionIds);
364 $this->attribute->expects($this->at(1))->method(
'getSource')
365 ->willReturn($this->abstractSource);
366 $this->attribute->expects($this->at(2))->method(
'getData')
368 ->willReturn($this->dependencyArray[0]);
369 $this->attribute->expects($this->at(3))->method(
'getId')
370 ->willReturn(self::ATTRIBUTE_ID);
371 $this->attribute->expects($this->at(4))->method(
'getData')
372 ->with(
'swatch/value')
373 ->willReturn([self::STORE_ID => $swatchValue]);
374 $this->attribute->expects($this->at(5))->method(
'getData')
375 ->with(
'option/delete/' . self::OPTION_ID)
378 $this->swatchFactory->expects($this->exactly(1))->method(
'create')
379 ->willReturn($this->swatch);
380 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
381 ->with($this->attribute)
383 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
384 ->with($this->attribute)
386 $this->swatchHelper->expects($this->never())->method(
'isTextSwatch');
388 $this->eavAttribute->afterAfterSave($this->attribute);
393 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
394 ->willReturn($this->allOptions);
396 $this->swatch->expects($this->any())->method(
'getId')
398 $this->swatch->expects($this->any())->method(
'save');
399 $this->swatch->expects($this->any())->method(
'isDeleted')
402 $this->collection->expects($this->any())->method(
'addFieldToFilter')
404 $this->collection->expects($this->any())->method(
'getFirstItem')
405 ->willReturn($this->swatch);
406 $this->collectionFactory->expects($this->any())->method(
'create')
407 ->willReturn($this->collection);
409 $this->attribute->expects($this->at(0))->method(
'getData')
410 ->willReturn($this->optionIds);
411 $this->attribute->expects($this->at(1))->method(
'getSource')
412 ->willReturn($this->abstractSource);
413 $this->attribute->expects($this->at(2))->method(
'getData')
417 $this->attribute->expects($this->at(3))->method(
'getData')
418 ->with(
'swatch/value')
430 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
431 ->with($this->attribute)
433 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
434 ->with($this->attribute)
436 $this->swatchHelper->expects($this->once())->method(
'isTextSwatch')
437 ->with($this->attribute)
440 $this->swatch->expects($this->any())->method(
'setData')
442 [
'option_id', self::OPTION_ID],
448 $this->eavAttribute->afterAfterSave($this->attribute);
453 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
454 ->willReturn($this->allOptions);
455 $this->resource->expects($this->once())->method(
'saveDefaultSwatchOption')
456 ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
458 $this->swatch->expects($this->once())->method(
'getResource')
459 ->willReturn($this->resource);
460 $this->swatch->expects($this->once())->method(
'getId')
462 $this->swatch->expects($this->once())->method(
'save');
463 $this->swatch->expects($this->once())->method(
'isDeleted')
465 $this->swatch->expects($this->exactly(4))->method(
'setData')
467 [
'option_id', self::OPTION_ID],
468 [
'store_id', self::OPTION_ID],
473 $this->collection->expects($this->exactly(2))->method(
'addFieldToFilter')
475 [
'option_id', self::OPTION_ID],
476 [
'store_id', self::OPTION_ID]
478 $this->collection->expects($this->once())->method(
'getFirstItem')
479 ->willReturn($this->swatch);
480 $this->collectionFactory->expects($this->once())->method(
'create')
481 ->willReturn($this->collection);
483 $this->attribute->expects($this->at(0))->method(
'getData')
484 ->willReturn($this->optionIds);
485 $this->attribute->expects($this->at(1))->method(
'getSource')
486 ->willReturn($this->abstractSource);
487 $this->attribute->expects($this->at(2))->method(
'getData')
489 ->willReturn($this->dependencyArray[0]);
490 $this->attribute->expects($this->at(3))->method(
'getId')
491 ->willReturn(self::ATTRIBUTE_ID);
492 $this->attribute->expects($this->at(4))->method(
'getData')
493 ->with(
'swatch/value')
494 ->willReturn([self::STORE_ID => [self::OPTION_ID =>
null]]);
495 $this->attribute->expects($this->at(5))->method(
'getData')
496 ->with(
'option/delete/' . self::OPTION_ID)
499 $this->swatchFactory->expects($this->exactly(1))->method(
'create')
500 ->willReturn($this->swatch);
501 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
502 ->with($this->attribute)
504 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
505 ->with($this->attribute)
507 $this->swatchHelper->expects($this->once())->method(
'isTextSwatch')
508 ->with($this->attribute)
511 $this->eavAttribute->afterAfterSave($this->attribute);
516 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
517 ->willReturn($this->allOptions);
518 $this->resource->expects($this->once())->method(
'saveDefaultSwatchOption')
519 ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
521 $this->swatch->expects($this->once())->method(
'getResource')
522 ->willReturn($this->resource);
524 $this->attribute->expects($this->at(0))->method(
'getData')
525 ->willReturn($this->optionIds);
526 $this->attribute->expects($this->at(1))->method(
'getSource')
527 ->willReturn($this->abstractSource);
528 $this->attribute->expects($this->at(2))->method(
'getData')
530 ->willReturn($this->dependencyArray[0]);
531 $this->attribute->expects($this->at(3))->method(
'getId')
532 ->willReturn(self::ATTRIBUTE_ID);
533 $this->attribute->expects($this->at(4))->method(
'getData')
534 ->with(
'swatch/value')
535 ->willReturn([self::STORE_ID =>
null]);
536 $this->attribute->expects($this->at(5))->method(
'getData')
537 ->with(
'option/delete/' . self::OPTION_ID)
540 $this->swatchFactory->expects($this->once())->method(
'create')
541 ->willReturn($this->swatch);
542 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
543 ->with($this->attribute)
545 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
546 ->with($this->attribute)
548 $this->swatchHelper->expects($this->never())->method(
'isTextSwatch');
550 $this->eavAttribute->afterAfterSave($this->attribute);
555 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
556 ->willReturn($this->allOptions);
557 $this->resource->expects($this->once())->method(
'saveDefaultSwatchOption')
558 ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
560 $this->swatch->expects($this->once())->method(
'getResource')
561 ->willReturn($this->resource);
563 $this->attribute->expects($this->at(0))->method(
'getData')
564 ->willReturn($this->optionIds);
565 $this->attribute->expects($this->at(1))->method(
'getSource')
566 ->willReturn($this->abstractSource);
567 $this->attribute->expects($this->at(2))->method(
'getData')
569 ->willReturn($this->dependencyArray[0]);
570 $this->attribute->expects($this->at(3))->method(
'getId')
571 ->willReturn(self::ATTRIBUTE_ID);
572 $this->attribute->expects($this->at(4))->method(
'getData')
573 ->with(
'swatch/value')
574 ->willReturn([self::STORE_ID => [self::OPTION_ID =>
null]]);
575 $this->attribute->expects($this->at(5))->method(
'getData')
576 ->with(
'option/delete/' . self::OPTION_ID)
579 $this->swatchFactory->expects($this->once())->method(
'create')
580 ->willReturn($this->swatch);
581 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
582 ->with($this->attribute)
584 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
585 ->with($this->attribute)
587 $this->swatchHelper->expects($this->once())->method(
'isTextSwatch')
588 ->with($this->attribute)
591 $this->eavAttribute->afterAfterSave($this->attribute);
596 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
597 ->willReturn($this->allOptions);
598 $this->resource->expects($this->once())->method(
'saveDefaultSwatchOption')
599 ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
601 $this->swatch->expects($this->once())->method(
'getResource')
602 ->willReturn($this->resource);
603 $this->swatch->expects($this->once())->method(
'getId')
605 $this->swatch->expects($this->once())->method(
'save');
606 $this->swatch->expects($this->once())->method(
'isDeleted')
608 $this->swatch->expects($this->exactly(2))->method(
'setData')
614 $this->collection->expects($this->exactly(2))->method(
'addFieldToFilter')
616 [
'option_id', self::OPTION_ID],
617 [
'store_id', self::OPTION_ID]
619 $this->collection->expects($this->once())->method(
'getFirstItem')
620 ->willReturn($this->swatch);
621 $this->collectionFactory->expects($this->once())->method(
'create')
622 ->willReturn($this->collection);
624 $this->attribute->expects($this->at(0))->method(
'getData')
625 ->willReturn($this->optionIds);
626 $this->attribute->expects($this->at(1))->method(
'getSource')
627 ->willReturn($this->abstractSource);
628 $this->attribute->expects($this->at(2))->method(
'getData')
630 ->willReturn($this->dependencyArray[0]);
631 $this->attribute->expects($this->at(3))->method(
'getId')
632 ->willReturn(self::ATTRIBUTE_ID);
633 $this->attribute->expects($this->at(4))->method(
'getData')
634 ->with(
'swatch/value')
635 ->willReturn([self::STORE_ID => [self::OPTION_ID =>
null]]);
636 $this->attribute->expects($this->at(5))->method(
'getData')
637 ->with(
'option/delete/' . self::OPTION_ID)
640 $this->swatchFactory->expects($this->exactly(1))->method(
'create')
641 ->willReturn($this->swatch);
642 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
643 ->with($this->attribute)
645 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
646 ->with($this->attribute)
648 $this->swatchHelper->expects($this->once())->method(
'isTextSwatch')
649 ->with($this->attribute)
652 $this->eavAttribute->afterAfterSave($this->attribute);
657 $this->abstractSource->expects($this->once())->method(
'getAllOptions')
658 ->willReturn($this->allOptions);
660 $this->swatch->expects($this->once())->method(
'getId')
662 $this->swatch->expects($this->once())->method(
'save');
663 $this->swatch->expects($this->once())->method(
'isDeleted')
665 $this->swatch->expects($this->exactly(2))->method(
'setData')
671 $this->collection->expects($this->exactly(2))->method(
'addFieldToFilter')
673 [
'option_id', self::OPTION_ID],
674 [
'store_id', self::OPTION_ID]
676 $this->collection->expects($this->once())->method(
'getFirstItem')
677 ->willReturn($this->swatch);
678 $this->collectionFactory->expects($this->once())->method(
'create')
679 ->willReturn($this->collection);
681 $this->attribute->expects($this->at(0))->method(
'getData')
683 ->willReturn($this->optionIds);
684 $this->attribute->expects($this->at(1))->method(
'getSource')
685 ->willReturn($this->abstractSource);
686 $this->attribute->expects($this->at(2))->method(
'getData')
687 ->with(
'swatch/value')
688 ->willReturn([self::STORE_ID => [self::OPTION_ID =>
null]]);
689 $this->attribute->expects($this->at(3))->method(
'getData')
690 ->with(
'option/delete/' . self::OPTION_ID)
693 $this->swatchHelper->expects($this->exactly(2))->method(
'isSwatchAttribute')
694 ->with($this->attribute)
695 ->will($this->onConsecutiveCalls(
true,
false));
696 $this->swatchHelper->expects($this->once())->method(
'isVisualSwatch')
697 ->with($this->attribute)
699 $this->swatchHelper->expects($this->once())->method(
'isTextSwatch')
700 ->with($this->attribute)
703 $this->eavAttribute->afterAfterSave($this->attribute);
testAfterAfterSaveVisualSwatch($swatchType, $swatchValue)
testAfterAfterSaveTextualSwatch()
testBeforeSaveVisualSwatch()
const SWATCH_INPUT_TYPE_DROPDOWN
testAfterAfterSaveIsSwatchExists()
const SWATCH_TYPE_VISUAL_IMAGE
const SWATCH_TYPE_TEXTUAL
testAfterAfterSaveTextualSwatchIsDelete()
testAfterAfterSaveNotSwatchAttribute()
testBeforeSaveWithFailedValidation()
testDefaultTextualSwatchAfterSave()
const ATTRIBUTE_OPTION_VALUE
testBeforeSaveWithDeletedOption()
const SWATCH_INPUT_TYPE_KEY
testBeforeSaveTextSwatch()
const ATTRIBUTE_SWATCH_VALUE
const SWATCH_TYPE_VISUAL_COLOR
const ATTRIBUTE_DEFAULT_VALUE
testAfterAfterSaveVisualSwatchIsDelete()
testBeforeSaveNotSwatch()