Completed
Push — master ( 047880...b09c02 )
by Kamil
92:08 queued 74:48
created

iSpecifiedTheShippingAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Ui\Shop\Checkout;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Page\Shop\Checkout\AddressPageInterface;
18
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
19
use Sylius\Behat\Service\SharedStorageInterface;
20
use Sylius\Component\Addressing\Comparator\AddressComparatorInterface;
21
use Sylius\Component\Addressing\Model\CountryInterface;
22
use Sylius\Component\Core\Model\AddressInterface;
23
use Sylius\Component\Resource\Factory\FactoryInterface;
24
use Webmozart\Assert\Assert;
25
26
final class CheckoutAddressingContext implements Context
27
{
28
    /** @var SharedStorageInterface */
29
    private $sharedStorage;
30
31
    /** @var AddressPageInterface */
32
    private $addressPage;
33
34
    /** @var FactoryInterface */
35
    private $addressFactory;
36
37
    /** @var AddressComparatorInterface */
38
    private $addressComparator;
39
40
    /** @var SelectShippingPageInterface */
41
    private $selectShippingPage;
42
43
    public function __construct(
44
        SharedStorageInterface $sharedStorage,
45
        AddressPageInterface $addressPage,
46
        FactoryInterface $addressFactory,
47
        AddressComparatorInterface $addressComparator,
48
        SelectShippingPageInterface $selectShippingPage
49
    ) {
50
        $this->sharedStorage = $sharedStorage;
51
        $this->addressPage = $addressPage;
52
        $this->addressFactory = $addressFactory;
53
        $this->addressComparator = $addressComparator;
54
        $this->selectShippingPage = $selectShippingPage;
55
    }
56
57
    /**
58
     * @Given I am at the checkout addressing step
59
     * @When I go back to addressing step of the checkout
60
     */
61
    public function iAmAtTheCheckoutAddressingStep()
62
    {
63
        $this->addressPage->open();
64
    }
65
66
    /**
67
     * @Given /^I have completed addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/
68
     * @When /^I complete addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/
69
     */
70
    public function iCompleteAddressingStepWithEmail(string $email, AddressInterface $address): void
71
    {
72
        $this->addressPage->open();
73
        $this->iSpecifyTheEmail($email);
74
        $this->iSpecifyTheBillingAddressAs($address);
75
        $this->iCompleteTheAddressingStep();
76
    }
77
78
    /**
79
     * @When /^I complete addressing step with ("[^"]+" based billing address)$/
80
     */
81
    public function iCompleteAddressingStepWithBasedBillingAddress(AddressInterface $address): void
82
    {
83
        $this->addressPage->open();
84
        $this->iSpecifyTheBillingAddressAs($address);
85
        $this->iCompleteTheAddressingStep();
86
    }
87
88
    /**
89
     * @When I specify the province name manually as :provinceName for shipping address
90
     */
91
    public function iSpecifyTheProvinceNameManuallyAsForShippingAddress($provinceName)
92
    {
93
        $this->addressPage->specifyShippingAddressProvince($provinceName);
94
    }
95
96
    /**
97
     * @When I specify the province name manually as :provinceName for billing address
98
     */
99
    public function iSpecifyTheProvinceNameManuallyAsForBillingAddress($provinceName)
100
    {
101
        $this->addressPage->specifyBillingAddressProvince($provinceName);
102
    }
103
104
    /**
105
     * @When I try to open checkout addressing page
106
     */
107
    public function iTryToOpenCheckoutAddressingPage()
108
    {
109
        $this->addressPage->tryToOpen();
110
    }
111
112
    /**
113
     * @When /^I choose ("[^"]+" street) for shipping address$/
114
     */
115
    public function iChooseForShippingAddress(AddressInterface $address)
116
    {
117
        $this->addressPage->chooseDifferentShippingAddress();
118
        $this->addressPage->selectShippingAddressFromAddressBook($address);
119
    }
120
121
    /**
122
     * @When /^I choose ("[^"]+" street) for billing address$/
123
     */
124
    public function iChooseForBillingAddress(AddressInterface $address)
125
    {
126
        $this->addressPage->selectBillingAddressFromAddressBook($address);
127
    }
128
129
    /**
130
     * @When /^I specify the shipping (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/
131
     * @When /^I specify the shipping (address for "[^"]+" from "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+")$/
132
     * @When /^I (do not specify any shipping address) information$/
133
     * @When /^I change the shipping (address to "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/
134
     */
135
    public function iSpecifyTheShippingAddressAs(AddressInterface $address)
136
    {
137
        $this->addressPage->chooseDifferentShippingAddress();
138
139
        $key = sprintf(
140
            'shipping_address_%s_%s',
141
            strtolower((string) $address->getFirstName()),
142
            strtolower((string) $address->getLastName())
143
        );
144
        $this->sharedStorage->set($key, $address);
145
146
        $this->addressPage->specifyShippingAddress($address);
147
    }
148
149
    /**
150
     * @When I specify shipping country province as :province
151
     */
152
    public function iSpecifyShippingCountryProvinceAs($province)
153
    {
154
        $this->addressPage->selectShippingAddressProvince($province);
155
    }
156
157
    /**
158
     * @When I specify billing country province as :province
159
     */
160
    public function iSpecifyBillingCountryProvinceAs($province)
161
    {
162
        $this->addressPage->selectBillingAddressProvince($province);
163
    }
164
165
    /**
166
     * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
167
     * @When /^I specify the billing (address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
168
     * @When /^I (do not specify any billing address) information$/
169
     */
170
    public function iSpecifyTheBillingAddressAs(AddressInterface $address)
171
    {
172
        $key = sprintf(
173
            'billing_address_%s_%s',
174
            strtolower((string) $address->getFirstName()),
175
            strtolower((string) $address->getLastName())
176
        );
177
        $this->sharedStorage->set($key, $address);
178
179
        $this->addressPage->specifyBillingAddress($address);
180
    }
181
182
    /**
183
     * @When /^I specified the billing (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/
184
     */
185
    public function iSpecifiedTheBillingAddress(AddressInterface $address = null)
186
    {
187
        if (null === $address) {
188
            $address = $this->createDefaultAddress();
189
        }
190
191
        $this->addressPage->open();
192
        $this->iSpecifyTheBillingAddressAs($address);
193
194
        $key = sprintf('shipping_address_%s_%s', strtolower((string) $address->getFirstName()), strtolower((string) $address->getLastName()));
195
        $this->sharedStorage->set($key, $address);
196
197
        $this->iCompleteTheAddressingStep();
198
    }
199
200
    /**
201
     * @When I specify the email as :email
202
     * @When I do not specify the email
203
     */
204
    public function iSpecifyTheEmail($email = null)
205
    {
206
        $this->addressPage->specifyEmail($email);
207
    }
208
209
    /**
210
     * @When I specify the first and last name as :fullName for billing address
211
     */
212
    public function iSpecifyTheStreetAsForBillingAddress(string $fullName): void
213
    {
214
        $this->addressPage->specifyBillingAddressFullName($fullName);
215
    }
216
217
    /**
218
     * @When I complete the addressing step
219
     * @When I try to complete the addressing step
220
     */
221
    public function iCompleteTheAddressingStep()
222
    {
223
        $this->addressPage->nextStep();
224
    }
225
226
    /**
227
     * @When I go back to store
228
     */
229
    public function iGoBackToStore()
230
    {
231
        $this->addressPage->backToStore();
232
    }
233
234
    /**
235
     * @When /^I proceed selecting ("[^"]+" as billing country)$/
236
     */
237
    public function iProceedSelectingBillingCountry(
238
        CountryInterface $shippingCountry = null,
239
        string $localeCode = 'en_US',
240
        ?string $email = null
241
    ) {
242
        $this->addressPage->open(['_locale' => $localeCode]);
243
        $shippingAddress = $this->createDefaultAddress();
244
        if (null !== $shippingCountry) {
245
            $shippingAddress->setCountryCode($shippingCountry->getCode());
246
        }
247
        if (null !== $email) {
248
            $this->addressPage->specifyEmail($email);
249
        }
250
        $this->addressPage->specifyBillingAddress($shippingAddress);
251
        $this->addressPage->nextStep();
252
    }
253
254
    /**
255
     * @When /^I proceed as guest "([^"]*)" with ("[^"]+" as billing country)$/
256
     */
257
    public function iProceedLoggingAsGuestWithAsBillingCountry(
258
        string $email,
259
        CountryInterface $shippingCountry = null
260
    ): void {
261
        $this->addressPage->open();
262
        $this->addressPage->specifyEmail($email);
263
        $shippingAddress = $this->createDefaultAddress();
264
        if (null !== $shippingCountry) {
265
            $shippingAddress->setCountryCode($shippingCountry->getCode());
266
        }
267
268
        $this->addressPage->specifyBillingAddress($shippingAddress);
269
        $this->addressPage->nextStep();
270
    }
271
272
    /**
273
     * @When I specify the password as :password
274
     */
275
    public function iSpecifyThePasswordAs($password)
276
    {
277
        $this->addressPage->specifyPassword($password);
278
    }
279
280
    /**
281
     * @When I sign in
282
     */
283
    public function iSignIn()
284
    {
285
        $this->addressPage->signIn();
286
    }
287
288
    /**
289
     * @Then I should have :countryName selected as country
290
     */
291
    public function iShouldHaveSelectedAsCountry($countryName)
292
    {
293
        Assert::same($this->addressPage->getShippingAddressCountry(), $countryName);
294
    }
295
296
    /**
297
     * @Then I should have no country selected
298
     */
299
    public function iShouldHaveNoCountrySelected()
300
    {
301
        Assert::same($this->addressPage->getShippingAddressCountry(), 'Select');
302
    }
303
304
    /**
305
     * @Then I should be able to log in
306
     */
307
    public function iShouldBeAbleToLogIn()
308
    {
309
        Assert::true($this->addressPage->canSignIn());
310
    }
311
312
    /**
313
     * @Then the login form should no longer be accessible
314
     */
315
    public function theLoginFormShouldNoLongerBeAccessible()
316
    {
317
        Assert::false($this->addressPage->canSignIn());
318
    }
319
320
    /**
321
     * @Then I should be notified about bad credentials
322
     */
323
    public function iShouldBeNotifiedAboutBadCredentials()
324
    {
325
        Assert::true($this->addressPage->checkInvalidCredentialsValidation());
326
    }
327
328
    /**
329
     * @Then I should be redirected to the addressing step
330
     * @Then I should be on the checkout addressing step
331
     */
332
    public function iShouldBeRedirectedToTheAddressingStep()
333
    {
334
        $this->addressPage->verify();
335
    }
336
337
    /**
338
     * @Then I should be able to go to the shipping step again
339
     */
340
    public function iShouldBeAbleToGoToTheShippingStepAgain()
341
    {
342
        $this->addressPage->nextStep();
343
344
        $this->selectShippingPage->verify();
345
    }
346
347
    /**
348
     * @Then I should not be able to specify province name manually for shipping address
349
     */
350
    public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForShippingAddress()
351
    {
352
        Assert::false($this->addressPage->hasShippingAddressInput());
353
    }
354
355
    /**
356
     * @Then I should not be able to specify province name manually for billing address
357
     */
358
    public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForBillingAddress()
359
    {
360
        Assert::false($this->addressPage->hasBillingAddressInput());
361
    }
362
363
    /**
364
     * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as shipping address$/
365
     */
366
    public function addressShouldBeFilledAsShippingAddress(AddressInterface $address)
367
    {
368
        Assert::true($this->addressComparator->equal($address, $this->addressPage->getPreFilledShippingAddress()));
369
    }
370
371
    /**
372
     * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as billing address$/
373
     */
374
    public function addressShouldBeFilledAsBillingAddress(AddressInterface $address)
375
    {
376
        Assert::true($this->addressComparator->equal($address, $this->addressPage->getPreFilledBillingAddress()));
377
    }
378
379
    /**
380
     * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/
381
     */
382
    public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type)
