Skip to content

Commit 2306110

Browse files
committed
MAGETWO-36269: Create /mine APIs for CartManagement
- Added/updated /mine URLs to webapi.xml for cart management - Added new method createEmptyCartForCustomer for CartManagementInterface to take responsibility of creating empty cart for logged in customer - Added/updated api-functional tests
1 parent 2aca647 commit 2306110

File tree

6 files changed

+118
-23
lines changed

6 files changed

+118
-23
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
@@ -98,11 +98,11 @@ public function __construct(
9898
/**
9999
* {@inheritdoc}
100100
*/
101-
public function createEmptyCart($customerId = null)
101+
public function createEmptyCart()
102102
{
103103
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
104104
$quoteIdMask = $this->quoteIdMaskFactory->create();
105-
$cartId = parent::createEmptyCart($customerId);
105+
$cartId = parent::createEmptyCart();
106106
$quoteIdMask->setId($cartId)->save();
107107
return $quoteIdMask->getMaskedId();
108108
}

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);

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
/**

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
<route url="/V1/carts/" method="POST">
2525
<service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCart"/>
2626
<resources>
27-
<resource ref="anonymous" />
27+
<resource ref="Magento_Cart::manage" />
28+
</resources>
29+
</route>
30+
<route url="/V1/customers/:customerId/carts" method="POST">
31+
<service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/>
32+
<resources>
33+
<resource ref="Magento_Cart::manage" />
2834
</resources>
2935
</route>
3036
<route url="/V1/carts/:cartId" method="PUT">
@@ -36,7 +42,7 @@
3642

3743
<!-- Managing my Cart -->
3844
<route url="/V1/carts/mine" method="POST">
39-
<service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCart"/>
45+
<service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/>
4046
<resources>
4147
<resource ref="self" />
4248
</resources>

dev/tests/api-functional/testsuite/Magento/Quote/Api/CartManagementTest.php

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ protected function setUp()
2727
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
2828
}
2929

30-
public function testCreate()
30+
public function tearDown()
31+
{
32+
/** @var \Magento\Quote\Model\Quote $quote */
33+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
34+
foreach ($this->createdQuotes as $quoteId) {
35+
$quote->load($quoteId);
36+
$quote->delete();
37+
}
38+
}
39+
40+
public function testCreateEmptyCartForGuest()
3141
{
3242
$serviceInfo = [
3343
'rest' => [
@@ -47,14 +57,71 @@ public function testCreate()
4757
$this->createdQuotes[] = $quoteId;
4858
}
4959

50-
public function tearDown()
60+
/**
61+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
62+
*/
63+
public function testCreateEmptyCartForCustomer()
5164
{
52-
/** @var \Magento\Quote\Model\Quote $quote */
53-
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
54-
foreach ($this->createdQuotes as $quoteId) {
55-
$quote->load($quoteId);
56-
$quote->delete();
57-
}
65+
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
66+
$repository = $this->objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
67+
/** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
68+
$customer = $repository->getById(1);
69+
$customerId = $customer->getId();
70+
71+
$serviceInfo = [
72+
'rest' => [
73+
'resourcePath' => '/V1/customers/' . $customerId . '/carts',
74+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
75+
],
76+
'soap' => [
77+
'service' => self::SERVICE_NAME,
78+
'serviceVersion' => self::SERVICE_VERSION,
79+
'operation' => self::SERVICE_NAME . 'CreateEmptyCartForCustomer',
80+
],
81+
];
82+
83+
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
84+
$this->assertGreaterThan(0, $quoteId);
85+
$this->createdQuotes[] = $quoteId;
86+
}
87+
88+
/**
89+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
90+
*/
91+
public function testCreateEmptyCartAndGetCartForCustomer()
92+
{
93+
$this->_markTestAsRestOnly();
94+
95+
// get customer ID token
96+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
97+
$customerTokenService = $this->objectManager->create(
98+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
99+
);
100+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
101+
102+
$serviceInfo = [
103+
'rest' => [
104+
'resourcePath' => '/V1/carts/mine',
105+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
106+
'token' => $token
107+
]
108+
];
109+
110+
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
111+
$this->assertGreaterThan(0, $quoteId);
112+
$this->createdQuotes[] = $quoteId;
113+
114+
$serviceInfo = [
115+
'rest' => [
116+
'resourcePath' => '/V1/carts/mine',
117+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
118+
'token' => $token
119+
]
120+
];
121+
122+
/** @var \Magento\Quote\Api\Data\CartInterface $cart */
123+
$cart = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
124+
$this->assertEquals($quoteId, $cart['id']);
58125
}
59126

60127
/**

0 commit comments

Comments
 (0)