Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertImportedProducts.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Constraint\AbstractConstraint;
10 use Magento\ImportExport\Test\Fixture\ImportData;
11 use Magento\Mtf\Client\BrowserInterface;
12 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductEdit;
14 use Magento\Mtf\Fixture\FixtureInterface;
15 
19 class AssertImportedProducts extends AbstractConstraint
20 {
26  protected $productType = 'simple';
27 
33  protected $neededKeys = [
34  'sku',
35  'name',
36  'price',
37  'qty',
38  'url_key',
39  ];
40 
46  private $import;
47 
54 
60  private $webApi;
61 
67  protected $browser;
68 
78  public function processAssert(
79  BrowserInterface $browser,
80  CatalogProductEdit $catalogProductEdit,
81  WebapiDecorator $webApi,
82  ImportData $import
83  ) {
84  $this->import = $import;
85  $this->catalogProductEdit = $catalogProductEdit;
86  $this->webApi = $webApi;
87  $this->browser = $browser;
88 
89  $products = $this->import->getDataFieldConfig('import_file')['source']->getEntities();
90  foreach ($products as $product) {
91  if ($product->getDataConfig()['type_id'] === $this->productType) {
92  $resultProductsData = $this->getDisplayedProductData($product);
93  $resultCsvData = $this->getResultCsv($product->getSku());
94  \PHPUnit\Framework\Assert::assertEquals(
95  $resultProductsData,
96  $resultCsvData,
97  'Products from page and csv are not match.'
98  );
99  }
100  }
101  }
102 
109  protected function getDisplayedProductData(FixtureInterface $product)
110  {
111  $productData = $this->getDisplayedOnProductPageData($product);
112 
113  return $this->getResultProductsData($productData);
114  }
115 
122  protected function getDisplayedOnProductPageData(FixtureInterface $product)
123  {
124  $productId = $this->retrieveProductBySku($product)['id'];
125  $this->catalogProductEdit->open(['id' => $productId]);
126 
127  return $this->catalogProductEdit->getProductForm()->getData($product);
128  }
129 
136  private function getResultCsv($productSku)
137  {
138  $csvData = $this->import->getDataFieldConfig('import_file')['source']->getCsv();
139 
140  $csvKeys = array_shift($csvData);
141  foreach ($csvData as $data) {
142  $data = array_combine($csvKeys, $data);
143  if ($data['sku'] === $productSku) {
144  return $this->deleteUnusedData($data);
145  }
146  }
147  return [];
148  }
149 
168  protected function getResultProductsData(array $productsData)
169  {
170  $resultProductsData = [];
171  array_walk_recursive(
173  function ($value, $key) use (&$resultProductsData) {
174  if (array_key_exists($key, array_flip($this->neededKeys)) !== false) {
175  $resultProductsData[$key] = $value;
176  }
177  }
178  );
179 
180  return $resultProductsData;
181  }
182 
189  private function deleteUnusedData(array $csvData)
190  {
191  $data = [];
192  foreach ($csvData as $key => $value) {
193  if (array_key_exists($key, array_flip($this->neededKeys)) !== false) {
194  $data[$key] = $value;
195  }
196  }
197 
198  return $data;
199  }
200 
207  public function retrieveProductBySku(FixtureInterface $product)
208  {
209  $url = $_ENV['app_frontend_url'] . 'rest/all/V1/products/' . $product->getSku();
210  $this->webApi->write($url, [], WebapiDecorator::GET);
211  $response = json_decode($this->webApi->read(), true);
212  $this->webApi->close();
213  return $response;
214  }
215 
221  public function toString()
222  {
223  return 'Products data from CSV import file and data from product edit page are correct and match.';
224  }
225 }
$response
Definition: 404.php:11
$productsData
Definition: products.php:19
$value
Definition: gender.phtml:16
$productData
processAssert(BrowserInterface $browser, CatalogProductEdit $catalogProductEdit, WebapiDecorator $webApi, ImportData $import)