Skip to content

Commit bc95fa4

Browse files
authored
ENGCOM-8028: ChangeQuoteControl minor refactor #29669
2 parents 8488a5a + e31086a commit bc95fa4

File tree

2 files changed

+26
-71
lines changed

2 files changed

+26
-71
lines changed

app/code/Magento/Quote/Model/ChangeQuoteControl.php

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

98
namespace Magento\Quote\Model;
@@ -12,13 +11,10 @@
1211
use Magento\Quote\Api\ChangeQuoteControlInterface;
1312
use Magento\Quote\Api\Data\CartInterface;
1413

15-
/**
16-
* {@inheritdoc}
17-
*/
1814
class ChangeQuoteControl implements ChangeQuoteControlInterface
1915
{
2016
/**
21-
* @var UserContextInterface $userContext
17+
* @var UserContextInterface
2218
*/
2319
private $userContext;
2420

@@ -31,25 +27,20 @@ public function __construct(UserContextInterface $userContext)
3127
}
3228

3329
/**
34-
* {@inheritdoc}
30+
* @inheritdoc
3531
*/
3632
public function isAllowed(CartInterface $quote): bool
3733
{
3834
switch ($this->userContext->getUserType()) {
3935
case UserContextInterface::USER_TYPE_CUSTOMER:
40-
$isAllowed = ($quote->getCustomerId() == $this->userContext->getUserId());
41-
break;
36+
return ($quote->getCustomerId() == $this->userContext->getUserId());
4237
case UserContextInterface::USER_TYPE_GUEST:
43-
$isAllowed = ($quote->getCustomerId() === null);
44-
break;
38+
return ($quote->getCustomerId() === null);
4539
case UserContextInterface::USER_TYPE_ADMIN:
4640
case UserContextInterface::USER_TYPE_INTEGRATION:
47-
$isAllowed = true;
48-
break;
49-
default:
50-
$isAllowed = false;
41+
return true;
5142
}
5243

53-
return $isAllowed;
44+
return false;
5445
}
5546
}

app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,130 +16,94 @@
1616

