Skip to content

Commit d8b75a0

Browse files
committed
MAGETWO-36378: Magento\Quote\Api\GuestCartManagement
- Use composition instead of inheritance fro GuestCartManagement - Add unit tests for GuestCartManagement
1 parent 9eed4e3 commit d8b75a0

File tree

2 files changed

+151
-61
lines changed

2 files changed

+151
-61
lines changed

app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@
66

77
namespace Magento\Quote\Model\GuestCart;
88

9-
use Magento\Authorization\Model\UserContextInterface;
10-
use Magento\Framework\Event\ManagerInterface as EventManager;
119
use Magento\Quote\Api\GuestCartManagementInterface;
12-
use Magento\Quote\Model\CustomerManagement;
13-
use Magento\Quote\Model\Quote as QuoteEntity;
14-
use Magento\Quote\Model\Quote\Address\ToOrder as ToOrderConverter;
15-
use Magento\Quote\Model\Quote\Address\ToOrderAddress as ToOrderAddressConverter;
16-
use Magento\Quote\Model\Quote\Item\ToOrderItem as ToOrderItemConverter;
17-
use Magento\Quote\Model\Quote\Payment\ToOrderPayment as ToOrderPaymentConverter;
10+
use Magento\Quote\Api\CartManagementInterface;
1811
use Magento\Quote\Model\QuoteIdMask;
1912
use Magento\Quote\Model\QuoteIdMaskFactory;
2013
use Magento\Quote\Model\QuoteManagement;
2114
use Magento\Quote\Model\QuoteRepository;
22-
use Magento\Quote\Model\QuoteValidator;
23-
use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
24-
use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
25-
use Magento\Store\Model\StoreManagerInterface;
2615

2716
/**
2817
* Cart Management class for guest carts.
2918
*
3019
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3120
*/
32-
class GuestCartManagement extends QuoteManagement implements GuestCartManagementInterface
21+
class GuestCartManagement implements GuestCartManagementInterface
3322
{
23+
/**
24+
* @var CartManagementInterface
25+
*/
26+
protected $quoteManagement;
27+
28+
/**
29+
* @var QuoteRepository
30+
*/
31+
protected $quoteRepository;
32+
3433
/**
3534
* @var QuoteIdMaskFactory
3635
*/
@@ -39,60 +38,19 @@ class GuestCartManagement extends QuoteManagement implements GuestCartManagement
3938
/**
4039
* Initialize dependencies.
4140
*
42-
* @param EventManager $eventManager
43-
* @param QuoteValidator $quoteValidator
44-
* @param OrderFactory $orderFactory
45-
* @param OrderManagement $orderManagement
46-
* @param CustomerManagement $customerManagement
47-
* @param ToOrderConverter $quoteAddressToOrder
48-
* @param ToOrderAddressConverter $quoteAddressToOrderAddress
49-
* @param ToOrderItemConverter $quoteItemToOrderItem
50-
* @param ToOrderPaymentConverter $quotePaymentToOrderPayment
51-
* @param UserContextInterface $userContext
41+
* @param CartManagementInterface $quoteManagement
5242
* @param QuoteRepository $quoteRepository
53-
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
54-
* @param \Magento\Customer\Model\CustomerFactory $customerModelFactory
55-
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
56-
* @param StoreManagerInterface $storeManager
5743
* @param QuoteIdMaskFactory $quoteIdMaskFactory
5844
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5945
*/
6046
public function __construct(
61-
EventManager $eventManager,
62-
QuoteValidator $quoteValidator,
63-
OrderFactory $orderFactory,
64-
OrderManagement $orderManagement,
65-
CustomerManagement $customerManagement,
66-
ToOrderConverter $quoteAddressToOrder,
67-
ToOrderAddressConverter $quoteAddressToOrderAddress,
68-
ToOrderItemConverter $quoteItemToOrderItem,
69-
ToOrderPaymentConverter $quotePaymentToOrderPayment,
70-
UserContextInterface $userContext,
47+
CartManagementInterface $quoteManagement,
7148
QuoteRepository $quoteRepository,
72-
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
73-
\Magento\Customer\Model\CustomerFactory $customerModelFactory,
74-
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
75-
StoreManagerInterface $storeManager,
7649
QuoteIdMaskFactory $quoteIdMaskFactory
7750
) {
51+
$this->quoteManagement = $quoteManagement;
52+
$this->quoteRepository = $quoteRepository;
7853
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
79-
parent::__construct(
80-
$eventManager,
81-
$quoteValidator,
82-
$orderFactory,
83-
$orderManagement,
84-
$customerManagement,
85-
$quoteAddressToOrder,
86-
$quoteAddressToOrderAddress,
87-
$quoteItemToOrderItem,
88-
$quotePaymentToOrderPayment,
89-
$userContext,
90-
$quoteRepository,
91-
$customerRepository,
92-
$customerModelFactory,
93-
$dataObjectHelper,
94-
$storeManager
95-
);
9654
}
9755

9856
/**
@@ -102,7 +60,7 @@ public function createEmptyCart($customerId = null)
10260
{
10361
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
10462
$quoteIdMask = $this->quoteIdMaskFactory->create();
105-
$cartId = parent::createEmptyCart($customerId);
63+
$cartId = $this->quoteManagement->createEmptyCart($customerId);
10664
$quoteIdMask->setId($cartId)->save();
10765
return $quoteIdMask->getMaskedId();
10866
}
@@ -114,7 +72,7 @@ public function assignCustomer($cartId, $customerId, $storeId)
11472
{
11573
/** @var $quoteIdMask QuoteIdMask */
11674
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
117-
return parent::assignCustomer($quoteIdMask->getId(), $customerId, $storeId);
75+
return $this->quoteManagement->assignCustomer($quoteIdMask->getId(), $customerId, $storeId);
11876
}
11977

