Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

AddressBookContext   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 30
lcom 2
cbo 10
dl 0
loc 365
rs 10
c 1
b 1
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A iEditAddressOf() 0 7 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 iTryToEdit() 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 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 "([^"]+)"$/
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
     * @Given I want to add a new address to my address book
109
     */
110
    public function iWantToAddANewAddressToMyAddressBook()
111
    {
112
        $this->addressBookCreatePage->open();
113
    }
114
115
    /**
116
     * @When I browse my address book
117
     */
118
    public function iBrowseMyAddresses()
119
    {
120
        $this->addressBookIndexPage->open();
121
    }
122
123
    /**
124
     * @When I specify :provinceName as my province
125
     */
126
    public function iSpecifyAsMyProvince($provinceName)
127
    {
128
        $this->addressBookUpdatePage->specifyProvince($provinceName);
129
    }
130
131
    /**
132
     * @When I choose :provinceName as my province
133
     */
134
    public function iChooseAsMyProvince($provinceName)
135
    {
136
        $this->addressBookUpdatePage->selectProvince($provinceName);
137
    }
138
139
    /**
140
     * @When I choose :countryName as my country
141
     */
142
    public function iChooseAsMyCountry($countryName)
143
    {
144
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
145
        $currentPage = $this->getCurrentPage();
146
        $currentPage->selectCountry($countryName);
147
    }
148
149
    /**
150
     * @When /^I change the ([^"]+) to "([^"]+)"$/
151
     */
152
    public function iChangeMyTo($field, $value)
153
    {
154
        $this->addressBookUpdatePage->fillField($field, $value);
155
    }
156
157
    /**
158
     * @When /^I specify the (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
159
     */
160
    public function iSpecifyTheAddressAs(AddressInterface $address)
161
    {
162
        $this->addressBookCreatePage->fillAddressData($address);
163
    }
164
165
    /**
166
     * @When I leave every field empty
167
     */
168
    public function iLeaveEveryFieldEmpty()
169
    {
170
        // Intentionally left empty
171
    }
172
173
    /**
174
     * @When I add it
175
     */
176
    public function iAddIt()
177
    {
178
        $this->addressBookCreatePage->addAddress();
179
    }
180
181
    /**
182
     * @When I save my changed address
183
     */
184
    public function iSaveChangedAddress()
185
    {
186
        $this->addressBookUpdatePage->saveChanges();
187
    }
188
189
    /**
190
     * @When I delete the :fullName address
191
     */
192
    public function iDeleteTheAddress($fullname)
193
    {
194
        $this->addressBookIndexPage->deleteAddress($fullname);
195
    }
196
197
    /**
198
     * @When /^I try to edit the address of "([^"]+)"$/
199
     */
200
    public function iTryToEdit($fullName)
201
    {
202
        $this->sharedStorage->set('full_name', $fullName);
203
204
        $address = $this->getAddressOf($fullName);
205
206
        $this->addressBookUpdatePage->tryToOpen(['id' => $address->getId()]);
207
    }
208
209
    /**
210
     * @Then /^it should contain "([^"]+)"$/
211
     */
212
    public function itShouldContain($value)
213
    {
214
        $fullName = $this->sharedStorage->get('full_name');
215
216
        $this->addressBookIndexPage->addressOfContains($fullName, $value);
217
    }
218
219
    /**
220
     * @Then there should( still) be a single address in my book
221
     */
222
    public function iShouldSeeASingleAddressInTheList()
223
    {
224
        $this->assertAddressesCountOnPage(1);
225
    }
226
227
    /**
228
     * @Then this address should be assigned to :fullName
229
     * @Then the address assigned to :fullName should appear in my book
230
     */
231
    public function thisAddressShouldHavePersonFirstNameAndLastName($fullName)
232
    {
233
        Assert::true(
234
            $this->addressBookIndexPage->hasAddressOf($fullName),
235
            sprintf('An address of "%s" should be on the list.', $fullName)
236
        );
237
    }
238
239
    /**
240
     * @Then I should still be on the address addition page
241
     */
242
    public function iShouldStillBeOnAddressAdditionPage()
243
    {
244
        Assert::true(
245
            $this->addressBookCreatePage->isOpen(),
246
            'The address creation page should be opened.'
247
        );
248
    }
249
250
    /**
251
     * @Then I should be notified that the province needs to be specified
252
     */
253
    public function iShouldBeNotifiedThatTheProvinceNeedsToBeSpecified()
