Skip to content

Commit 75028b8

Browse files
author
Stanislav Idolov
authored
MAGETWO-86332: Issues #10559 - Extend swatch using mixins (M2.2) #12929
2 parents 178fa2b + 10c9417 commit 75028b8

File tree

9 files changed

+328
-77
lines changed

9 files changed

+328
-77
lines changed

app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ protected function wrapResult($html)
115115
{
116116
return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
117117
'data-role="priceBox" ' .
118-
'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
118+
'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
119+
'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
119120
'>' . $html . '</div>';
120121
}
121122

app/code/Magento/Catalog/Test/Unit/Pricing/Render/FinalPriceBoxTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ public function testRenderMsrpEnabled()
246246

247247
//assert price wrapper
248248
$this->assertEquals(
249-
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="">test</div>',
249+
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="" ' .
250+
'data-price-box="product-id-">test</div>',
250251
$result
251252
);
252253
}

app/code/Magento/ConfigurableProduct/Block/Product/View/Type/Configurable.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
7474
*/
7575
private $customerSession;
7676

77+
/**
78+
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
79+
*/
80+
private $variationPrices;
81+
7782
/**
7883
* @param \Magento\Catalog\Block\Product\Context $context
7984
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
@@ -86,6 +91,7 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
8691
* @param array $data
8792
* @param Format|null $localeFormat
8893
* @param Session|null $customerSession
94+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices|null $variationPrices
8995
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9096
*/
9197
public function __construct(
@@ -99,7 +105,8 @@ public function __construct(
99105
ConfigurableAttributeData $configurableAttributeData,
100106
array $data = [],
101107
Format $localeFormat = null,
102-
Session $customerSession = null
108+
Session $customerSession = null,
109+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices $variationPrices = null
103110
) {
104111
$this->priceCurrency = $priceCurrency;
105112
$this->helper = $helper;
@@ -109,6 +116,9 @@ public function __construct(
109116
$this->configurableAttributeData = $configurableAttributeData;
110117
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class);
111118
$this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(Session::class);
119+
$this->variationPrices = $variationPrices ?: ObjectManager::getInstance()->get(
120+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
121+
);
112122

113123
parent::__construct(
114124
$context,
@@ -211,9 +221,6 @@ public function getJsonConfig()
211221
$store = $this->getCurrentStore();
212222
$currentProduct = $this->getProduct();
213223

214-
$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
215-
$finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');
216-
217224
$options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
218225
$attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);
219226

@@ -223,17 +230,7 @@ public function getJsonConfig()
223230
'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
224231
'optionPrices' => $this->getOptionPrices(),
225232
'priceFormat' => $this->localeFormat->getPriceFormat(),
226-
'prices' => [
227-
'oldPrice' => [
228-
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
229-
],
230-
'basePrice' => [
231-
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
232-
],
233-
'finalPrice' => [
234-
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
235-
],
236-
],
233+
'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
237234
'productId' => $currentProduct->getId(),
238235
'chooseText' => __('Choose an Option...'),
239236
'images' => $this->getOptionImages(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations;
8+
9+
/**
10+
* Configurable product variation prices.
11+
*/
12+
class Prices
13+
{
14+
/**
15+
* @var \Magento\Framework\Locale\Format
16+
*/
17+
private $localeFormat;
18+
19+
/**
20+
* Prices constructor.
21+
* @param \Magento\Framework\Locale\Format $localeFormat
22+
*/
23+
public function __construct(\Magento\Framework\Locale\Format $localeFormat)
24+
{
25+
$this->localeFormat = $localeFormat;
26+
}
27+
28+
/**
29+
* Get product prices for configurable variations
30+
*
31+
* @param \Magento\Framework\Pricing\PriceInfo\Base $priceInfo
32+
* @return array
33+
*/
34+
public function getFormattedPrices(\Magento\Framework\Pricing\PriceInfo\Base $priceInfo)
35+
{
36+
$regularPrice = $priceInfo->getPrice('regular_price');
37+
$finalPrice = $priceInfo->getPrice('final_price');
38+
39+
return [
40+
'oldPrice' => [
41+
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
42+
],
43+
'basePrice' => [
44+
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
45+
],
46+
'finalPrice' => [
47+
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
48+
],
49+
];
50+
}
51+
}

app/code/Magento/ConfigurableProduct/Test/Unit/Block/Product/View/Type/ConfigurableTest.php

+36-30
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
7373
*/
7474
private $customerSession;
7575

76+
/**
77+
* @var \PHPUnit_Framework_MockObject_MockObject
78+
*/
79+
private $variationPricesMock;
80+
7681
protected function setUp()
7782
{
7883
$this->mockContextObject();
@@ -136,6 +141,10 @@ protected function setUp()
136141
->disableOriginalConstructor()
137142
->getMock();
138143

144+
$this->variationPricesMock = $this->createMock(
145+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
146+
);
147+
139148
$this->block = new \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable(
140149
$this->context,
141150
$this->arrayUtils,
@@ -147,7 +156,8 @@ protected function setUp()
147156
$this->configurableAttributeData,
148157
[],
149158
$this->localeFormat,
150-
$this->customerSession
159+
$this->customerSession,
160+
$this->variationPricesMock
151161
);
152162
}
153163

@@ -249,12 +259,8 @@ public function testGetJsonConfig()
249259
'getAmount',
250260
])
251261
->getMockForAbstractClass();
252-
$priceMock->expects($this->any())
253-
->method('getAmount')
254-
->willReturn($amountMock);
255-
262+
$priceMock->expects($this->any())->method('getAmount')->willReturn($amountMock);
256263
$tierPriceMock = $this->getTierPriceMock($amountMock, $priceQty, $percentage);
257-
258264
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
259265
->disableOriginalConstructor()
260266
->getMock();
@@ -272,27 +278,16 @@ public function testGetJsonConfig()
272278
['tier_price', $tierPriceMock],
273279
]);
274280

