88
99namespace Magento \Customer \Test \Fixture ;
1010
11+ use Magento \Customer \Api \AccountManagementInterface ;
1112use Magento \Customer \Api \CustomerRepositoryInterface ;
1213use Magento \Customer \Api \Data \AddressInterface ;
1314use Magento \Customer \Api \Data \CustomerInterface ;
14- use Magento \Customer \Model \Customer as CustomerModel ;
15- use Magento \Customer \Model \CustomerFactory ;
1615use Magento \Customer \Model \CustomerRegistry ;
1716use Magento \Framework \DataObject ;
1817use Magento \Framework \Exception \LocalizedException ;
@@ -36,10 +35,10 @@ class Customer implements RevertibleDataFixtureInterface
3635 CustomerInterface::CREATED_IN => null ,
3736 CustomerInterface::DOB => null ,
3837 CustomerInterface::EMAIL => 'customer%uniqid%@mail.com ' ,
39- CustomerInterface::FIRSTNAME => 'Firstname %uniqid% ' ,
38+ CustomerInterface::FIRSTNAME => 'Firstname%uniqid% ' ,
4039 CustomerInterface::GENDER => null ,
4140 CustomerInterface::GROUP_ID => null ,
42- CustomerInterface::LASTNAME => 'Lastname %uniqid% ' ,
41+ CustomerInterface::LASTNAME => 'Lastname%uniqid% ' ,
4342 CustomerInterface::MIDDLENAME => null ,
4443 CustomerInterface::PREFIX => null ,
4544 CustomerInterface::STORE_ID => null ,
@@ -49,7 +48,9 @@ class Customer implements RevertibleDataFixtureInterface
4948 CustomerInterface::DEFAULT_BILLING => null ,
5049 CustomerInterface::DEFAULT_SHIPPING => null ,
5150 CustomerInterface::KEY_ADDRESSES => [],
52- CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null
51+ CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null ,
52+ CustomerInterface::CUSTOM_ATTRIBUTES => [],
53+ CustomerInterface::EXTENSION_ATTRIBUTES_KEY => [],
5354 ];
5455
5556 private const DEFAULT_DATA_ADDRESS = [
@@ -58,80 +59,68 @@ class Customer implements RevertibleDataFixtureInterface
5859 AddressInterface::REGION => 'Massachusetts ' ,
5960 AddressInterface::REGION_ID => '32 ' ,
6061 AddressInterface::COUNTRY_ID => 'US ' ,
61- AddressInterface::STREET => ' 123 Test Street' ,
62+ AddressInterface::STREET => [ ' %street_number% Test Street%uniqid% ' ] ,
6263 AddressInterface::COMPANY => null ,
6364 AddressInterface::TELEPHONE => '1234567890 ' ,
6465 AddressInterface::FAX => null ,
6566 AddressInterface::POSTCODE => '02108 ' ,
6667 AddressInterface::CITY => 'Boston ' ,
67- AddressInterface::FIRSTNAME => 'Firstname %uniqid% ' ,
68- AddressInterface::LASTNAME => 'Lastname %uniqid% ' ,
68+ AddressInterface::FIRSTNAME => 'Firstname%uniqid% ' ,
69+ AddressInterface::LASTNAME => 'Lastname%uniqid% ' ,
6970 AddressInterface::MIDDLENAME => null ,
7071 AddressInterface::PREFIX => null ,
7172 AddressInterface::SUFFIX => null ,
7273 AddressInterface::VAT_ID => null ,
7374 AddressInterface::DEFAULT_BILLING => true ,
74- AddressInterface::DEFAULT_SHIPPING => true
75+ AddressInterface::DEFAULT_SHIPPING => true ,
76+ AddressInterface::CUSTOM_ATTRIBUTES => [],
77+ AddressInterface::EXTENSION_ATTRIBUTES_KEY => [],
7578 ];
7679
7780 /**
7881 * @var ServiceFactory
7982 */
80- private $ serviceFactory ;
83+ private ServiceFactory $ serviceFactory ;
8184
8285 /**
83- * @var CustomerRepositoryInterface
86+ * @var AccountManagementInterface
8487 */
85- private $ customerRepository ;
86-
87- /**
88- * @var CustomerFactory
89- */
90- private $ customerFactory ;
88+ private AccountManagementInterface $ accountManagement ;
9189
9290 /**
9391 * @var CustomerRegistry
9492 */
95- private $ customerRegistry ;
93+ private CustomerRegistry $ customerRegistry ;
9694
9795 /**
9896 * @var ProcessorInterface
9997 */
100- private $ dataProcessor ;
98+ private ProcessorInterface $ dataProcessor ;
10199
102100 /**
103101 * @var DataMerger
104102 */
105- private $ dataMerger ;
106-
107- /**
108- * @var CustomerModel|null
109- */
110- private $ customer ;
103+ private DataMerger $ dataMerger ;
111104
112105 /**
113106 * @param ServiceFactory $serviceFactory
114- * @param CustomerRepositoryInterface $customerRepository
115- * @param CustomerFactory $customerFactory
107+ * @param AccountManagementInterface $accountManagement
116108 * @param CustomerRegistry $customerRegistry
117109 * @param ProcessorInterface $dataProcessor
118110 * @param DataMerger $dataMerger
119111 */
120112 public function __construct (
121113 ServiceFactory $ serviceFactory ,
122- CustomerRepositoryInterface $ customerRepository ,
123- CustomerFactory $ customerFactory ,
114+ AccountManagementInterface $ accountManagement ,
124115 CustomerRegistry $ customerRegistry ,
125116 ProcessorInterface $ dataProcessor ,
126- DataMerger $ dataMerger,
117+ DataMerger $ dataMerger
127118 ) {
128119 $ this ->serviceFactory = $ serviceFactory ;
129- $ this ->customerRepository = $ customerRepository ;
130- $ this ->customerFactory = $ customerFactory ;
120+ $ this ->accountManagement = $ accountManagement ;
131121 $ this ->customerRegistry = $ customerRegistry ;
132122 $ this ->dataProcessor = $ dataProcessor ;
133123 $ this ->dataMerger = $ dataMerger ;
134- $ this ->customer = null ;
135124 }
136125
137126 /**
@@ -145,14 +134,12 @@ public function apply(array $data = []): ?DataObject
145134 {
146135 $ customerSaveService = $ this ->serviceFactory ->create (CustomerRepositoryInterface::class, 'save ' );
147136 $ data = $ this ->prepareData ($ data );
148- if (count ($ data [CustomerInterface::KEY_ADDRESSES ])) {
149- $ addresses = $ this ->prepareCustomerAddress ($ data [CustomerInterface::KEY_ADDRESSES ]);
150- $ data [CustomerInterface::KEY_ADDRESSES ] = $ addresses ;
151- }
137+ $ passwordHash = $ this ->accountManagement ->getPasswordHash ($ data ['password ' ]);
138+ unset($ data ['password ' ]);
152139 $ customerSaveService ->execute (
153140 [
154141 'customer ' => $ data ,
155- 'passwordHash ' => $ this -> customer -> getPasswordHash ()
142+ 'passwordHash ' => $ passwordHash
156143 ]
157144 );
158145 return $ this ->customerRegistry ->retrieveByEmail ($ data ['email ' ], $ data ['website_id ' ]);
@@ -180,29 +167,33 @@ public function revert(DataObject $data): void
180167 */
181168 private function prepareData (array $ data ): array
182169 {
183- $ data = $ this ->dataMerger ->merge (self ::DEFAULT_DATA , $ data , false );
184-
185- $ this ->customer = $ this ->customerFactory ->create (['data ' => $ data ]);
186- $ this ->customer ->setPassword ($ data ['password ' ]);
187- if (isset ($ data ['password ' ])) {
188- unset($ data ['password ' ]);
189- }
170+ $ data = $ this ->dataMerger ->merge (self ::DEFAULT_DATA , $ data );
171+ $ data [CustomerInterface::KEY_ADDRESSES ] = $ this ->prepareAddresses ($ data [CustomerInterface::KEY_ADDRESSES ]);
190172
191173 return $ this ->dataProcessor ->process ($ this , $ data );
192174 }
193175
194176 /**
195- * Prepare customer address
177+ * Prepare customer addresses
196178 *
197179 * @param array $data
198180 * @return array
199181 */
200- private function prepareCustomerAddress (array $ data ): array
182+ private function prepareAddresses (array $ data ): array
201183 {
202184 $ addresses = [];
185+ $ default = self ::DEFAULT_DATA_ADDRESS ;
186+ $ streetNumber = 123 ;
203187 foreach ($ data as $ dataAddress ) {
204- $ dataAddress = $ this ->dataMerger ->merge (self ::DEFAULT_DATA_ADDRESS , $ dataAddress , false );
205- $ addresses [] = $ this ->dataProcessor ->process ($ this , $ dataAddress );
188+ $ dataAddress = $ this ->dataMerger ->merge ($ default , $ dataAddress );
189+ $ placeholders = ['%street_number% ' => $ streetNumber ++];
190+ $ dataAddress [AddressInterface::STREET ] = array_map (
191+ fn ($ str ) => strtr ($ str , $ placeholders ),
192+ $ dataAddress [AddressInterface::STREET ]
193+ );
194+ $ addresses [] = $ dataAddress ;
195+ $ default [AddressInterface::DEFAULT_BILLING ] = false ;
196+ $ default [AddressInterface::DEFAULT_SHIPPING ] = false ;
206197 }
207198
208199 return $ addresses ;
0 commit comments