Completed
Push — checkout-context-cleanup ( c82d5d...3b30b9 )
by Kamil
20:03
created

AddressingContext::iSpecifyTheShippingAddressAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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