|
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 withPrefix($prefix) : CustomerBuilder |
|
110
|
|
|
{ |
|
111
|
|
|
$builder = clone $this; |
|
112
|
|
|
$builder->customer->setPrefix($prefix); |
|
113
|
|
|
return $builder; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function withFirstname($firstname) : CustomerBuilder |
|
117
|
|
|
{ |
|
118
|
|
|
$builder = clone $this; |
|
119
|
|
|
$builder->customer->setFirstname($firstname); |
|
120
|
|
|
return $builder; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function withMiddlename($middlename) : CustomerBuilder |
|
124
|
|
|
{ |
|
125
|
|
|
$builder = clone $this; |
|
126
|
|
|
$builder->customer->setMiddlename($middlename); |
|
127
|
|
|
return $builder; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function withLastname($lastname) : CustomerBuilder |
|
131
|
|
|
{ |
|
132
|
|
|
$builder = clone $this; |
|
133
|
|
|
$builder->customer->setLastname($lastname); |
|
134
|
|
|
return $builder; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function withSuffix($suffix) : CustomerBuilder |
|
138
|
|
|
{ |
|
139
|
|
|
$builder = clone $this; |
|
140
|
|
|
$builder->customer->setSuffix($suffix); |
|
141
|
|
|
return $builder; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function withTaxvat($taxvat) : CustomerBuilder |
|
145
|
|
|
{ |
|
146
|
|
|
$builder = clone $this; |
|
147
|
|
|
$builder->customer->setTaxvat($taxvat); |
|
148
|
|
|
return $builder; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function withCustomAttributes(array $values) : CustomerBuilder |
|
152
|
|
|
{ |
|
153
|
|
|
$builder = clone $this; |
|
154
|
|
|
foreach ($values as $code => $value) { |
|
155
|
|
|
$builder->customer->setCustomAttribute($code, $value); |
|
156
|
|
|
} |
|
157
|
|
|
return $builder; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function withConfirmation(string $confirmation) : CustomerBuilder |
|
161
|
|
|
{ |
|
162
|
|
|
$builder = clone $this; |
|
163
|
|
|
$builder->customer->setConfirmation($confirmation); |
|
164
|
|
|
return $builder; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function build() : CustomerInterface |
|
168
|
|
|
{ |
|
169
|
|
|
$builder = clone $this; |
|
170
|
|
|
if (!$builder->customer->getEmail()) { |
|
171
|
|
|
$builder->customer->setEmail(sha1(uniqid('', true)) . '@example.com'); |
|
172
|
|
|
} |
|
173
|
|
|
$addresses = array_map( |
|
174
|
|
|
function (AddressBuilder $addressBuilder) { |
|
175
|
|
|
return $addressBuilder->buildWithoutSave(); |
|
176
|
|
|
}, |
|
177
|
|
|
$builder->addressBuilders |
|
178
|
|
|
); |
|
179
|
|
|
$builder->customer->setAddresses($addresses); |
|
180
|
|
|
$customer = $builder->saveNewCustomer(); |
|
181
|
|
|
/* |
|
182
|
|
|
* Magento automatically sets random confirmation key for new account with password. |
|
183
|
|
|
* We need to save again with our own confirmation (null for confirmed customer) |
|
184
|
|
|
*/ |
|
185
|
|
|
$customer->setConfirmation($builder->customer->getConfirmation()); |
|
186
|
|
|
return $builder->customerRepository->save($customer); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) False positive: the method is used in build() on the cloned builder |
|
191
|
|
|
*/ |
|
192
|
|
|
private function saveNewCustomer(): CustomerInterface |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->customerRepository->save($this->customer, $this->encryptor->getHash($this->password, true)); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
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