Completed
Push — remove-content-bundle ( 201341...8d07b3 )
by Kamil
52:29 queued 32:39
created

AddressBookContext   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 410
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 34
c 0
b 0
f 0
lcom 2
cbo 10
dl 0
loc 410
rs 9.2

34 Methods

Rating   Name   Duplication   Size   Complexity  
A iEditAddressOf() 0 7 1
A __construct() 0 17 1
A iSetTheAddressOfAsDefault() 0 6 1
A iWantToAddANewAddressToMyAddressBook() 0 4 1
A iBrowseMyAddresses() 0 4 1
A iSpecifyAsMyProvince() 0 4 1
A iChooseAsMyProvince() 0 4 1
A iChooseAsMyCountry() 0 6 1
A iChangeMyTo() 0 4 1
A iSpecifyTheAddressAs() 0 4 1
A iLeaveEveryFieldEmpty() 0 4 1
A iAddIt() 0 4 1
A iSaveChangedAddress() 0 4 1
A iDeleteTheAddress() 0 4 1
A iTryToEditTheAddressOf() 0 8 1
A itShouldContain() 0 6 1
A iShouldSeeASingleAddressInTheList() 0 4 1
A thisAddressShouldHavePersonFirstNameAndLastName() 0 7 1
A iShouldStillBeOnAddressAdditionPage() 0 7 1
A iShouldBeNotifiedThatTheProvinceNeedsToBeSpecified() 0 7 1
A iShouldBeNotifiedAboutErrors() 0 10 1
A thereShouldBeNoAddresses() 0 7 1
A iShouldNotSeeAddressOf() 0 7 1
A iShouldHaveAddresses() 0 6 1
A iShouldBeNotifiedThatAddressHasBeenSuccessfullyAdded() 0 4 1
A iShouldBeNotifiedAboutSuccessfulDelete() 0 4 1
A iShouldBeUnableToEditTheirAddress() 0 13 1
A iShouldBeNotifiedAboutSuccessfulUpdate() 0 4 1
A iShouldBeNotifiedThatAddressHasBeenSetAsDefault() 0 4 1
A iShouldHaveNoDefaultAddress() 0 7 1
A addressShouldBeMarkedAsMyDefaultAddress() 0 11 1
A getAddressOf() 0 10 1
A getCurrentPage() 0 9 1
A assertAddressesCountOnPage() 0 14 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\Page\UnexpectedPageException;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Behat\Service\SharedStorageInterface;
24
use Sylius\Component\Core\Model\AddressInterface;
25
use Sylius\Component\Resource\Repository\RepositoryInterface;
26
use Webmozart\Assert\Assert;
27
28
/**
29
 * @author Anna Walasek <[email protected]>
30
 * @author Jan Góralski <[email protected]>
31
 */
