Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertBundleProductOnConfigureCartPage.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Product\CatalogProductView;
12 use Magento\Checkout\Test\Page\CheckoutCart;
14 
19 {
28  public function processAssert(CheckoutCart $checkoutCart, Cart $cart, CatalogProductView $catalogProductView)
29  {
30  $checkoutCart->open();
31  $sourceProducts = $cart->getDataFieldConfig('items')['source'];
32  $products = $sourceProducts->getProducts();
33  foreach ($cart->getItems() as $key => $item) {
34  $product = $products[$key];
35  $cartItem = $checkoutCart->getCartBlock()->getCartItem($product);
36  $cartItem->edit();
37  $formOptions = $catalogProductView->getBundleViewBlock()->getBundleBlock()->getOptions($product);
38  $this->checkOptions($product, $formOptions, $item->getData()['options']);
39  }
40  }
41 
47  public function toString()
48  {
49  return 'Bundle options data on cart configuration page is correct.';
50  }
51 
60  private function checkOptions(BundleProduct $product, array $formOptions, array $cartItemOptions)
61  {
62  $productOptions = $this->prepareBundleOptions($product, $cartItemOptions);
63  $productOptions = $this->sortDataByPath($productOptions, '::title');
64  foreach ($productOptions as $key => $productOption) {
65  $productOptions[$key] = $this->sortDataByPath($productOption, 'options::title');
66  }
67  $formOptions = $this->sortDataByPath($formOptions, '::title');
68  foreach ($formOptions as $key => $formOption) {
69  $formOptions[$key] = $this->sortDataByPath($formOption, 'options::title');
70  }
71 
72  $error = $this->verifyData($productOptions, $formOptions);
73  \PHPUnit\Framework\Assert::assertEmpty($error, $error);
74  }
75 
83  private function prepareBundleOptions(BundleProduct $product, array $cartItemOptions)
84  {
85  $bundleSelections = $product->getBundleSelections();
86  $bundleOptions = $bundleSelections['bundle_options'] ?? [];
87  $result = [];
88  foreach ($bundleOptions as $optionKey => $bundleOption) {
89  $optionData = [
90  'title' => $bundleOption['title'],
91  'type' => $bundleOption['frontend_type'],
92  'is_require' => $bundleOption['required'],
93  ];
94  foreach ($bundleOption['assigned_products'] as $productKey => $assignedProduct) {
95  $price = $assignedProduct['data']['selection_price_value']
96  ?? $bundleSelections['products'][$optionKey][$productKey]->getPrice();
97  $title = $assignedProduct['search_data']['name'];
98  $optionData['options'][$productKey] = [
99  'title' => $title,
100  'price' => number_format($price, 2),
101  ];
102  foreach ($cartItemOptions as $option) {
103  if (strpos($option['value'], $title) !== false) {
104  $optionData['options'][$productKey]['selected'] = true;
105  }
106  }
107  }
108  $result[$optionKey] = $optionData;
109  }
110 
111  return $result;
112  }
113 }
$title
Definition: default.phtml:14
processAssert(CheckoutCart $checkoutCart, Cart $cart, CatalogProductView $catalogProductView)
$optionData
$price
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)