1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Context\Setup; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Sylius\Behat\Service\SharedStorageInterface; |
17
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
18
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
20
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
21
|
|
|
use Sylius\Component\Customer\Model\CustomerGroupInterface; |
22
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Anna Walasek <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class CustomerContext implements Context |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var SharedStorageInterface |
31
|
|
|
*/ |
32
|
|
|
private $sharedStorage; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CustomerRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $customerRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ObjectManager |
41
|
|
|
*/ |
42
|
|
|
private $customerManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FactoryInterface |
46
|
|
|
*/ |
47
|
|
|
private $customerFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FactoryInterface |
51
|
|
|
*/ |
52
|
|
|
private $userFactory; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param SharedStorageInterface $sharedStorage |
56
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
57
|
|
|
* @param ObjectManager $customerManager |
58
|
|
|
* @param FactoryInterface $customerFactory |
59
|
|
|
* @param FactoryInterface $userFactory |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
SharedStorageInterface $sharedStorage, |
63
|
|
|
CustomerRepositoryInterface $customerRepository, |
64
|
|
|
ObjectManager $customerManager, |
65
|
|
|
FactoryInterface $customerFactory, |
66
|
|
|
FactoryInterface $userFactory |
67
|
|
|
) { |
68
|
|
|
$this->sharedStorage = $sharedStorage; |
69
|
|
|
$this->customerRepository = $customerRepository; |
70
|
|
|
$this->customerManager = $customerManager; |
71
|
|
|
$this->customerFactory = $customerFactory; |
72
|
|
|
$this->userFactory = $userFactory; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Given the store has customer :name with email :email |
77
|
|
|
*/ |
78
|
|
|
public function theStoreHasCustomerWithNameAndEmail($name, $email) |
79
|
|
|
{ |
80
|
|
|
$partsOfName = explode(' ', $name); |
81
|
|
|
$this->createCustomer($email, $partsOfName[0], $partsOfName[1]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Given the store has customer :email |
86
|
|
|
*/ |
87
|
|
|
public function theStoreHasCustomer($email) |
88
|
|
|
{ |
89
|
|
|
$this->createCustomer($email); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Given the store has customer :email with first name :firstName |
94
|
|
|
*/ |
95
|
|
|
public function theStoreHasCustomerWithFirstName($email, $firstName) |
96
|
|
|
{ |
97
|
|
|
$this->createCustomer($email, $firstName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @Given the store has customer :email with last name :lastName |
102
|
|
|
*/ |
103
|
|
|
public function theStoreHasCustomerWithLastName($email, $lastName) |
104
|
|
|
{ |
105
|
|
|
$this->createCustomer($email, null, $lastName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Given the store has customer :email with name :fullName since :since |
110
|
|
|
*/ |
111
|
|
|
public function theStoreHasCustomerWithNameAndRegistrationDate($email, $fullName, $since) |
112
|
|
|
{ |
113
|
|
|
$names = explode(' ', $fullName); |
114
|
|
|
$this->createCustomer($email, $names[0], $names[1], new \DateTime($since)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @Given there is disabled customer account :email with password :password |
119
|
|
|
*/ |
120
|
|
|
public function thereIsDisabledCustomerAccountWithPassword($email, $password) |
121
|
|
|
{ |
122
|
|
|
$this->createCustomerWithUserAccount($email, $password, false); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @Given there is enabled customer account :email with password :password |
127
|
|
|
* @Given there is a customer account :email identified by :password |
128
|
|
|
*/ |
129
|
|
|
public function theStoreHasEnabledCustomerAccountWithPassword($email, $password) |
130
|
|
|
{ |
131
|
|
|
$this->createCustomerWithUserAccount($email, $password, true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @Given there is a customer :name identified by an email :email and a password :password |
136
|
|
|
*/ |
137
|
|
|
public function theStoreHasCustomerAccountWithEmailAndPassword($name, $email, $password) |
138
|
|
|
{ |
139
|
|
|
$names = explode(' ', $name); |
140
|
|
|
$firstName = $names[0]; |
141
|
|
|
$lastName = count($names) > 1 ? $names[1] : null; |
142
|
|
|
|
143
|
|
|
$this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @Given there is an administrator :name identified by an email :email and a password :password |
148
|
|
|
*/ |
149
|
|
|
public function thereIsAdministratorIdentifiedByEmailAndPassword($name, $email, $password) |
150
|
|
|
{ |
151
|
|
|
$names = explode(' ', $name); |
152
|
|
|
$firstName = $names[0]; |
153
|
|
|
$lastName = count($names) > 1 ? $names[1] : null; |
154
|
|
|
|
155
|
|
|
$this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName, 'ROLE_ADMINISTRATION_ACCESS'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @Given /^(his) default (address is "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)" for "(?:[^"]+)")$/ |
160
|
|
|
* @Given /^(his) default (address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
161
|
|
|
*/ |
162
|
|
|
public function heHasDefaultAddress(CustomerInterface $customer, AddressInterface $address) |
163
|
|
|
{ |
164
|
|
|
$customer->setDefaultAddress($address); |
165
|
|
|
|
166
|
|
|
$this->customerManager->flush(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @Given /^(the customer) subscribed to the newsletter$/ |
171
|
|
|
*/ |
172
|
|
|
public function theCustomerSubscribedToTheNewsletter(CustomerInterface $customer) |
173
|
|
|
{ |
174
|
|
|
$customer->setSubscribedToNewsletter(true); |
175
|
|
|
|
176
|
|
|
$this->customerManager->flush(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @Given /^(I) have an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in my address book$/ |
181
|
|
|
*/ |
182
|
|
|
public function iHaveAnAddressInAddressBook(ShopUserInterface $user, AddressInterface $address) |
183
|
|
|
{ |
184
|
|
|
/** @var CustomerInterface $customer */ |
185
|
|
|
$customer = $user->getCustomer(); |
186
|
|
|
|
187
|
|
|
$this->thisCustomerHasAnAddressInAddressBook($customer, $address); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @Given /^(this customer) has an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in their address book$/ |
192
|
|
|
*/ |
193
|
|
|
public function thisCustomerHasAnAddressInAddressBook(CustomerInterface $customer, AddressInterface $address) |
194
|
|
|
{ |
195
|
|
|
$customer->addAddress($address); |
196
|
|
|
|
197
|
|
|
$this->customerManager->flush(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @Given /^(this customer) verified their email$/ |
202
|
|
|
*/ |
203
|
|
|
public function theCustomerVerifiedTheirEmail(CustomerInterface $customer) |
204
|
|
|
{ |
205
|
|
|
$customer->getUser()->setVerifiedAt(new \DateTime()); |
206
|
|
|
|
207
|
|
|
$this->customerManager->flush(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @Given /^(the customer) belongs to (group "([^"]+)")$/ |
212
|
|
|
*/ |
213
|
|
|
public function theCustomerBelongsToGroup(CustomerInterface $customer, CustomerGroupInterface $customerGroup) |
214
|
|
|
{ |
215
|
|
|
$customer->setGroup($customerGroup); |
216
|
|
|
|
217
|
|
|
$this->customerManager->flush(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $email |
222
|
|
|
* @param string|null $firstName |
223
|
|
|
* @param string|null $lastName |
224
|
|
|
* @param \DateTime|null $createdAt |
225
|
|
|
*/ |
226
|
|
|
private function createCustomer($email, $firstName = null, $lastName = null, \DateTime $createdAt = null) |
227
|
|
|
{ |
228
|
|
|
/** @var CustomerInterface $customer */ |
229
|
|
|
$customer = $this->customerFactory->createNew(); |
230
|
|
|
|
231
|
|
|
$customer->setFirstName($firstName); |
232
|
|
|
$customer->setLastName($lastName); |
233
|
|
|
$customer->setEmail($email); |
234
|
|
|
if (null !== $createdAt) { |
235
|
|
|
$customer->setCreatedAt($createdAt); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$this->sharedStorage->set('customer', $customer); |
239
|
|
|
$this->customerRepository->add($customer); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $email |
244
|
|
|
* @param string $password |
245
|
|
|
* @param bool $enabled |
246
|
|
|
* @param string|null $firstName |
247
|
|
|
* @param string|null $lastName |
248
|
|
|
* @param string|null $role |
249
|
|
|
*/ |
250
|
|
|
private function createCustomerWithUserAccount( |
251
|
|
|
$email, |
252
|
|
|
$password, |
253
|
|
|
$enabled = true, |
254
|
|
|
$firstName = null, |
255
|
|
|
$lastName = null, |
256
|
|
|
$role = null |
257
|
|
|
) { |
258
|
|
|
$user = $this->userFactory->createNew(); |
259
|
|
|
/** @var CustomerInterface $customer */ |
260
|
|
|
$customer = $this->customerFactory->createNew(); |
261
|
|
|
|
262
|
|
|
$customer->setFirstName($firstName); |
263
|
|
|
$customer->setLastName($lastName); |
264
|
|
|
$customer->setEmail($email); |
265
|
|
|
|
266
|
|
|
$user->setUsername($email); |
267
|
|
|
$user->setPlainPassword($password); |
268
|
|
|
$user->setEnabled($enabled); |
269
|
|
|
$user->addRole($role); |
270
|
|
|
|
271
|
|
|
$customer->setUser($user); |
272
|
|
|
|
273
|
|
|
$this->sharedStorage->set('customer', $customer); |
274
|
|
|
$this->customerRepository->add($customer); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|