Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RulesApplierTest.php
Go to the documentation of this file.
1 <?php
8 
12 class RulesApplierTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $rulesApplier;
18 
22  protected $calculatorFactory;
23 
27  protected $eventManager;
28 
32  protected $validatorUtility;
33 
38 
39  protected function setUp()
40  {
41  $this->calculatorFactory = $this->createMock(
42  \Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory::class
43  );
44  $this->eventManager = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
45  $this->validatorUtility = $this->createPartialMock(
46  \Magento\SalesRule\Model\Utility::class,
47  ['canProcessRule', 'minFix', 'deltaRoundingFix', 'getItemQty']
48  );
49  $this->childrenValidationLocator = $this->createPartialMock(
50  \Magento\SalesRule\Model\Quote\ChildrenValidationLocator::class,
51  ['isChildrenValidationRequired']
52  );
53  $this->rulesApplier = new \Magento\SalesRule\Model\RulesApplier(
54  $this->calculatorFactory,
55  $this->eventManager,
56  $this->validatorUtility,
57  $this->childrenValidationLocator
58  );
59  }
60 
67  public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren, $isContinue)
68  {
69  $positivePrice = 1;
70  $skipValidation = false;
71  $item = $this->getPreparedItem();
72  $couponCode = 111;
73 
74  $ruleId = 1;
75  $appliedRuleIds = [$ruleId => $ruleId];
76 
80  $ruleWithStopFurtherProcessing = $this->createPartialMock(
81  \Magento\SalesRule\Model\Rule::class,
82  ['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions']
83  );
85  $ruleThatShouldNotBeRun = $this->createPartialMock(
86  \Magento\SalesRule\Model\Rule::class,
87  ['getStopRulesProcessing', '__wakeup']
88  );
89 
90  $actionMock = $this->createPartialMock(\Magento\Rule\Model\Action\Collection::class, ['validate']);
91 
92  $ruleWithStopFurtherProcessing->setName('ruleWithStopFurtherProcessing');
93  $ruleThatShouldNotBeRun->setName('ruleThatShouldNotBeRun');
94  $rules = [$ruleWithStopFurtherProcessing, $ruleThatShouldNotBeRun];
95 
96  $item->setDiscountCalculationPrice($positivePrice);
97  $item->setData('calculation_price', $positivePrice);
98 
99  $this->childrenValidationLocator->expects($this->any())
100  ->method('isChildrenValidationRequired')
101  ->willReturn(true);
102 
103  $this->validatorUtility->expects($this->atLeastOnce())
104  ->method('canProcessRule')
105  ->will($this->returnValue(true));
106 
107  $ruleWithStopFurtherProcessing->expects($this->atLeastOnce())
108  ->method('getActions')
109  ->willReturn($actionMock);
110  $actionMock->expects($this->at(0))
111  ->method('validate')
112  ->with($item)
113  ->willReturn(!$isChildren);
114 
115  // if there are child elements, check them
116  if ($isChildren) {
117  $item->expects($this->atLeastOnce())
118  ->method('getChildren')
119  ->willReturn([$item]);
120  $actionMock->expects($this->at(1))
121  ->method('validate')
122  ->with($item)
123  ->willReturn(!$isContinue);
124  }
125 
126  //
127  if (!$isContinue || !$isChildren) {
128  $ruleWithStopFurtherProcessing->expects($this->any())
129  ->method('getRuleId')
130  ->will($this->returnValue($ruleId));
131 
132  $this->applyRule($item, $ruleWithStopFurtherProcessing);
133 
134  $ruleWithStopFurtherProcessing->setStopRulesProcessing(true);
135  $ruleThatShouldNotBeRun->expects($this->never())
136  ->method('getStopRulesProcessing');
137  }
138 
139  $result = $this->rulesApplier->applyRules($item, $rules, $skipValidation, $couponCode);
140  $this->assertEquals($appliedRuleIds, $result);
141  }
142 
146  public function dataProviderChildren()
147  {
148  return [
149  ['isChildren' => true, 'isContinue' => false],
150  ['isChildren' => false, 'isContinue' => true],
151  ];
152  }
153 
157  protected function getPreparedItem()
158  {
160  $address = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
161  'getQuote',
162  'setCouponCode',
163  'setAppliedRuleIds',
164  '__wakeup'
165  ]);
167  $item = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, [
168  'setDiscountAmount',
169  'setBaseDiscountAmount',
170  'setDiscountPercent',
171  'getAddress',
172  'setAppliedRuleIds',
173  '__wakeup',
174  'getChildren'
175  ]);
176  $quote = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getStore', '__wakeUp']);
177  $item->expects($this->any())->method('getAddress')->will($this->returnValue($address));
178  $address->expects($this->any())
179  ->method('getQuote')
180  ->will($this->returnValue($quote));
181 
182  return $item;
183  }
184 
189  protected function applyRule($item, $rule)
190  {
191  $qty = 2;
192  $discountCalc = $this->createPartialMock(
193  \Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface::class,
194  ['fixQuantity', 'calculate']
195  );
196  $discountData = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class)
197  ->setConstructorArgs(
198  [
199  'amount' => 30,
200  'baseAmount' => 30,
201  'originalAmount' => 30,
202  'baseOriginalAmount' => 30
203  ]
204  )
205  ->getMock();
206  $this->validatorUtility->expects($this->any())
207  ->method('getItemQty')
208  ->with($this->anything(), $this->anything())
209  ->will($this->returnValue($qty));
210  $discountCalc->expects($this->any())
211  ->method('fixQuantity')
212  ->with($this->equalTo($qty), $this->equalTo($rule))
213  ->will($this->returnValue($qty));
214 
215  $discountCalc->expects($this->any())
216  ->method('calculate')
217  ->with($this->equalTo($rule), $this->equalTo($item), $this->equalTo($qty))
218  ->will($this->returnValue($discountData));
219  $this->calculatorFactory->expects($this->any())
220  ->method('create')
221  ->with($this->anything())
222  ->will($this->returnValue($discountCalc));
223  }
224 }
$quote
$address
Definition: customer.php:38