Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateCustomerBackendEntityTest.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\TestCase\Injectable;
14 use Magento\Mtf\Fixture\FixtureFactory;
15 
30 class UpdateCustomerBackendEntityTest extends Injectable
31 {
32  /* tags */
33  const MVP = 'yes';
34  /* end tags */
35 
41  protected $customerIndexPage;
42 
49 
55  private $fixtureFactory;
56 
63  public function __prepare(FixtureFactory $fixtureFactory)
64  {
65  $this->fixtureFactory = $fixtureFactory;
66  }
67 
75  public function __inject(
76  CustomerIndex $customerIndexPage,
77  CustomerIndexEdit $customerIndexEditPage
78  ) {
79  $this->customerIndexPage = $customerIndexPage;
80  $this->customerIndexEditPage = $customerIndexEditPage;
81  }
82 
92  private function prepareCustomer(
93  Customer $customer,
94  Customer $initialCustomer,
95  Address $address = null,
96  Address $addressToDelete = null
97  ) {
98  $data = $customer->hasData()
99  ? array_replace_recursive($initialCustomer->getData(), $customer->getData())
100  : $initialCustomer->getData();
101  $groupId = $customer->hasData('group_id') ? $customer : $initialCustomer;
102  $data['group_id'] = ['customerGroup' => $groupId->getDataFieldConfig('group_id')['source']->getCustomerGroup()];
103  $customerAddress = $this->prepareCustomerAddress($initialCustomer, $address, $addressToDelete);
104  if (!empty($customerAddress)) {
105  $data['address'] = $customerAddress;
106  }
107 
108  return $this->fixtureFactory->createByCode(
109  'customer',
110  ['data' => $data]
111  );
112  }
113 
122  private function prepareCustomerAddress(
123  Customer $initialCustomer,
124  Address $address = null,
125  Address $addressToDelete = null
126  ) {
127  $customerAddress = [];
128 
129  if ($initialCustomer->hasData('address')) {
130  $addressesList = $initialCustomer->getDataFieldConfig('address')['source']->getAddresses();
131  foreach ($addressesList as $key => $addressFixture) {
132  if ($addressToDelete === null || $addressFixture != $address) {
133  $customerAddress = ['addresses' => [$key => $addressFixture]];
134  }
135  }
136  }
137  if ($address !== null) {
138  $customerAddress['addresses'][] = $address;
139  }
140 
141  return $customerAddress;
142  }
143 
155  Customer $initialCustomer,
156  Customer $customer,
157  Address $address = null,
158  $addressIndexToDelete = null
159  ) {
160  // Precondition
161  $initialCustomer->persist();
162 
163  $addressToDelete = null;
164  if ($addressIndexToDelete !== null) {
165  $addressToDelete =
166  $initialCustomer->getDataFieldConfig('address')['source']->getAddresses()[$addressIndexToDelete];
167  }
168 
169  // Steps
170  $filter = ['email' => $initialCustomer->getEmail()];
171  $this->customerIndexPage->open();
172  $this->customerIndexPage->getCustomerGridBlock()->searchAndOpen($filter);
173  $this->customerIndexEditPage->getCustomerForm()->updateCustomer($customer, $address, $addressToDelete);
174  $this->customerIndexEditPage->getPageActionsBlock()->save();
175 
176  return [
177  'customer' => $this->prepareCustomer($customer, $initialCustomer, $address, $addressToDelete),
178  'addressToDelete' => $addressToDelete,
179  ];
180  }
181 }
$customer
Definition: customers.php:11
$address
Definition: customer.php:38
__inject(CustomerIndex $customerIndexPage, CustomerIndexEdit $customerIndexEditPage)
testUpdateCustomerBackendEntity(Customer $initialCustomer, Customer $customer, Address $address=null, $addressIndexToDelete=null)
$customerAddress