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  const RESOURCE_PATH = '/V1/carts/';
15 
19  protected $objectManager;
20 
21  protected function setUp()
22  {
24  }
25 
29  public function testGetAll()
30  {
32  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
33  'test_order_bundle',
34  'reserved_order_id'
35  );
36  $quoteId = $quote->getId();
37 
38  $serviceInfo = [
39  'rest' => [
40  'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
42  ],
43  'soap' => [
44  'service' => self::SERVICE_NAME,
45  'serviceVersion' => self::SERVICE_VERSION,
46  'operation' => self::SERVICE_NAME . 'GetList',
47  ],
48  ];
49  $response = $this->_webApiCall($serviceInfo, ['cartId' => $quoteId]);
50  $this->assertEquals(1, count($response));
51  $response = $response[0];
52  $bundleOption = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOption();
53  $bundleOptionQty = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOptionQty();
54  $actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
55 
56  $this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
57  $this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
58  foreach ($actualOptions as $option) {
59  $id = $option['option_id'];
60  $expectedSelections = is_array($bundleOption[$id]) ? $bundleOption[$id] : [$bundleOption[$id]];
61  $this->assertEquals($expectedSelections, $option['option_selections']);
62  }
63  }
64 
69  public function testAddItem()
70  {
72  $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(3);
73  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
74  'test_order_item_with_items',
75  'reserved_order_id'
76  );
77 
78  $itemQty = 1;
79  $bundleProductOptions = $product->getExtensionAttributes()->getBundleProductOptions();
80  $bundleOptionId = $bundleProductOptions[0]->getId();
81  $optionSelections = $bundleProductOptions[0]->getProductLinks()[0]->getId();
82  $buyRequest = [
83  'bundle_option' => [$bundleOptionId => [$optionSelections]],
84  'bundle_option_qty' => [$bundleOptionId => 1],
85  'qty' => $itemQty,
86  'original_qty' => $itemQty
87  ];
88 
89  $productSku = $product->getSku();
90  $productId = $product->getId();
93  $cartId = $quote->getId();
94 
95  $serviceInfo = [
96  'rest' => [
97  'resourcePath' => self::RESOURCE_PATH . $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  $requestData = [
108  "cartItem" => [
109  "sku" => $productSku,
110  "qty" => $itemQty,
111  "quote_id" => $cartId,
112  "product_option" => [
113  "extension_attributes" => [
114  "bundle_options" => [
115  [
116  "option_id" => (int)$bundleOptionId,
117  "option_qty" => $itemQty,
118  "option_selections" => [(int)$optionSelections]
119  ]
120  ]
121  ]
122  ]
123  ]
124  ];
125  $response = $this->_webApiCall($serviceInfo, $requestData);
126  $this->assertTrue($quote->hasProductId($productId));
127  $this->assertEquals($buyRequest, $quote->getItemById($response['item_id'])->getBuyRequest()->getData());
128  }
129 
133  public function testUpdate()
134  {
136  $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
137  'test_order_bundle',
138  'reserved_order_id'
139  );
140  $cartId = $quote->getId();
141  $cartItem = $quote->getAllVisibleItems()[0];
142  $itemSku = $cartItem->getSku();
143  $itemId = $cartItem->getId();
144 
145  $product = $cartItem->getProduct();
147  $typeInstance = $product->getTypeInstance();
148  $typeInstance->setStoreFilter($product->getStoreId(), $product);
149  $optionCollection = $typeInstance->getOptionsCollection($product);
150  $bundleOptions = [];
152  foreach ($optionCollection as $option) {
153  if (!$option->getRequired()) {
154  continue;
155  }
156  $selectionsCollection = $typeInstance->getSelectionsCollection([$option->getId()], $product);
157  $option = ['option_id' => $option->getId(), 'option_qty' => 1];
158  $option['option_selections'] = [$selectionsCollection->getLastItem()->getSelectionId()];
160  }
161 
162  $serviceInfo = [
163  'rest' => [
164  'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
166  ],
167  'soap' => [
168  'service' => self::SERVICE_NAME,
169  'serviceVersion' => self::SERVICE_VERSION,
170  'operation' => self::SERVICE_NAME . 'Save',
171  ],
172  ];
173  $requestData = [
174  "cartItem" => [
175  "sku" => $itemSku,
176  "qty" => 2,
177  "quote_id" => $cartId,
178  "item_id" => $itemId,
179  "product_option" => [
180  "extension_attributes" => [
181  "bundle_options" => $bundleOptions
182  ]
183  ]
184  ]
185  ];
186  $this->_webApiCall($serviceInfo, $requestData);
187 
188  $quoteUpdated = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
189  'test_order_bundle',
190  'reserved_order_id'
191  );
192  $cartItems = $quoteUpdated->getAllVisibleItems();
193  $buyRequest = $cartItems[0]->getBuyRequest()->toArray();
194 
195  $this->assertEquals(1, count($cartItems));
196  $this->assertEquals(count($buyRequest['bundle_option']), count($bundleOptions));
197  foreach ($bundleOptions as $option) {
198  $optionId = $option['option_id'];
199  $optionQty = $option['option_qty'];
200  $optionSelections = $option['option_selections'];
201  $this->assertArrayHasKey($optionId, $buyRequest['bundle_option']);
202  $this->assertEquals($optionQty, $buyRequest['bundle_option_qty'][$optionId]);
203  $this->assertEquals($optionSelections, $buyRequest['bundle_option'][$optionId]);
204  }
205  }
206 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$id
Definition: fieldset.phtml:14
$quote
$cartId
Definition: quote.php:22
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest