Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartManagementTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Quote\Api;
8 
11 
18 {
19  const SERVICE_VERSION = 'V1';
20  const SERVICE_NAME = 'quoteCartManagementV1';
21  const RESOURCE_PATH = '/V1/carts/';
22  const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
23 
24  protected $createdQuotes = [];
25 
29  protected $objectManager;
30 
31  protected function setUp()
32  {
34  $appConfig = $this->objectManager->get(Config::class);
35  $appConfig->clean();
36  }
37 
38  public function tearDown()
39  {
41  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
42  foreach ($this->createdQuotes as $quoteId) {
43  $quote->load($quoteId);
44  $quote->delete();
45  }
46  }
47 
48  public function testCreateEmptyCartForGuest()
49  {
50  $serviceInfo = [
51  'rest' => [
52  'resourcePath' => self::RESOURCE_PATH,
54  ],
55  'soap' => [
56  'service' => self::SERVICE_NAME,
57  'serviceVersion' => self::SERVICE_VERSION,
58  'operation' => self::SERVICE_NAME . 'CreateEmptyCart',
59  ],
60  ];
61 
62  $requestData = ['storeId' => 1];
63  $quoteId = $this->_webApiCall($serviceInfo, $requestData);
64  $this->assertGreaterThan(0, $quoteId);
65  $this->createdQuotes[] = $quoteId;
66  }
67 
71  public function testCreateEmptyCartForCustomer()
72  {
74  $repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
76  $customer = $repository->getById(1);
77  $customerId = $customer->getId();
78 
79  $serviceInfo = [
80  'rest' => [
81  'resourcePath' => '/V1/customers/' . $customerId . '/carts',
83  ],
84  'soap' => [
85  'service' => self::SERVICE_NAME,
86  'serviceVersion' => self::SERVICE_VERSION,
87  'operation' => self::SERVICE_NAME . 'CreateEmptyCartForCustomer',
88  ],
89  ];
90 
91  $quoteId = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
92  $this->assertGreaterThan(0, $quoteId);
93  $this->createdQuotes[] = $quoteId;
94  }
95 
99  public function testCreateEmptyCartAndGetCartForCustomer()
100  {
101  $this->_markTestAsRestOnly();
102 
103  // get customer ID token
105  $customerTokenService = $this->objectManager->create(
106  \Magento\Integration\Api\CustomerTokenServiceInterface::class
107  );
108  $token = $customerTokenService->createCustomerAccessToken('[email protected]', 'password');
109 
110  $serviceInfo = [
111  'rest' => [
112  'resourcePath' => '/V1/carts/mine',
114  'token' => $token
115  ]
116  ];
117 
118  $quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
119  $this->assertGreaterThan(0, $quoteId);
120  $this->createdQuotes[] = $quoteId;
121 
122  $serviceInfo = [
123  'rest' => [
124  'resourcePath' => '/V1/carts/mine',
126  'token' => $token
127  ]
128  ];
129 
131  $cart = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
132  $this->assertEquals($quoteId, $cart['id']);
133  }
134 
139  public function testAssignCustomer()
140  {
142  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
143  $cartId = $quote->getId();
145  $repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
147  $customer = $repository->getById(1);
148  $customerId = $customer->getId();
149 
150  $serviceInfo = [
151  'rest' => [
152  'resourcePath' => '/V1/carts/' . $cartId,
154  ],
155  'soap' => [
156  'service' => self::SERVICE_NAME,
157  'serviceVersion' => 'V1',
158  'operation' => self::SERVICE_NAME . 'AssignCustomer',
159  ],
160  ];
161 
162  $requestData = [
163  'cartId' => $cartId,
164  'customerId' => $customerId,
165  'storeId' => 1,
166  ];
167  // Cart must be anonymous (see fixture)
168  $this->assertEmpty($quote->getCustomerId());
169 
170  $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
171  // Reload target quote
172  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
173  $this->assertEquals(0, $quote->getCustomerIsGuest());
174  $this->assertEquals($customer->getId(), $quote->getCustomerId());
175  $this->assertEquals($customer->getFirstname(), $quote->getCustomerFirstname());
176  $this->assertEquals($customer->getLastname(), $quote->getCustomerLastname());
177  }
178 
183  public function testAssignCustomerThrowsExceptionIfThereIsNoCustomerWithGivenId()
184  {
186  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
187  $cartId = $quote->getId();
188  $customerId = 9999;
189  $serviceInfo = [
190  'soap' => [
191  'serviceVersion' => 'V1',
192  'service' => self::SERVICE_NAME,
193  'operation' => self::SERVICE_NAME . 'AssignCustomer',
194  ],
195  'rest' => [
196  'resourcePath' => '/V1/carts/' . $cartId,
198  ],
199  ];
200  $requestData = [
201  'cartId' => $cartId,
202  'customerId' => $customerId,
203  'storeId' => 1,
204  ];
205 
206  $this->_webApiCall($serviceInfo, $requestData);
207  }
208 
214  {
215  $cartId = 9999;
216  $customerId = 1;
217  $serviceInfo = [
218  'soap' => [
219  'service' => self::SERVICE_NAME,
220  'serviceVersion' => 'V1',
221  'operation' => self::SERVICE_NAME . 'AssignCustomer',
222  ],
223  'rest' => [
224  'resourcePath' => '/V1/carts/' . $cartId,
226  ],
227  ];
228  $requestData = [
229  'cartId' => $cartId,
230  'customerId' => $customerId,
231  'storeId' => 1,
232  ];
233 
234  $this->_webApiCall($serviceInfo, $requestData);
235  }
236 
242  public function testAssignCustomerThrowsExceptionIfTargetCartIsNotAnonymous()
243  {
245  $customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->load(1);
246  $customerId = $customer->getId();
248  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
249  $cartId = $quote->getId();
250 
251  $serviceInfo = [
252  'rest' => [
254  'resourcePath' => '/V1/carts/' . $cartId,
255  ],
256  'soap' => [
257  'service' => self::SERVICE_NAME,
258  'serviceVersion' => 'V1',
259  'operation' => self::SERVICE_NAME . 'AssignCustomer',
260  ],
261  ];
262 
263  $requestData = [
264  'cartId' => $cartId,
265  'customerId' => $customerId,
266  'storeId' => 1,
267  ];
268  $this->_webApiCall($serviceInfo, $requestData);
269  }
270 
277  public function testAssignCustomerThrowsExceptionIfCartIsAssignedToDifferentStore()
278  {
279  $repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
281  $customer = $repository->getById(1);
283  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
284 
285  $customerId = $customer->getId();
286  $cartId = $quote->getId();
287 
288  $serviceInfo = [
289  'soap' => [
290  'service' => self::SERVICE_NAME,
291  'serviceVersion' => 'V1',
292  'operation' => self::SERVICE_NAME . 'AssignCustomer',
293  ],
294  'rest' => [
296  'resourcePath' => '/V1/carts/' . $cartId,
297  ],
298  ];
299 
300  $requestData = [
301  'cartId' => $cartId,
302  'customerId' => $customerId,
303  'storeId' => 1,
304  ];
305  $this->_webApiCall($serviceInfo, $requestData);
306  }
307 
313  public function testAssignCustomerThrowsExceptionIfCustomerAlreadyHasActiveCart()
314  {
316  $customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class)->load(1);
317  // Customer has a quote with reserved order ID test_order_1 (see fixture)
319  $customerQuote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)
320  ->load('test_order_1', 'reserved_order_id');
321  $customerQuote->setIsActive(1)->save();
323  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load('test01', 'reserved_order_id');
324 
325  $cartId = $quote->getId();
326  $customerId = $customer->getId();
327 
328  $serviceInfo = [
329  'soap' => [
330  'service' => self::SERVICE_NAME,
331  'operation' => self::SERVICE_NAME . 'AssignCustomer',
332  'serviceVersion' => 'V1',
333  ],
334  'rest' => [
335  'resourcePath' => '/V1/carts/' . $cartId,
337  ],
338  ];
339 
340  $requestData = [
341  'cartId' => $cartId,
342  'customerId' => $customerId,
343  'storeId' => 1,
344  ];
345  $this->_webApiCall($serviceInfo, $requestData);
346 
347  $this->expectExceptionMessage(
348  "The customer can't be assigned to the cart because the customer already has an active cart."
349  );
350  }
351 
355  public function testPlaceOrder()
356  {
358  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)
359  ->load('test_order_1', 'reserved_order_id');
360  $cartId = $quote->getId();
361 
362  $serviceInfo = [
363  'soap' => [
364  'service' => 'quoteCartManagementV1',
365  'operation' => 'quoteCartManagementV1PlaceOrder',
366  'serviceVersion' => 'V1',
367  ],
368  'rest' => [
369  'resourcePath' => '/V1/carts/' . $cartId . '/order',
371  ],
372  ];
373 
374  $orderId = $this->_webApiCall($serviceInfo, ['cartId' => $cartId]);
375 
377  $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
378  $items = $order->getAllItems();
379  $this->assertCount(1, $items);
380  $this->assertEquals('Simple Product', $items[0]->getName());
381  }
382 
386  public function testPlaceOrderForMyCart()
387  {
388  $this->_markTestAsRestOnly();
389 
390  // get customer ID token
392  $customerTokenService = $this->objectManager->create(
393  \Magento\Integration\Api\CustomerTokenServiceInterface::class
394  );
395  $token = $customerTokenService->createCustomerAccessToken('[email protected]', 'password');
396 
397  $serviceInfo = [
398  'rest' => [
399  'resourcePath' => '/V1/carts/mine/order',
401  'token' => $token
402  ],
403  ];
404 
405  $orderId = $this->_webApiCall($serviceInfo, []);
406 
408  $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
409  $items = $order->getAllItems();
410  $this->assertCount(1, $items);
411  $this->assertEquals('Simple Product', $items[0]->getName());
412  }
413 
419  public function testGetCartForCustomer()
420  {
421  // get customer ID token
423  $customerTokenService = $this->objectManager->create(
424  \Magento\Integration\Api\CustomerTokenServiceInterface::class
425  );
426  $token = $customerTokenService->createCustomerAccessToken('[email protected]', 'password');
427 
428  $cart = $this->getCart('test01');
429  $customerId = $cart->getCustomer()->getId();
430 
431  $serviceInfo = [
432  'rest' => [
433  'resourcePath' => '/V1/carts/mine',
435  'token' => $token
436  ],
437  'soap' => [
438  'service' => 'quoteCartManagementV1',
439  'serviceVersion' => 'V1',
440  'operation' => 'quoteCartManagementV1GetCartForCustomer',
441  'token' => $token
442  ],
443  ];
444 
445  $requestData = ['customerId' => $customerId];
446  $cartData = $this->_webApiCall($serviceInfo, $requestData);
447 
448  $this->assertEquals($cart->getId(), $cartData['id']);
449  $this->assertEquals($cart->getCreatedAt(), $cartData['created_at']);
450  $this->assertEquals($cart->getUpdatedAt(), $cartData['updated_at']);
451  $this->assertEquals($cart->getIsActive(), $cartData['is_active']);
452  $this->assertEquals($cart->getIsVirtual(), $cartData['is_virtual']);
453  $this->assertEquals($cart->getOrigOrderId(), $cartData['orig_order_id']);
454  $this->assertEquals($cart->getItemsCount(), $cartData['items_count']);
455  $this->assertEquals($cart->getItemsQty(), $cartData['items_qty']);
456 
457  $this->assertContains('customer', $cartData);
458  $this->assertEquals(false, $cartData['customer_is_guest']);
459  $this->assertContains('currency', $cartData);
460  $this->assertEquals($cart->getGlobalCurrencyCode(), $cartData['currency']['global_currency_code']);
461  $this->assertEquals($cart->getBaseCurrencyCode(), $cartData['currency']['base_currency_code']);
462  $this->assertEquals($cart->getQuoteCurrencyCode(), $cartData['currency']['quote_currency_code']);
463  $this->assertEquals($cart->getStoreCurrencyCode(), $cartData['currency']['store_currency_code']);
464  $this->assertEquals($cart->getBaseToGlobalRate(), $cartData['currency']['base_to_global_rate']);
465  $this->assertEquals($cart->getBaseToQuoteRate(), $cartData['currency']['base_to_quote_rate']);
466  $this->assertEquals($cart->getStoreToBaseRate(), $cartData['currency']['store_to_base_rate']);
467  $this->assertEquals($cart->getStoreToQuoteRate(), $cartData['currency']['store_to_quote_rate']);
468  }
469 
477  protected function getCart($reservedOrderId)
478  {
480  $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class);
481  $cart->load($reservedOrderId, 'reserved_order_id');
482  if (!$cart->getId()) {
483  throw new \InvalidArgumentException('There is no quote with provided reserved order ID.');
484  }
485  return $cart;
486  }
487 }
$customer
Definition: customers.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$quote
$order
Definition: order.php:55
$cartId
Definition: quote.php:22
$items