1717
/**
1818
* Unit test for \Magento\Quote\Model\ChangeQuoteControl
19-
*
20-
* Class \Magento\Quote\Test\Unit\Model\ChangeQuoteControlTest
2119
*/
2220
class ChangeQuoteControlTest extends TestCase
2321
{
24-
/**
25-
* @var ObjectManager
26-
*/
27-
protected $objectManager;
28-
2922
/**
3023
* @var ChangeQuoteControl
3124
*/
3225
protected $model;
3326

3427
/**
35-
* @var MockObject
28+
* @var MockObject|UserContextInterface
3629
*/
3730
protected $userContextMock;
3831

3932
/**
40-
* @var MockObject
33+
* @var MockObject|CartInterface
4134
*/
4235
protected $quoteMock;
4336

4437
protected function setUp(): void
4538
{
46-
$this->objectManager = new ObjectManager($this);
4739
$this->userContextMock = $this->getMockForAbstractClass(UserContextInterface::class);
4840

49-
$this->model = $this->objectManager->getObject(
50-
ChangeQuoteControl::class,
51-
[
52-
'userContext' => $this->userContextMock
53-
]
54-
);
55-
56-
$this->quoteMock = $this->getMockForAbstractClass(
57-
CartInterface::class,
58-
[],
59-
'',
60-
false,
61-
true,
62-
true,
63-
['getCustomerId']
64-
);
41+
$this->model = new ChangeQuoteControl($this->userContextMock);
42+
43+
$this->quoteMock = $this->getMockBuilder(CartInterface::class)
44+
->disableOriginalConstructor()
45+
->addMethods(['getCustomerId'])
46+
->getMockForAbstractClass();
6547
}
6648

67-
/**
68-
* Test if the quote is belonged to customer
69-
*/
7049
public function testIsAllowedIfTheQuoteIsBelongedToCustomer()
7150
{
7251
$quoteCustomerId = 1;
73-
$this->quoteMock->expects($this->any())->method('getCustomerId')
52+
$this->quoteMock->method('getCustomerId')
7453
->willReturn($quoteCustomerId);
75-
$this->userContextMock->expects($this->any())->method('getUserType')
54+
$this->userContextMock->method('getUserType')
7655
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
77-
$this->userContextMock->expects($this->any())->method('getUserId')
56+
$this->userContextMock->method('getUserId')
7857
->willReturn($quoteCustomerId);
7958

8059
$this->assertTrue($this->model->isAllowed($this->quoteMock));
8160
}
8261

83-
/**
84-
* Test if the quote is not belonged to customer
85-
*/
8662
public function testIsAllowedIfTheQuoteIsNotBelongedToCustomer()
8763
{
8864
$currentCustomerId = 1;
8965
$quoteCustomerId = 2;
9066

91-
$this->quoteMock->expects($this->any())->method('getCustomerId')
67+
$this->quoteMock->method('getCustomerId')
9268
->willReturn($quoteCustomerId);
93-
$this->userContextMock->expects($this->any())->method('getUserType')
69+
$this->userContextMock->method('getUserType')
9470
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
95-
$this->userContextMock->expects($this->any())->method('getUserId')
71+
$this->userContextMock->method('getUserId')
9672
->willReturn($currentCustomerId);
9773

9874
$this->assertFalse($this->model->isAllowed($this->quoteMock));
9975
}
10076

101-
/**
102-
* Test if the quote is belonged to guest and the context is guest
103-
*/
10477
public function testIsAllowedIfQuoteIsBelongedToGuestAndContextIsGuest()
10578
{
10679
$quoteCustomerId = null;
107-
$this->quoteMock->expects($this->any())->method('getCustomerId')
80+
$this->quoteMock->method('getCustomerId')
10881
->willReturn($quoteCustomerId);
109-
$this->userContextMock->expects($this->any())->method('getUserType')
82+
$this->userContextMock->method('getUserType')
11083
->willReturn(UserContextInterface::USER_TYPE_GUEST);
11184
$this->assertTrue($this->model->isAllowed($this->quoteMock));
11285
}
11386

114-
/**
115-
* Test if the quote is belonged to customer and the context is guest
116-
*/
11787
public function testIsAllowedIfQuoteIsBelongedToCustomerAndContextIsGuest()
11888
{
11989
$quoteCustomerId = 1;
120-
$this->quoteMock->expects($this->any())->method('getCustomerId')
90+
$this->quoteMock->method('getCustomerId')
12191
->willReturn($quoteCustomerId);
122-
$this->userContextMock->expects($this->any())->method('getUserType')
92+
$this->userContextMock->method('getUserType')
12393
->willReturn(UserContextInterface::USER_TYPE_GUEST);
12494
$this->assertFalse($this->model->isAllowed($this->quoteMock));
12595
}
12696

127-
/**
128-
* Test if the context is admin
129-
*/
13097
public function testIsAllowedIfContextIsAdmin()
13198
{
132-
$this->userContextMock->expects($this->any())->method('getUserType')
99+
$this->userContextMock->method('getUserType')
133100
->willReturn(UserContextInterface::USER_TYPE_ADMIN);
134101
$this->assertTrue($this->model->isAllowed($this->quoteMock));
135102
}
136103

137-
/**
138-
* Test if the context is integration
139-
*/
140104
public function testIsAllowedIfContextIsIntegration()
141105
{
142-
$this->userContextMock->expects($this->any())->method('getUserType')
106+
$this->userContextMock->method('getUserType')
143107
->willReturn(UserContextInterface::USER_TYPE_INTEGRATION);
144108
$this->assertTrue($this->model->isAllowed($this->quoteMock));
145109
}

0 commit comments

Comments
 (0)