Skip to content

Commit 5f0d6c1

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-691' into PR_L3_18_04_2022
2 parents 887f99d + 8458c7e commit 5f0d6c1

File tree

2 files changed

+174
-2
lines changed

2 files changed

+174
-2
lines changed

app/code/Magento/SalesRule/Model/Coupon/Usage/Processor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Processor
2323
private $ruleFactory;
2424

2525
/**
26-
* @var RuleFactory
26+
* @var CustomerFactory
2727
*/
2828
private $ruleCustomerFactory;
2929

@@ -132,6 +132,7 @@ private function updateRuleUsages(bool $isIncrement, int $ruleId): void
132132
* @param bool $isIncrement
133133
* @param int $ruleId
134134
* @param int $customerId
135+
* @throws \Exception
135136
*/
136137
private function updateCustomerRuleUsages(bool $isIncrement, int $ruleId, int $customerId): void
137138
{
@@ -144,6 +145,9 @@ private function updateCustomerRuleUsages(bool $isIncrement, int $ruleId, int $c
144145
} elseif ($isIncrement) {
145146
$ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
146147
}
147-
$ruleCustomer->save();
148+
149+
if ($ruleCustomer->hasData()) {
150+
$ruleCustomer->save();
151+
}
148152
}
149153
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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\Coupon\Usage;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\SalesRule\Model\Coupon;
12+
use Magento\SalesRule\Model\Coupon\Usage\Processor;
13+
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo;
14+
use Magento\SalesRule\Model\ResourceModel\Coupon\Usage;
15+
use Magento\SalesRule\Model\Rule;
16+
use Magento\SalesRule\Model\Rule\Customer;
17+
use Magento\SalesRule\Model\Rule\CustomerFactory;
18+
use Magento\SalesRule\Model\RuleFactory;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class ProcessorTest extends TestCase
22+
{
23+
/**
24+
* @var Processor
25+
*/
26+
private $processor;
27+
28+
/**
29+
* @var RuleFactory
30+
*/
31+
private $ruleFactoryMock;
32+
33+
/**
34+
* @var CustomerFactory
35+
*/
36+
private $ruleCustomerFactoryMock;
37+
38+
/**
39+
* @var Coupon
40+
*/
41+
private $couponMock;
42+
43+
/**
44+
* @var Usage
45+
*/
46+
private $couponUsageMock;
47+
48+
/**
49+
* @var UpdateInfo
50+
*/
51+
private $updateInfoMock;
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
protected function setUp(): void
57+
{
58+
$this->ruleFactoryMock = $this->getMockBuilder(RuleFactory::class)
59+
->disableOriginalConstructor()
60+
->getMock();
61+
62+
$this->ruleCustomerFactoryMock = $this->getMockBuilder(CustomerFactory::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
66+
$this->couponMock = $this->getMockBuilder(Coupon::class)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
70+
$this->couponUsageMock = $this->getMockBuilder(Usage::class)
71+
->disableOriginalConstructor()
72+
->getMock();
73+
74+
$this->updateInfoMock = $this->getMockBuilder(UpdateInfo::class)
75+
->disableOriginalConstructor()
76+
->getMock();
77+
78+
$this->processor = (new ObjectManager($this))->getObject(
79+
Processor::class,
80+
[
81+
'ruleFactory' => $this->ruleFactoryMock,
82+
'ruleCustomerFactory' => $this->ruleCustomerFactoryMock,
83+
'coupon' => $this->couponMock,
84+
'couponUsage' => $this->couponUsageMock
85+
]
86+
);
87+
}
88+
89+
/**
90+
* Test to update coupon usage
91+
*
92+
* @param $isIncrement
93+
* @param $timesUsed
94+
*
95+
* @return void
96+
* @dataProvider dataProvider
97+
*/
98+
public function testProcess($isIncrement, $timesUsed): void
99+
{
100+
$ruleId = 1;
101+
$customerId = 256;
102+
$couponId = 1;
103+
$couponCode = 'DISCOUNT-10';
104+
$setTimesUsed = $timesUsed + ($isIncrement ? 1 : -1);
105+
$ruleCustomerId = 13;
106+
107+
$this->updateInfoMock->expects($this->exactly(2))->method('getAppliedRuleIds')->willReturn([$couponId]);
108+
$this->updateInfoMock->expects($this->exactly(2))->method('getCouponCode')->willReturn($couponCode);
109+
$this->updateInfoMock->expects($this->exactly(3))->method('isIncrement')->willReturn($isIncrement);
110+
111+
$this->couponMock->expects($this->once())->method('load')->with($couponCode, 'code')
112+
->willReturnSelf();
113+
$this->couponMock->expects($this->exactly(2))->method('getId')->willReturn($couponId);
114+
$this->couponMock->expects($this->atLeastOnce())->method('getTimesUsed')->willReturn($timesUsed);
115+
$this->couponMock->expects($this->any())->method('setTimesUsed')->with($setTimesUsed)->willReturnSelf();
116+
$this->couponMock->expects($this->any())->method('save')->willReturnSelf();
117+
118+
$this->updateInfoMock->expects($this->exactly(3))->method('getCustomerId')->willReturn($customerId);
119+
120+
$this->couponUsageMock->expects($this->once())
121+
->method('updateCustomerCouponTimesUsed')
122+
->with($customerId, $couponId, $isIncrement)
123+
->willReturnSelf();
124+
125+
$customerRuleMock = $this->getMockBuilder(Customer::class)
126+
->disableOriginalConstructor()
127+
->onlyMethods(['loadByCustomerRule', 'getId', 'hasData', 'save'])
128+
->addMethods(['getTimesUsed', 'setTimesUsed', 'setCustomerId', 'setRuleId'])
129+
->getMock();
130+
$customerRuleMock->expects($this->once())->method('loadByCustomerRule')->with($customerId, $ruleId)
131+
->willReturnSelf();
132+
$customerRuleMock->expects($this->once())->method('getId')->willReturn($ruleCustomerId);
133+
$customerRuleMock->expects($this->any())->method('getTimesUsed')->willReturn($timesUsed);
134+
$customerRuleMock->expects($this->any())->method('setTimesUsed')->willReturn($setTimesUsed);
135+
$customerRuleMock->expects($this->any())->method('setCustomerId')->willReturn($customerId);
136+
$customerRuleMock->expects($this->any())->method('setRuleId')->willReturn($ruleId);
137+
$customerRuleMock->expects($this->once())->method('hasData')->willReturn(true);
138+
$customerRuleMock->expects($this->once())->method('save')->willReturnSelf();
139+
$this->ruleCustomerFactoryMock->expects($this->once())->method('create')->willReturn($customerRuleMock);
140+
141+
$ruleMock = $this->getMockBuilder(Rule::class)
142+
->onlyMethods(['load', 'getId', 'loadCouponCode', 'save'])
143+
->addMethods(['getTimesUsed', 'setTimesUsed'])
144+
->disableOriginalConstructor()
145+
->getMock();
146+
$ruleMock->expects($this->once())->method('load')->willReturnSelf();
147+
$ruleMock->expects($this->once())->method('getId')->willReturn(true);
148+
$ruleMock->expects($this->once())->method('loadCouponCode')->willReturnSelf();
149+
$ruleMock->expects($this->any())->method('getTimesUsed')->willReturn($timesUsed);
150+
$ruleMock->expects($this->any())->method('setTimesUsed')->willReturn($setTimesUsed);
151+
$this->ruleFactoryMock->expects($this->once())->method('create')->willReturn($ruleMock);
152+
153+
$this->processor->process($this->updateInfoMock);
154+
}
155+
156+
/**
157+
* @return array
158+
*/
159+
public function dataProvider(): array
160+
{
161+
return [
162+
[true, 1],
163+
[true, 0],
164+
[false, 1],
165+
[false, 0]
166+
];
167+
}
168+
}

0 commit comments

Comments
 (0)