Skip to content

Commit 0ffd732

Browse files
author
Serhiy Shkolyarenko
committed
MAGETWO-32501: Implement Cart Service interfaces
added unit test for QuoteManagement
1 parent 047662b commit 0ffd732

File tree

1 file changed

+349
-1
lines changed

1 file changed

+349
-1
lines changed

dev/tests/unit/testsuite/Magento/Quote/Model/QuoteManagementTest.php

Lines changed: 349 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Quote\Model;
88

9+
use \Magento\Framework\Exception\NoSuchEntityException;
10+
911
class QuoteManagementTest extends \PHPUnit_Framework_TestCase
1012
{
1113
/**
@@ -63,6 +65,21 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase
6365
*/
6466
protected $customerManagement;
6567

68+
/**
69+
* @var \PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
protected $userContextMock;
72+
73+
/**
74+
* @var \PHPUnit_Framework_MockObject_MockObject
75+
*/
76+
protected $customerRepositoryMock;
77+
78+
/**
79+
* @var \PHPUnit_Framework_MockObject_MockObject
80+
*/
81+
protected $customerFactoryMock;
82+
6683
protected function setUp()
6784
{
6885
$objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
@@ -106,6 +123,22 @@ protected function setUp()
106123
$this->customerManagement = $this->getMock('Magento\Quote\Model\CustomerManagement', [], [], '', false);
107124
$this->quoteRepositoryMock = $this->getMock('\Magento\Quote\Model\QuoteRepository', [], [], '', false);
108125

126+
$this->userContextMock = $this->getMock('\Magento\Authorization\Model\UserContextInterface', [], [], '', false);
127+
$this->customerRepositoryMock = $this->getMock(
128+
'\Magento\Customer\Api\CustomerRepositoryInterface',
129+
['create', 'save', 'get', 'getById', 'getList', 'delete', 'deleteById'],
130+
[],
131+
'',
132+
false
133+
);
134+
$this->customerFactoryMock = $this->getMock(
135+
'\Magento\Customer\Model\CustomerFactory',
136+
['create'],
137+
[],
138+
'',
139+
false
140+
);
141+
109142
$this->model = $objectManager->getObject(
110143
'Magento\Quote\Model\QuoteManagement',
111144
[
@@ -118,11 +151,326 @@ protected function setUp()
118151
'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
119152
'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
120153
'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
121-
'quoteRepository' => $this->quoteRepositoryMock
154+
'quoteRepository' => $this->quoteRepositoryMock,
155+
'userContext' => $this->userContextMock,
156+
'customerRepository' => $this->customerRepositoryMock,
157+
'customerModelFactory' => $this->customerFactoryMock,
122158
]
123159
);
124160
}
125161

162+
public function testCreateEmptyCartAnonymous()
163+
{
164+
$storeId = 345;
165+
$quoteId = 2311;
166+
167+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
168+
169+
$this->userContextMock->expects($this->once())->method('getUserType')
170+
->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_GUEST);
171+
172+
$this->quoteRepositoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
173+
$quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
174+
175+
176+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
177+
$quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
178+
179+
$this->assertEquals($quoteId, $this->model->createEmptyCart($storeId));
180+
}
181+
182+
public function testCreateEmptyCartLoggedInUser()
183+
{
184+
$storeId = 345;
185+
$quoteId = 2311;
186+
$userId = 567;
187+
188+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
189+
190+
$this->userContextMock->expects($this->once())->method('getUserType')
191+
->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER);
192+
193+
$this->userContextMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($userId);
194+
195+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
196+
$this->customerRepositoryMock
197+
->expects($this->once())
198+
->method('getById')
199+
->with($userId)
200+
->willReturn($customerMock);
201+
202+
$this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer')->with($userId);
203+
204+
$this->quoteRepositoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
205+
$quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
206+
$quoteMock->expects($this->any())->method('setCustomer')->with($customerMock);
207+
$quoteMock->expects($this->any())->method('setCustomerIsGuest')->with(0);
208+
209+
210+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
211+
$quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
212+
213+
$this->assertEquals($quoteId, $this->model->createEmptyCart($storeId));
214+
}
215+
216+
/**
217+
* @expectedException \Magento\Framework\Exception\CouldNotSaveException
218+
*/
219+
public function testCreateEmptyCartLoggedInUserException()
220+
{
221+
$storeId = 345;
222+
$userId = 567;
223+
224+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
225+
226+
$this->userContextMock->expects($this->once())->method('getUserType')
227+
->willReturn(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER);
228+
229+
$this->userContextMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($userId);
230+
231+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
232+
$this->customerRepositoryMock
233+
->expects($this->once())
234+
->method('getById')
235+
->with($userId)
236+
->willReturn($customerMock);
237+
238+
$this->quoteRepositoryMock
239+
->expects($this->once())
240+
->method('getActiveForCustomer')
241+
->willThrowException(new NoSuchEntityException('123'));
242+
243+
$this->quoteRepositoryMock->expects($this->never())->method('create')->willReturn($quoteMock);
244+
245+
$this->quoteRepositoryMock->expects($this->never())->method('save')->with($quoteMock);
246+
247+
$this->model->createEmptyCart($storeId);
248+
}
249+
250+
/**
251+
* @expectedException \Magento\Framework\Exception\StateException
252+
* @expectedExceptionMessage Cannot assign customer to the given cart. The cart belongs to different store
253+
*/
254+
public function testAssignCustomerFromAnotherStore()
255+
{
256+
$cartId = 220;
257+
$customerId = 455;
258+
$storeId = 5;
259+
260+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
261+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
262+
263+
$this->quoteRepositoryMock
264+
->expects($this->once())
265+
->method('getActive')
266+
->with($cartId)
267+
->willReturn($quoteMock);
268+
269+
$this->customerRepositoryMock
270+
->expects($this->once())
271+
->method('getById')
272+
->with($customerId)
273+
->willReturn($customerMock);
274+
275+
$customerModelMock = $this->getMock(
276+
'\Magento\Customer\Model\Customer',
277+
['load', 'getSharedStoreIds'],
278+
[],
279+
'',
280+
false
281+
);
282+
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
283+
$customerModelMock
284+
->expects($this->once())
285+
->method('load')
286+
->with($customerId)
287+
->willReturnSelf();
288+
289+
$customerModelMock
290+
->expects($this->once())
291+
->method('getSharedStoreIds')
292+
->willReturn([]);
293+
294+
$this->model->assignCustomer($cartId, $customerId, $storeId);
295+
}
296+
297+
/**
298+
* @expectedException \Magento\Framework\Exception\StateException
299+
* @expectedExceptionMessage Cannot assign customer to the given cart. The cart is not anonymous.
300+
*/
301+
public function testAssignCustomerToNonanonymousCart()
302+
{
303+
$cartId = 220;
304+
$customerId = 455;
305+
$storeId = 5;
306+
307+
$quoteMock = $this->getMock(
308+
'\Magento\Quote\Model\Quote',
309+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest'],
310+
[],
311+
'',
312+
false
313+
);
314+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
315+
316+
$this->quoteRepositoryMock
317+
->expects($this->once())
318+
->method('getActive')
319+
->with($cartId)
320+
->willReturn($quoteMock);
321+
322+
$this->customerRepositoryMock
323+
->expects($this->once())
324+
->method('getById')
325+
->with($customerId)
326+
->willReturn($customerMock);
327+
328+
$customerModelMock = $this->getMock(
329+
'\Magento\Customer\Model\Customer',
330+
['load', 'getSharedStoreIds'],
331+
[],
332+
'',
333+
false
334+
);
335+
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
336+
$customerModelMock
337+
->expects($this->once())
338+
->method('load')
339+
->with($customerId)
340+
->willReturnSelf();
341+
342+
$customerModelMock
343+
->expects($this->once())
344+
->method('getSharedStoreIds')
345+
->willReturn([$storeId, 'some store value']);
346+
347+
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(753);
348+
349+
$this->model->assignCustomer($cartId, $customerId, $storeId);
350+
}
351+
352+
/**
353+
* @expectedException \Magento\Framework\Exception\StateException
354+
* @expectedExceptionMessage Cannot assign customer to the given cart. Customer already has active cart.
355+
*/
356+
public function testAssignCustomerNoSuchCustomer()
357+
{
358+
$cartId = 220;
359+
$customerId = 455;
360+
$storeId = 5;
361+
362+
$quoteMock = $this->getMock(
363+
'\Magento\Quote\Model\Quote',
364+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest'],
365+
[],
366+
'',
367+
false
368+
);
369+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
370+
371+
$this->quoteRepositoryMock
372+
->expects($this->once())
373+
->method('getActive')
374+
->with($cartId)
375+
->willReturn($quoteMock);
376+
377+
$this->customerRepositoryMock
378+
->expects($this->once())
379+
->method('getById')
380+
->with($customerId)
381+
->willReturn($customerMock);
382+
383+
$customerModelMock = $this->getMock(
384+
'\Magento\Customer\Model\Customer',
385+
['load', 'getSharedStoreIds'],
386+
[],
387+
'',
388+
false
389+
);
390+
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
391+
$customerModelMock
392+
->expects($this->once())
393+
->method('load')
394+
->with($customerId)
395+
->willReturnSelf();
396+
397+
$customerModelMock
398+
->expects($this->once())
399+
->method('getSharedStoreIds')
400+
->willReturn([$storeId, 'some store value']);
401+
402+
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
403+
404+
$this->quoteRepositoryMock
405+
->expects($this->once())
406+
->method('getForCustomer')
407+
->with($customerId)
408+
->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
409+
410+
$this->model->assignCustomer($cartId, $customerId, $storeId);
411+
}
412+
413+
public function testAssignCustomer()
414+
{
415+
$cartId = 220;
416+
$customerId = 455;
417+
$storeId = 5;
418+
419+
$quoteMock = $this->getMock(
420+
'\Magento\Quote\Model\Quote',
421+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest'],
422+
[],
423+
'',
424+
false
425+
);
426+
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface', [], [], '', false);
427+
428+
$this->quoteRepositoryMock
429+
->expects($this->once())
430+
->method('getActive')
431+
->with($cartId)
432+
->willReturn($quoteMock);
433+
434+
$this->customerRepositoryMock
435+
->expects($this->once())
436+
->method('getById')
437+
->with($customerId)
438+
->willReturn($customerMock);
439+
440+
$customerModelMock = $this->getMock(
441+
'\Magento\Customer\Model\Customer',
442+
['load', 'getSharedStoreIds'],
443+
[],
444+
'',
445+
false
446+
);
447+
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
448+
$customerModelMock
449+
->expects($this->once())
450+
->method('load')
451+
->with($customerId)
452+
->willReturnSelf();
453+
454+
$customerModelMock
455+
->expects($this->once())
456+
->method('getSharedStoreIds')
457+
->willReturn([$storeId, 'some store value']);
458+
459+
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
460+
461+
$this->quoteRepositoryMock
462+
->expects($this->once())
463+
->method('getForCustomer')
464+
->with($customerId);
465+
466+
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
467+
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
468+
469+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
470+
471+
$this->model->assignCustomer($cartId, $customerId, $storeId);
472+
}
473+
126474
public function testSubmit()
127475
{
128476
$orderData = [];

0 commit comments

Comments
 (0)