383
    {
384
        $this->assertElementValidationMessage($type, $firstElement, sprintf('Please enter %s.', $firstElement));
385
        $this->assertElementValidationMessage($type, $secondElement, sprintf('Please enter %s.', $secondElement));
386
    }
387
388
    /**
389
     * @return AddressInterface
390
     */
391
    private function createDefaultAddress()
392
    {
393
        /** @var AddressInterface $address */
394
        $address = $this->addressFactory->createNew();
395
        $address->setFirstName('John');
396
        $address->setLastName('Doe');
397
        $address->setCountryCode('US');
398
        $address->setCity('North Bridget');
399
        $address->setPostcode('93-554');
400
        $address->setStreet('0635 Myron Hollow Apt. 711');
401
        $address->setPhoneNumber('321123456');
402
403
        return $address;
404
    }
405
406
    /**
407
     * @param string $type
408
     * @param string $element
409
     * @param string $expectedMessage
410
     *
411
     * @throws \InvalidArgumentException
412
     */
413
    private function assertElementValidationMessage($type, $element, $expectedMessage)
414
    {
415
        $element = sprintf('%s_%s', $type, str_replace(' ', '_', $element));
416
        Assert::true($this->addressPage->checkValidationMessageFor($element, $expectedMessage));
417
    }
418
}
419