12078
/**
@@ -124,7 +82,7 @@ public function placeOrder($cartId)
12482
{
12583
/** @var $quoteIdMask QuoteIdMask */
12684
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
127-
return parent::placeOrder($quoteIdMask->getId());
85+
return $this->quoteManagement->placeOrder($quoteIdMask->getId());
12886
}
12987

13088
/**
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\Quote\Test\Unit\Model\GuestCart;
9+
10+
class GuestCartManagementTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \PHPUnit_Framework_MockObject_MockObject
14+
*/
15+
protected $quoteManagementMock;
16+
17+
/**
18+
* @var \PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $quoteRepositoryMock;
21+
22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $quoteIdMaskFactoryMock;
26+
27+
/**
28+
* @var \PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $quoteIdMaskMock;
31+
32+
/**
33+
* @var \Magento\Quote\Model\GuestCart\GuestCartManagement
34+
*/
35+
protected $guestCartManagement;
36+
37+
protected function setUp()
38+
{
39+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40+
41+
$this->quoteManagementMock = $this->getMockForAbstractClass(
42+
'Magento\Quote\Api\CartManagementInterface',
43+
[],
44+
'',
45+
false,
46+
true,
47+
true,
48+
[]
49+
);
50+
$this->quoteRepositoryMock = $this->getMock('Magento\Quote\Model\QuoteRepository', [], [], '', false);
51+
$this->quoteIdMaskFactoryMock = $this->getMock(
52+
'Magento\Quote\Model\QuoteIdMaskFactory',
53+
['create'],
54+
[],
55+
'',
56+
false
57+
);
58+
$this->quoteIdMaskMock = $this->getMock(
59+
'Magento\Quote\Model\QuoteIdMask',
60+
['getId', 'getMaskedId', 'load', 'save', 'setId'],
61+
[],
62+
'',
63+
false
64+
);
65+
66+
$this->guestCartManagement = $objectManager->getObject(
67+
'Magento\Quote\Model\GuestCart\GuestCartManagement',
68+
[
69+
'quoteManagement' => $this->quoteManagementMock,
70+
'quoteRepository' => $this->quoteRepositoryMock,
71+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
72+
]
73+
);
74+
}
75+
76+
public function testCreateEmptyCart()
77+
{
78+
$maskedCartId = 'masked1cart2id3';
79+
$cartId = 1;
80+
81+
$this->quoteIdMaskMock->expects($this->once())->method('setId')->with($cartId)->willReturnSelf();
82+
$this->quoteIdMaskMock->expects($this->once())->method('save')->willReturnSelf();
83+
$this->quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willreturn($maskedCartId);
84+
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
85+
$this->quoteManagementMock->expects($this->once())->method('createEmptyCart')->willReturn($cartId);
86+
87+
$this->assertEquals($maskedCartId, $this->guestCartManagement->createEmptyCart());
88+
}
89+
90+
public function testAssignCustomer()
91+
{
92+
$maskedCartId = 'masked1cart2id3';
93+
$cartId = 1;
94+
$customerId = 1;
95+
$storeId = 1;
96+
97+
$this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
98+
$this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($maskedCartId);
99+
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
100+
$this->quoteManagementMock->expects($this->once())->method('assignCustomer')->willReturn(true);
101+
102+
$this->assertEquals(true, $this->guestCartManagement->assignCustomer($cartId, $customerId, $storeId));
103+
}
104+
105+
public function testPlaceOrder()
106+
{
107+
$maskedCartId = 'masked1cart2id3';
108+
$cartId = 1;
109+
$orderId = 1;
110+
111+
$this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
112+
$this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($maskedCartId);
113+
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
114+
$this->quoteManagementMock->expects($this->once())->method('placeOrder')->willReturn($orderId);
115+
116+
$this->assertEquals($orderId, $this->guestCartManagement->placeOrder($cartId));
117+
}
118+
119+
public function testGetCartForCustomer()
120+
{
121+
$maskedCartId = 'masked1cart2id3';
122+
$cartId = 1;
123+
$orderId = 1;
124+
125+
$this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
126+
$this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($maskedCartId);
127+
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
128+
$this->quoteManagementMock->expects($this->once())->method('placeOrder')->willReturn($orderId);
129+
130+
$this->assertEquals($orderId, $this->guestCartManagement->placeOrder($cartId));
131+
}
132+
}

0 commit comments

Comments
 (0)