Skip to content

Commit 386b250

Browse files
author
Cari Spruiell
committed
MAGETWO-34841: [PR] Sprint 46
- merged branches MAGETWO-35023-Code-Coverage-Guest-Sales-And-Checkout-APIs and MAGETWO-36189-SalesCheckoutAPIs-Mine-ParamOverrider for new PR
2 parents 7d0e8a0 + ea32f1d commit 386b250

25 files changed

+1417
-83
lines changed

app/code/Magento/Quote/Api/CartManagementInterface.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88
interface CartManagementInterface
99
{
1010
/**
11-
* Enables an customer or guest user to create an empty cart and quote for an anonymous customer.
11+
* Creates an empty cart and quote for a guest.
1212
*
13-
* @param int|null $customerId The customer ID.
1413
* @return int Cart ID.
1514
* @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created.
1615
*/
17-
public function createEmptyCart($customerId = null);
16+
public function createEmptyCart();
17+
18+
/**
19+
* Creates an empty cart and quote for a specified customer.
20+
*
21+
* @param int $customerId The customer ID.
22+
* @return int Cart ID.
23+
* @throws \Magento\Framework\Exception\CouldNotSaveException The empty cart and quote could not be created.
24+
*/
25+
public function createEmptyCartForCustomer($customerId);
1826

1927
/**
2028
* Returns information for the cart for a specified customer.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function __construct(
4646
/**
4747
* {@inheritdoc}
4848
*/
49-
public function createEmptyCart($customerId = null)
49+
public function createEmptyCart()
5050
{
5151
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
5252
$quoteIdMask = $this->quoteIdMaskFactory->create();
53-
$cartId = $this->quoteManagement->createEmptyCart($customerId);
53+
$cartId = $this->quoteManagement->createEmptyCart();
5454
$quoteIdMask->setId($cartId)->save();
5555
return $quoteIdMask->getMaskedId();
5656
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,26 @@ public function __construct(
156156
/**
157157
* {@inheritdoc}
158158
*/
159-
public function createEmptyCart($customerId = null)
159+
public function createEmptyCart()
160160
{
161161
$storeId = $this->storeManager->getStore()->getStoreId();
162-
$quote = $customerId
163-
? $this->createCustomerCart($customerId, $storeId)
164-
: $this->createAnonymousCart($storeId);
162+
$quote = $this->createAnonymousCart($storeId);
163+
164+
try {
165+
$this->quoteRepository->save($quote);
166+
} catch (\Exception $e) {
167+
throw new CouldNotSaveException(__('Cannot create quote'));
168+
}
169+
return $quote->getId();
170+
}
171+
172+
/**
173+
* {@inheritdoc}
174+
*/
175+
public function createEmptyCartForCustomer($customerId)
176+
{
177+
$storeId = $this->storeManager->getStore()->getStoreId();
178+
$quote = $this->createCustomerCart($customerId, $storeId);
165179

166180
try {
167181
$this->quoteRepository->save($quote);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Model\Webapi;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Quote\Api\CartManagementInterface;
13+
14+
/**
15+
* Replaces a "%cart_id%" value with the current authenticated customer's cart
16+
*/
17+
class ParamOverriderCartId implements ParamOverriderInterface
18+
{
19+
/**
20+
* @var UserContextInterface
21+
*/
22+
private $userContext;
23+
24+
/**
25+
* @var CartManagementInterface
26+
*/
27+
private $cartManagement;
28+
29+
/**
30+
* Constructs an object to override the cart ID parameter on a request.
31+
*
32+
* @param UserContextInterface $userContext
33+
* @param CartManagementInterface $cartManagement
34+
*/
35+
public function __construct(
36+
UserContextInterface $userContext,
37+
CartManagementInterface $cartManagement
38+
) {
39+
$this->userContext = $userContext;
40+
$this->cartManagement = $cartManagement;
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function getOverriddenValue()
47+
{
48+
try {
49+
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
50+
$customerId = $this->userContext->getUserId();
51+
52+
/** @var \Magento\Quote\Api\Data\CartInterface */
53+
$cart = $this->cartManagement->getCartForCustomer($customerId);
54+
if ($cart) {
55+
return $cart->getId();
56+
}
57+
}
58+
} catch (NoSuchEntityException $e) {
59+
/* do nothing and just return null */
60+
}
61+
return null;
62+
}
63+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function testCreateEmptyCartAnonymous()
198198
$this->assertEquals($quoteId, $this->model->createEmptyCart());
199199
}
200200

201-
public function testCreateEmptyCartLoggedInUser()
201+
public function testCreateEmptyCartForCustomer()
202202
{
203203
$storeId = 345;
204204
$quoteId = 2311;
@@ -222,13 +222,13 @@ public function testCreateEmptyCartLoggedInUser()
222222
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
223223
$this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
224224

225-
$this->assertEquals($quoteId, $this->model->createEmptyCart($userId));
225+
$this->assertEquals($quoteId, $this->model->createEmptyCartForCustomer($userId));
226226
}
227227

228228
/**
229229
* @expectedException \Magento\Framework\Exception\CouldNotSaveException
230230
*/
231-
public function testCreateEmptyCartLoggedInUserException()
231+
public function testCreateEmptyCartForCustomerException()
232232
{
233233
$storeId = 345;
234234
$userId = 567;
@@ -246,7 +246,7 @@ public function testCreateEmptyCartLoggedInUserException()
246246
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
247247
$this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
248248

249-
$this->model->createEmptyCart($userId);
249+
$this->model->createEmptyCartForCustomer($userId);
250250
}
251251

252252
/**
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Test\Unit\Model\Webapi;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Quote\Api\CartManagementInterface;
13+
use Magento\Quote\Model\Webapi\ParamOverriderCartId;
14+
15+
/**
16+
* Test for \Magento\Quote\Model\Webapi\ParamOverriderCartId
17+
*/
18+
class ParamOverriderCartIdTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var ParamOverriderCartId
22+
*/
23+
private $model;
24+
25+
/**
26+
* @var UserContextInterface
27+
*/
28+
private $userContext;
29+
30+
public function setUp()
31+
{
32+
$this->userContext = $this->getMockBuilder('Magento\Authorization\Model\UserContextInterface')
33+
->getMockForAbstractClass();
34+
$this->cartManagement = $this->getMockBuilder('Magento\Quote\Api\CartManagementInterface')
35+
->getMockForAbstractClass();
36+
$this->model = (new ObjectManager($this))->getObject(
37+
'Magento\Quote\Model\Webapi\ParamOverriderCartId',
38+
[
39+
'userContext' => $this->userContext,
40+
'cartManagement' => $this->cartManagement,
41+
]
42+
);
43+
}
44+
45+
public function testGetOverriddenValueIsCustomerAndCartExists()
46+
{
47+
$retValue = 'retValue';
48+
$customerId = 1;
49+
50+
$this->userContext->expects($this->once())
51+
->method('getUserType')
52+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
53+
$this->userContext->expects($this->once())
54+
->method('getUserId')
55+
->will($this->returnValue($customerId));
56+
57+
$cart = $this->getMockBuilder('Magento\Quote\Api\Data\CartInterface')
58+
->getMockForAbstractClass();
59+
$this->cartManagement->expects($this->once())
60+
->method('getCartForCustomer')
61+
->with($customerId)
62+
->will($this->returnValue($cart));
63+
$cart->expects($this->once())
64+
->method('getId')
65+
->will($this->returnValue($retValue));
66+
67+
$this->assertSame($retValue, $this->model->getOverriddenValue());
68+
}
69+
70+
public function testGetOverriddenValueIsCustomerAndCartDoesNotExist()
71+
{
72+
$customerId = 1;
73+
74+
$this->userContext->expects($this->once())
75+
->method('getUserType')
76+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
77+
$this->userContext->expects($this->once())
78+
->method('getUserId')
79+
->will($this->returnValue($customerId));
80+
81+
$this->cartManagement->expects($this->once())
82+
->method('getCartForCustomer')
83+
->with($customerId)
84+
->will($this->throwException(new NoSuchEntityException()));
85+
86+
$this->assertNull($this->model->getOverriddenValue());
87+
}
88+
89+
public function testGetOverriddenValueIsCustomerAndCartIsNull()
90+
{
91+
$customerId = 1;
92+
93+
$this->userContext->expects($this->once())
94+
->method('getUserType')
95+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
96+
$this->userContext->expects($this->once())
97+
->method('getUserId')
98+
->will($this->returnValue($customerId));
99+
100+
$this->cartManagement->expects($this->once())
101+
->method('getCartForCustomer')
102+
->with($customerId)
103+
->will($this->returnValue(null));
104+
105+
$this->assertNull($this->model->getOverriddenValue());
106+
}
107+
108+
public function testGetOverriddenValueIsNotCustomer()
109+
{
110+
$this->userContext->expects($this->once())
111+
->method('getUserType')
112+
->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN));
113+
114+
$this->assertNull($this->model->getOverriddenValue());
115+
}
116+
}

app/code/Magento/Quote/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@
3535
<preference for="Magento\Quote\Api\GuestShippingAddressManagementInterface" type="Magento\Quote\Model\GuestCart\GuestShippingAddressManagement" />
3636
<preference for="Magento\Quote\Api\GuestShippingMethodManagementInterface" type="Magento\Quote\Model\GuestCart\GuestShippingMethodManagement" />
3737
<preference for="Magento\Quote\Api\GuestBillingAddressManagementInterface" type="Magento\Quote\Model\GuestCart\GuestBillingAddressManagement" />
38+
39+
<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
40+
<arguments>
41+
<argument name="paramOverriders" xsi:type="array">
42+
<item name="%cart_id%" xsi:type="object">Magento\Quote\Model\Webapi\ParamOverriderCartId</item>
43+
</argument>
44+
</arguments>
45+
</type>
3846
</config>

0 commit comments

Comments
 (0)