1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Customer; |
4
|
|
|
|
5
|
|
|
use Faker\Factory as FakerFactory; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Magento\Customer\Api\AddressRepositoryInterface; |
8
|
|
|
use Magento\Customer\Api\Data\AddressInterface; |
9
|
|
|
use Magento\Framework\Exception\LocalizedException; |
10
|
|
|
use Magento\Framework\ObjectManagerInterface; |
11
|
|
|
use Magento\Directory\Model\Region; |
12
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Builder to be used by fixtures |
16
|
|
|
*/ |
17
|
|
|
class AddressBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var AddressInterface |
21
|
|
|
*/ |
22
|
|
|
private $address; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AddressRepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $addressRepository; |
28
|
|
|
|
29
|
|
|
public function __construct(AddressRepositoryInterface $addressRepository, AddressInterface $address) |
30
|
|
|
{ |
31
|
|
|
$this->address = $address; |
32
|
|
|
$this->addressRepository = $addressRepository; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __clone() |
36
|
|
|
{ |
37
|
|
|
$this->address = clone $this->address; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function anAddress( |
41
|
|
|
ObjectManagerInterface $objectManager = null, |
42
|
|
|
string $locale = 'de_DE' |
43
|
|
|
): AddressBuilder { |
44
|
|
|
if ($objectManager === null) { |
45
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$address = self::prepareFakeAddress($objectManager, $locale); |
49
|
|
|
return new self($objectManager->create(AddressRepositoryInterface::class), $address); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function aCompanyAddress( |
53
|
|
|
ObjectManagerInterface $objectManager = null, |
54
|
|
|
string $locale = 'de_DE', |
55
|
|
|
string $vatId = '1234567890' |
56
|
|
|
): AddressBuilder { |
57
|
|
|
if ($objectManager === null) { |
58
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$address = self::prepareFakeAddress($objectManager, $locale); |
62
|
|
|
$address->setVatId($vatId); |
63
|
|
|
return new self($objectManager->create(AddressRepositoryInterface::class), $address); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function asDefaultShipping(): AddressBuilder |
67
|
|
|
{ |
68
|
|
|
$builder = clone $this; |
69
|
|
|
$builder->address->setIsDefaultShipping(true); |
70
|
|
|
return $builder; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function asDefaultBilling(): AddressBuilder |
74
|
|
|
{ |
75
|
|
|
$builder = clone $this; |
76
|
|
|
$builder->address->setIsDefaultBilling(true); |
77
|
|
|
return $builder; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function withPrefix($prefix): AddressBuilder |
81
|
|
|
{ |
82
|
|
|
$builder = clone $this; |
83
|
|
|
$builder->address->setPrefix($prefix); |
84
|
|
|
return $builder; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function withFirstname($firstname): AddressBuilder |
88
|
|
|
{ |
89
|
|
|
$builder = clone $this; |
90
|
|
|
$builder->address->setFirstname($firstname); |
91
|
|
|
return $builder; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function withLastname($lastname): AddressBuilder |
95
|
|
|
{ |
96
|
|
|
$builder = clone $this; |
97
|
|
|
$builder->address->setLastname($lastname); |
98
|
|
|
return $builder; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function withStreet($street): AddressBuilder |
102
|
|
|
{ |
103
|
|
|
$builder = clone $this; |
104
|
|
|
$builder->address->setStreet((array)$street); |
105
|
|
|
return $builder; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function withCompany($company): AddressBuilder |
109
|
|
|
{ |
110
|
|
|
$builder = clone $this; |
111
|
|
|
$builder->address->setCompany($company); |
112
|
|
|
return $builder; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function withTelephone($telephone): AddressBuilder |
116
|
|
|
{ |
117
|
|
|
$builder = clone $this; |
118
|
|
|
$builder->address->setTelephone($telephone); |
119
|
|
|
return $builder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function withPostcode($postcode): AddressBuilder |
123
|
|
|
{ |
124
|
|
|
$builder = clone $this; |
125
|
|
|
$builder->address->setPostcode($postcode); |
126
|
|
|
return $builder; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function withCity($city): AddressBuilder |
130
|
|
|
{ |
131
|
|
|
$builder = clone $this; |
132
|
|
|
$builder->address->setCity($city); |
133
|
|
|
return $builder; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function withCountryId($countryId): AddressBuilder |
137
|
|
|
{ |
138
|
|
|
$builder = clone $this; |
139
|
|
|
$builder->address->setCountryId($countryId); |
140
|
|
|
return $builder; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function withRegionId($regionId): AddressBuilder |
144
|
|
|
{ |
145
|
|
|
$builder = clone $this; |
146
|
|
|
$builder->address->setRegionId($regionId); |
147
|
|
|
return $builder; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function withCustomAttributes(array $values): AddressBuilder |
151
|
|
|
{ |
152
|
|
|
$builder = clone $this; |
153
|
|
|
foreach ($values as $code => $value) { |
154
|
|
|
$builder->address->setCustomAttribute($code, $value); |
155
|
|
|
} |
156
|
|
|
return $builder; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return AddressInterface |
161
|
|
|
* @throws LocalizedException |
162
|
|
|
*/ |
163
|
|
|
public function build(): AddressInterface |
164
|
|
|
{ |
165
|
|
|
return $this->addressRepository->save($this->address); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function buildWithoutSave(): AddressInterface |
169
|
|
|
{ |
170
|
|
|
return clone $this->address; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private static function prepareFakeAddress( |
174
|
|
|
ObjectManagerInterface $objectManager, |
175
|
|
|
string $locale = 'de_DE' |
176
|
|
|
): AddressInterface { |
177
|
|
|
$faker = FakerFactory::create($locale); |
178
|
|
|
$countryCode = substr($locale, -2); |
179
|
|
|
|
180
|
|
|
try { |
181
|
|
|
$region = $faker->province; |
182
|
|
|
} catch (InvalidArgumentException $exception) { |
|
|
|
|
183
|
|
|
$region = $faker->state; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$regionId = $objectManager->create(Region::class)->loadByName($region, $countryCode)->getId(); |
187
|
|
|
|
188
|
|
|
/** @var AddressInterface $address */ |
189
|
|
|
$address = $objectManager->create(AddressInterface::class); |
190
|
|
|
$address |
191
|
|
|
->setTelephone($faker->phoneNumber) |
192
|
|
|
->setPostcode($faker->postcode) |
193
|
|
|
->setCountryId($countryCode) |
194
|
|
|
->setCity($faker->city) |
195
|
|
|
->setCompany($faker->company) |
196
|
|
|
->setStreet([$faker->streetAddress]) |
197
|
|
|
->setLastname($faker->lastName) |
198
|
|
|
->setFirstname($faker->firstName) |
199
|
|
|
->setRegionId($regionId); |
200
|
|
|
|
201
|
|
|
return $address; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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