8
8
9
9
namespace Magento \Customer \Test \Fixture ;
10
10
11
+ use Magento \Customer \Api \AccountManagementInterface ;
11
12
use Magento \Customer \Api \CustomerRepositoryInterface ;
12
13
use Magento \Customer \Api \Data \AddressInterface ;
13
14
use Magento \Customer \Api \Data \CustomerInterface ;
14
- use Magento \Customer \Model \Customer as CustomerModel ;
15
- use Magento \Customer \Model \CustomerFactory ;
16
15
use Magento \Customer \Model \CustomerRegistry ;
17
16
use Magento \Framework \DataObject ;
18
17
use Magento \Framework \Exception \LocalizedException ;
@@ -36,10 +35,10 @@ class Customer implements RevertibleDataFixtureInterface
36
35
CustomerInterface::CREATED_IN => null ,
37
36
CustomerInterface::DOB => null ,
38
37
CustomerInterface::EMAIL => 'customer%uniqid%@mail.com ' ,
39
- CustomerInterface::FIRSTNAME => 'Firstname %uniqid% ' ,
38
+ CustomerInterface::FIRSTNAME => 'Firstname%uniqid% ' ,
40
39
CustomerInterface::GENDER => null ,
41
40
CustomerInterface::GROUP_ID => null ,
42
- CustomerInterface::LASTNAME => 'Lastname %uniqid% ' ,
41
+ CustomerInterface::LASTNAME => 'Lastname%uniqid% ' ,
43
42
CustomerInterface::MIDDLENAME => null ,
44
43
CustomerInterface::PREFIX => null ,
45
44
CustomerInterface::STORE_ID => null ,
@@ -49,7 +48,9 @@ class Customer implements RevertibleDataFixtureInterface
49
48
CustomerInterface::DEFAULT_BILLING => null ,
50
49
CustomerInterface::DEFAULT_SHIPPING => null ,
51
50
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 => [],
53
54
];
54
55
55
56
private const DEFAULT_DATA_ADDRESS = [
@@ -58,80 +59,68 @@ class Customer implements RevertibleDataFixtureInterface
58
59
AddressInterface::REGION => 'Massachusetts ' ,
59
60
AddressInterface::REGION_ID => '32 ' ,
60
61
AddressInterface::COUNTRY_ID => 'US ' ,
61
- AddressInterface::STREET => ' 123 Test Street' ,
62
+ AddressInterface::STREET => [ ' %street_number% Test Street%uniqid% ' ] ,
62
63
AddressInterface::COMPANY => null ,
63
64
AddressInterface::TELEPHONE => '1234567890 ' ,
64
65
AddressInterface::FAX => null ,
65
66
AddressInterface::POSTCODE => '02108 ' ,
66
67
AddressInterface::CITY => 'Boston ' ,
67
- AddressInterface::FIRSTNAME => 'Firstname %uniqid% ' ,
68
- AddressInterface::LASTNAME => 'Lastname %uniqid% ' ,
68
+ AddressInterface::FIRSTNAME => 'Firstname%uniqid% ' ,
69
+ AddressInterface::LASTNAME => 'Lastname%uniqid% ' ,
69
70
AddressInterface::MIDDLENAME => null ,
70
71
AddressInterface::PREFIX => null ,
71
72
AddressInterface::SUFFIX => null ,
72
73
AddressInterface::VAT_ID => null ,
73
74
AddressInterface::DEFAULT_BILLING => true ,
74
- AddressInterface::DEFAULT_SHIPPING => true
75
+ AddressInterface::DEFAULT_SHIPPING => true ,
76
+ AddressInterface::CUSTOM_ATTRIBUTES => [],
77
+ AddressInterface::EXTENSION_ATTRIBUTES_KEY => [],
75
78
];
76
79
77
80
/**
78
81
* @var ServiceFactory
79
82
*/
80
- private $ serviceFactory ;
83
+ private ServiceFactory $ serviceFactory ;
81
84
82
85
/**
83
- * @var CustomerRepositoryInterface
86
+ * @var AccountManagementInterface
84
87
*/
85
- private $ customerRepository ;
86
-
87
- /**
88
- * @var CustomerFactory
89
- */
90
- private $ customerFactory ;
88
+ private AccountManagementInterface $ accountManagement ;
91
89
92
90
/**
93
91
* @var CustomerRegistry
94
92
*/
95
- private $ customerRegistry ;
93
+ private CustomerRegistry $ customerRegistry ;
96
94
97
95
/**
98
96
* @var ProcessorInterface
99
97
*/
100
- private $ dataProcessor ;
98
+ private ProcessorInterface $ dataProcessor ;
101
99
102
100
/**
103
101
* @var DataMerger
104
102
*/
105
- private $ dataMerger ;
106
-
107
- /**
108
- * @var CustomerModel|null
109
- */
110
- private $ customer ;
103
+ private DataMerger $ dataMerger ;
111
104
112
105
/**
113
106
* @param ServiceFactory $serviceFactory
114
- * @param CustomerRepositoryInterface $customerRepository
115
- * @param CustomerFactory $customerFactory
107
+ * @param AccountManagementInterface $accountManagement
116
108
* @param CustomerRegistry $customerRegistry
117
109
* @param ProcessorInterface $dataProcessor
118
110
* @param DataMerger $dataMerger
119
111
*/
120
112
public function __construct (
121
113
ServiceFactory $ serviceFactory ,
122
- CustomerRepositoryInterface $ customerRepository ,
123
- CustomerFactory $ customerFactory ,
114
+ AccountManagementInterface $ accountManagement ,
124
115
CustomerRegistry $ customerRegistry ,
125
116
ProcessorInterface $ dataProcessor ,
126
- DataMerger $ dataMerger,
117
+ DataMerger $ dataMerger
127
118
) {
128
119
$ this ->serviceFactory = $ serviceFactory ;
129
- $ this ->customerRepository = $ customerRepository ;
130
- $ this ->customerFactory = $ customerFactory ;
120
+ $ this ->accountManagement = $ accountManagement ;
131
121
$ this ->customerRegistry = $ customerRegistry ;
132
122
$ this ->dataProcessor = $ dataProcessor ;
133
123
$ this ->dataMerger = $ dataMerger ;
134
- $ this ->customer = null ;
135
124
}
136
125
137
126
/**
@@ -145,14 +134,12 @@ public function apply(array $data = []): ?DataObject
145
134
{
146
135
$ customerSaveService = $ this ->serviceFactory ->create (CustomerRepositoryInterface::class, 'save ' );
147
136
$ 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 ' ]);
152
139
$ customerSaveService ->execute (
153
140
[
154
141
'customer ' => $ data ,
155
- 'passwordHash ' => $ this -> customer -> getPasswordHash ()
142
+ 'passwordHash ' => $ passwordHash
156
143
]
157
144
);
158
145
return $ this ->customerRegistry ->retrieveByEmail ($ data ['email ' ], $ data ['website_id ' ]);
@@ -180,29 +167,33 @@ public function revert(DataObject $data): void
180
167
*/
181
168
private function prepareData (array $ data ): array
182
169
{
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 ]);
190
172
191
173
return $ this ->dataProcessor ->process ($ this , $ data );
192
174
}
193
175
194
176
/**
195
- * Prepare customer address
177
+ * Prepare customer addresses
196
178
*
197
179
* @param array $data
198
180
* @return array
199
181
*/
200
- private function prepareCustomerAddress (array $ data ): array
182
+ private function prepareAddresses (array $ data ): array
201
183
{
202
184
$ addresses = [];
185
+ $ default = self ::DEFAULT_DATA_ADDRESS ;
186
+ $ streetNumber = 123 ;
203
187
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 ;
206
197
}
207
198
208
199
return $ addresses ;
0 commit comments