275-
$productMock->expects($this->any())
276-
->method('getTypeInstance')
277-
->willReturn($productTypeMock);
278-
$productMock->expects($this->any())
279-
->method('getPriceInfo')
280-
->willReturn($priceInfoMock);
281-
$productMock->expects($this->any())
282-
->method('isSaleable')
283-
->willReturn(true);
284-
$productMock->expects($this->any())
285-
->method('getId')
286-
->willReturn($productId);
281+
$productMock->expects($this->any())->method('getTypeInstance')->willReturn($productTypeMock);
282+
$productMock->expects($this->any())->method('getPriceInfo')->willReturn($priceInfoMock);
283+
$productMock->expects($this->any())->method('isSaleable')->willReturn(true);
284+
$productMock->expects($this->any())->method('getId')->willReturn($productId);
287285

288286
$this->helper->expects($this->any())
289287
->method('getOptions')
290288
->with($productMock, [$productMock])
291289
->willReturn([]);
292-
293-
$this->product->expects($this->any())
294-
->method('getSkipSaleableCheck')
295-
->willReturn(true);
290+
$this->product->expects($this->any())->method('getSkipSaleableCheck')->willReturn(true);
296291

297292
$attributesData = [
298293
'attributes' => [],
@@ -304,9 +299,7 @@ public function testGetJsonConfig()
304299
->with($productMock, [])
305300
->willReturn($attributesData);
306301

307-
$this->localeFormat->expects($this->any())
308-
->method('getPriceFormat')
309-
->willReturn([]);
302+
$this->localeFormat->expects($this->atLeastOnce())->method('getPriceFormat')->willReturn([]);
310303
$this->localeFormat->expects($this->any())
311304
->method('getNumber')
312305
->willReturnMap([
@@ -315,16 +308,29 @@ public function testGetJsonConfig()
315308
[$percentage, $percentage],
316309
]);
317310

311+
$this->variationPricesMock->expects($this->once())
312+
->method('getFormattedPrices')
313+
->with($priceInfoMock)
314+
->willReturn(
315+
[
316+
'oldPrice' => [
317+
'amount' => $amount,
318+
],
319+
'basePrice' => [
320+
'amount' => $amount,
321+
],
322+
'finalPrice' => [
323+
'amount' => $amount,
324+
],
325+
]
326+
);
327+
318328
$expectedArray = $this->getExpectedArray($productId, $amount, $priceQty, $percentage);
319329
$expectedJson = json_encode($expectedArray);
320330

321-
$this->jsonEncoder->expects($this->once())
322-
->method('encode')
323-
->with($expectedArray)
324-
->willReturn($expectedJson);
331+
$this->jsonEncoder->expects($this->once())->method('encode')->with($expectedArray)->willReturn($expectedJson);
325332

326333
$this->block->setData('product', $productMock);
327-
328334
$result = $this->block->getJsonConfig();
329335
$this->assertEquals($expectedJson, $result);
330336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Type\Configurable\Variations;
8+
9+
use PHPUnit\Framework\TestCase;
10+
11+
class PricesTest extends TestCase
12+
{
13+
/**
14+
* @var \PHPUnit_Framework_MockObject_MockObject
15+
*/
16+
private $localeFormatMock;
17+
18+
/**
19+
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
20+
*/
21+
private $model;
22+
23+
protected function setUp()
24+
{
25+
$this->localeFormatMock = $this->createMock(\Magento\Framework\Locale\Format::class);
26+
$this->model = new \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices(
27+
$this->localeFormatMock
28+
);
29+
}
30+
31+
public function testGetFormattedPrices()
32+
{
33+
$expected = [
34+
'oldPrice' => [
35+
'amount' => 500
36+
],
37+
'basePrice' => [
38+
'amount' => 1000
39+
],
40+
'finalPrice' => [
41+
'amount' => 500
42+
]
43+
];
44+
$priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
45+
$priceMock = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
46+
$priceInfoMock->expects($this->atLeastOnce())->method('getPrice')->willReturn($priceMock);
47+
48+
$amountMock = $this->createMock(\Magento\Framework\Pricing\Amount\AmountInterface::class);
49+
$amountMock->expects($this->atLeastOnce())->method('getValue')->willReturn(500);
50+
$amountMock->expects($this->atLeastOnce())->method('getBaseAmount')->willReturn(1000);
51+
$priceMock->expects($this->atLeastOnce())->method('getAmount')->willReturn($amountMock);
52+
53+
$this->localeFormatMock->expects($this->atLeastOnce())
54+
->method('getNumber')
55+
->withConsecutive([500], [1000], [500])
56+
->will($this->onConsecutiveCalls(500, 1000, 500));
57+
58+
$this->assertEquals($expected, $this->model->getFormattedPrices($priceInfoMock));
59+
}
60+
}

0 commit comments

Comments
 (0)