Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableOptions.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Mtf\Client\Locator;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 use Magento\Mtf\Client\Element\SimpleElement;
14 
20 {
26  protected $optionSelector = '//*[./label[contains(.,"%s")]]//select';
27 
33  protected $priceBlock = '.product-info-price .price-box';
34 
40  private $tierPricesSelector = '.prices-tier li';
41 
47  private $configurableOptionElement = '#product-options-wrapper > * > .configurable';
48 
56  public function getOptions(FixtureInterface $product)
57  {
59  $attributesData = $product->hasData('configurable_attributes_data')
60  ? $product->getConfigurableAttributesData()['attributes_data']
61  : [];
62  $listOptions = $this->getListOptions();
63  $result = [];
64 
65  foreach ($attributesData as $option) {
66  $title = $option['label'];
67  if (!isset($listOptions[$title])) {
68  throw new \Exception("Can't find option: \"{$title}\"");
69  }
70 
72  $optionElement = $listOptions[$title];
73  $typeMethod = preg_replace('/[^a-zA-Z]/', '', $option['frontend_input']);
74  $getTypeData = 'get' . ucfirst(strtolower($typeMethod)) . 'Data';
75 
76  $optionData = $this->$getTypeData($optionElement);
77  $optionData['title'] = $title;
78  $optionData['type'] = $option['frontend_input'];
79  $optionData['is_require'] = $optionElement->find($this->required, Locator::SELECTOR_XPATH)->isVisible()
80  ? 'Yes'
81  : 'No';
82 
84  // Select first attribute option to be able proceed with next attribute
85  $this->selectOption($title, $optionData['options'][0]['title']);
86  }
87 
88  return $result;
89  }
90 
97  public function getOptionsPrices(FixtureInterface $product)
98  {
100  $attributesData = [];
101  $productVariations = [];
102  if ($product->hasData('configurable_attributes_data')) {
103  $attributesData = $product->getConfigurableAttributesData()['attributes_data'];
104  $productVariations = $product->getConfigurableAttributesData()['matrix'];
105  }
106 
107  $productVariations = array_keys($productVariations);
108  $result = [];
109  foreach ($productVariations as $variation) {
110  $variationOptions = explode(' ', $variation);
111  //Select all options specified in variation
112  $this->chooseOptions($variationOptions, $attributesData);
113  $result[$variation]['price'] = $this->getOptionPrice();
114  $tierPrices = $this->getOptionTierPrices();
115  if (count($tierPrices) > 0) {
116  $result[$variation]['tierPrices'] = $tierPrices;
117  }
118  }
119 
120  return $result;
121  }
122 
128  protected function getOptionPrice()
129  {
130  $priceBlock = $this->getPriceBlock();
131  $price = ($priceBlock->isOldPriceVisible()) ? $priceBlock->getOldPrice() : $priceBlock->getPrice();
132  return $price;
133  }
134 
140  private function getOptionTierPrices()
141  {
142  $prices = [];
143  $tierPricesNodes = $this->_rootElement->getElements($this->tierPricesSelector);
144  foreach ($tierPricesNodes as $node) {
145  preg_match('#^[^\d]+(\d+)[^\d]+(\d+(?:(?:,\d+)*)+(?:.\d+)*).*#i', $node->getText(), $matches);
146  $prices[] = [
147  'qty' => isset($matches[1]) ? $matches[1] : null,
148  'price_qty' => isset($matches[2]) ? $matches[2] : null,
149  ];
150  }
151  return $prices;
152  }
153 
159  protected function getPriceBlock()
160  {
162  $priceBlock = $this->blockFactory->create(
163  \Magento\Catalog\Test\Block\Product\Price::class,
164  ['element' => $this->_rootElement->find($this->priceBlock)]
165  );
166  return $priceBlock;
167  }
168 
175  protected function selectOption($attributeTitle, $optionTitle)
176  {
177  $this->_rootElement->find(sprintf($this->optionSelector, $attributeTitle), Locator::SELECTOR_XPATH, 'select')
178  ->setValue($optionTitle);
179  }
180 
188  protected function chooseOptions($variationOptions, $attributesData)
189  {
190  //Select all options specified in variation
191  foreach ($variationOptions as $variationSelection) {
192  list ($attribute, $option) = explode(':', $variationSelection);
193  $attributeTitle = $attributesData[$attribute]['label'];
194  $optionTitle = $attributesData[$attribute]['options'][$option]['label'];
195  $this->selectOption($attributeTitle, $optionTitle);
196  }
197  }
198 
204  public function getPresentOptions()
205  {
206  $options = [];
207 
208  $optionElements = $this->_rootElement->getElements($this->configurableOptionElement);
209  foreach ($optionElements as $optionElement) {
210  $title = $optionElement->find($this->title)->getText();
212  }
213 
214  return $options;
215  }
216 
222  public function isVisible()
223  {
224  return $this->_rootElement->find($this->optionsContext, Locator::SELECTOR_XPATH)->isVisible();
225  }
226 
234  public function selectConfigurableOption(FixtureInterface $product, $variation)
235  {
237  $attributesData = [];
238  $productVariations = [];
239  if ($product->hasData('configurable_attributes_data')) {
240  $attributesData = $product->getConfigurableAttributesData()['attributes_data'];
241  $productVariations = $product->getConfigurableAttributesData()['matrix'];
242  }
243  if (array_key_exists($variation, $productVariations)) {
244  $variationOption = explode(' ', $variation);
245  //Select option specified in variation
246  $this->chooseOptions($variationOption, $attributesData);
247  }
248  }
249 }
$optionData
$price
$attributesData
getPriceBlock(FixtureInterface $product=null)
Definition: View.php:29
getOptions(FixtureInterface $product=null)
Definition: View.php:94