32
final class AddressBookContext implements Context
33
{
34
    /**
35
     * @var SharedStorageInterface
36
     */
37
    private $sharedStorage;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $addressRepository;
43
44
    /**
45
     * @var IndexPageInterface
46
     */
47
    private $addressBookIndexPage;
48
49
    /**
50
     * @var CreatePageInterface
51
     */
52
    private $addressBookCreatePage;
53
54
    /**
55
     * @var UpdatePageInterface
56
     */
57
    private $addressBookUpdatePage;
58
59
    /**
60
     * @var CurrentPageResolverInterface
61
     */
62
    private $currentPageResolver;
63
64
    /**
65
     * @var NotificationCheckerInterface
66
     */
67
    private $notificationChecker;
68
69
    /**
70
     * @param SharedStorageInterface $sharedStorage
71
     * @param RepositoryInterface $addressRepository
72
     * @param IndexPageInterface $addressBookIndexPage
73
     * @param CreatePageInterface $addressBookCreatePage
74
     * @param UpdatePageInterface $addressBookUpdatePage
75
     * @param CurrentPageResolverInterface $currentPageResolver
76
     * @param NotificationCheckerInterface $notificationChecker
77
     */
78
    public function __construct(
79
        SharedStorageInterface $sharedStorage,
80
        RepositoryInterface $addressRepository,
81
        IndexPageInterface $addressBookIndexPage,
82
        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...
83
        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...
84
        CurrentPageResolverInterface $currentPageResolver,
85
        NotificationCheckerInterface $notificationChecker
86
    ) {
87
        $this->sharedStorage = $sharedStorage;
88
        $this->addressRepository = $addressRepository;
89
        $this->addressBookIndexPage = $addressBookIndexPage;
90
        $this->addressBookCreatePage = $addressBookCreatePage;
91
        $this->addressBookUpdatePage = $addressBookUpdatePage;
92
        $this->currentPageResolver = $currentPageResolver;
93
        $this->notificationChecker = $notificationChecker;
94
    }
95
96
    /**
97
     * @Given I am editing the address of :fullName
98
     */
99
    public function iEditAddressOf($fullName)
100
    {
101
        $this->sharedStorage->set('full_name', $fullName);
102
103
        $this->addressBookIndexPage->open();
104
        $this->addressBookIndexPage->editAddress($fullName);
105
    }
106
107
    /**
108
     * @When I set the address of :fullName as default
109
     */
110
    public function iSetTheAddressOfAsDefault($fullName)
111
    {
112
        $this->sharedStorage->set('full_name', $fullName);
113
114
        $this->addressBookIndexPage->setAsDefault($fullName);
115
    }
116
117
    /**
118
     * @Given I want to add a new address to my address book
119
     */
120
    public function iWantToAddANewAddressToMyAddressBook()
121
    {
122
        $this->addressBookCreatePage->open();
123
    }
124
125
    /**
126
     * @Given I am browsing my address book
127
     * @When I browse my address book
128
     */
129
    public function iBrowseMyAddresses()
130
    {
131
        $this->addressBookIndexPage->open();
132
    }
133
134
    /**
135
     * @When I specify :provinceName as my province
136
     */
137
    public function iSpecifyAsMyProvince($provinceName)
138
    {
139
        $this->addressBookUpdatePage->specifyProvince($provinceName);
140
    }
141
142
    /**
143
     * @When I choose :provinceName as my province
144
     */
145
    public function iChooseAsMyProvince($provinceName)
146
    {
147
        $this->addressBookUpdatePage->selectProvince($provinceName);
148
    }
149
150
    /**
151
     * @When I choose :countryName as my country
152
     */
153
    public function iChooseAsMyCountry($countryName)
154
    {
155
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
156
        $currentPage = $this->getCurrentPage();
157
        $currentPage->selectCountry($countryName);
158
    }
159
160
    /**
161
     * @When /^I change the ([^"]+) to "([^"]+)"$/
162
     */
163
    public function iChangeMyTo($field, $value)
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 there should( still) be a single address in my book
232
     */
233
    public function iShouldSeeASingleAddressInTheList()
234
    {
235
        $this->assertAddressesCountOnPage(1);
236
    }
237
238
    /**
239
     * @Then this address should be assigned to :fullName
240
     * @Then /^the address assigned to "([^"]+)" should (appear|be) in my book$/
241
     */
242
    public function thisAddressShouldHavePersonFirstNameAndLastName($fullName)
243
    {
244
        Assert::true(
245
            $this->addressBookIndexPage->hasAddressOf($fullName),
246
            sprintf('An address of "%s" should be on the list.', $fullName)
247
        );
248
    }
249
250
    /**
251
     * @Then I should still be on the address addition page
252
     */
253
    public function iShouldStillBeOnAddressAdditionPage()
254
    {
255
        Assert::true(
256
            $this->addressBookCreatePage->isOpen(),
257
            'The address creation page should be opened.'
258
        );
259
    }
260
261
    /**
262
     * @Then I should be notified that the province needs to be specified
263
     */
264
    public function iShouldBeNotifiedThatTheProvinceNeedsToBeSpecified()
265
    {
266
        Assert::true(
267
            $this->addressBookCreatePage->hasProvinceValidationMessage(),
268
            'Province validation messages should be visible.'
269
        );
270
    }
271
272
    /**
273
     * @Then /^I should be notified about (\d+) errors$/
274
     */
275
    public function iShouldBeNotifiedAboutErrors($expectedCount)
276
    {
277
        $actualCount = $this->addressBookCreatePage->countValidationMessages();
278
279
        Assert::same(
280
            (int) $expectedCount,
281
            $actualCount,
282
            sprintf('There should be %d validation messages, but %d has been found.', $expectedCount, $actualCount)
283
        );
284
    }
285
286
    /**
287
     * @Then there should be no addresses
288
     */
289
    public function thereShouldBeNoAddresses()
290
    {
291
        Assert::true(
292
            $this->addressBookIndexPage->hasNoAddresses(),
293
            'There should be no addresses on the list.'
294
        );
295
    }
296
297
    /**
298
     * @Then I should not see the address assigned to :fullName
299
     */
300
    public function iShouldNotSeeAddressOf($fullName)
301
    {
302
        Assert::false(
303
            $this->addressBookIndexPage->hasAddressOf($fullName),
304
            sprintf('The address of "%s" should not be on the list.', $fullName)
305
        );
306
    }
307
308
    /**
309
     * @Then /^I should(?:| still) have (\d+) address(?:|es) in my address book$/
310
     */
311
    public function iShouldHaveAddresses($count)
312
    {
313
        $this->addressBookIndexPage->open();
314
315
        $this->assertAddressesCountOnPage((int) $count);
316
    }
317
318
    /**
319
     * @Then I should be notified that the address has been successfully added
320
     */
321
    public function iShouldBeNotifiedThatAddressHasBeenSuccessfullyAdded()
322
    {
323
        $this->notificationChecker->checkNotification('Address has been successfully added.', NotificationType::success());
324
    }
325
326
    /**
327
     * @Then I should be notified that the address has been successfully deleted
328
     */
329
    public function iShouldBeNotifiedAboutSuccessfulDelete()
330
    {
331
        $this->notificationChecker->checkNotification('Address has been successfully deleted.', NotificationType::success());
332
    }
333
334
    /**
335
     * @Then I should be unable to edit their address
336
     */
337
    public function iShouldBeUnableToEditTheirAddress()
338
    {
339
        $address = $this->getAddressOf($this->sharedStorage->getLatestResource());
340
341
        Assert::false(
342
            $this->addressBookUpdatePage->isOpen(['id' => $address->getId()]),
343
            sprintf(
344
                'I should be unable to edit the address of "%s %s"',
345
                $address->getFirstName(),
346
                $address->getLastName()
347
            )
348
        );
349
    }
350
351
    /**
352
     * @Then I should be notified that the address has been successfully updated
353
     */
354
    public function iShouldBeNotifiedAboutSuccessfulUpdate()
355
    {
356
        $this->notificationChecker->checkNotification('Address has been successfully updated.', NotificationType::success());
357
    }
358
359
    /**
360
     * @Then I should be notified that the address has been set as default
361
     */
362
    public function iShouldBeNotifiedThatAddressHasBeenSetAsDefault()
363
    {
364
        $this->notificationChecker->checkNotification('Address has been set as default', NotificationType::success());
365
    }
366
367
    /**
368
     * @Then I should not have a default address
369
     */
370
    public function iShouldHaveNoDefaultAddress()
371
    {
372
        Assert::true(
373
            $this->addressBookIndexPage->hasNoDefaultAddress(),
374
            'There should be no default address.'
375
        );
376
    }
377
378
    /**
379
     * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) should be marked as my default address$/
380
     */
381
    public function addressShouldBeMarkedAsMyDefaultAddress(AddressInterface $address)
382
    {
383
        $actualFullName = $this->addressBookIndexPage->getFullNameOfDefaultAddress();
384
        $expectedFullName = sprintf('%s %s', $address->getFirstName(), $address->getLastName());
385
386
        Assert::same(
387
            $expectedFullName,
388
            $actualFullName,
389
            sprintf('The default address should be of "%s", but is of "%s".', $expectedFullName, $actualFullName)
390
        );
391
    }
392
393
    /**
394
     * @param string $fullName
395
     *
396
     * @return AddressInterface
397
     */
398
    private function getAddressOf($fullName)
399
    {
400
        list($firstName, $lastName) = explode(' ', $fullName);
401
402
        /** @var AddressInterface $address */
403
        $address = $this->addressRepository->findOneBy(['firstName' => $firstName, 'lastName' => $lastName]);
404
        Assert::notNull($address);
405
406
        return $address;
407
    }
408
409
    /**
410
     * @return SymfonyPageInterface
411
     */
412
    private function getCurrentPage()
413
    {
414
        return $this
415
            ->currentPageResolver
416
            ->getCurrentPageWithForm([
417
                $this->addressBookCreatePage,
418
                $this->addressBookUpdatePage
419
        ]);
420
    }
421
422
    /**
423
     * @param int $expectedCount
424
     *
425
     * @throws \InvalidArgumentException
426
     */
427
    private function assertAddressesCountOnPage($expectedCount)
428
    {
429
        $actualCount = $this->addressBookIndexPage->getAddressesCount();
430
431
        Assert::same(
432
            $expectedCount,
433
            $actualCount,
434
            sprintf(
435
                'There should be %d addresses on the list, but %d addresses has been found.',
436
                $expectedCount,
437
                $actualCount
438
            )
439
        );
440
    }
441
}
442