Completed
Push — composer-autoload ( 2ad06b )
by Kamil
18:19
created

AddressBookContext::iTryToEditTheAddressOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $addressBookCreatePage exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
82
        UpdatePageInterface $addressBookUpdatePage,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $addressBookUpdatePage exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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(
237
            $this->addressBookIndexPage->hasAddressOf($fullName),
238
            sprintf('An address of "%s" should be on the list.', $fullName)
239
        );
240
    }
241
242
    /**
243
     * @Then I should still be on the address addition page
244
     */
245
    public function iShouldStillBeOnAddressAdditionPage()
246
    {
247
        Assert::true(
248
            $this->addressBookCreatePage->isOpen(),
249
            'The address creation page should be opened.'
250
        );
251
    }
252
253
    /**
254
     * @Then I should still be on the :fullName address edit page
255
     */
256
    public function iShouldStillBeOnTheAddressEditPage($fullName)
257
    {
258
        $address = $this->getAddressOf($fullName);
259
260
        Assert::true($this->addressBookUpdatePage->isOpen(['id' => $address->getId()]));
261
    }
262
263
    /**
264
     * @Then I should still have :value as my specified province
265
     */
266
    public function iShouldStillHaveAsMySpecifiedProvince($value)
267
    {
268
        $actualValue = $this->addressBookUpdatePage->getSpecifiedProvince();
269
270
        Assert::same(
271
            $actualValue,
272
            $value,
273
            sprintf('Address\'s province should be %s, but is %s.', $value, $actualValue)
274
        );
275
    }
276
277
    /**
278
     * @Then I should still have :value as my chosen province
279
     */
280
    public function iShouldStillHaveAsMyChosenProvince($value)
281
    {
282
        $actualValue = $this->addressBookUpdatePage->getSelectedProvince();
283
284
        Assert::same(
285
            $actualValue,
286
            $value,
287
            sprintf('Address\'s province should be %s, but is %s.', $value, $actualValue)
288
        );
289
    }
290
291
    /**
292
     * @Then I should be notified that the province needs to be specified
293
     */
294
    public function iShouldBeNotifiedThatTheProvinceNeedsToBeSpecified()
295
    {
296
        Assert::true(
297
            $this->addressBookCreatePage->hasProvinceValidationMessage(),
298
            'Province validation messages should be visible.'
299
        );
300
    }
301
302
    /**
303
     * @Then /^I should be notified about (\d+) errors$/
304
     */
305
    public function iShouldBeNotifiedAboutErrors($expectedCount)
306
    {
307
        $actualCount = $this->addressBookCreatePage->countValidationMessages();
308
309
        Assert::same(
310
            (int) $expectedCount,
311
            $actualCount,
312
            sprintf('There should be %d validation messages, but %d has been found.', $expectedCount, $actualCount)
313
        );
314
    }
315
316
    /**
317
     * @Then there should be no addresses
318
     */
319
    public function thereShouldBeNoAddresses()
320
    {
321
        Assert::true(
322
            $this->addressBookIndexPage->hasNoAddresses(),
323
            'There should be no addresses on the list.'
324
        );
325
    }
326
327
    /**
328
     * @Then I should not see the address assigned to :fullName
329
     */
330
    public function iShouldNotSeeAddressOf($fullName)
331
    {
332
        Assert::false(
333
            $this->addressBookIndexPage->hasAddressOf($fullName),
334
            sprintf('The address of "%s" should not be on the list.', $fullName)
335
        );
336
    }
337
338
    /**
339
     * @Then /^I should(?:| still) have a single address in my address book$/
340
     * @Then /^I should(?:| still) have (\d+) addresses in my address book$/
341
     */
342
    public function iShouldHaveAddresses($count = 1)
343
    {
344
        $this->addressBookIndexPage->open();
345
346
        $this->assertAddressesCountOnPage((int) $count);
347
    }
348
349
    /**
350
     * @Then I should be notified that the address has been successfully added
351
     */
