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