Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ByPercentTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ByPercentTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $validator;
19 
24 
25  protected function setUp()
26  {
27  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28 
29  $this->validator = $this->getMockBuilder(
30  \Magento\SalesRule\Model\Validator::class
31  )->disableOriginalConstructor()->setMethods(
32  ['getItemPrice', 'getItemBasePrice', 'getItemOriginalPrice', 'getItemBaseOriginalPrice', '__wakeup']
33  )->getMock();
34 
35  $this->discountDataFactory = $this->getMockBuilder(
36  \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory::class
37  )->disableOriginalConstructor()->setMethods(
38  ['create']
39  )->getMock();
40 
41  $this->model = $helper->getObject(
42  \Magento\SalesRule\Model\Rule\Action\Discount\ByPercent::class,
43  ['discountDataFactory' => $this->discountDataFactory, 'validator' => $this->validator]
44  );
45  }
46 
57  public function testCalculate(
58  $qty,
59  $ruleData,
60  $itemData,
61  $validItemData,
62  $expectedRuleDiscountQty,
63  $expectedDiscountData
64  ) {
65  $discountData = $this->getMockBuilder(
66  \Magento\SalesRule\Model\Rule\Action\Discount\Data::class
67  )->disableOriginalConstructor()->setMethods(
68  ['setAmount', 'setBaseAmount', 'setOriginalAmount', 'setBaseOriginalAmount']
69  )->getMock();
70 
71  $this->discountDataFactory->expects($this->once())->method('create')->will($this->returnValue($discountData));
72 
73  $rule = $this->getMockBuilder(
74  \Magento\SalesRule\Model\Rule::class
75  )->disableOriginalConstructor()->setMethods(
76  ['getDiscountAmount', 'getDiscountQty', '__wakeup']
77  )->getMock();
78 
79  $item = $this->getMockBuilder(
80  \Magento\Quote\Model\Quote\Item\AbstractItem::class
81  )->disableOriginalConstructor()->setMethods(
82  [
83  'getDiscountAmount',
84  'getBaseDiscountAmount',
85  'getDiscountPercent',
86  'setDiscountPercent',
87  '__wakeup',
88  'getQuote',
89  'getAddress',
90  'getOptionByCode',
91  ]
92  )->getMock();
93 
94  $this->validator->expects(
95  $this->atLeastOnce()
96  )->method(
97  'getItemPrice'
98  )->with(
99  $item
100  )->will(
101  $this->returnValue($validItemData['price'])
102  );
103  $this->validator->expects(
104  $this->atLeastOnce()
105  )->method(
106  'getItemBasePrice'
107  )->with(
108  $item
109  )->will(
110  $this->returnValue($validItemData['basePrice'])
111  );
112  $this->validator->expects(
113  $this->atLeastOnce()
114  )->method(
115  'getItemOriginalPrice'
116  )->with(
117  $item
118  )->will(
119  $this->returnValue($validItemData['originalPrice'])
120  );
121  $this->validator->expects(
122  $this->atLeastOnce()
123  )->method(
124  'getItemBaseOriginalPrice'
125  )->with(
126  $item
127  )->will(
128  $this->returnValue($validItemData['baseOriginalPrice'])
129  );
130 
131  $rule->expects(
132  $this->atLeastOnce()
133  )->method(
134  'getDiscountAmount'
135  )->will(
136  $this->returnValue($ruleData['discountAmount'])
137  );
138  $rule->expects(
139  $this->atLeastOnce()
140  )->method(
141  'getDiscountQty'
142  )->will(
143  $this->returnValue($ruleData['discountQty'])
144  );
145 
146  $item->expects(
147  $this->atLeastOnce()
148  )->method(
149  'getDiscountAmount'
150  )->will(
151  $this->returnValue($itemData['discountAmount'])
152  );
153  $item->expects(
154  $this->atLeastOnce()
155  )->method(
156  'getBaseDiscountAmount'
157  )->will(
158  $this->returnValue($itemData['baseDiscountAmount'])
159  );
160  if (!$ruleData['discountQty'] || $ruleData['discountQty'] > $qty) {
161  $item->expects(
162  $this->atLeastOnce()
163  )->method(
164  'getDiscountPercent'
165  )->will(
166  $this->returnValue($itemData['discountPercent'])
167  );
168  $item->expects($this->atLeastOnce())->method('setDiscountPercent')->with($expectedRuleDiscountQty);
169  }
170 
171  $discountData->expects($this->once())->method('setAmount')->with($expectedDiscountData['amount']);
172  $discountData->expects($this->once())->method('setBaseAmount')->with($expectedDiscountData['baseAmount']);
173  $discountData->expects(
174  $this->once()
175  )->method(
176  'setOriginalAmount'
177  )->with(
178  $expectedDiscountData['originalAmount']
179  );
180  $discountData->expects(
181  $this->once()
182  )->method(
183  'setBaseOriginalAmount'
184  )->with(
185  $expectedDiscountData['baseOriginalAmount']
186  );
187 
188  $this->assertEquals($discountData, $this->model->calculate($rule, $item, $qty));
189  }
190 
194  public function calculateDataProvider()
195  {
196  return [
197  [
198  'qty' => 3,
199  'ruleData' => ['discountAmount' => 30, 'discountQty' => 5],
200  'itemData' => ['discountAmount' => 10, 'baseDiscountAmount' => 50, 'discountPercent' => 55],
201  'validItemData' => [
202  'price' => 50,
203  'basePrice' => 45,
204  'originalPrice' => 60,
205  'baseOriginalPrice' => 55,
206  ],
207  'expectedRuleDiscountQty' => 85,
208  'expectedDiscountData' => [
209  'amount' => 42,
210  'baseAmount' => 25.5,
211  'originalAmount' => 51,
212  'baseOriginalAmount' => 34.5,
213  ],
214  ]
215  ];
216  }
217 
224  public function testFixQuantity($step, $qty, $expected)
225  {
226  $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['getDiscountStep', '__wakeup']);
227  $rule->expects($this->once())->method('getDiscountStep')->will($this->returnValue($step));
228 
229  $this->assertEquals($expected, $this->model->fixQuantity($qty, $rule));
230  }
231 
235  public function fixQuantityDataProvider()
236  {
237  return [
238  ['step' => 0, 'qty' => 23, 'expected' => 23],
239  ['step' => 10, 'qty' => 23.5, 'expected' => 20],
240  ['step' => 20, 'qty' => 33, 'expected' => 20],
241  ['step' => 25, 'qty' => 23, 'expected' => 0]
242  ];
243  }
244 }
$helper
Definition: iframe.phtml:13
testCalculate( $qty, $ruleData, $itemData, $validItemData, $expectedRuleDiscountQty, $expectedDiscountData)
$ruleData
Definition: tax_rule.php:26