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 Sylius\Component\Addressing\Model\AddressInterface; |
16
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
17
|
|
|
use Sylius\Component\Core\Test\Services\SharedStorageInterface; |
18
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
20
|
|
|
use Symfony\Component\Intl\Intl; |
21
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class UserContext implements Context |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RepositoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $userRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SharedStorageInterface |
35
|
|
|
*/ |
36
|
|
|
private $sharedStorage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var FactoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $userFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var FactoryInterface |
45
|
|
|
*/ |
46
|
|
|
private $customerFactory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FactoryInterface |
50
|
|
|
*/ |
51
|
|
|
private $addressFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param RepositoryInterface $userRepository |
55
|
|
|
* @param SharedStorageInterface $sharedStorage |
56
|
|
|
* @param FactoryInterface $userFactory |
57
|
|
|
* @param FactoryInterface $customerFactory |
58
|
|
|
* @param FactoryInterface $addressFactory |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
RepositoryInterface $userRepository, |
62
|
|
|
SharedStorageInterface $sharedStorage, |
63
|
|
|
FactoryInterface $userFactory, |
64
|
|
|
FactoryInterface $customerFactory, |
65
|
|
|
FactoryInterface $addressFactory |
66
|
|
|
) { |
67
|
|
|
$this->userRepository = $userRepository; |
68
|
|
|
$this->sharedStorage = $sharedStorage; |
69
|
|
|
$this->userFactory = $userFactory; |
70
|
|
|
$this->customerFactory = $customerFactory; |
71
|
|
|
$this->addressFactory = $addressFactory; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Given there is user :email identified by :password |
76
|
|
|
*/ |
77
|
|
|
public function thereIsUserIdentifiedBy($email, $password) |
78
|
|
|
{ |
79
|
|
|
$customer = $this->createCustomer(); |
80
|
|
|
$user = $this->createUser($customer, $email, $password); |
81
|
|
|
|
82
|
|
|
$this->sharedStorage->setCurrentResource('user', $user); |
83
|
|
|
$this->userRepository->add($user); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @Given there is user :email identified by :password, with :country as shipping country |
88
|
|
|
*/ |
89
|
|
|
public function thereIsUserWithShippingCountry($email, $password, $country) |
90
|
|
|
{ |
91
|
|
|
$customer = $this->createCustomer(); |
92
|
|
|
$user = $this->createUser($customer, $email, $password); |
93
|
|
|
|
94
|
|
|
$customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country)); |
95
|
|
|
|
96
|
|
|
$this->sharedStorage->setCurrentResource('user', $user); |
97
|
|
|
$this->userRepository->add($user); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $firstName |
102
|
|
|
* @param string $lastName |
103
|
|
|
* |
104
|
|
|
* @return CustomerInterface |
105
|
|
|
*/ |
106
|
|
|
private function createCustomer($firstName = 'John', $lastName = 'Doe') |
107
|
|
|
{ |
108
|
|
|
$customer = $this->customerFactory->createNew(); |
109
|
|
|
$customer->setFirstName($firstName); |
110
|
|
|
$customer->setLastName($lastName); |
111
|
|
|
|
112
|
|
|
return $customer; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param CustomerInterface $customer |
117
|
|
|
* @param string $email |
118
|
|
|
* @param string $password |
119
|
|
|
* |
120
|
|
|
* @return UserInterface |
121
|
|
|
*/ |
122
|
|
|
private function createUser( |
123
|
|
|
CustomerInterface $customer, |
124
|
|
|
$email = '[email protected]', |
125
|
|
|
$password = 'testPassword' |
126
|
|
|
) { |
127
|
|
|
$user = $this->userFactory->createNew(); |
128
|
|
|
|
129
|
|
|
$user->setCustomer($customer); |
130
|
|
|
$user->setUsername($email); |
131
|
|
|
$user->setEmail($email); |
132
|
|
|
$user->setPlainPassword($password); |
133
|
|
|
|
134
|
|
|
return $user; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $firstName |
139
|
|
|
* @param string $lastName |
140
|
|
|
* @param string $country |
141
|
|
|
* @param string $street |
142
|
|
|
* @param string $city |
143
|
|
|
* @param string $postcode |
144
|
|
|
* |
145
|
|
|
* @return AddressInterface |
146
|
|
|
*/ |
147
|
|
|
private function createAddress( |
148
|
|
|
$firstName, |
149
|
|
|
$lastName, |
150
|
|
|
$country = 'United States', |
151
|
|
|
$street = 'Jones St. 114', |
152
|
|
|
$city = 'Paradise City', |
153
|
|
|
$postcode = '99999' |
154
|
|
|
) { |
155
|
|
|
$address = $this->addressFactory->createNew(); |
156
|
|
|
$address->setFirstName($firstName); |
157
|
|
|
$address->setLastName($lastName); |
158
|
|
|
$address->setStreet($street); |
159
|
|
|
$address->setCity($city); |
160
|
|
|
$address->setPostcode($postcode); |
161
|
|
|
$address->setCountryCode($this->getCountryCodeByEnglishCountryName($country)); |
162
|
|
|
|
163
|
|
|
return $address; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $name |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
* |
171
|
|
|
* @throws \InvalidArgumentException If name is not found in country code registry. |
172
|
|
|
*/ |
173
|
|
|
private function getCountryCodeByEnglishCountryName($name) |
174
|
|
|
{ |
175
|
|
|
$names = Intl::getRegionBundle()->getCountryNames('en'); |
176
|
|
|
$countryCode = array_search(trim($name), $names); |
177
|
|
|
|
178
|
|
|
if (null === $countryCode) { |
179
|
|
|
throw new \InvalidArgumentException(sprintf( |
180
|
|
|
'Country "%s" not found! Available names: %s.', $name, implode(', ', $names) |
181
|
|
|
)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $countryCode; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|