254
    {
255
        Assert::true(
256
            $this->addressBookCreatePage->hasProvinceValidationMessage(),
257
            'Province validation messages should be visible.'
258
        );
259
    }
260
261
    /**
262
     * @Then /^I should be notified about (\d+) errors$/
263
     */
264
    public function iShouldBeNotifiedAboutErrors($expectedCount)
265
    {
266
        $actualCount = $this->addressBookCreatePage->countValidationMessages();
267
268
        Assert::same(
269
            (int) $expectedCount,
270
            $actualCount,
271
            sprintf('There should be %d validation messages, but %d has been found.', $expectedCount, $actualCount)
272
        );
273
    }
274
275
    /**
276
     * @Then there should be no addresses
277
     */
278
    public function thereShouldBeNoAddresses()
279
    {
280
        Assert::true(
281
            $this->addressBookIndexPage->hasNoAddresses(),
282
            'There should be no addresses on the list.'
283
        );
284
    }
285
286
    /**
287
     * @Then I should not see the address assigned to :fullName
288
     */
289
    public function iShouldNotSeeAddressOf($fullName)
290
    {
291
        Assert::false(
292
            $this->addressBookIndexPage->hasAddressOf($fullName),
293
            sprintf('The address of "%s" should not be on the list.', $fullName)
294
        );
295
    }
296
297
    /**
298
     * @Then /^I should(?:| still) have (\d+) address(?:|es) in my address book$/
299
     */
300
    public function iShouldHaveAddresses($count)
301
    {
302
        $this->addressBookIndexPage->open();
303
304
        $this->assertAddressesCountOnPage((int) $count);
305
    }
306
307
    /**
308
     * @Then I should be notified that the address has been successfully added
309
     */
310
    public function iShouldBeNotifiedThatAddressHasBeenSuccessfullyAdded()
311
    {
312
        $this->notificationChecker->checkNotification('Address has been successfully added.', NotificationType::success());
313
    }
314
315
    /**
316
     * @Then I should be notified that the address has been successfully deleted
317
     */
318
    public function iShouldBeNotifiedAboutSuccessfulDelete()
319
    {
320
        $this->notificationChecker->checkNotification('Address has been successfully deleted.', NotificationType::success());
321
    }
322
323
    /**
324
     * @Then I should be unable to edit their address
325
     */
326
    public function iShouldBeUnableToEditTheirAddress()
327
    {
328
        $address = $this->getAddressOf($this->sharedStorage->getLatestResource());
329
330
        Assert::false(
331
            $this->addressBookUpdatePage->isOpen(['id' => $address->getId()]),
332
            sprintf(
333
                'I should be unable to edit the address of "%s %s"',
334
                $address->getFirstName(),
335
                $address->getLastName()
336
            )
337
        );
338
    }
339
340
    /**
341
     * @Then I should be notified that the address has been successfully updated
342
     */
343
    public function iShouldBeNotifiedAboutSuccessfulUpdate()
344
    {
345
        $this->notificationChecker->checkNotification('Address has been successfully updated.', NotificationType::success());
346
    }
347
348
    /**
349
     * @param string $fullName
350
     *
351
     * @return AddressInterface
352
     */
353
    private function getAddressOf($fullName)
354
    {
355
        list($firstName, $lastName) = explode(' ', $fullName);
356
357
        /** @var AddressInterface $address */
358
        $address = $this->addressRepository->findOneBy(['firstName' => $firstName, 'lastName' => $lastName]);
359
        Assert::notNull($address);
360
361
        return $address;
362
    }
363
364
    /**
365
     * @return SymfonyPageInterface
366
     */
367
    private function getCurrentPage()
368
    {
369
        return $this
370
            ->currentPageResolver
371
            ->getCurrentPageWithForm([
372
                $this->addressBookCreatePage,
373
                $this->addressBookUpdatePage
374
        ]);
375
    }
376
377
    /**
378
     * @param int $expectedCount
379
     *
380
     * @throws \InvalidArgumentException
381
     */
382
    private function assertAddressesCountOnPage($expectedCount)
383
    {
384
        $actualCount = $this->addressBookIndexPage->getAddressesCount();
385
386
        Assert::same(
387
            $expectedCount,
388
            $actualCount,
389
            sprintf(
390
                'There should be %d addresses on the list, but %d addresses has been found.',
391
                $expectedCount,
392
                $actualCount
393
            )
394
        );
395
    }
396
}
397