Skip to content

Commit 1cbe5cb

Browse files
committed
MAGETWO-61106: Configurable product page does not display swatches
1 parent 6c7bd78 commit 1cbe5cb

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Test\Unit\Block\Product\Renderer\Listing;
7+
8+
use Magento\Swatches\Block\Product\Renderer\Configurable;
9+
10+
/**
11+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12+
*/
13+
class ConfigurableTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var Configurable */
16+
private $configurable;
17+
18+
/** @var \Magento\Framework\Stdlib\ArrayUtils|\PHPUnit_Framework_MockObject_MockObject */
19+
private $arrayUtils;
20+
21+
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
22+
private $jsonEncoder;
23+
24+
/** @var \Magento\ConfigurableProduct\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
25+
private $helper;
26+
27+
/** @var \Magento\Swatches\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
28+
private $swatchHelper;
29+
30+
/** @var \Magento\Swatches\Helper\Media|\PHPUnit_Framework_MockObject_MockObject */
31+
private $swatchMediaHelper;
32+
33+
/** @var \Magento\Catalog\Helper\Product|\PHPUnit_Framework_MockObject_MockObject */
34+
private $catalogProduct;
35+
36+
/** @var \Magento\Customer\Helper\Session\CurrentCustomer|\PHPUnit_Framework_MockObject_MockObject */
37+
private $currentCustomer;
38+
39+
/** @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject */
40+
private $priceCurrency;
41+
42+
/** @var \Magento\ConfigurableProduct\Model\ConfigurableAttributeData|\PHPUnit_Framework_MockObject_MockObject */
43+
private $configurableAttributeData;
44+
45+
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
46+
private $product;
47+
48+
/** @var \Magento\Catalog\Model\Product\Type\AbstractType|\PHPUnit_Framework_MockObject_MockObject */
49+
private $typeInstance;
50+
51+
/** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
52+
private $scopeConfig;
53+
54+
/** @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject */
55+
private $imageHelper;
56+
57+
/** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
58+
private $urlBuilder;
59+
60+
public function setUp()
61+
{
62+
$this->arrayUtils = $this->getMock('\Magento\Framework\Stdlib\ArrayUtils', [], [], '', false);
63+
$this->jsonEncoder = $this->getMock('\Magento\Framework\Json\EncoderInterface', [], [], '', false);
64+
$this->helper = $this->getMock('\Magento\ConfigurableProduct\Helper\Data', [], [], '', false);
65+
$this->swatchHelper = $this->getMock('\Magento\Swatches\Helper\Data', [], [], '', false);
66+
$this->swatchMediaHelper = $this->getMock('\Magento\Swatches\Helper\Media', [], [], '', false);
67+
$this->catalogProduct = $this->getMock('\Magento\Catalog\Helper\Product', [], [], '', false);
68+
$this->currentCustomer = $this->getMock('\Magento\Customer\Helper\Session\CurrentCustomer', [], [], '', false);
69+
$this->priceCurrency = $this->getMock('\Magento\Framework\Pricing\PriceCurrencyInterface', [], [], '', false);
70+
$this->configurableAttributeData = $this->getMock(
71+
'Magento\ConfigurableProduct\Model\ConfigurableAttributeData',
72+
[],
73+
[],
74+
'',
75+
false
76+
);
77+
$this->product = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
78+
$this->typeInstance = $this->getMock('\Magento\Catalog\Model\Product\Type\AbstractType', [], [], '', false);
79+
$this->scopeConfig = $this->getMock('\Magento\Framework\App\Config\ScopeConfigInterface', [], [], '', false);
80+
$this->imageHelper = $this->getMock('\Magento\Catalog\Helper\Image', [], [], '', false);
81+
$this->urlBuilder = $this->getMock('\Magento\Framework\UrlInterface');
82+
83+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
84+
$this->configurable = $objectManagerHelper->getObject(
85+
'\Magento\Swatches\Block\Product\Renderer\Listing\Configurable',
86+
[
87+
'scopeConfig' => $this->scopeConfig,
88+
'imageHelper' => $this->imageHelper,
89+
'urlBuilder' => $this->urlBuilder,
90+
'arrayUtils' => $this->arrayUtils,
91+
'jsonEncoder' => $this->jsonEncoder,
92+
'helper' => $this->helper,
93+
'swatchHelper' => $this->swatchHelper,
94+
'swatchMediaHelper' => $this->swatchMediaHelper,
95+
'catalogProduct' => $this->catalogProduct,
96+
'currentCustomer' => $this->currentCustomer,
97+
'priceCurrency' => $this->priceCurrency,
98+
'configurableAttributeData' => $this->configurableAttributeData,
99+
'data' => [],
100+
]
101+
);
102+
}
103+
104+
/**
105+
* @covers Magento\Swatches\Block\Product\Renderer\Listing\Configurable::getSwatchAttributesData
106+
*/
107+
public function testGetJsonSwatchConfigWithoutSwatches()
108+
{
109+
$this->prepareGetJsonSwatchConfig();
110+
$this->configurable->setProduct($this->product);
111+
$this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')
112+
->with($this->product)
113+
->willReturn([]);
114+
$this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')
115+
->willReturn([]);
116+
$this->jsonEncoder->expects($this->once())->method('encode')->with([]);
117+
$this->configurable->getJsonSwatchConfig();
118+
}
119+
120+
/**
121+
* @covers Magento\Swatches\Block\Product\Renderer\Listing\Configurable::getSwatchAttributesData
122+
*/
123+
public function testGetJsonSwatchNotUsedInProductListing()
124+
{
125+
$this->prepareGetJsonSwatchConfig();
126+
$this->configurable->setProduct($this->product);
127+
$this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')
128+
->with($this->product)
129+
->willReturn([
130+
1 => [
131+
'options' => [1 => 'testA', 3 => 'testB'],
132+
'use_product_image_for_swatch' => true,
133+
'used_in_product_listing' => false,
134+
'attribute_code' => 'code',
135+
],
136+
]);
137+
$this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')
138+
->willReturn([]);
139+
$this->jsonEncoder->expects($this->once())->method('encode')->with([]);
140+
$this->configurable->getJsonSwatchConfig();
141+
}
142+
143+
/**
144+
* @covers Magento\Swatches\Block\Product\Renderer\Listing\Configurable::getSwatchAttributesData
145+
*/
146+
public function testGetJsonSwatchUsedInProductListing()
147+
{
148+
$products = [
149+
1 => 'testA',
150+
3 => 'testB'
151+
];
152+
$expected =
153+
[
154+
'type' => null,
155+
'value' => 'hello',
156+
'label' => $products[3]
157+
];
158+
$this->prepareGetJsonSwatchConfig();
159+
$this->configurable->setProduct($this->product);
160+
$this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')
161+
->with($this->product)
162+
->willReturn([
163+
1 => [
164+
'options' => $products,
165+
'use_product_image_for_swatch' => true,
166+
'used_in_product_listing' => true,
167+
'attribute_code' => 'code',
168+
],
169+
]);
170+
$this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')
171+
->with([1, 3])
172+
->willReturn([
173+
3 => ['type' => $expected['type'], 'value' => $expected['value']]
174+
]);
175+
$this->jsonEncoder->expects($this->once())->method('encode');
176+
$this->configurable->getJsonSwatchConfig();
177+
}
178+
179+
private function prepareGetJsonSwatchConfig()
180+
{
181+
$product1 = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
182+
$product1->expects($this->atLeastOnce())->method('isSaleable')->willReturn(true);
183+
$product1->expects($this->any())->method('getData')->with('code')->willReturn(1);
184+
185+
$product2 = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
186+
$product2->expects($this->atLeastOnce())->method('isSaleable')->willReturn(true);
187+
$product2->expects($this->any())->method('getData')->with('code')->willReturn(3);
188+
189+
$simpleProducts = [$product1, $product2];
190+
$configurableType = $this->getMock(
191+
'\Magento\ConfigurableProduct\Model\Product\Type\Configurable',
192+
[],
193+
[],
194+
'',
195+
false
196+
);
197+
$configurableType->expects($this->atLeastOnce())->method('getUsedProducts')->with($this->product, null)
198+
->willReturn($simpleProducts);
199+
$this->product->expects($this->any())->method('getTypeInstance')->willReturn($configurableType);
200+
201+
$productAttribute1 = $this->getMock('\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', [], [], '', false);
202+
$productAttribute1->expects($this->any())->method('getId')->willReturn(1);
203+
$productAttribute1->expects($this->any())->method('getAttributeCode')->willReturn('code');
204+
205+
$attribute1 = $this->getMock(
206+
'\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute',
207+
['getProductAttribute'],
208+
[],
209+
'',
210+
false
211+
);
212+
$attribute1->expects($this->any())->method('getProductAttribute')->willReturn($productAttribute1);
213+
214+
$this->helper->expects($this->any())->method('getAllowAttributes')->with($this->product)
215+
->willReturn([$attribute1]);
216+
}
217+
}

0 commit comments

Comments
 (0)