Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertBundleItemsOnProductPage.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Product\CatalogProductView;
11 use Magento\Mtf\Client\BrowserInterface;
13 
18 {
27  public function processAssert(
28  CatalogProductView $catalogProductView,
29  BundleProduct $product,
30  BrowserInterface $browser
31  ) {
32  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
33 
34  $productOptions = $this->prepareBundleOptions($product);
35  $productOptions = $this->sortDataByPath($productOptions, '::title');
36  foreach ($productOptions as $key => $productOption) {
37  $productOptions[$key] = $this->sortDataByPath($productOption, 'options::title');
38  }
39  $formOptions = $catalogProductView->getViewBlock()->getOptions($product)['bundle_options'];
40  $formOptions = $this->sortDataByPath($formOptions, '::title');
41  foreach ($formOptions as $key => $formOption) {
42  $formOptions[$key] = $this->sortDataByPath($formOption, 'options::title');
43  }
44 
45  $error = $this->verifyData($productOptions, $formOptions);
46  \PHPUnit\Framework\Assert::assertEmpty($error, $error);
47  }
48 
55  protected function prepareBundleOptions(BundleProduct $product)
56  {
57  $bundleSelections = $product->getBundleSelections();
58  $bundleOptions = isset($bundleSelections['bundle_options']) ? $bundleSelections['bundle_options'] : [];
59  $result = [];
60 
61  foreach ($bundleOptions as $optionKey => $bundleOption) {
62  $optionData = [
63  'title' => $bundleOption['title'],
64  'type' => $bundleOption['frontend_type'],
65  'is_require' => $bundleOption['required'],
66  ];
67 
68  $key = 0;
69  foreach ($bundleOption['assigned_products'] as $productKey => $assignedProduct) {
70  if ($this->isInStock($product, $key++)) {
71  $price = isset($assignedProduct['data']['selection_price_value'])
72  ? $assignedProduct['data']['selection_price_value']
73  : $bundleSelections['products'][$optionKey][$productKey]->getPrice();
74 
75  $optionData['options'][$productKey] = [
76  'title' => $assignedProduct['search_data']['name'],
77  'price' => number_format($price, 2),
78  ];
79  }
80  }
81 
82  $result[$optionKey] = $optionData;
83  }
84 
85  return $result;
86  }
87 
95  private function isInStock(BundleProduct $product, $key)
96  {
97  $assignedProducts = $product->getBundleSelections()['products'][0];
98  $status = $assignedProducts[$key]->getData()['quantity_and_stock_status']['is_in_stock'];
99 
100  if ($status == 'In Stock') {
101  return true;
102  }
103  return false;
104  }
105 
111  public function toString()
112  {
113  return 'Bundle options data on product page equals to passed from fixture dataset.';
114  }
115 }
$optionData
$price
processAssert(CatalogProductView $catalogProductView, BundleProduct $product, BrowserInterface $browser)
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
$status
Definition: order_status.php:8