18 class DataTest extends \PHPUnit\Framework\TestCase
57 private $metaDataPoolMock;
62 private $swatchAttributesProvider;
67 private $imageUrlBuilderMock;
71 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
72 $this->imageHelperMock = $this->createMock(\
Magento\Catalog\Helper\Image::class);
73 $this->productCollectionFactoryMock = $this->createPartialMock(
77 $this->productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
78 $this->productCollectionMock = $this->objectManager->getCollectionMock(
86 $this->configurableMock = $this->createMock(
89 $this->productModelFactoryMock = $this->createPartialMock(
90 \
Magento\Catalog\Model\ProductFactory::class,
94 $this->productRepoMock = $this->createMock(\
Magento\Catalog\Api\ProductRepositoryInterface::class);
96 $this->storeManagerMock = $this->createMock(\
Magento\Store\Model\StoreManager::class);
97 $this->swatchCollectionFactoryMock = $this->createPartialMock(
102 $this->attributeMock = $this->getMockBuilder(Attribute::class)
103 ->disableOriginalConstructor()
104 ->setMethods([
'setStoreId',
'getData',
'setData',
'getSource',
'hasData'])
106 $this->metaDataPoolMock = $this->getMockBuilder(MetadataPool::class)
107 ->disableOriginalConstructor()
111 \
Magento\Framework\Serialize\Serializer\Json::class,
112 [
'serialize',
'unserialize']
115 ->method(
'serialize')->willReturnCallback(
function ($parameter) {
116 return json_encode($parameter);
119 ->method(
'unserialize')->willReturnCallback(
function ($parameter) {
120 return json_decode($parameter,
true);
123 $this->swatchAttributesProvider = $this->getMockBuilder(SwatchAttributesProvider::class)
124 ->disableOriginalConstructor()
126 $this->imageUrlBuilderMock = $this->getMockBuilder(\
Magento\Catalog\Model\Product\Image\UrlBuilder::class)
127 ->disableOriginalConstructor()
128 ->setMethods([
'getUrl'])
131 $this->swatchHelperObject = $this->objectManager->getObject(
132 \
Magento\Swatches\Helper\Data::class,
134 'productCollectionFactory' => $this->productCollectionFactoryMock,
135 'configurable' => $this->configurableMock,
136 'productRepository' => $this->productRepoMock,
137 'storeManager' => $this->storeManagerMock,
138 'swatchCollectionFactory' => $this->swatchCollectionFactoryMock,
139 'imageUrlBuilder' => $this->imageUrlBuilderMock,
141 'swatchAttributesProvider' => $this->swatchAttributesProvider,
144 $this->objectManager->setBackwardCompatibleProperty(
145 $this->swatchHelperObject,
147 $this->metaDataPoolMock
157 'swatch_input_type' =>
'visual',
158 'update_product_preview_image' => 1,
159 'use_product_image_for_swatch' => 0
163 json_encode($additionalData),
185 ->expects($this->at(0))
187 ->with(
'additional_data')
188 ->will($this->returnValue($dataFromDb));
193 ->expects($this->at(
$i))
200 $this->attributeMock->expects($this->once())->method(
'setData');
202 $this->swatchHelperObject->assembleAdditionalDataEavAttribute($this->attributeMock);
211 'swatch_input_type' =>
'visual',
212 'update_product_preview_image' => 1,
213 'use_product_image_for_swatch' => 0
217 json_encode($additionalData),
219 'swatch_input_type' =>
'visual',
220 'update_product_preview_image' => 1,
221 'use_product_image_for_swatch' => 1,
227 'swatch_input_type' =>
null,
228 'update_product_preview_image' => 0,
229 'use_product_image_for_swatch' => 0,
241 $this->
getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
243 $result = $this->swatchHelperObject->loadFirstVariationWithSwatchImage($this->productMock, $requiredAttributes);
245 if ($expected ===
false) {
248 $this->assertInstanceOf(\
Magento\Catalog\Model\Product::class,
$result);
260 'image' =>
'/m/a/magento.png',
261 'small_image' =>
'/m/a/magento.png',
262 'thumbnail' =>
'/m/a/magento.png',
263 'swatch_image' =>
'/m/a/magento.png',
265 \Magento\Catalog\Model\Product::class,
270 'image' =>
'/m/a/magento.png',
271 'small_image' =>
'/m/a/magento.png',
272 'thumbnail' =>
'/m/a/magento.png',
285 $metadataMock = $this->createMock(\
Magento\Framework\EntityManager\EntityMetadataInterface::class);
286 $this->metaDataPoolMock->expects($this->once())->method(
'getMetadata')->willReturn($metadataMock);
287 $metadataMock->expects($this->once())->method(
'getLinkField')->willReturn(
'id');
293 $this->productCollectionMock->method(
'getFirstItem')->willReturn($this->productMock);
294 $this->productMock->method(
'getData')->with(
'id')->willReturn(95);
295 $this->productModelFactoryMock->method(
'create')->willReturn($this->productMock);
296 $this->productMock->method(
'load')->with(95)->will($this->returnSelf());
298 $this->swatchHelperObject->loadVariationByFallback($this->productMock, [
'color' => 31]);
307 $this->
getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
309 $result = $this->swatchHelperObject->loadFirstVariationWithImage($this->productMock, $requiredAttributes);
311 if ($expected ===
false) {
314 $this->assertInstanceOf(\
Magento\Catalog\Model\Product::class,
$result);
326 'image' =>
'/m/a/magento.png',
327 'small_image' =>
'/m/a/magento.png',
328 'thumbnail' =>
'/m/a/magento.png',
329 'swatch_image' =>
'/m/a/magento.png',
331 \Magento\Catalog\Model\Product::class,
336 'small_image' =>
'/m/a/magento.png',
337 'thumbnail' =>
'/m/a/magento.png',
338 'swatch_image' =>
'/m/a/magento.png',
348 $result = $this->swatchHelperObject->loadVariationByFallback($this->productMock, [
'color' => 31]);
354 $result = $this->swatchHelperObject->loadFirstVariationWithImage($this->productMock, [
'color' => 31]);
363 $mediaGalleryEntries = [];
366 foreach ($mediaGallery as $mediaType => $mediaFile) {
367 $mediaGalleryEntryMock = $this->getMockBuilder(
368 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
370 $mediaGalleryEntryMock->expects($this->atLeastOnce())
371 ->method(
'isDisabled')
373 $mediaGalleryEntryMock->expects($this->atLeastOnce())
375 ->willReturn([$mediaType]);
376 $mediaGalleryEntryMock->expects($this->atLeastOnce())
378 ->willReturn($mediaFile);
379 $mediaGalleryEntryMock->expects($this->atLeastOnce())
383 $mediaGalleryEntries[] = $mediaGalleryEntryMock;
384 $mediaUrls[] = [$mediaFile,
'product_swatch_image_large',
'http://full_path_to_image' . $mediaFile];
385 $mediaUrls[] = [$mediaFile,
'product_swatch_image_medium' ,
'http://full_path_to_image' . $mediaFile];
386 $mediaUrls[] = [$mediaFile,
'product_swatch_image_small',
'http://full_path_to_image' . $mediaFile];
389 $this->productMock->expects($this->once())
390 ->method(
'getMediaGalleryEntries')
391 ->willReturn($mediaGalleryEntries);
394 $this->imageUrlBuilderMock->expects($this->any())
396 ->willReturnMap($mediaUrls);
399 $productMediaGallery = $this->swatchHelperObject->getProductMediaGallery($this->productMock);
401 $this->assertContains(
$image, $productMediaGallery[
'large']);
402 $this->assertContains(
$image, $productMediaGallery[
'medium']);
403 $this->assertContains(
$image, $productMediaGallery[
'small']);
405 $this->assertEmpty($productMediaGallery);
417 'image' =>
'/m/a/magento1.png',
418 'small_image' =>
'/m/a/magento2.png',
419 'thumbnail' =>
'/m/a/magento3.png',
420 'swatch_image' =>
'/m/a/magento4.png',
426 'small_image' =>
'/m/a/magento4.png',
427 'thumbnail' =>
'/m/a/magento5.png',
428 'swatch_image' =>
'/m/a/magento6.png',
443 $this->swatchAttributesProvider
445 ->with($this->productMock)
446 ->willReturn($returnFromProvideMethod);
456 ->expects($this->atLeastOnce())
457 ->method(
'getTypeInstance')
458 ->willReturn($this->configurableMock);
461 for (
$i = 0;
$i < 2;
$i++) {
463 ->disableOriginalConstructor()
464 ->setMethods([
'hasData',
'getMediaGalleryEntries'])
468 $mediaGalleryEntries = [];
469 foreach (array_keys($imageTypes) as $mediaType) {
470 $mediaGalleryEntryMock = $this->getMockBuilder(
471 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
473 $mediaGalleryEntryMock->expects($this->any())
474 ->method(
'isDisabled')
476 $mediaGalleryEntryMock->expects($this->any())
478 ->willReturn([$mediaType]);
480 $mediaGalleryEntries[] = $mediaGalleryEntryMock;
483 ->method(
'getMediaGalleryEntries')
484 ->willReturn($mediaGalleryEntries);
489 $this->configurableMock->expects($this->once())
490 ->method(
'getUsedProducts')
491 ->with($this->productMock)
497 $confAttribute = $this->createMock(
501 $this->configurableMock
502 ->expects($this->any())
503 ->method(
'getConfigurableAttributes')
504 ->with($this->productMock)
505 ->willReturn([$confAttribute, $confAttribute]);
508 ->expects($this->any())
510 ->with(
'getProductAttribute')
511 ->willReturn($this->attributeMock);
516 $this->productCollectionFactoryMock
517 ->expects($this->any())
519 ->willReturn($this->productCollectionMock);
526 $this->productCollectionMock
528 ->with(
'catalog_product_relation')
529 ->willReturn(
'catalog_product_relation');
531 $zendDbSelectMock = $this->createMock(\
Magento\Framework\DB\Select::class);
533 $this->productCollectionMock->method(
'getSelect')->willReturn($zendDbSelectMock);
534 $zendDbSelectMock->method(
'join')->willReturn($zendDbSelectMock);
535 $zendDbSelectMock->method(
'where')->willReturn($zendDbSelectMock);
552 'swatch_image' =>
'',
558 'small_image' =>
'img1.png',
559 'thumbnail' =>
'img1.png',
569 'image' =>
'img1.png',
570 'small_image' =>
'img1.png',
571 'thumbnail' =>
'img1.png',
599 $this->swatchAttributesProvider
601 ->with($this->productMock)
602 ->willReturn([$this->attributeMock]);
606 $this->attributeMock->method(
'setStoreId')->with(
$storeId)->will($this->returnSelf());
607 $storeMock = $this->createMock(\
Magento\Store\Model\Store::class);
608 $storeMock->method(
'getId')->willReturn(
$storeId);
609 $this->storeManagerMock->method(
'getStore')->willReturn($storeMock);
611 $this->attributeMock->method(
'getData')->with(
'')->willReturn(
$attributeData);
613 $sourceMock = $this->createMock(\
Magento\Eav\Model\Entity\
Attribute\Source\AbstractSource::class);
614 $sourceMock->expects($this->any())->method(
'getAllOptions')->with(
false)->willReturn($optionsArray);
615 $this->attributeMock->method(
'getSource')->willReturn($sourceMock);
617 $result = $this->swatchHelperObject->getSwatchAttributesAsArray($this->productMock);
618 $this->assertEquals(
$result, $expected);
629 [
'value' => 45,
'label' =>
'green'],
630 [
'value' => 46,
'label' =>
'yellow'],
631 [
'value' => 47,
'label' =>
'red'],
632 [
'value' => 48,
'label' =>
'blue'],
639 'attribute_id' => 52,
651 [
'value' => 45,
'label' =>
'green'],
652 [
'value' => 46,
'label' =>
'yellow'],
655 'attribute_id' => 324
659 'attribute_id' => 324,
672 $swatchMock = $this->createMock(\
Magento\Swatches\Model\Swatch::class);
678 'value' =>
'#324234',
691 $swatchMock->expects($this->at(0))->method(
'offsetGet')->with(
'type')
693 $swatchMock->expects($this->at(1))->method(
'offsetGet')->with(
'option_id')
695 $swatchMock->expects($this->at(2))->method(
'getData')->with(
'')
697 $swatchMock->expects($this->at(3))->method(
'offsetGet')->with(
'type')
699 $swatchMock->expects($this->at(4))->method(
'offsetGet')->with(
'store_id')
701 $swatchMock->expects($this->at(5))->method(
'offsetGet')->with(
'store_id')
703 $swatchMock->expects($this->at(6))->method(
'offsetGet')->with(
'option_id')
705 $swatchMock->expects($this->at(7))->method(
'getData')->with(
'')
708 $swatchCollectionMock = $this->objectManager
709 ->getCollectionMock(Collection::class, [$swatchMock, $swatchMock]);
710 $swatchCollectionMock->method(
'addFilterByOptionsIds')->with([35])->will($this->returnSelf());
711 $this->swatchCollectionFactoryMock->method(
'create')->willReturn($swatchCollectionMock);
713 $storeMock = $this->createMock(\
Magento\Store\Model\Store::class);
714 $this->storeManagerMock->method(
'getStore')->willReturn($storeMock);
715 $storeMock->method(
'getId')->willReturn(1);
717 $this->swatchHelperObject->getSwatchesByOptionsId([35]);
722 $swatchMock = $this->createMock(\
Magento\Swatches\Model\Swatch::class);
741 $swatchMock->expects($this->at(0))->method(
'offsetGet')->with(
'type')->willReturn(0);
742 $swatchMock->expects($this->at(1))->method(
'offsetGet')->with(
'store_id')->willReturn(1);
743 $swatchMock->expects($this->at(2))->method(
'offsetGet')->with(
'value')->willReturn(
'test');
744 $swatchMock->expects($this->at(3))->method(
'offsetGet')->with(
'option_id')->willReturn(35);
745 $swatchMock->expects($this->at(4))->method(
'getData')->with(
'')->willReturn(
$optionsData[0]);
746 $swatchMock->expects($this->at(5))->method(
'offsetGet')->with(
'type')->willReturn(0);
747 $swatchMock->expects($this->at(6))->method(
'offsetGet')->with(
'store_id')->willReturn(1);
748 $swatchMock->expects($this->at(7))->method(
'offsetGet')->with(
'value')->willReturn(
'test2');
749 $swatchMock->expects($this->at(8))->method(
'offsetGet')->with(
'option_id')->willReturn(36);
750 $swatchMock->expects($this->at(9))->method(
'getData')->with(
'')->willReturn(
$optionsData[1]);
752 $swatchCollectionMock = $this->objectManager->getCollectionMock(
759 $this->swatchCollectionFactoryMock->method(
'create')->willReturn($swatchCollectionMock);
761 $swatchCollectionMock->method(
'addFilterByOptionsIds')->with([35])->will($this->returnSelf());
763 $storeMock = $this->createMock(\
Magento\Store\Model\Store::class);
764 $this->storeManagerMock->method(
'getStore')->willReturn($storeMock);
765 $storeMock->method(
'getId')->willReturn(1);
767 $this->swatchHelperObject->getSwatchesByOptionsId([35]);
772 $swatchMock = $this->createMock(\
Magento\Swatches\Model\Swatch::class);
777 'value' =>
'test_test',
782 $swatchMock->expects($this->at(0))->method(
'offsetGet')->with(
'type')->willReturn(0);
783 $swatchMock->expects($this->at(1))->method(
'offsetGet')->with(
'store_id')->willReturn(0);
784 $swatchMock->expects($this->at(2))->method(
'offsetGet')->with(
'store_id')->willReturn(0);
785 $swatchMock->expects($this->at(3))->method(
'offsetGet')->with(
'option_id')->willReturn(35);
786 $swatchMock->expects($this->at(4))->method(
'getData')->with(
'')->willReturn(
$optionsData);
788 $swatchCollectionMock = $this->objectManager->getCollectionMock(
794 $this->swatchCollectionFactoryMock->method(
'create')->willReturn($swatchCollectionMock);
796 $swatchCollectionMock->method(
'addFilterByOptionsIds')->with([35])->will($this->returnSelf());
798 $storeMock = $this->createMock(\
Magento\Store\Model\Store::class);
799 $this->storeManagerMock->method(
'getStore')->willReturn($storeMock);
800 $storeMock->method(
'getId')->willReturn(1);
802 $this->swatchHelperObject->getSwatchesByOptionsId([35]);
808 $result = $this->swatchHelperObject->isProductHasSwatch($this->productMock);
809 $this->assertEquals(
true,
$result);
testGetSwatchesByOptionsIdIf1()
$productCollectionFactoryMock
testGetProductMediaGallery($mediaGallery, $image)
testLoadVariationByFallbackWithoutProduct()
getAttributesFromConfigurable()
testGetSwatchesByOptionsIdIf2()
testGetSwatchesByOptionsIdIf3()
testLoadVariationByFallback($product)
getUsedProducts(array $attributes, array $imageTypes)
dataForVariationWithSwatchImage()
testAssembleAdditionalDataEavAttribute($dataFromDb, $attributeData)
dataForGettingSwatchAsArray()
testGetSwatchAttributesAsArray($optionsArray, $attributeData, $expected)
dataForCreateSwatchProduct()
prepareVariationCollection()
testLoadFirstVariationWithImage($imageTypes, $expected, $requiredAttributes)
dataForCreateSwatchProductByFallback()
$swatchCollectionFactoryMock
dataForAssembleEavAttribute()
testLoadFirstVariationWithImageNoProduct()
testLoadFirstVariationWithSwatchImage($imageTypes, $expected, $requiredAttributes)
dataForVariationWithImage()