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 
9 
11 {
12  const SERVICE_VERSION = 'V1';
13  const SERVICE_NAME = 'quoteCartItemRepositoryV1';
14 
18  protected $objectManager;
19 
20  protected function setUp()
21  {
23  }
24 
29  public function testAddItem()
30  {
32  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
33  $productSku = $product->getSku();
35  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
36  $quote->load('reserved_order_id', 'reserved_order_id');
37  $cartId = $quote->getId();
38  $serviceInfo = [
39  'rest' => [
40  'resourcePath' => '/V1/carts/' . $cartId . '/items',
42  ],
43  'soap' => [
44  'service' => self::SERVICE_NAME,
45  'serviceVersion' => self::SERVICE_VERSION,
46  'operation' => self::SERVICE_NAME . 'Save',
47  ],
48  ];
49 
50  // use ID of the first downloadable link
51  $linkId = array_values($product->getDownloadableLinks())[0]->getId();
52 
53  $requestData = [
54  'cartItem' => [
55  'sku' => $productSku,
56  'qty' => 1,
57  'quote_id' => $cartId,
58  'product_option' => [
59  'extension_attributes' => [
60  'downloadable_option' => [
61  'downloadable_links' => [$linkId]
62  ]
63  ]
64  ]
65  ],
66  ];
67  $response = $this->_webApiCall($serviceInfo, $requestData);
68  $this->assertNotEmpty($response);
69  $this->assertEquals('downloadable-product', $response['sku']);
70  $this->assertEquals(1, $response['qty']);
71  $this->assertCount(
72  1,
73  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
74  );
75  $this->assertContains(
76  $linkId,
77  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
78  );
79  }
80 
86  public function testAddItemWithInvalidLinkId()
87  {
89  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
91  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
92  $quote->load('reserved_order_id', 'reserved_order_id');
93  $cartId = $quote->getId();
94  $productSku = $product->getSku();
95  $serviceInfo = [
96  'rest' => [
97  'resourcePath' => '/V1/carts/' . $cartId . '/items',
99  ],
100  'soap' => [
101  'service' => self::SERVICE_NAME,
102  'serviceVersion' => self::SERVICE_VERSION,
103  'operation' => self::SERVICE_NAME . 'Save',
104  ],
105  ];
106 
107  $linkId = 9999;
108 
109  $requestData = [
110  'cartItem' => [
111  'sku' => $productSku,
112  'qty' => 1,
113  'quote_id' => $cartId,
114  'product_option' => [
115  'extension_attributes' => [
116  'downloadable_option' => [
117  'downloadable_links' => [$linkId]
118  ]
119  ]
120  ]
121  ],
122  ];
123  $this->_webApiCall($serviceInfo, $requestData);
124  }
125 
129  public function testUpdateItem()
130  {
132  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
133  $quote->load('reserved_order_id_1', 'reserved_order_id');
134  $cartId = $quote->getId();
135  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
136  $product->load($product->getIdBySku('downloadable-product'));
137  // use ID of the first quote item
138  $itemId = $quote->getAllItems()[0]->getId();
139  $serviceInfo = [
140  'rest' => [
141  'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
143  ],
144  'soap' => [
145  'service' => self::SERVICE_NAME,
146  'serviceVersion' => self::SERVICE_VERSION,
147  'operation' => self::SERVICE_NAME . 'Save',
148  ],
149  ];
150 
151  // use ID of the first downloadable link
152  $linkId = array_values($product->getDownloadableLinks())[0]->getId();
153 
154  $requestData = [
155  'cartItem' => [
156  'qty' => 2,
157  'quote_id' => $cartId,
158  'item_id' => $itemId,
159  'sku' => 'downloadable-product',
160  'product_option' => [
161  'extension_attributes' => [
162  'downloadable_option' => [
163  'downloadable_links' => [$linkId]
164  ]
165  ]
166  ]
167  ],
168  ];
169  $response = $this->_webApiCall($serviceInfo, $requestData);
170  $this->assertNotEmpty($response);
171  $this->assertEquals('downloadable-product', $response['sku']);
172  $this->assertEquals(2, $response['qty']);
173  $this->assertCount(
174  1,
175  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
176  );
177  $this->assertContains(
178  $linkId,
179  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
180  );
181  }
182 
187  public function testUpdateItemWithInvalidLinkId()
188  {
190  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
191  $quote->load('reserved_order_id_1', 'reserved_order_id');
192  $cartId = $quote->getId();
193  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
194  $product->load($product->getIdBySku('downloadable-product'));
195  // use ID of the first quote item
196  $itemId = $quote->getAllItems()[0]->getId();
197  $serviceInfo = [
198  'soap' => [
199  'service' => self::SERVICE_NAME,
200  'serviceVersion' => self::SERVICE_VERSION,
201  'operation' => self::SERVICE_NAME . 'Save',
202  ],
203  'rest' => [
204  'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
206  ],
207  ];
208 
209  $linkId = 9999;
210 
211  $requestData = [
212  'cartItem' => [
213  'qty' => 2,
214  'quote_id' => $cartId,
215  'item_id' => $itemId,
216  'sku' => 'downloadable-product',
217  'product_option' => [
218  'extension_attributes' => [
219  'downloadable_option' => [
220  'downloadable_links' => [$linkId]
221  ]
222  ]
223  ]
224  ],
225  ];
226  $this->_webApiCall($serviceInfo, $requestData);
227  }
228 
232  public function testGetList()
233  {
235  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
236  $quote->load('reserved_order_id_1', 'reserved_order_id');
237  $cartId = $quote->getId();
238  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
239  $product->load($product->getIdBySku('downloadable-product'));
240  // use ID of the first downloadable link
241  $linkId = array_values($product->getDownloadableLinks())[0]->getId();
242 
244  $item = $quote->getAllItems()[0];
245  $expectedResult = [[
246  'item_id' => $item->getItemId(),
247  'sku' => $item->getSku(),
248  'name' => $item->getName(),
249  'price' => $item->getPrice(),
250  'qty' => $item->getQty(),
251  'product_type' => $item->getProductType(),
252  'quote_id' => $item->getQuoteId(),
253  'product_option' => [
254  'extension_attributes' => [
255  'downloadable_option' => [
256  'downloadable_links' => [$linkId]
257  ]
258  ]
259  ]
260  ]];
261 
262  $serviceInfo = [
263  'rest' => [
264  'resourcePath' => '/V1/carts/' . $cartId . '/items',
266  ],
267  'soap' => [
268  'service' => self::SERVICE_NAME,
269  'serviceVersion' => self::SERVICE_VERSION,
270  'operation' => self::SERVICE_NAME . 'GetList',
271  ],
272  ];
273 
274  $requestData = ["cartId" => $cartId];
275  $this->assertEquals($expectedResult, $this->_webApiCall($serviceInfo, $requestData));
276  }
277 
281  public function testUpdateItemQty()
282  {
284  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
285  $quote->load('reserved_order_id_1', 'reserved_order_id');
286  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
287  $product->load($product->getIdBySku('downloadable-product'));
288  $cartId = $quote->getId();
289  // use ID of the first quote item
290  $itemId = $quote->getAllItems()[0]->getId();
291  $serviceInfo = [
292  'rest' => [
293  'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
295  ],
296  'soap' => [
297  'service' => self::SERVICE_NAME,
298  'serviceVersion' => self::SERVICE_VERSION,
299  'operation' => self::SERVICE_NAME . 'Save',
300  ],
301  ];
302 
303  // use ID of the first downloadable link
304  $linkId = array_values($product->getDownloadableLinks())[0]->getId();
305 
306  $requestData = [
307  'cartItem' => [
308  'qty' => 2,
309  'quote_id' => $cartId,
310  'item_id' => $itemId,
311  'sku' => 'downloadable-product',
312  ],
313  ];
314  $response = $this->_webApiCall($serviceInfo, $requestData);
315  $this->assertNotEmpty($response);
316  $this->assertEquals('downloadable-product', $response['sku']);
317  $this->assertEquals(2, $response['qty']);
318  $this->assertCount(
319  1,
320  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
321  );
322  $this->assertContains(
323  $linkId,
324  $response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
325  );
326  }
327 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$quote
$cartId
Definition: quote.php:22