Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyCatalogPriceRulesTest.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\CatalogRule\Test\Fixture\CatalogRule;
13 use Magento\CatalogRule\Test\Page\Adminhtml\CatalogRuleEdit;
14 use Magento\Mtf\TestStep\TestStepFactory;
15 use Magento\Mtf\Fixture\FixtureInterface;
16 
32 {
33  /* tags */
34  const TEST_TYPE = 'acceptance_test, extended_acceptance_test';
35  const MVP = 'yes';
36  /* end tags */
37 
43  protected $promo;
44 
58  public function test(
59  array $catalogRules,
60  CatalogRuleEdit $catalogRuleEdit,
61  TestStepFactory $stepFactory,
62  Cron $cron,
63  $isCronEnabled = false,
64  Customer $customer = null,
65  array $products = [],
66  $promo = 0
67  ) {
68  $this->promo = $promo;
69  if ($customer !== null) {
70  $customer->persist();
71  }
72 
73  $products = $stepFactory->create(
74  \Magento\Catalog\Test\TestStep\CreateProductsStep::class,
75  ['products' => $products]
76  )->run()['products'];
77 
78  foreach ($catalogRules as $catalogRule) {
79  foreach ($products as $product) {
80  $catalogPriceRule = $this->createCatalogPriceRule($catalogRule, $product, $customer);
81  if ($isCronEnabled) {
82  $cron->run();
83  $cron->run();
84  } else {
85  $catalogRuleEdit->open(['id' => $catalogPriceRule->getId()]);
86  $this->catalogRuleNew->getFormPageActions()->saveAndApply();
87  }
88  }
89  }
90  return ['products' => $products];
91  }
92 
100  protected function prepareCondition(FixtureInterface $product, array $catalogPriceRule)
101  {
102  $conditionEntity = explode('|', trim($catalogPriceRule['data']['rule'], '[]'))[0];
103  $actionName = 'get' . $conditionEntity;
104  if (method_exists($this, $actionName)) {
105  $result = $this->$actionName($product);
106  foreach ($result as $key => $value) {
107  $catalogPriceRule['data']['rule'] = str_replace($key, $value, $catalogPriceRule['data']['rule']);
108  }
109  return $catalogPriceRule;
110  } else {
111  $message = sprintf('Method "%s" does not exist in %s', $actionName, get_class($this));
112  throw new \BadMethodCallException($message);
113  }
114  }
115 
122  protected function getCategory(FixtureInterface $product)
123  {
124  $result['%category_id%'] = $product->getDataFieldConfig('category_ids')['source']->getIds()[0];
125  return $result;
126  }
127 
134  protected function getAttribute(FixtureInterface $product)
135  {
136  $attributes = $product->getDataFieldConfig('attribute_set_id')['source']
137  ->getAttributeSet()->getDataFieldConfig('assigned_attributes')['source']->getAttributes();
138  $result['%attribute_id%'] = $attributes[0]->getAttributeCode();
139  $result['%attribute_value%'] = $attributes[0]->getOptions()[$this->promo]['id'];
140  return $result;
141  }
142 
150  protected function applyCustomerGroup(array $catalogPriceRule, Customer $customer)
151  {
153  $customerGroup = $customer->getDataFieldConfig('group_id')['source']->getCustomerGroup();
154  $catalogPriceRule['data']['customer_group_ids']['option_0'] = $customerGroup->getCustomerGroupId();
155 
156  return $catalogPriceRule;
157  }
158 
167  protected function createCatalogPriceRule(
168  array $catalogPriceRule,
169  FixtureInterface $product,
170  Customer $customer = null
171  ) {
172  if (isset($catalogPriceRule['data']['rule'])) {
173  $catalogPriceRule = $this->prepareCondition($product, $catalogPriceRule);
174  }
175  if ($customer !== null) {
176  $catalogPriceRule = $this->applyCustomerGroup($catalogPriceRule, $customer);
177  }
178 
179  $catalogPriceRule = $this->fixtureFactory->createByCode('catalogRule', $catalogPriceRule);
180  $catalogPriceRule->persist();
181 
182  return $catalogPriceRule;
183  }
184 }
$customer
Definition: customers.php:11
test(array $catalogRules, CatalogRuleEdit $catalogRuleEdit, TestStepFactory $stepFactory, Cron $cron, $isCronEnabled=false, Customer $customer=null, array $products=[], $promo=0)
$message
createCatalogPriceRule(array $catalogPriceRule, FixtureInterface $product, Customer $customer=null)
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
prepareCondition(FixtureInterface $product, array $catalogPriceRule)