Skip to content

Commit c6d8407

Browse files
committed
MAGETWO-36274: Create /mine API for ShippingMethodManagement
- Added /mine URLs to webapi.xml for shipping method management - Added api-functional test cases to cover newly added endpoints
1 parent 1442d33 commit c6d8407

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@
108108
</resources>
109109
</route>
110110

111+
<!-- Managing My Cart Shipment Method -->
112+
<route url="/V1/carts/mine/selected-shipping-method" method="PUT">
113+
<service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="set"/>
114+
<resources>
115+
<resource ref="self" />
116+
</resources>
117+
<data>
118+
<parameter name="cartId" force="true">%cart_id%</parameter>
119+
</data>
120+
</route>
121+
<route url="/V1/carts/mine/selected-shipping-method" method="GET">
122+
<service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="get"/>
123+
<resources>
124+
<resource ref="self" />
125+
</resources>
126+
<data>
127+
<parameter name="cartId" force="true">%cart_id%</parameter>
128+
</data>
129+
</route>
130+
<route url="/V1/carts/mine/shipping-methods" method="GET">
131+
<service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="getList"/>
132+
<resources>
133+
<resource ref="self" />
134+
</resources>
135+
<data>
136+
<parameter name="cartId" force="true">%cart_id%</parameter>
137+
</data>
138+
</route>
139+
111140
<!-- Managing Guest Cart Shipment Method -->
112141
<route url="/V1/guest-carts/:cartId/selected-shipping-method" method="PUT">
113142
<service class="Magento\Quote\Api\GuestShippingMethodManagementInterface" method="set"/>

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ public function testSetMethodWithoutShippingAddress()
117117
$this->assertEquals('Shipping address is not set', $message);
118118
}
119119

120+
/**
121+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
122+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
123+
*/
124+
public function testSetMethodForMyCart()
125+
{
126+
$this->_markTestAsRestOnly();
127+
128+
$this->quote->load('test_order_1', 'reserved_order_id');
129+
130+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
131+
$customerTokenService = $this->objectManager->create(
132+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
133+
);
134+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
135+
136+
$serviceInfo = [
137+
'rest' => [
138+
'resourcePath' => '/V1/carts/mine/selected-shipping-method',
139+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
140+
'token' => $token
141+
]
142+
];
143+
144+
$requestData = [
145+
'cartId' => 999,
146+
'carrierCode' => 'flatrate',
147+
'methodCode' => 'flatrate',
148+
]; // cartId 999 will be overridden
149+
150+
$result = $this->_webApiCall($serviceInfo, $requestData);
151+
$this->assertEquals(true, $result);
152+
153+
/** @var \Magento\Quote\Api\ShippingMethodManagementInterface $ShippingMethodManagementService */
154+
$ShippingMethodManagementService = $this->objectManager->create(
155+
'Magento\Quote\Api\ShippingMethodManagementInterface'
156+
);
157+
$shippingMethod = $ShippingMethodManagementService->get($this->quote->getId());
158+
159+
$this->assertNotNull($shippingMethod);
160+
$this->assertEquals('flatrate', $shippingMethod->getCarrierCode());
161+
$this->assertEquals('flatrate', $shippingMethod->getMethodCode());
162+
}
163+
120164
/**
121165
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
122166
*/
@@ -169,6 +213,41 @@ public function testGetMethodOfCartWithNoShippingMethod()
169213
$this->assertEquals([], $result);
170214
}
171215

216+
/**
217+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
218+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
219+
*/
220+
public function testGetMethodForMyCart()
221+
{
222+
$this->_markTestAsRestOnly();
223+
224+
$this->quote->load('test_order_1', 'reserved_order_id');
225+
226+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
227+
$customerTokenService = $this->objectManager->create(
228+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
229+
);
230+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
231+
232+
/** @var \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManagementService */
233+
$shippingMethodManagementService = $this->objectManager->create(
234+
'Magento\Quote\Api\ShippingMethodManagementInterface'
235+
);
236+
$shippingMethodManagementService->set($this->quote->getId(), 'flatrate', 'flatrate');
237+
238+
$serviceInfo = [
239+
'rest' => [
240+
'resourcePath' => '/V1/carts/mine/selected-shipping-method',
241+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
242+
'token' => $token
243+
]
244+
];
245+
246+
$result = $this->_webApiCall($serviceInfo, []);
247+
$this->assertEquals('flatrate', $result[ShippingMethodInterface::KEY_CARRIER_CODE]);
248+
$this->assertEquals('flatrate', $result[ShippingMethodInterface::KEY_METHOD_CODE]);
249+
}
250+
172251
/**
173252
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
174253
*
@@ -204,6 +283,57 @@ public function testGetList()
204283
$this->assertEquals($expectedData, $returnedRates);
205284
}
206285

286+
/**
287+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
288+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
289+
*/
290+
public function testGetListForMyCart()
291+
{
292+
$this->_markTestAsRestOnly();
293+
294+
$this->quote->load('test_order_1', 'reserved_order_id');
295+
296+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
297+
$customerTokenService = $this->objectManager->create(
298+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
299+
);
300+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
301+
302+
/** @var \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManagementService */
303+
$shippingMethodManagementService = $this->objectManager->create(
304+
'Magento\Quote\Api\ShippingMethodManagementInterface'
305+
);
306+
$shippingMethodManagementService->set($this->quote->getId(), 'flatrate', 'flatrate');
307+
308+
$serviceInfo = [
309+
'rest' => [
310+
'resourcePath' => '/V1/carts/mine/shipping-methods',
311+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
312+
'token' => $token
313+
]
314+
];
315+
316+
$result = $this->_webApiCall($serviceInfo, []);
317+
$this->assertNotEmpty($result);
318+
319+
/** @var \Magento\Quote\Api\ShippingMethodManagementInterface $ShippingMethodManagementService */
320+
$ShippingMethodManagementService = $this->objectManager->create(
321+
'Magento\Quote\Api\ShippingMethodManagementInterface'
322+
);
323+
$shippingMethod = $ShippingMethodManagementService->get($this->quote->getId());
324+
$expectedData = [
325+
ShippingMethodInterface::KEY_CARRIER_CODE => $shippingMethod->getCarrierCode(),
326+
ShippingMethodInterface::KEY_METHOD_CODE => $shippingMethod->getMethodCode(),
327+
ShippingMethodInterface::KEY_CARRIER_TITLE => $shippingMethod->getCarrierTitle(),
328+
ShippingMethodInterface::KEY_METHOD_TITLE => $shippingMethod->getMethodTitle(),
329+
ShippingMethodInterface::KEY_SHIPPING_AMOUNT => $shippingMethod->getAmount(),
330+
ShippingMethodInterface::KEY_BASE_SHIPPING_AMOUNT => $shippingMethod->getBaseAmount(),
331+
ShippingMethodInterface::KEY_AVAILABLE => $shippingMethod->getAvailable(),
332+
];
333+
334+
$this->assertEquals($expectedData, $result[0]);
335+
}
336+
207337
/**
208338
* @param string $cartId
209339
* @return array

0 commit comments

Comments
 (0)