Skip to content

Commit 70d4fdb

Browse files
committed
ACP2E-793: Discount value is incorrect in GraphQL response
1 parent 7b98ea7 commit 70d4fdb

File tree

1 file changed

+270
-0
lines changed
  • app/code/Magento/SalesRule/Test/Unit/Model/Rule/Action/Discount

1 file changed

+270
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Test\Unit\Model\Rule\Action\Discount;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\Quote\Item\AbstractItem;
13+
use Magento\SalesRule\Model\Rule;
14+
use Magento\SalesRule\Model\Rule\Action\Discount\ByFixed;
15+
use Magento\SalesRule\Model\Rule\Action\Discount\Data;
16+
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
17+
use Magento\SalesRule\Model\Validator;
18+
use Magento\Framework\Pricing\PriceCurrencyInterface;
19+
use Magento\Store\Model\Store;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class ByFixedTest extends TestCase
24+
{
25+
/**
26+
* @var ByFixed
27+
*/
28+
protected $model;
29+
30+
/**
31+
* @var Validator|MockObject
32+
*/
33+
protected $validator;
34+
35+
/**
36+
* @var PriceCurrencyInterface|MockObject
37+
*/
38+
protected $priceCurrency;
39+
40+
/**
41+
* @var DataFactory|MockObject
42+
*/
43+
protected $discountDataFactory;
44+
45+
protected function setUp(): void
46+
{
47+
$helper = new ObjectManager($this);
48+
49+
$this->validator = $this->getMockBuilder(
50+
Validator::class
51+
)->disableOriginalConstructor()
52+
->setMethods(
53+
['getItemPrice', 'getItemBasePrice', 'getItemOriginalPrice', 'getItemBaseOriginalPrice']
54+
)->getMock();
55+
56+
$this->priceCurrency = $this->getMockBuilder(PriceCurrencyInterface::class)
57+
->getMockForAbstractClass();
58+
59+
$this->discountDataFactory = $this->getMockBuilder(
60+
DataFactory::class
61+
)->disableOriginalConstructor()
62+
->setMethods(
63+
['create']
64+
)->getMock();
65+
66+
$this->model = $helper->getObject(
67+
ByFixed::class,
68+
['discountDataFactory' => $this->discountDataFactory, 'validator' => $this->validator, 'priceCurrency' => $this->priceCurrency]
69+
);
70+
}
71+
72+
/**
73+
* @param $qty
74+
* @param $ruleData
75+
* @param $itemData
76+
* @param $validItemData
77+
* @param $expectedDiscountData
78+
* @dataProvider calculateDataProvider
79+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
80+
*/
81+
public function testCalculate(
82+
$qty,
83+
$ruleData,
84+
$itemData,
85+
$validItemData,
86+
$expectedDiscountData
87+
) {
88+
$discountData = $this->getMockBuilder(
89+
Data::class
90+
)->disableOriginalConstructor()
91+
->setMethods(
92+
['setAmount', 'setBaseAmount', 'setOriginalAmount', 'setBaseOriginalAmount']
93+
)->getMock();
94+
95+
$this->discountDataFactory->expects($this->once())->method('create')->willReturn($discountData);
96+
97+
$rule = $this->getMockBuilder(
98+
Rule::class
99+
)->disableOriginalConstructor()
100+
->setMethods(
101+
['getDiscountAmount']
102+
)->getMock();
103+
104+
$quote = $this->getMockBuilder(Quote::class)
105+
->onlyMethods(['getStore'])
106+
->disableOriginalConstructor()
107+
->getMock();
108+
$store = $this->createMock(Store::class);
109+
$quote->expects($this->any())->method('getStore')->will($this->returnValue($store));
110+
111+
$item = $this->getMockBuilder(
112+
AbstractItem::class
113+
)->disableOriginalConstructor()
114+
->setMethods(
115+
[
116+
'getDiscountAmount',
117+
'getBaseDiscountAmount',
118+
'getQuote',
119+
'getAddress',
120+
'getOptionByCode',
121+
]
122+
)->getMock();
123+
124+
$this->validator->expects(
125+
$this->atLeastOnce()
126+
)->method(
127+
'getItemPrice'
128+
)->with(
129+
$item
130+
)->willReturn(
131+
$validItemData['price']
132+
);
133+
$this->validator->expects(
134+
$this->any()
135+
)->method(
136+
'getItemBasePrice'
137+
)->with(
138+
$item
139+
)->willReturn(
140+
$validItemData['basePrice']
141+
);
142+
$this->validator->expects(
143+
$this->any()
144+
)->method(
145+
'getItemOriginalPrice'
146+
)->with(
147+
$item
148+
)->willReturn(
149+
$validItemData['originalPrice']
150+
);
151+
$this->validator->expects(
152+
$this->any()
153+
)->method(
154+
'getItemBaseOriginalPrice'
155+
)->with(
156+
$item
157+
)->willReturn(
158+
$validItemData['baseOriginalPrice']
159+
);
160+
161+
$this->priceCurrency->expects(
162+
$this->any()
163+
)->method(
164+
'convert'
165+
)->with(
166+
$ruleData['discountAmount'], $store
167+
)->willReturn(
168+
$validItemData['baseOriginalPrice']
169+
);
170+
171+
$rule->expects(
172+
$this->atLeastOnce()
173+
)->method(
174+
'getDiscountAmount'
175+
)->willReturn(
176+
$ruleData['discountAmount']
177+
);
178+
179+
$item->expects(
180+
$this->atLeastOnce()
181+
)->method(
182+
'getDiscountAmount'
183+
)->willReturn(
184+
$itemData['discountAmount']
185+
);
186+
$item->expects(
187+
$this->atLeastOnce()
188+
)->method(
189+
'getBaseDiscountAmount'
190+
)->willReturn(
191+
$itemData['baseDiscountAmount']
192+
);
193+
$item->expects($this->atLeastOnce())->method('getQuote')->willReturn($quote);
194+
195+
$discountData->expects($this->once())->method('setAmount')->with($expectedDiscountData['amount']);
196+
$discountData->expects($this->once())->method('setBaseAmount')->with($expectedDiscountData['baseAmount']);
197+
$discountData->expects(
198+
$this->any()
199+
)->method(
200+
'setOriginalAmount'
201+
)->with(
202+
$expectedDiscountData['originalAmount']
203+
);
204+
$discountData->expects(
205+
$this->any()
206+
)->method(
207+
'setBaseOriginalAmount'
208+
)->with(
209+
$expectedDiscountData['baseOriginalAmount']
210+
);
211+
212+
$this->assertEquals($discountData, $this->model->calculate($rule, $item, $qty));
213+
}
214+
215+
/**
216+
* @return array
217+
*/
218+
public function calculateDataProvider()
219+
{
220+
return [
221+
[
222+
'qty' => 2,
223+
'ruleData' => ['discountAmount' => 100],
224+
'itemData' => ['discountAmount' => 139, 'baseDiscountAmount' => 139],
225+
'validItemData' => [
226+
'price' => 139,
227+
'basePrice' => 139,
228+
'originalPrice' => 139,
229+
'baseOriginalPrice' => 139,
230+
],
231+
'expectedDiscountData' => [
232+
'amount' => 139,
233+
'baseAmount' => 139,
234+
'originalAmount' => 0,
235+
'baseOriginalAmount' => 0,
236+
],
237+
]
238+
];
239+
}
240+
241+
/**
242+
* @param int $step
243+
* @param int|float $qty
244+
* @param int $expected
245+
* @dataProvider fixQuantityDataProvider
246+
*/
247+
public function testFixQuantity($step, $qty, $expected)
248+
{
249+
$rule = $this->getMockBuilder(Rule::class)
250+
->addMethods(['getDiscountStep'])
251+
->disableOriginalConstructor()
252+
->getMock();
253+
$rule->expects($this->once())->method('getDiscountStep')->willReturn($step);
254+
255+
$this->assertEquals($expected, $this->model->fixQuantity($qty, $rule));
256+
}
257+
258+
/**
259+
* @return array
260+
*/
261+
public function fixQuantityDataProvider()
262+
{
263+
return [
264+
['step' => 0, 'qty' => 23, 'expected' => 23],
265+
['step' => 10, 'qty' => 23.5, 'expected' => 20],
266+
['step' => 20, 'qty' => 33, 'expected' => 20],
267+
['step' => 25, 'qty' => 23, 'expected' => 0]
268+
];
269+
}
270+
}

0 commit comments

Comments
 (0)