Skip to content

Commit cbf2d4d

Browse files
committed
ACP2E-3255: [GRAPHQL] model value should be specified when getting customerCart
- Added the test coverage.
1 parent e866141 commit cbf2d4d

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Test\Unit\Plugin\Model;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\Quote;
14+
use Magento\Quote\Model\QuoteFactory;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
use Magento\Quote\Model\QuoteManagement;
19+
use Magento\QuoteGraphQl\Plugin\Model\CreateEmptyCartWithoutCountryValidation;
20+
use Magento\Store\Model\Store;
21+
use Magento\Customer\Api\Data\CustomerInterface as Customer;
22+
use PHPUnit\Framework\MockObject\Exception;
23+
use PHPUnit\Framework\TestCase;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
26+
class CreateEmptyCartWithoutCountryValidationTest extends TestCase
27+
{
28+
/**
29+
* @var MockObject|StoreManagerInterface
30+
*/
31+
private MockObject|StoreManagerInterface $storeManager;
32+
33+
/**
34+
* @var MockObject|CartRepositoryInterface
35+
*/
36+
private MockObject|CartRepositoryInterface $quoteRepository;
37+
38+
/**
39+
* @var MockObject|CustomerRepositoryInterface
40+
*/
41+
private MockObject|CustomerRepositoryInterface $customerRepository;
42+
43+
/**
44+
* @var MockObject|QuoteFactory
45+
*/
46+
private MockObject|QuoteFactory $quoteFactory;
47+
48+
/**
49+
* @var CreateEmptyCartWithoutCountryValidation
50+
*/
51+
private CreateEmptyCartWithoutCountryValidation $model;
52+
53+
/**
54+
* @var MockObject|QuoteManagement
55+
*/
56+
private MockObject|QuoteManagement $quoteManagement;
57+
58+
/**
59+
* @var MockObject|Store
60+
*/
61+
private MockObject|Store $store;
62+
63+
/**
64+
* @var MockObject|Quote
65+
*/
66+
private MockObject|Quote $quote;
67+
68+
/**
69+
* @throws Exception
70+
*/
71+
protected function setUp(): void
72+
{
73+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
74+
$this->quoteRepository = $this->createMock(CartRepositoryInterface::class);
75+
$this->customerRepository = $this->createMock(CustomerRepositoryInterface::class);
76+
$this->quoteFactory = $this->createMock(QuoteFactory::class);
77+
$this->quoteManagement = $this->createMock(QuoteManagement::class);
78+
$this->store = $this->getMockBuilder(Store::class)
79+
->addMethods(['getStoreId'])
80+
->disableOriginalConstructor()
81+
->getMock();
82+
$this->quote = $this->createMock(Quote::class);
83+
84+
$this->model = new CreateEmptyCartWithoutCountryValidation(
85+
$this->storeManager,
86+
$this->quoteRepository,
87+
$this->customerRepository,
88+
$this->quoteFactory
89+
);
90+
}
91+
92+
/**
93+
* @dataProvider aroundCreateEmptyCartForCustomerDataProvider
94+
* @throws NoSuchEntityException
95+
* @throws CouldNotSaveException
96+
* @throws Exception
97+
* @throws LocalizedException
98+
*/
99+
public function testAroundCreateEmptyCartForCustomerCreatesNewCart(
100+
int $storeId,
101+
int $customerId,
102+
object $callBack) {
103+
$expectedResult = 123;
104+
$this->storeManager->expects($this->once())
105+
->method('getStore')
106+
->willReturn($this->store);
107+
108+
$this->store->expects($this->once())
109+
->method('getStoreId')
110+
->willReturn($storeId);
111+
112+
$this->quoteRepository->expects($this->once())
113+
->method('getActiveForCustomer')
114+
->willThrowException(new NoSuchEntityException(__('No such entity')));
115+
116+
$this->customerRepository->expects($this->once())
117+
->method('getById')
118+
->with($customerId)
119+
->willReturn($this->createMock(Customer::class));
120+
121+
$this->quoteFactory->expects($this->once())
122+
->method('create')
123+
->willReturn($this->quote);
124+
125+
$this->quote->expects($this->once())
126+
->method('setStoreId')
127+
->with($storeId);
128+
$this->quote->expects($this->once())
129+
->method('setCustomer')
130+
->with($this->isInstanceOf(Customer::class));
131+
$this->quote->expects($this->once())
132+
->method('setCustomerIsGuest')
133+
->with(0);
134+
135+
$this->quoteRepository->expects($this->once())
136+
->method('save')
137+
->with($this->quote);
138+
139+
$this->quote->expects($this->once())
140+
->method('getId')
141+
->willReturn(123);
142+
143+
$result = $this->model->aroundCreateEmptyCartForCustomer(
144+
$this->quoteManagement,
145+
$callBack,
146+
$customerId
147+
);
148+
149+
$this->assertEquals($expectedResult, $result);
150+
}
151+
152+
/**
153+
* @dataProvider aroundCreateEmptyCartForCustomerDataProvider
154+
* @throws NoSuchEntityException
155+
* @throws Exception
156+
* @throws LocalizedException
157+
*/
158+
public function testAroundCreateEmptyCartForCustomerHandlesSaveException(
159+
int $storeId,
160+
int $customerId,
161+
object $callBack) {
162+
$this->storeManager->expects($this->once())
163+
->method('getStore')
164+
->willReturn($this->store);
165+
$this->store->expects($this->once())
166+
->method('getStoreId')
167+
->willReturn($storeId);
168+
169+
$this->quoteRepository->expects($this->once())
170+
->method('getActiveForCustomer')
171+
->willThrowException(new NoSuchEntityException(__('No such entity')));
172+
173+
$this->customerRepository->expects($this->once())
174+
->method('getById')
175+
->with($customerId)
176+
->willReturn($this->createMock(Customer::class));
177+
178+
$this->quoteFactory->expects($this->once())
179+
->method('create')
180+
->willReturn($this->quote);
181+
182+
$this->quote->expects($this->once())
183+
->method('setStoreId')
184+
->with($storeId);
185+
$this->quote->expects($this->once())
186+
->method('setCustomer')
187+
->with($this->isInstanceOf(Customer::class));
188+
$this->quote->expects($this->once())
189+
->method('setCustomerIsGuest')
190+
->with(0);
191+
192+
$this->quoteRepository->expects($this->once())
193+
->method('save')
194+
->willThrowException(new CouldNotSaveException(__('The quote can\'t be created.')));
195+
196+
$this->expectException(CouldNotSaveException::class);
197+
198+
$this->model->aroundCreateEmptyCartForCustomer(
199+
$this->quoteManagement,
200+
$callBack,
201+
$customerId
202+
);
203+
}
204+
205+
/**
206+
* @return array
207+
*/
208+
public static function aroundCreateEmptyCartForCustomerDataProvider(): array
209+
{
210+
return [
211+
[1, 1, function() {}]
212+
];
213+
}
214+
}

0 commit comments

Comments
 (0)