1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Customer; |
4
|
|
|
|
5
|
|
|
use Magento\Customer\Api\CustomerRepositoryInterface; |
6
|
|
|
use Magento\Customer\Api\Data\CustomerInterface; |
7
|
|
|
use Magento\Framework\ObjectManagerInterface; |
8
|
|
|
use Magento\Framework\Encryption\EncryptorInterface as Encryptor; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Builder to be used by fixtures |
12
|
|
|
*/ |
13
|
|
|
class CustomerBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var CustomerInterface |
17
|
|
|
*/ |
18
|
|
|
private $customer; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $password; |
23
|
|
|
/** |
24
|
|
|
* @var CustomerRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $customerRepository; |
27
|
|
|
/** |
28
|
|
|
* @var AddressBuilder[] |
29
|
|
|
*/ |
30
|
|
|
private $addressBuilders = []; |
31
|
|
|
/** |
32
|
|
|
* @var Encryptor |
33
|
|
|
*/ |
34
|
|
|
private $encryptor; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
CustomerRepositoryInterface $customerRepository, |
38
|
|
|
CustomerInterface $customer, |
39
|
|
|
Encryptor $encryptor, |
40
|
|
|
string $password, |
41
|
|
|
AddressBuilder ...$addressBuilders |
42
|
|
|
) { |
43
|
|
|
$this->customerRepository = $customerRepository; |
44
|
|
|
$this->customer = $customer; |
45
|
|
|
$this->encryptor = $encryptor; |
46
|
|
|
$this->password = $password; |
47
|
|
|
$this->addressBuilders = $addressBuilders; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function __clone() |
51
|
|
|
{ |
52
|
|
|
$this->customer = clone $this->customer; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function aCustomer(ObjectManagerInterface $objectManager = null) : CustomerBuilder |
56
|
|
|
{ |
57
|
|
|
if ($objectManager === null) { |
58
|
|
|
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
/** @var CustomerInterface $customer */ |
61
|
|
|
$customer = $objectManager->create(CustomerInterface::class); |
62
|
|
|
$customer->setWebsiteId(1) |
63
|
|
|
->setGroupId(1) |
64
|
|
|
->setStoreId(1) |
65
|
|
|
->setPrefix('Mr.') |
66
|
|
|
->setFirstname('John') |
67
|
|
|
->setMiddlename('A') |
68
|
|
|
->setLastname('Smith') |
69
|
|
|
->setSuffix('Esq.') |
70
|
|
|
->setTaxvat('12') |
71
|
|
|
->setGender(0); |
72
|
|
|
$password = 'Test#123'; |
73
|
|
|
return new self( |
74
|
|
|
$objectManager->create(CustomerRepositoryInterface::class), |
75
|
|
|
$customer, |
76
|
|
|
$objectManager->create(Encryptor::class), |
77
|
|
|
$password |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function withAddresses(AddressBuilder ...$addressBuilders) : CustomerBuilder |
82
|
|
|
{ |
83
|
|
|
$builder = clone $this; |
84
|
|
|
$builder->addressBuilders = $addressBuilders; |
85
|
|
|
return $builder; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withEmail(string $email) : CustomerBuilder |
89
|
|
|
{ |
90
|
|
|
$builder = clone $this; |
91
|
|
|
$builder->customer->setEmail($email); |
92
|
|
|
return $builder; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function withGroupId($groupId) : CustomerBuilder |
96
|
|
|
{ |
97
|
|
|
$builder = clone $this; |
98
|
|
|
$builder->customer->setGroupId($groupId); |
99
|
|
|
return $builder; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function withStoreId($storeId) : CustomerBuilder |
103
|
|
|
{ |
104
|
|
|
$builder = clone $this; |
105
|
|
|
$builder->customer->setStoreId($storeId); |
106
|
|
|
return $builder; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function withWebsiteId($websiteId) : CustomerBuilder |
110
|
|
|
{ |
111
|
|
|
$builder = clone $this; |
112
|
|
|
$builder->customer->setWebsiteId($websiteId); |
113
|
|
|
return $builder; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function withPrefix($prefix) : CustomerBuilder |
117
|
|
|
{ |
118
|
|
|
$builder = clone $this; |
119
|
|
|
$builder->customer->setPrefix($prefix); |
120
|
|
|
return $builder; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function withFirstname($firstname) : CustomerBuilder |
124
|
|
|
{ |
125
|
|
|
$builder = clone $this; |
126
|
|
|
$builder->customer->setFirstname($firstname); |
127
|
|
|
return $builder; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function withMiddlename($middlename) : CustomerBuilder |
131
|
|
|
{ |
132
|
|
|
$builder = clone $this; |
133
|
|
|
$builder->customer->setMiddlename($middlename); |
134
|
|
|
return $builder; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function withLastname($lastname) : CustomerBuilder |
138
|
|
|
{ |
139
|
|
|
$builder = clone $this; |
140
|
|
|
$builder->customer->setLastname($lastname); |
141
|
|
|
return $builder; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function withSuffix($suffix) : CustomerBuilder |
145
|
|
|
{ |
146
|
|
|
$builder = clone $this; |
147
|
|
|
$builder->customer->setSuffix($suffix); |
148
|
|
|
return $builder; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function withTaxvat($taxvat) : CustomerBuilder |
152
|
|
|
{ |
153
|
|
|
$builder = clone $this; |
154
|
|
|
$builder->customer->setTaxvat($taxvat); |
155
|
|
|
return $builder; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function withDob($dob) : CustomerBuilder |
159
|
|
|
{ |
160
|
|
|
$builder = clone $this; |
161
|
|
|
$builder->customer->setDob($dob); |
162
|
|
|
return $builder; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
public function withCustomAttributes(array $values) : CustomerBuilder |
167
|
|
|
{ |
168
|
|
|
$builder = clone $this; |
169
|
|
|
foreach ($values as $code => $value) { |
170
|
|
|
$builder->customer->setCustomAttribute($code, $value); |
171
|
|
|
} |
172
|
|
|
return $builder; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function withConfirmation(string $confirmation) : CustomerBuilder |
176
|
|
|
{ |
177
|
|
|
$builder = clone $this; |
178
|
|
|
$builder->customer->setConfirmation($confirmation); |
179
|
|
|
return $builder; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function build() : CustomerInterface |
183
|
|
|
{ |
184
|
|
|
$builder = clone $this; |
185
|
|
|
if (!$builder->customer->getEmail()) { |
186
|
|
|
$builder->customer->setEmail(sha1(uniqid('', true)) . '@example.com'); |
187
|
|
|
} |
188
|
|
|
$addresses = array_map( |
189
|
|
|
function (AddressBuilder $addressBuilder) { |
190
|
|
|
return $addressBuilder->buildWithoutSave(); |
191
|
|
|
}, |
192
|
|
|
$builder->addressBuilders |
193
|
|
|
); |
194
|
|
|
$builder->customer->setAddresses($addresses); |
195
|
|
|
$customer = $builder->saveNewCustomer(); |
196
|
|
|
/* |
197
|
|
|
* Magento automatically sets random confirmation key for new account with password. |
198
|
|
|
* We need to save again with our own confirmation (null for confirmed customer) |
199
|
|
|
*/ |
200
|
|
|
$customer->setConfirmation($builder->customer->getConfirmation()); |
201
|
|
|
return $builder->customerRepository->save($customer); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) False positive: the method is used in build() on the cloned builder |
206
|
|
|
*/ |
207
|
|
|
private function saveNewCustomer(): CustomerInterface |
208
|
|
|
{ |
209
|
|
|
return $this->customerRepository->save($this->customer, $this->encryptor->getHash($this->password, true)); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths