Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertCustomerForm.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Customer\Test\Fixture\Address;
11 use Magento\Customer\Test\Page\Adminhtml\CustomerIndex;
12 use Magento\Customer\Test\Page\Adminhtml\CustomerIndexEdit;
13 use Magento\Mtf\Constraint\AbstractConstraint;
14 
18 class AssertCustomerForm extends AbstractConstraint
19 {
20  /* tags */
21  const SEVERITY = 'middle';
22  /* end tags */
23 
29  private $customerSkippedFields = [
30  'id',
31  'password',
32  'password_confirmation',
33  'current_password',
34  'is_subscribed',
35  'address',
36  'group_id'
37  ];
38 
44  private $localeMap = [
45  'en_GB' => 'd/m/Y'
46  ];
47 
53  private $localeFormat = 'm/d/Y';
54 
65  public function processAssert(
66  Customer $customer,
67  CustomerIndex $pageCustomerIndex,
68  CustomerIndexEdit $pageCustomerIndexEdit,
69  Address $address = null,
70  $locale = ''
71  ) {
72  $this->localeFormat = '' !== $locale && isset($this->localeMap[$locale])
73  ? $this->localeMap[$locale]
74  : $this->localeFormat;
75  $data = [];
76  $filter = [];
77 
78  $data['customer'] = $customer->getData();
79  if ($address) {
80  $data['addresses'][1] = $address->hasData() ? $address->getData() : [];
81  } else {
82  $data['addresses'] = [];
83  }
84  if (isset($data['customer']['dob'])) {
85  $data['customer']['dob'] = date($this->localeFormat, strtotime($data['customer']['dob']));
86  }
87  $filter['email'] = $data['customer']['email'];
88 
89  $pageCustomerIndex->open();
90  $pageCustomerIndex->getCustomerGridBlock()->searchAndOpen($filter);
91 
92  $dataForm = $pageCustomerIndexEdit->getCustomerForm()->getDataCustomer($customer, $address);
93  $dataDiff = $this->verify($data, $dataForm);
94  \PHPUnit\Framework\Assert::assertTrue(
95  empty($dataDiff),
96  'Customer data on edit page(backend) not equals to passed from fixture.'
97  . "\nFailed values: " . implode(', ', $dataDiff)
98  );
99  $this->assertCustomerGroupName($customer, $dataForm);
100  }
101 
109  private function verify(array $dataFixture, array $dataForm)
110  {
111  $result = [];
112 
113  $customerDiff = array_diff_assoc($dataFixture['customer'], $dataForm['customer']);
114  foreach ($customerDiff as $name => $value) {
115  if (in_array($name, $this->customerSkippedFields)) {
116  continue;
117  }
118  if (isset($dataForm['customer'][$name])) {
119  $result[] = "\ncustomer {$name}: \"{$dataForm['customer'][$name]}\" instead of \"{$value}\"";
120  } else {
121  $result[] = "\ncustomer {$name}: Field is absent. Expected value \"{$value}\"";
122  }
123  }
124  foreach ($dataFixture['addresses'] as $key => $address) {
125  $addressDiff = array_diff($address, $dataForm['addresses'][$key]);
126  foreach ($addressDiff as $name => $value) {
127  if (isset($dataForm['addresses'][$key][$name])) {
128  $result[] = "\naddress #{$key} {$name}: \"{$dataForm['addresses'][$key][$name]}"
129  . "\" instead of \"{$value}\"";
130  } else {
131  $result[] = "\naddress #{$key} {$name}: Field absent. Expected value \"{$value}\"";
132  }
133  }
134  }
135 
136  return $result;
137  }
138 
146  private function assertCustomerGroupName(Customer $customer, array $formData)
147  {
148  $customerGroupName = $customer->getGroupId();
149 
150  if ($customerGroupName) {
151  \PHPUnit\Framework\Assert::assertNotEmpty(
152  $formData['customer']['group_id'],
153  'Customer Group value is empty.'
154  );
155 
156  if (!empty($formData['customer']['group_id'])) {
157  \PHPUnit\Framework\Assert::assertContains(
158  $customerGroupName,
159  $formData['customer']['group_id'],
160  'Customer Group name is incorrect.'
161  );
162  }
163  }
164  }
165 
171  public function toString()
172  {
173  return 'Displayed customer data on edit page(backend) equals to passed from fixture.';
174  }
175 }
processAssert(Customer $customer, CustomerIndex $pageCustomerIndex, CustomerIndexEdit $pageCustomerIndexEdit, Address $address=null, $locale='')
$customer
Definition: customers.php:11
$address
Definition: customer.php:38
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14