Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateProductsStep.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Fixture\FixtureFactory;
10 use Magento\Mtf\Fixture\FixtureInterface;
11 use Magento\Mtf\TestStep\TestStepInterface;
12 
17 class CreateProductsStep implements TestStepInterface
18 {
24  protected $products;
25 
31  protected $data;
32 
38  protected $fixtureFactory;
39 
48  public function __construct(FixtureFactory $fixtureFactory, $products, array $data = [])
49  {
50  $this->products = $products;
51  $this->data = $data;
52  $this->fixtureFactory = $fixtureFactory;
53  }
54 
60  public function run()
61  {
62  $products = [];
63  if (!is_array($this->products)) { // for backward compatible changes
64  $this->products = explode(',', $this->products);
65  }
66  foreach ($this->products as $key => $productDataSet) {
67  if ($productDataSet instanceof FixtureInterface) {
68  $products[$key] = $productDataSet;
69  } else {
70  $productDataSet = explode('::', $productDataSet);
71  $fixtureClass = $productDataSet[0];
72  $dataset = isset($productDataSet[1]) ? $productDataSet[1] : '';
73  $data = isset($this->data[$key]) ? $this->data[$key] : [];
75  $products[$key] = $this->fixtureFactory->createByCode(
76  trim($fixtureClass),
77  ['dataset' => trim($dataset), 'data' => $data]
78  );
79  }
80  if ($products[$key]->hasData('id') === false) {
81  $products[$key]->persist();
82  }
83  }
84 
85  return ['products' => $products];
86  }
87 }
__construct(FixtureFactory $fixtureFactory, $products, array $data=[])