Skip to content

Commit 0d47bd9

Browse files
committed
MTA-3996: Create util for troubleshooting analysis with functional test execution
- merge
2 parents eb35a05 + 02e1c6b commit 0d47bd9

26 files changed

+1708
-167
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Pricing\Price;
8+
9+
use Magento\Framework\Pricing\SaleableInterface;
10+
use Magento\Framework\Pricing\Amount\AmountInterface;
11+
12+
/**
13+
* Interface define methods which control display of "As low as" price
14+
*/
15+
interface MinimalPriceCalculatorInterface
16+
{
17+
/**
18+
* Get raw value for "as low as" price
19+
*
20+
* @param SaleableInterface $saleableItem
21+
* @return float|null
22+
*/
23+
public function getValue(SaleableInterface $saleableItem);
24+
25+
/**
26+
* Return structured object with "as low as" value
27+
*
28+
* @param SaleableInterface $saleableItem
29+
* @return AmountInterface|null
30+
*/
31+
public function getAmount(SaleableInterface $saleableItem);
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Pricing\Price;
8+
9+
use Magento\Framework\Pricing\SaleableInterface;
10+
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
11+
use Magento\Framework\Pricing\Amount\AmountInterface;
12+
13+
/**
14+
* As Low As shows minimal value of Tier Prices
15+
*/
16+
class MinimalTierPriceCalculator implements MinimalPriceCalculatorInterface
17+
{
18+
/**
19+
* @var CalculatorInterface
20+
*/
21+
private $calculator;
22+
23+
/**
24+
* @param CalculatorInterface $calculator
25+
*/
26+
public function __construct(CalculatorInterface $calculator)
27+
{
28+
$this->calculator = $calculator;
29+
}
30+
31+
/**
32+
* Get raw value of "as low as" as a minimal among tier prices
33+
* {@inheritdoc}
34+
*/
35+
public function getValue(SaleableInterface $saleableItem)
36+
{
37+
/** @var TierPrice $price */
38+
$price = $saleableItem->getPriceInfo()->getPrice(TierPrice::PRICE_CODE);
39+
$tierPriceList = $price->getTierPriceList();
40+
41+
$tierPrices = [];
42+
foreach ($tierPriceList as $tierPrice) {
43+
/** @var AmountInterface $price */
44+
$price = $tierPrice['price'];
45+
$tierPrices[] = $price->getValue();
46+
}
47+
48+
return $tierPrices ? min($tierPrices) : null;
49+
}
50+
51+
/**
52+
* Return calculated amount object that keeps "as low as" value
53+
* {@inheritdoc}
54+
*/
55+
public function getAmount(SaleableInterface $saleableItem)
56+
{
57+
$value = $this->getValue($saleableItem);
58+
59+
return $value === null
60+
? null
61+
: $this->calculator->getAmount($value, $saleableItem);
62+
}
63+
}

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
namespace Magento\Catalog\Pricing\Render;
88

99
use Magento\Catalog\Pricing\Price;
10-
use Magento\Framework\Pricing\Render;
1110
use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
1211
use Magento\Msrp\Pricing\Price\MsrpPrice;
1312
use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
1413
use Magento\Framework\View\Element\Template\Context;
1514
use Magento\Framework\Pricing\SaleableInterface;
1615
use Magento\Framework\Pricing\Price\PriceInterface;
1716
use Magento\Framework\Pricing\Render\RendererPool;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;
1819

1920
/**
2021
* Class for final_price rendering
@@ -29,25 +30,33 @@ class FinalPriceBox extends BasePriceBox
2930
*/
3031
private $salableResolver;
3132

33+
/**
34+
* @var MinimalPriceCalculatorInterface
35+
*/
36+
private $minimalPriceCalculator;
37+
3238
/**
3339
* @param Context $context
3440
* @param SaleableInterface $saleableItem
3541
* @param PriceInterface $price
3642
* @param RendererPool $rendererPool
3743
* @param array $data
3844
* @param SalableResolverInterface $salableResolver
45+
* @param MinimalPriceCalculatorInterface $minimalPriceCalculator
3946
*/
4047
public function __construct(
4148
Context $context,
4249
SaleableInterface $saleableItem,
4350
PriceInterface $price,
4451
RendererPool $rendererPool,
4552
array $data = [],
46-
SalableResolverInterface $salableResolver = null
53+
SalableResolverInterface $salableResolver = null,
54+
MinimalPriceCalculatorInterface $minimalPriceCalculator = null
4755
) {
4856
parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
49-
$this->salableResolver = $salableResolver ?: \Magento\Framework\App\ObjectManager::getInstance()
50-
->get(SalableResolverInterface::class);
57+
$this->salableResolver = $salableResolver ?: ObjectManager::getInstance()->get(SalableResolverInterface::class);
58+
$this->minimalPriceCalculator = $minimalPriceCalculator
59+
?: ObjectManager::getInstance()->get(MinimalPriceCalculatorInterface::class);
5160
}
5261

5362
/**
@@ -117,11 +126,15 @@ protected function wrapResult($html)
117126
*/
118127
public function renderAmountMinimal()
119128
{
120-
/** @var \Magento\Catalog\Pricing\Price\FinalPrice $price */
121-
$price = $this->getPriceType(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE);
122129
$id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId();
130+
131+
$amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem());
132+
if ($amount === null) {
133+
return '';
134+
}
135+
123136
return $this->renderAmount(
124-
$price->getMinimalPrice(),
137+
$amount,
125138
[
126139
'display_label' => __('As low as'),
127140
'price_id' => $id,
@@ -150,13 +163,15 @@ public function hasSpecialPrice()
150163
*/
151164
public function showMinimalPrice()
152165
{
166+
$minTierPrice = $this->minimalPriceCalculator->getValue($this->getSaleableItem());
167+
153168
/** @var Price\FinalPrice $finalPrice */
154169
$finalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE);
155170
$finalPriceValue = $finalPrice->getAmount()->getValue();
156-
$minimalPriceAValue = $finalPrice->getMinimalPrice()->getValue();
171+
157172
return $this->getDisplayMinimalPrice()
158-
&& $minimalPriceAValue
159-
&& $minimalPriceAValue < $finalPriceValue;
173+
&& $minTierPrice !== null
174+
&& $minTierPrice < $finalPriceValue;
160175
}
161176

162177
/**
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Pricing\Price;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Catalog\Pricing\Price\MinimalTierPriceCalculator;
11+
use Magento\Framework\Pricing\SaleableInterface;
12+
use Magento\Framework\Pricing\PriceInfoInterface;
13+
use Magento\Catalog\Pricing\Price\TierPrice;
14+
use Magento\Framework\Pricing\Amount\AmountInterface;
15+
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
16+
17+
class MinimalTierPriceCalculatorTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var ObjectManager
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var MinimalTierPriceCalculator
26+
*/
27+
private $object;
28+
29+
/**
30+
* @var SaleableInterface|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $saleable;
33+
34+
/**
35+
* @var PriceInfoInterface|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $priceInfo;
38+
39+
/**
40+
* @var TierPrice|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $price;
43+
44+
/**
45+
* @var CalculatorInterface|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
private $calculator;
48+
49+
public function setUp()
50+
{
51+
$this->price = $this->getMock(TierPrice::class, [], [], '', false);
52+
$this->priceInfo = $this->getMockForAbstractClass(PriceInfoInterface::class);
53+
$this->saleable = $this->getMockForAbstractClass(SaleableInterface::class);
54+
55+
$this->objectManager = new ObjectManager($this);
56+
57+
$this->calculator = $this->getMockForAbstractClass(CalculatorInterface::class);
58+
$this->object = $this->objectManager->getObject(
59+
MinimalTierPriceCalculator::class,
60+
['calculator' => $this->calculator]
61+
);
62+
}
63+
64+
private function getValueTierPricesExistShouldReturnMinTierPrice()
65+
{
66+
$minPrice = 5;
67+
$notMinPrice = 10;
68+
69+
$minAmount = $this->getMockForAbstractClass(AmountInterface::class);
70+
$minAmount->expects($this->once())->method('getValue')->willReturn($minPrice);
71+
72+
$notMinAmount = $this->getMockForAbstractClass(AmountInterface::class);
73+
$notMinAmount->expects($this->once())->method('getValue')->willReturn($notMinPrice);
74+
75+
$tierPriceList = [
76+
[
77+
'price' => $minAmount
78+
],
79+
[
80+
'price' => $notMinAmount
81+
]
82+
];
83+
84+
$this->price->expects($this->once())->method('getTierPriceList')->willReturn($tierPriceList);
85+
86+
$this->priceInfo->expects($this->once())->method('getPrice')->with(TierPrice::PRICE_CODE)
87+
->willReturn($this->price);
88+
89+
$this->saleable->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfo);
90+
return $minPrice;
91+
}
92+
93+
public function testGetValueTierPricesExistShouldReturnMinTierPrice()
94+
{
95+
$minPrice = $this->getValueTierPricesExistShouldReturnMinTierPrice();
96+
$this->assertEquals($minPrice, $this->object->getValue($this->saleable));
97+
}
98+
99+
public function testGetGetAmountMinTierPriceExistShouldReturnAmountObject()
100+
{
101+
$minPrice = $this->getValueTierPricesExistShouldReturnMinTierPrice();
102+
103+
$amount = $this->getMockForAbstractClass(AmountInterface::class);
104+
105+
$this->calculator->expects($this->once())
106+
->method('getAmount')
107+
->with($minPrice, $this->saleable)
108+
->willReturn($amount);
109+
110+
$this->assertSame($amount, $this->object->getAmount($this->saleable));
111+
}
112+
}

0 commit comments

Comments
 (0)