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\Ui\Shop; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Sylius\Behat\NotificationType; |
16
|
|
|
use Sylius\Behat\Page\Shop\Account\AddressBook\CreatePageInterface; |
17
|
|
|
use Sylius\Behat\Page\Shop\Account\AddressBook\IndexPageInterface; |
18
|
|
|
use Sylius\Behat\Page\Shop\Account\AddressBook\UpdatePageInterface; |
19
|
|
|
use Sylius\Behat\Page\SymfonyPageInterface; |
20
|
|
|
use Sylius\Behat\Service\NotificationCheckerInterface; |
21
|
|
|
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface; |
22
|
|
|
use Sylius\Behat\Service\SharedStorageInterface; |
23
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
24
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
25
|
|
|
use Webmozart\Assert\Assert; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Anna Walasek <[email protected]> |
29
|
|
|
* @author Jan Góralski <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class AddressBookContext implements Context |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var SharedStorageInterface |
35
|
|
|
*/ |
36
|
|
|
private $sharedStorage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var RepositoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $addressRepository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var IndexPageInterface |
45
|
|
|
*/ |
46
|
|
|
private $addressBookIndexPage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var CreatePageInterface |
50
|
|
|
*/ |
51
|
|
|
private $addressBookCreatePage; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var UpdatePageInterface |
55
|
|
|
*/ |
56
|
|
|
private $addressBookUpdatePage; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var CurrentPageResolverInterface |
60
|
|
|
*/ |
61
|
|
|
private $currentPageResolver; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var NotificationCheckerInterface |
65
|
|
|
*/ |
66
|
|
|
private $notificationChecker; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param SharedStorageInterface $sharedStorage |
70
|
|
|
* @param RepositoryInterface $addressRepository |
71
|
|
|
* @param IndexPageInterface $addressBookIndexPage |
72
|
|
|
* @param CreatePageInterface $addressBookCreatePage |
73
|
|
|
* @param UpdatePageInterface $addressBookUpdatePage |
74
|
|
|
* @param CurrentPageResolverInterface $currentPageResolver |
75
|
|
|
* @param NotificationCheckerInterface $notificationChecker |
76
|
|
|
*/ |
77
|
|
|
public function __construct( |
78
|
|
|
SharedStorageInterface $sharedStorage, |
79
|
|
|
RepositoryInterface $addressRepository, |
80
|
|
|
IndexPageInterface $addressBookIndexPage, |
81
|
|
|
CreatePageInterface $addressBookCreatePage, |
|
|
|
|
82
|
|
|
UpdatePageInterface $addressBookUpdatePage, |
|
|
|
|
83
|
|
|
CurrentPageResolverInterface $currentPageResolver, |
84
|
|
|
NotificationCheckerInterface $notificationChecker |
85
|
|
|
) { |
86
|
|
|
$this->sharedStorage = $sharedStorage; |
87
|
|
|
$this->addressRepository = $addressRepository; |
88
|
|
|
$this->addressBookIndexPage = $addressBookIndexPage; |
89
|
|
|
$this->addressBookCreatePage = $addressBookCreatePage; |
90
|
|
|
$this->addressBookUpdatePage = $addressBookUpdatePage; |
91
|
|
|
$this->currentPageResolver = $currentPageResolver; |
92
|
|
|
$this->notificationChecker = $notificationChecker; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @Given I am editing the address of :fullName |
97
|
|
|
*/ |
98
|
|
|
public function iEditAddressOf($fullName) |
99
|
|
|
{ |
100
|
|
|
$this->sharedStorage->set('full_name', $fullName); |
101
|
|
|
|
102
|
|
|
$this->addressBookIndexPage->open(); |
103
|
|
|
$this->addressBookIndexPage->editAddress($fullName); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @When I set the address of :fullName as default |
108
|
|
|
*/ |
109
|
|
|
public function iSetTheAddressOfAsDefault($fullName) |
110
|
|
|
{ |
111
|
|
|
$this->sharedStorage->set('full_name', $fullName); |
112
|
|
|
|
113
|
|
|
$this->addressBookIndexPage->setAsDefault($fullName); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @When I want to add a new address to my address book |
118
|
|
|
*/ |
119
|
|
|
public function iWantToAddANewAddressToMyAddressBook() |
120
|
|
|
{ |
121
|
|
|
$this->addressBookCreatePage->open(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @Given I am browsing my address book |
126
|
|
|
* @When I browse my address book |
127
|
|
|
*/ |
128
|
|
|
public function iBrowseMyAddresses() |
129
|
|
|
{ |
130
|
|
|
$this->addressBookIndexPage->open(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @When I specify :provinceName as my province |
135
|
|
|
*/ |
136
|
|
|
public function iSpecifyAsMyProvince($provinceName) |
137
|
|
|
{ |
138
|
|
|
$this->addressBookUpdatePage->specifyProvince($provinceName); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @When I choose :provinceName as my province |
143
|
|
|
*/ |
144
|
|
|
public function iChooseAsMyProvince($provinceName) |
145
|
|
|
{ |
146
|
|
|
$this->addressBookUpdatePage->selectProvince($provinceName); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @When I choose :countryName as my country |
151
|
|
|
*/ |
152
|
|
|
public function iChooseAsMyCountry($countryName) |
153
|
|
|
{ |
154
|
|
|
/** @var CreatePageInterface|UpdatePageInterface $currentPage */ |
155
|
|
|
$currentPage = $this->getCurrentPage(); |
156
|
|
|
$currentPage->selectCountry($countryName); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @When /^I change the ([^"]+) to "([^"]+)"$/ |
161
|
|
|
* @When /^I remove the ([^"]+)$/ |
162
|
|
|
*/ |
163
|
|
|
public function iChangeMyTo($field, $value = null) |
164
|
|
|
{ |
165
|
|
|
$this->addressBookUpdatePage->fillField($field, $value); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @When /^I specify the (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
170
|
|
|
*/ |
171
|
|
|
public function iSpecifyTheAddressAs(AddressInterface $address) |
172
|
|
|
{ |
173
|
|
|
$this->addressBookCreatePage->fillAddressData($address); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @When I leave every field empty |
178
|
|
|
*/ |
179
|
|
|
public function iLeaveEveryFieldEmpty() |
180
|
|
|
{ |
181
|
|
|
// Intentionally left empty |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @When I add it |
186
|
|
|
*/ |
187
|
|
|
public function iAddIt() |
188
|
|
|
{ |
189
|
|
|
$this->addressBookCreatePage->addAddress(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @When I save my changed address |
194
|
|
|
*/ |
195
|
|
|
public function iSaveChangedAddress() |
196
|
|
|
{ |
197
|
|
|
$this->addressBookUpdatePage->saveChanges(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @When I delete the :fullName address |
202
|
|
|
*/ |
203
|
|
|
public function iDeleteTheAddress($fullname) |
204
|
|
|
{ |
205
|
|
|
$this->addressBookIndexPage->deleteAddress($fullname); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @When /^I try to edit the address of "([^"]+)"$/ |
210
|
|
|
*/ |
211
|
|
|
public function iTryToEditTheAddressOf($fullName) |
212
|
|
|
{ |
213
|
|
|
$address = $this->getAddressOf($fullName); |
214
|
|
|
|
215
|
|
|
$this->sharedStorage->set('full_name', sprintf('%s %s', $address->getFirstName(), $address->getLastName())); |
216
|
|
|
|
217
|
|
|
$this->addressBookUpdatePage->tryToOpen(['id' => $address->getId()]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @Then /^it should contain "([^"]+)"$/ |
222
|
|
|
*/ |
223
|
|
|
public function itShouldContain($value) |
224
|
|
|
{ |
225
|
|
|
$fullName = $this->sharedStorage->get('full_name'); |
226
|
|
|
|
227
|
|
|
$this->addressBookIndexPage->addressOfContains($fullName, $value); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @Then this address should be assigned to :fullName |
232
|
|
|
* @Then /^the address assigned to "([^"]+)" should (appear|be) in my book$/ |
233
|
|
|
*/ |
234
|
|
|
public function thisAddressShouldHavePersonFirstNameAndLastName($fullName) |
235
|
|
|
{ |
236
|
|
|
Assert::true($this->addressBookIndexPage->hasAddressOf($fullName)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @Then I should still be on the address addition page |
241
|
|
|
*/ |
242
|
|
|
public function iShouldStillBeOnAddressAdditionPage() |
243
|
|
|
{ |
244
|
|
|
Assert::true($this->addressBookCreatePage->isOpen()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @Then I should still be on the :fullName address edit page |
249
|
|
|
*/ |
250
|
|
|
public function iShouldStillBeOnTheAddressEditPage($fullName) |
251
|
|
|
{ |
252
|
|
|
$address = $this->getAddressOf($fullName); |
253
|
|
|
|
254
|
|
|
Assert::true($this->addressBookUpdatePage->isOpen(['id' => $address->getId()])); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @Then I should still have :value as my specified province |
259
|
|
|
*/ |
260
|
|
|
public function iShouldStillHaveAsMySpecifiedProvince($value) |
261
|
|
|
{ |
262
|
|
|
Assert::same($this->addressBookUpdatePage->getSpecifiedProvince(), $value); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @Then I should still have :value as my chosen province |
267
|
|
|
*/ |
268
|
|
|
public function iShouldStillHaveAsMyChosenProvince($value) |
269
|
|
|
{ |
270
|
|
|
Assert::same($this->addressBookUpdatePage->getSelectedProvince(), $value); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @Then I should be notified that the province needs to be specified |
275
|
|
|
*/ |
276
|
|
|
public function iShouldBeNotifiedThatTheProvinceNeedsToBeSpecified() |
277
|
|
|
{ |
278
|
|
|
Assert::true($this->addressBookCreatePage->hasProvinceValidationMessage()); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @Then /^I should be notified about (\d+) errors$/ |
283
|
|
|
*/ |
284
|
|
|
public function iShouldBeNotifiedAboutErrors($expectedCount) |
285
|
|
|
{ |
286
|
|
|
Assert::same($this->addressBookCreatePage->countValidationMessages(), (int) $expectedCount); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @Then there should be no addresses |
291
|
|
|
*/ |
292
|
|
|
public function thereShouldBeNoAddresses() |
293
|
|
|
{ |
294
|
|
|
Assert::true($this->addressBookIndexPage->hasNoAddresses()); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @Then I should not see the address assigned to :fullName |
299
|
|
|
*/ |
300
|
|
|
public function iShouldNotSeeAddressOf($fullName) |
301
|
|
|
{ |
302
|
|
|
Assert::false($this->addressBookIndexPage->hasAddressOf($fullName)); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @Then /^I should(?:| still) have a single address in my address book$/ |
307
|
|
|
* @Then /^I should(?:| still) have (\d+) addresses in my address book$/ |
308
|
|
|
*/ |
309
|
|
|
public function iShouldHaveAddresses($count = 1) |
310
|
|
|
{ |
311
|
|
|
$this->addressBookIndexPage->open(); |
312
|
|
|
|
313
|
|
|
Assert::same($this->addressBookIndexPage->getAddressesCount(), (int) $count); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @Then I should be notified that the address has been successfully added |
318
|
|
|
*/ |
319
|
|
|
public function iShouldBeNotifiedThatAddressHasBeenSuccessfullyAdded() |
320
|
|
|
{ |
321
|
|
|
$this->notificationChecker->checkNotification('Address has been successfully added.', NotificationType::success()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @Then I should be notified that the address has been successfully deleted |
326
|
|
|
*/ |
327
|
|
|
public function iShouldBeNotifiedAboutSuccessfulDelete() |
328
|
|
|
{ |
329
|
|
|
$this->notificationChecker->checkNotification('Address has been successfully deleted.', NotificationType::success()); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @Then I should be unable to edit their address |
334
|
|
|
*/ |
335
|
|
|
public function iShouldBeUnableToEditTheirAddress() |
336
|
|
|
{ |
337
|
|
|
$address = $this->getAddressOf($this->sharedStorage->getLatestResource()); |
338
|
|
|
|
339
|
|
|
Assert::false($this->addressBookUpdatePage->isOpen(['id' => $address->getId()])); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @Then I should be notified that the address has been successfully updated |
344
|
|
|
*/ |
345
|
|
|
public function iShouldBeNotifiedAboutSuccessfulUpdate() |
346
|
|
|
{ |
347
|
|
|
$this->notificationChecker->checkNotification('Address has been successfully updated.', NotificationType::success()); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @Then I should be notified that the address has been set as default |
352
|
|
|
*/ |
353
|
|
|
public function iShouldBeNotifiedThatAddressHasBeenSetAsDefault() |
354
|
|
|
{ |
355
|
|
|
$this->notificationChecker->checkNotification('Address has been set as default', NotificationType::success()); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @Then I should not have a default address |
360
|
|
|
*/ |
361
|
|
|
public function iShouldHaveNoDefaultAddress() |
362
|
|
|
{ |
363
|
|
|
Assert::true($this->addressBookIndexPage->hasNoDefaultAddress()); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) should(?:| still) be marked as my default address$/ |
368
|
|
|
*/ |
369
|
|
|
public function addressShouldBeMarkedAsMyDefaultAddress(AddressInterface $address) |
370
|
|
|
{ |
371
|
|
|
$actualFullName = $this->addressBookIndexPage->getFullNameOfDefaultAddress(); |
372
|
|
|
$expectedFullName = sprintf('%s %s', $address->getFirstName(), $address->getLastName()); |
373
|
|
|
|
374
|
|
|
Assert::same($actualFullName, $expectedFullName); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param string $fullName |
379
|
|
|
* |
380
|
|
|
* @return AddressInterface |
381
|
|
|
*/ |
382
|
|
|
private function getAddressOf($fullName) |
383
|
|
|
{ |
384
|
|
|
list($firstName, $lastName) = explode(' ', $fullName); |
385
|
|
|
|
386
|
|
|
/** @var AddressInterface $address */ |
387
|
|
|
$address = $this->addressRepository->findOneBy(['firstName' => $firstName, 'lastName' => $lastName]); |
388
|
|
|
Assert::notNull($address); |
389
|
|
|
|
390
|
|
|
return $address; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @return SymfonyPageInterface |
395
|
|
|
*/ |
396
|
|
|
private function getCurrentPage() |
397
|
|
|
{ |
398
|
|
|
return $this |
399
|
|
|
->currentPageResolver |
400
|
|
|
->getCurrentPageWithForm([ |
401
|
|
|
$this->addressBookCreatePage, |
402
|
|
|
$this->addressBookUpdatePage |
403
|
|
|
]); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.