Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertImportCustomerAddresses.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Customer\Test\Page\Adminhtml\CustomerIndexEdit;
10 use Magento\ImportExport\Test\Fixture\ImportData;
11 use Magento\Mtf\Constraint\AbstractConstraint;
12 use Magento\Mtf\Fixture\FixtureFactory;
13 
17 class AssertImportCustomerAddresses extends AbstractConstraint
18 {
24  private $mappingKeys = [
25  'region' => 'region_id'
26  ];
27 
33  private $importedFieldsKeys = [
34  'firstname',
35  'lastname',
36  'postcode',
37  'region_id',
38  'city',
39  'company',
40  'country_id',
41  'street',
42  'telephone'
43  ];
44 
50  private $mappingCountries = [
51  'US' => 'United States'
52  ];
53 
59  private $customerIndexEdit;
60 
66  private $fixtureFactory;
67 
73  private $import;
74 
83  public function processAssert(
84  CustomerIndexEdit $customerIndexEdit,
85  FixtureFactory $fixtureFactory,
86  ImportData $import
87  ) {
88  $this->customerIndexEdit = $customerIndexEdit;
89  $this->fixtureFactory = $fixtureFactory;
90  $this->import = $import;
91 
92  $resultArrays = $this->getPrepareAddresses();
93 
94  \PHPUnit\Framework\Assert::assertEquals(
95  $resultArrays['pageData'],
96  $resultArrays['csvData'],
97  'Addresses from page and csv are not match.'
98  );
99  }
100 
106  private function getPrepareAddresses()
107  {
108  $addressTemplate = ($this->import->getBehavior() !== 'Delete Entities')
109  ? $this->fixtureFactory->createByCode('address', ['dataset' => 'US_address_1_without_email'])
110  : null;
111  $customers = $this->import->getDataFieldConfig('import_file')['source']->getEntities();
112  $customerForm = $this->customerIndexEdit->getCustomerForm();
113 
114  // Prepare customer address data from page form.
115  $resultAddressesArray = [];
116  foreach ($customers as $customer) {
117  $this->customerIndexEdit->open(['id' => $customer->getId()]);
118  $customerForm->openTab('addresses');
119  $addresses = $customerForm->getTab('addresses')->getDataAddresses($addressTemplate);
120  $address = array_shift($addresses);
121  if (!empty($address)) {
122  $resultAddressesArray[] = $address;
123  }
124  }
125 
126  // Prepare customer address data from csv file.
127  $resultCsvArray = [];
128  if ($this->import->getBehavior() !== 'Delete Entities') {
129  $resultCsvArray = $this->getResultCsv();
130  }
131  return ['pageData' => $resultAddressesArray, 'csvData' => $resultCsvArray];
132  }
133 
139  private function getResultCsv()
140  {
141  $csvData = $this->import->getDataFieldConfig('import_file')['source']->getCsv();
142 
143  $csvKeys = [];
144  foreach (array_shift($csvData) as $csvKey) {
145  $csvKeys[] = isset($this->mappingKeys[$csvKey]) ? $this->mappingKeys[$csvKey] : $csvKey;
146  }
147 
148  $resultCsvData = [];
149  foreach ($csvData as $csvRowData) {
150  $csvRowData = array_combine($csvKeys, $csvRowData);
151  $csvRowData = $this->deleteWasteData($csvRowData);
152  if (isset($this->mappingCountries[$csvRowData['country_id']])) {
153  $csvRowData['country_id'] = $this->mappingCountries[$csvRowData['country_id']];
154  }
155  $resultCsvData[] = $csvRowData;
156  }
157  return $resultCsvData;
158  }
159 
166  private function deleteWasteData(array $csvData)
167  {
168  $necessaryData = array_flip($this->importedFieldsKeys);
169 
170  return array_intersect_key($csvData, $necessaryData);
171  }
172 
178  public function toString()
179  {
180  return 'Imported customer addresses are correct.';
181  }
182 }
processAssert(CustomerIndexEdit $customerIndexEdit, FixtureFactory $fixtureFactory, ImportData $import)
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
$address
Definition: customer.php:38