Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bundle.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Product\CatalogProductView;
12 use Magento\Mtf\Client\Element\SimpleElement;
13 use Magento\Mtf\Block\Block;
14 use Magento\Mtf\Client\Locator;
15 use Magento\Mtf\Fixture\FixtureInterface;
16 use Magento\Mtf\Fixture\InjectableFixture;
17 
22 class Bundle extends Block
23 {
29  protected $assignedProductName = '.product-name';
30 
36  protected $assignedProductPrice = '.bundle-options-wrapper .price';
37 
43  protected $optionElement = './div[contains(@class,"option")][%d]';
44 
50  protected $title = './label/span';
51 
57  protected $required = './self::*[contains(@class,"required")]';
58 
64  protected $selectOption = './/div[@class="control"]/select';
65 
71  protected $optionLabel = './/div[@class="control"]//label[.//*[@class="product-name"]]';
72 
78  protected $option = './/option[%d]';
79 
85  protected $bundleOptionBlock = './/div[label[span[contains(text(), "%s")]]]';
86 
92  private $product;
93 
99  protected $optionIndex;
100 
108  public function addToCart(BundleProduct $product, CatalogProductView $catalogProductView)
109  {
110  $catalogProductView->getViewBlock()->fillOptions($product);
111  $catalogProductView->getViewBlock()->clickAddToCart();
112  }
113 
121  public function getOptions(FixtureInterface $product)
122  {
124  $this->product = $product;
125  $bundleSelections = $product->getBundleSelections();
126  $bundleOptions = isset($bundleSelections['bundle_options']) ? $bundleSelections['bundle_options'] : [];
127 
128  $listFormOptions = $this->getListOptions();
129  $formOptions = [];
130 
131  foreach ($bundleOptions as $index => $option) {
132  $title = $option['title'];
133  if (!isset($listFormOptions[$title])) {
134  throw new \Exception("Can't find option: \"{$title}\"");
135  }
136  $this->optionIndex = $index;
137 
139  $optionElement = $listFormOptions[$title];
140  $getTypeData = 'get' . $this->optionNameConvert($option['frontend_type']) . 'Data';
141 
142  $optionData = $this->$getTypeData($optionElement);
143  $optionData['title'] = $title;
144  $optionData['type'] = $option['frontend_type'];
145  $optionData['is_require'] = $optionElement->find($this->required, Locator::SELECTOR_XPATH)->isVisible()
146  ? 'Yes'
147  : 'No';
148 
149  $formOptions[] = $optionData;
150  }
151  return $formOptions;
152  }
153 
160  public function isOptionVisible($optionTitle)
161  {
162  return isset($this->getListOptions()[$optionTitle]);
163  }
164 
170  protected function getListOptions()
171  {
172  $options = [];
173 
174  $count = 1;
175  $optionElement = $this->_rootElement->find(sprintf($this->optionElement, $count), Locator::SELECTOR_XPATH);
176  while ($optionElement->isVisible()) {
177  $title = $optionElement->find($this->title, Locator::SELECTOR_XPATH)->getText();
179 
180  ++$count;
181  $optionElement = $this->_rootElement->find(sprintf($this->optionElement, $count), Locator::SELECTOR_XPATH);
182  }
183  return $options;
184  }
185 
192  protected function getDropdownData(SimpleElement $option)
193  {
194  if ($this->isOneProductInStock($this->product)) {
195  return ['options' => $this->getFlatTextData()];
196  }
197  $select = $option->find($this->selectOption, Locator::SELECTOR_XPATH, 'select');
198  // Skip "Choose option ..."(option #1)
199  return $this->getSelectOptionsData($select, 2);
200  }
201 
208  protected function getMultipleselectData(SimpleElement $option)
209  {
210  $multiselect = $option->find($this->selectOption, Locator::SELECTOR_XPATH, 'multiselect');
211  $data = $this->getSelectOptionsData($multiselect, 1);
212 
213  foreach ($data['options'] as $key => $option) {
214  $option['title'] = trim(preg_replace('/^[\d]+ x/', '', $option['title']));
215  $data['options'][$key] = $option;
216  }
217 
218  return $data;
219  }
220 
227  protected function getRadiobuttonsData(SimpleElement $option)
228  {
229  $listOptions = [];
230  $optionLabels = $option->getElements($this->optionLabel, Locator::SELECTOR_XPATH);
231 
232  foreach ($optionLabels as $optionLabel) {
233  if ($optionLabel->isVisible()) {
234  $listOptions[] = $this->parseOptionText($optionLabel->getText());
235  }
236  }
237 
238  return ['options' => $listOptions];
239  }
240 
247  protected function getCheckboxData(SimpleElement $option)
248  {
249  $data = $this->getRadiobuttonsData($option);
250 
251  foreach ($data['options'] as $key => $option) {
252  $option['title'] = trim(preg_replace('/^[\d]+ x/', '', $option['title']));
253  $data['options'][$key] = $option;
254  }
255 
256  return $data;
257  }
258 
266  protected function getSelectOptionsData(SimpleElement $element, $firstOption = 1)
267  {
268  $listOptions = [];
269 
270  $count = $firstOption;
271  $selectOption = $element->find(sprintf($this->option, $count), Locator::SELECTOR_XPATH);
272  while ($selectOption->isVisible()) {
273  $option = $this->parseOptionText($selectOption->getText());
274  $selected = $selectOption->getAttribute('selected');
275  if ($selected) {
276  $option['selected'] = $selected;
277  }
278  $listOptions[] = $option;
279  ++$count;
280  $selectOption = $element->find(sprintf($this->option, $count), Locator::SELECTOR_XPATH);
281  }
282 
283  return ['options' => $listOptions];
284  }
285 
292  protected function parseOptionText($optionText)
293  {
294  preg_match('`^(.*?)\+ ?\$(\d.*?)$`sim', $optionText, $match);
295  $optionPrice = isset($match[2]) ? str_replace(',', '', $match[2]) : 0;
296  $optionTitle = isset($match[1]) ? trim($match[1]) : $optionText;
297 
298  return [
299  'title' => $optionTitle,
300  'price' => $optionPrice
301  ];
302  }
303 
310  public function fillBundleOptions($bundleOptions)
311  {
312  foreach ($bundleOptions as $option) {
313  $selector = sprintf($this->bundleOptionBlock, $option['title']);
314  $useDefault = isset($option['use_default']) && strtolower($option['use_default']) == 'true' ? true : false;
315  if (!$useDefault) {
317  $optionBlock = $this->blockFactory->create(
318  'Magento\Bundle\Test\Block\Catalog\Product\View\Type\Option\\'
319  . $this->optionNameConvert($option['frontend_type']),
320  ['element' => $this->_rootElement->find($selector, Locator::SELECTOR_XPATH)]
321  );
322  $optionBlock->fillOption($option['value']);
323  }
324  }
325  }
326 
333  protected function optionNameConvert($optionType)
334  {
335  $trimmedOptionType = preg_replace('/[^a-zA-Z]/', '', $optionType);
336  return ucfirst(strtolower($trimmedOptionType));
337  }
338 
345  private function isOneProductInStock(BundleProduct $products)
346  {
347  $result = [];
348  $products = $products->getBundleSelections()['products'][$this->optionIndex];
349  foreach ($products as $product) {
350  $status = $product->getData()['quantity_and_stock_status']['is_in_stock'];
351  if ($status == 'In Stock') {
352  $result[] = $product;
353  }
354  }
355  if (count($result) == 1) {
356  return true;
357  }
358  return false;
359  }
360 
366  private function getFlatTextData()
367  {
368  $productPrice = $this->_rootElement->find($this->assignedProductPrice)->getText();
369  $productPrice = preg_replace("/[^0-9.,]/", '', $productPrice);
370  $productName = $this->_rootElement->find($this->assignedProductName)->getText();
372  'title' => $productName,
373  'price' => number_format($productPrice, 2)
374  ];
375  return $options;
376  }
377 }
$optionData
$count
Definition: recent.phtml:13
$status
Definition: order_status.php:8
getOptions(FixtureInterface $product=null)
Definition: View.php:107
getSelectOptionsData(SimpleElement $element, $firstOption=1)
Definition: Bundle.php:266
addToCart(BundleProduct $product, CatalogProductView $catalogProductView)
Definition: Bundle.php:108
$index
Definition: list.phtml:44
$element
Definition: element.phtml:12