Skip to content

Commit 988c723

Browse files
ENGCOM-6179: Unit test to cover ChangeQuoteControl Class #25286
2 parents 317b77c + 83ea7cc commit 988c723

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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\Quote\Test\Unit\Model;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Authorization\Model\UserContextInterface;
12+
use Magento\Quote\Model\ChangeQuoteControl;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
15+
/**
16+
* Unit test for \Magento\Quote\Model\ChangeQuoteControl
17+
*
18+
* Class \Magento\Quote\Test\Unit\Model\ChangeQuoteControlTest
19+
*/
20+
class ChangeQuoteControlTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
24+
*/
25+
protected $objectManager;
26+
27+
/**
28+
* @var \Magento\Quote\Model\ChangeQuoteControl
29+
*/
30+
protected $model;
31+
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $userContextMock;
36+
37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $quoteMock;
41+
42+
protected function setUp()
43+
{
44+
$this->objectManager = new ObjectManager($this);
45+
$this->userContextMock = $this->createMock(UserContextInterface::class);
46+
47+
$this->model = $this->objectManager->getObject(
48+
ChangeQuoteControl::class,
49+
[
50+
'userContext' => $this->userContextMock
51+
]
52+
);
53+
54+
$this->quoteMock = $this->getMockForAbstractClass(
55+
CartInterface::class,
56+
[],
57+
'',
58+
false,
59+
true,
60+
true,
61+
['getCustomerId']
62+
);
63+
}
64+
65+
/**
66+
* Test if the quote is belonged to customer
67+
*/
68+
public function testIsAllowedIfTheQuoteIsBelongedToCustomer()
69+
{
70+
$quoteCustomerId = 1;
71+
$this->quoteMock->expects($this->any())->method('getCustomerId')
72+
->will($this->returnValue($quoteCustomerId));
73+
$this->userContextMock->expects($this->any())->method('getUserType')
74+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
75+
$this->userContextMock->expects($this->any())->method('getUserId')
76+
->will($this->returnValue($quoteCustomerId));
77+
78+
$this->assertEquals(true, $this->model->isAllowed($this->quoteMock));
79+
}
80+
81+
/**
82+
* Test if the quote is not belonged to customer
83+
*/
84+
public function testIsAllowedIfTheQuoteIsNotBelongedToCustomer()
85+
{
86+
$currentCustomerId = 1;
87+
$quoteCustomerId = 2;
88+
89+
$this->quoteMock->expects($this->any())->method('getCustomerId')
90+
->will($this->returnValue($quoteCustomerId));
91+
$this->userContextMock->expects($this->any())->method('getUserType')
92+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
93+
$this->userContextMock->expects($this->any())->method('getUserId')
94+
->will($this->returnValue($currentCustomerId));
95+
96+
$this->assertEquals(false, $this->model->isAllowed($this->quoteMock));
97+
}
98+
99+
/**
100+
* Test if the quote is belonged to guest and the context is guest
101+
*/
102+
public function testIsAllowedIfQuoteIsBelongedToGuestAndContextIsGuest()
103+
{
104+
$quoteCustomerId = null;
105+
$this->quoteMock->expects($this->any())->method('getCustomerId')
106+
->will($this->returnValue($quoteCustomerId));
107+
$this->userContextMock->expects($this->any())->method('getUserType')
108+
->will($this->returnValue(UserContextInterface::USER_TYPE_GUEST));
109+
$this->assertEquals(true, $this->model->isAllowed($this->quoteMock));
110+
}
111+
112+
/**
113+
* Test if the quote is belonged to customer and the context is guest
114+
*/
115+
public function testIsAllowedIfQuoteIsBelongedToCustomerAndContextIsGuest()
116+
{
117+
$quoteCustomerId = 1;
118+
$this->quoteMock->expects($this->any())->method('getCustomerId')
119+
->will($this->returnValue($quoteCustomerId));
120+
$this->userContextMock->expects($this->any())->method('getUserType')
121+
->will($this->returnValue(UserContextInterface::USER_TYPE_GUEST));
122+
$this->assertEquals(false, $this->model->isAllowed($this->quoteMock));
123+
}
124+
125+
/**
126+
* Test if the context is admin
127+
*/
128+
public function testIsAllowedIfContextIsAdmin()
129+
{
130+
$this->userContextMock->expects($this->any())->method('getUserType')
131+
->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN));
132+
$this->assertEquals(true, $this->model->isAllowed($this->quoteMock));
133+
}
134+
135+
/**
136+
* Test if the context is integration
137+
*/
138+
public function testIsAllowedIfContextIsIntegration()
139+
{
140+
$this->userContextMock->expects($this->any())->method('getUserType')
141+
->will($this->returnValue(UserContextInterface::USER_TYPE_INTEGRATION));
142+
$this->assertEquals(true, $this->model->isAllowed($this->quoteMock));
143+
}
144+
}

0 commit comments

Comments
 (0)