Skip to content

Commit 0c20fe8

Browse files
author
Cari Spruiell
committed
MAGETWO-36275: Create /mine API for BillingAddressManagement
- add /mine URLs to webapi.xml for billing address get & assign - create corresponding api-functional tests
1 parent 3b0ba30 commit 0c20fe8

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
</resources>
267267
</route>
268268

269-
<!-- Guest Managing Cart Billing address -->
269+
<!-- Managing Guest Cart Billing address -->
270270
<route url="/V1/guest-carts/:cartId/billing-address" method="GET">
271271
<service class="Magento\Quote\Api\GuestBillingAddressManagementInterface" method="get"/>
272272
<resources>
@@ -280,6 +280,26 @@
280280
</resources>
281281
</route>
282282

283+
<!-- Managing My Cart Billing address -->
284+
<route url="/V1/carts/mine/billing-address" method="GET">
285+
<service class="Magento\Quote\Api\BillingAddressManagementInterface" method="get"/>
286+
<resources>
287+
<resource ref="self" />
288+
</resources>
289+
<data>
290+
<parameter name="cartId" force="true">%cart_id%</parameter>
291+
</data>
292+
</route>
293+
<route url="/V1/carts/mine/billing-address" method="POST">
294+
<service class="Magento\Quote\Api\BillingAddressManagementInterface" method="assign"/>
295+
<resources>
296+
<resource ref="self" />
297+
</resources>
298+
<data>
299+
<parameter name="cartId" force="true">%cart_id%</parameter>
300+
</data>
301+
</route>
302+
283303
<!-- Managing Cart Coupons -->
284304
<route url="/V1/carts/:cartId/coupons" method="GET">
285305
<service class="Magento\Quote\Api\CouponManagementInterface" method="get"/>

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,130 @@ public function testSetAddress()
132132
$this->assertEquals($value, $savedData[$key]);
133133
}
134134
}
135+
136+
/**
137+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
138+
*/
139+
public function testGetMyAddress()
140+
{
141+
// get customer ID token
142+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
143+
$customerTokenService = $this->objectManager->create(
144+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
145+
);
146+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
147+
148+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
149+
$quote->load('test_order_1', 'reserved_order_id');
150+
151+
/** @var \Magento\Quote\Model\Quote\Address $address */
152+
$address = $quote->getBillingAddress();
153+
154+
$data = [
155+
AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
156+
AddressInterface::KEY_ID => (int)$address->getId(),
157+
AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(),
158+
AddressInterface::KEY_REGION => $address->getRegion(),
159+
AddressInterface::KEY_REGION_ID => $address->getRegionId(),
160+
AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
161+
AddressInterface::KEY_STREET => $address->getStreet(),
162+
AddressInterface::KEY_COMPANY => $address->getCompany(),
163+
AddressInterface::KEY_TELEPHONE => $address->getTelephone(),
164+
AddressInterface::KEY_POSTCODE => $address->getPostcode(),
165+
AddressInterface::KEY_CITY => $address->getCity(),
166+
AddressInterface::KEY_FIRSTNAME => $address->getFirstname(),
167+
AddressInterface::KEY_LASTNAME => $address->getLastname(),
168+
AddressInterface::KEY_EMAIL => $address->getEmail()
169+
];
170+
171+
$cartId = $quote->getId();
172+
173+
$serviceInfo = [
174+
'rest' => [
175+
'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address',
176+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
177+
'token' => $token
178+
],
179+
'soap' => [
180+
'service' => self::SERVICE_NAME,
181+
'serviceVersion' => self::SERVICE_VERSION,
182+
'operation' => self::SERVICE_NAME . 'Get',
183+
'token' => $token
184+
],
185+
];
186+
187+
$requestData = ["cartId" => $cartId];
188+
$this->assertEquals($data, $this->_webApiCall($serviceInfo, $requestData));
189+
}
190+
191+
/**
192+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
193+
*/
194+
public function testSetMyAddress()
195+
{
196+
// get customer ID token
197+
/** @var \Magento\Integration\Service\V1\CustomerTokenServiceInterface $customerTokenService */
198+
$customerTokenService = $this->objectManager->create(
199+
'Magento\Integration\Service\V1\CustomerTokenServiceInterface'
200+
);
201+
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
202+
203+
/** @var \Magento\Quote\Model\Quote $quote */
204+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
205+
$quote->load('test_order_1', 'reserved_order_id');
206+
207+
$serviceInfo = [
208+
'rest' => [
209+
'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address',
210+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
211+
'token' => $token
212+
],
213+
'soap' => [
214+
'service' => self::SERVICE_NAME,
215+
'serviceVersion' => self::SERVICE_VERSION,
216+
'operation' => self::SERVICE_NAME . 'Assign',
217+
'token' => $token
218+
],
219+
];
220+
221+
$addressData = [
222+
'firstname' => 'John',
223+
'lastname' => 'Smith',
224+
'email' => 'cat@dog.com',
225+
'company' => 'eBay Inc',
226+
'street' => ['Typical Street', 'Tiny House 18'],
227+
'city' => 'Big City',
228+
'region_id' => 12,
229+
'region' => 'California',
230+
'region_code' => 'CA',
231+
'postcode' => '0985432',
232+
'country_id' => 'US',
233+
'telephone' => '88776655',
234+
'fax' => '44332255',
235+
];
236+
$requestData = [
237+
"cartId" => $quote->getId(),
238+
'address' => $addressData,
239+
];
240+
241+
$addressId = $this->_webApiCall($serviceInfo, $requestData);
242+
243+
//reset $quote to reload data
244+
$quote = $this->objectManager->create('Magento\Quote\Model\Quote');
245+
$quote->load('test_order_1', 'reserved_order_id');
246+
$address = $quote->getBillingAddress();
247+
$address->getRegionCode();
248+
$savedData = $address->getData();
249+
$this->assertEquals($addressId, $savedData['address_id']);
250+
//custom checks for street, region and address_type
251+
foreach ($addressData['street'] as $streetLine) {
252+
$this->assertContains($streetLine, $quote->getBillingAddress()->getStreet());
253+
}
254+
unset($addressData['street']);
255+
$this->assertEquals('billing', $savedData['address_type']);
256+
//check the rest of fields
257+
foreach ($addressData as $key => $value) {
258+
$this->assertEquals($value, $savedData[$key]);
259+
}
260+
}
135261
}

0 commit comments

Comments
 (0)