Skip to content

Commit 3c4b977

Browse files
authored
ENGCOM-8027: Minor refactoring to AccessChangeQuoteControl and it's Unit Tests #29670
2 parents bc95fa4 + 5374385 commit 3c4b977

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

app/code/Magento/Quote/Model/QuoteRepository/Plugin/AccessChangeQuoteControl.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Quote\Model\QuoteRepository\Plugin;
89

@@ -32,16 +33,17 @@ public function __construct(ChangeQuoteControlInterface $changeQuoteControl)
3233
/**
3334
* Checks if change quote's customer id is allowed for current user.
3435
*
36+
* A StateException is thrown if Guest's or Customer's customer_id not match user_id or unknown user type
37+
*
3538
* @param CartRepositoryInterface $subject
3639
* @param CartInterface $quote
37-
* @throws StateException if Guest has customer_id or Customer's customer_id not much with user_id
38-
* or unknown user's type
3940
* @return void
41+
* @throws StateException
4042
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4143
*/
42-
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote)
44+
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote): void
4345
{
44-
if (! $this->changeQuoteControl->isAllowed($quote)) {
46+
if (!$this->changeQuoteControl->isAllowed($quote)) {
4547
throw new StateException(__("Invalid state change requested"));
4648
}
4749
}

app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AccessChangeQuoteControlTest.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Quote\Test\Unit\Model\QuoteRepository\Plugin;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\Exception\StateException;
1112
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1213
use Magento\Quote\Model\ChangeQuoteControl;
1314
use Magento\Quote\Model\Quote;
@@ -52,7 +53,7 @@ protected function setUp(): void
5253

5354
$this->quoteMock = $this->getMockBuilder(Quote::class)
5455
->disableOriginalConstructor()
55-
->setMethods(['getCustomerId'])
56+
->addMethods(['getCustomerId'])
5657
->getMock();
5758

5859
$this->quoteRepositoryMock = $this->getMockBuilder(QuoteRepository::class)
@@ -63,17 +64,10 @@ protected function setUp(): void
6364
->disableOriginalConstructor()
6465
->getMock();
6566

66-
$objectManagerHelper = new ObjectManager($this);
67-
$this->accessChangeQuoteControl = $objectManagerHelper->getObject(
68-
AccessChangeQuoteControl::class,
69-
['changeQuoteControl' => $this->changeQuoteControlMock]
70-
);
67+
$this->accessChangeQuoteControl = new AccessChangeQuoteControl($this->changeQuoteControlMock);
7168
}
7269

73-
/**
74-
* User with role Customer and customer_id matches context user_id.
75-
*/
76-
public function testBeforeSaveForCustomer()
70+
public function testBeforeSaveForCustomerWithCustomerIdMatchinQuoteUserIdIsAllowed()
7771
{
7872
$this->quoteMock->method('getCustomerId')
7973
->willReturn(1);
@@ -84,17 +78,12 @@ public function testBeforeSaveForCustomer()
8478
$this->changeQuoteControlMock->method('isAllowed')
8579
->willReturn(true);
8680

87-
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
88-
89-
$this->assertNull($result);
81+
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
9082
}
9183

92-
/**
93-
* The user_id and customer_id from the quote are different.
94-
*/
95-
public function testBeforeSaveException()
84+
public function testBeforeSaveThrowsExceptionForCustomerWithCustomerIdNotMatchingQuoteUserId()
9685
{
97-
$this->expectException('Magento\Framework\Exception\StateException');
86+
$this->expectException(StateException::class);
9887
$this->expectExceptionMessage('Invalid state change requested');
9988
$this->quoteMock->method('getCustomerId')
10089
->willReturn(2);
@@ -108,10 +97,7 @@ public function testBeforeSaveException()
10897
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
10998
}
11099

111-
/**
112-
* User with role Admin and customer_id not much with user_id.
113-
*/
114-
public function testBeforeSaveForAdmin()
100+
public function testBeforeSaveForAdminUserRoleIsAllowed()
115101
{
116102
$this->quoteMock->method('getCustomerId')
117103
->willReturn(2);
@@ -122,15 +108,10 @@ public function testBeforeSaveForAdmin()
122108
$this->changeQuoteControlMock->method('isAllowed')
123109
->willReturn(true);
124110

125-
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
126-
127-
$this->assertNull($result);
111+
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
128112
}
129113

130-
/**
131-
* User with role Guest and customer_id === null.
132-
*/
133-
public function testBeforeSaveForGuest()
114+
public function testBeforeSaveForGuestIsAllowed()
134115
{
135116
$this->quoteMock->method('getCustomerId')
136117
->willReturn(null);
@@ -141,17 +122,12 @@ public function testBeforeSaveForGuest()
141122
$this->changeQuoteControlMock->method('isAllowed')
142123
->willReturn(true);
143124

144-
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
145-
146-
$this->assertNull($result);
125+
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
147126
}
148127

149-
/**
150-
* User with role Guest and customer_id !== null.
151-
*/
152-
public function testBeforeSaveForGuestException()
128+
public function testBeforeSaveThrowsExceptionForGuestDoesNotEquals()
153129
{
154-
$this->expectException('Magento\Framework\Exception\StateException');
130+
$this->expectException(StateException::class);
155131
$this->expectExceptionMessage('Invalid state change requested');
156132
$this->quoteMock->method('getCustomerId')
157133
->willReturn(1);
@@ -165,12 +141,9 @@ public function testBeforeSaveForGuestException()
165141
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
166142
}
167143

168-
/**
169-
* User with unknown role.
170-
*/
171-
public function testBeforeSaveForUnknownUserTypeException()
144+
public function testBeforeSaveThrowsExceptionForUnknownUserType()
172145
{
173-
$this->expectException('Magento\Framework\Exception\StateException');
146+
$this->expectException(StateException::class);
174147
$this->expectExceptionMessage('Invalid state change requested');
175148
$this->quoteMock->method('getCustomerId')
176149
->willReturn(2);

0 commit comments

Comments
 (0)