Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartItemRepositoryTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Quote\Api;
8 
13 
15 {
16  const SERVICE_VERSION = 'V1';
17  const SERVICE_NAME = 'quoteCartItemRepositoryV1';
18  const RESOURCE_PATH = '/V1/carts/';
19 
23  protected $objectManager;
24 
25  protected function setUp()
26  {
28  }
29 
33  public function testGetList()
34  {
36  $quote = $this->objectManager->create(Quote::class);
37  $quote->load('test_order_item_with_items_and_custom_options', 'reserved_order_id');
38  $cartId = $quote->getId();
39  $output = [];
40  $customOptionProcessor = $this->objectManager->get(CustomOptionProcessor::class);
41 
43  foreach ($quote->getAllItems() as $item) {
44  $customOptionProcessor->processOptions($item);
45  $data = [
46  'item_id' => (int)$item->getItemId(),
47  'sku' => $item->getSku(),
48  'name' => $item->getName(),
49  'price' => (float)$item->getPrice(),
50  'qty' => (float)$item->getQty(),
51  'product_type' => $item->getProductType(),
52  'quote_id' => $item->getQuoteId(),
53  ];
54 
55  if ($item->getProductOption() !== null) {
56  $customOptions = $item->getProductOption()->getExtensionAttributes()->getCustomOptions();
57  foreach ($customOptions as $option) {
58  $data['product_option']['extension_attributes']['custom_options'][] = $option->getData();
59  }
60  }
61 
62  $output[] = $data;
63  }
64  $serviceInfo = [
65  'rest' => [
66  'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
67  'httpMethod' => Request::HTTP_METHOD_GET,
68  ],
69  'soap' => [
70  'service' => self::SERVICE_NAME,
71  'serviceVersion' => self::SERVICE_VERSION,
72  'operation' => self::SERVICE_NAME . 'GetList',
73  ],
74  ];
75 
76  $requestData = ["cartId" => $cartId];
77  $this->assertEquals($output, $this->_webApiCall($serviceInfo, $requestData));
78  }
79 
84  public function testAddItem()
85  {
87  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(2);
88  $productSku = $product->getSku();
90  $quote = $this->objectManager->create(Quote::class);
91  $quote->load('test_order_1', 'reserved_order_id');
92  $cartId = $quote->getId();
93  $serviceInfo = [
94  'rest' => [
95  'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
96  'httpMethod' => Request::HTTP_METHOD_POST,
97  ],
98  'soap' => [
99  'service' => self::SERVICE_NAME,
100  'serviceVersion' => self::SERVICE_VERSION,
101  'operation' => self::SERVICE_NAME . 'Save',
102  ],
103  ];
104 
105  $requestData = [
106  "cartItem" => [
107  "sku" => $productSku,
108  "qty" => 7,
109  "quote_id" => $cartId,
110  ],
111  ];
112  $this->_webApiCall($serviceInfo, $requestData);
113  $this->assertTrue($quote->hasProductId(2));
114  $this->assertEquals(7, $quote->getItemByProduct($product)->getQty());
115  }
116 
120  public function testRemoveItem()
121  {
123  $quote = $this->objectManager->create(Quote::class);
124  $quote->load('test_order_item_with_items', 'reserved_order_id');
125  $cartId = $quote->getId();
126  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
127  $productId = $product->getIdBySku('simple_one');
128  $product->load($productId);
129  $itemId = $quote->getItemByProduct($product)->getId();
130  $serviceInfo = [
131  'rest' => [
132  'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
133  'httpMethod' => Request::HTTP_METHOD_DELETE,
134  ],
135  'soap' => [
136  'service' => self::SERVICE_NAME,
137  'serviceVersion' => self::SERVICE_VERSION,
138  'operation' => self::SERVICE_NAME . 'DeleteById',
139  ],
140  ];
141 
142  $requestData = [
143  "cartId" => $cartId,
144  "itemId" => $itemId,
145  ];
146  $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
147  $quote = $this->objectManager->create(Quote::class);
148  $quote->load('test_order_item_with_items', 'reserved_order_id');
149  $this->assertFalse($quote->hasProductId($productId));
150  }
151 
155  public function testUpdateItem()
156  {
158  $quote = $this->objectManager->create(Quote::class);
159  $quote->load('test_order_item_with_items', 'reserved_order_id');
160  $cartId = $quote->getId();
161  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
162  $productId = $product->getIdBySku('simple_one');
163  $product->load($productId);
164  $itemId = $quote->getItemByProduct($product)->getId();
165  $serviceInfo = [
166  'rest' => [
167  'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
168  'httpMethod' => Request::HTTP_METHOD_PUT,
169  ],
170  'soap' => [
171  'service' => self::SERVICE_NAME,
172  'serviceVersion' => self::SERVICE_VERSION,
173  'operation' => self::SERVICE_NAME . 'Save',
174  ],
175  ];
176 
177  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
178  $requestData = [
179  "cartItem" => [
180  "qty" => 5,
181  "quote_id" => $cartId,
182  "itemId" => $itemId,
183  ],
184  ];
185  } else {
186  $requestData = [
187  "cartItem" => [
188  "qty" => 5,
189  "quote_id" => $cartId,
190  ],
191  ];
192  }
193  $this->_webApiCall($serviceInfo, $requestData);
194  $quote = $this->objectManager->create(Quote::class);
195  $quote->load('test_order_item_with_items', 'reserved_order_id');
196  $this->assertTrue($quote->hasProductId(1));
197  $item = $quote->getItemByProduct($product);
198  $this->assertEquals(5, $item->getQty());
199  $this->assertEquals($itemId, $item->getItemId());
200  }
201 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$quote
$cartId
Definition: quote.php:22