352
    public function iShouldBeNotifiedThatAddressHasBeenSuccessfullyAdded()
353
    {
354
        $this->notificationChecker->checkNotification('Address has been successfully added.', NotificationType::success());
355
    }
356
357
    /**
358
     * @Then I should be notified that the address has been successfully deleted
359
     */
360
    public function iShouldBeNotifiedAboutSuccessfulDelete()
361
    {
362
        $this->notificationChecker->checkNotification('Address has been successfully deleted.', NotificationType::success());
363
    }
364
365
    /**
366
     * @Then I should be unable to edit their address
367
     */
368
    public function iShouldBeUnableToEditTheirAddress()
369
    {
370
        $address = $this->getAddressOf($this->sharedStorage->getLatestResource());
371
372
        Assert::false(
373
            $this->addressBookUpdatePage->isOpen(['id' => $address->getId()]),
374
            sprintf(
375
                'I should be unable to edit the address of "%s %s"',
376
                $address->getFirstName(),
377
                $address->getLastName()
378
            )
379
        );
380
    }
381
382
    /**
383
     * @Then I should be notified that the address has been successfully updated
384
     */
385
    public function iShouldBeNotifiedAboutSuccessfulUpdate()
386
    {
387
        $this->notificationChecker->checkNotification('Address has been successfully updated.', NotificationType::success());
388
    }
389
390
    /**
391
     * @Then I should be notified that the address has been set as default
392
     */
393
    public function iShouldBeNotifiedThatAddressHasBeenSetAsDefault()
394
    {
395
        $this->notificationChecker->checkNotification('Address has been set as default', NotificationType::success());
396
    }
397
398
    /**
399
     * @Then I should not have a default address
400
     */
401
    public function iShouldHaveNoDefaultAddress()
402
    {
403
        Assert::true(
404
            $this->addressBookIndexPage->hasNoDefaultAddress(),
405
            'There should be no default address.'
406
        );
407
    }
408
409
    /**
410
     * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) should be marked as my default address$/
411
     */
412
    public function addressShouldBeMarkedAsMyDefaultAddress(AddressInterface $address)
413
    {
414
        $actualFullName = $this->addressBookIndexPage->getFullNameOfDefaultAddress();
415
        $expectedFullName = sprintf('%s %s', $address->getFirstName(), $address->getLastName());
416
417
        Assert::same(
418
            $expectedFullName,
419
            $actualFullName,
420
            sprintf('The default address should be of "%s", but is of "%s".', $expectedFullName, $actualFullName)
421
        );
422
    }
423
424
    /**
425
     * @param string $fullName
426
     *
427
     * @return AddressInterface
428
     */
429
    private function getAddressOf($fullName)
430
    {
431
        list($firstName, $lastName) = explode(' ', $fullName);
432
433
        /** @var AddressInterface $address */
434
        $address = $this->addressRepository->findOneBy(['firstName' => $firstName, 'lastName' => $lastName]);
435
        Assert::notNull($address);
436
437
        return $address;
438
    }
439
440
    /**
441
     * @return SymfonyPageInterface
442
     */
443
    private function getCurrentPage()
444
    {
445
        return $this
446
            ->currentPageResolver
447
            ->getCurrentPageWithForm([
448
                $this->addressBookCreatePage,
449
                $this->addressBookUpdatePage
450
        ]);
451
    }
452
453
    /**
454
     * @param int $expectedCount
455
     *
456
     * @throws \InvalidArgumentException
457
     */
458
    private function assertAddressesCountOnPage($expectedCount)
459
    {
460
        $actualCount = $this->addressBookIndexPage->getAddressesCount();
461
462
        Assert::same(
463
            $expectedCount,
464
            $actualCount,
465
            sprintf(
466
                'There should be %d addresses on the list, but %d addresses has been found.',
467
                $expectedCount,
468
                $actualCount
469
            )
470
        );
471
    }
472
}
473