1 | <?php |
||
18 | final class CheckoutAddressingContext 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 | * @Given /^I have completed addressing step with email "([^"]+)" and ("[^"]+" based shipping address)$/ |
||
77 | * @When /^I complete addressing step with email "([^"]+)" and ("[^"]+" based shipping address)$/ |
||
78 | */ |
||
79 | public function iCompleteAddressingStepWithEmail($email, AddressInterface $address) |
||
80 | { |
||
81 | $this->addressPage->open(); |
||
82 | $this->iSpecifyTheEmail($email); |
||
83 | $this->iSpecifyTheShippingAddressAs($address); |
||
84 | $this->iCompleteTheAddressingStep(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @When I specify the province name manually as :provinceName for shipping address |
||
89 | */ |
||
90 | public function iSpecifyTheProvinceNameManuallyAsForShippingAddress($provinceName) |
||
91 | { |
||
92 | $this->addressPage->specifyShippingAddressProvince($provinceName); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @When I specify the province name manually as :provinceName for billing address |
||
97 | */ |
||
98 | public function iSpecifyTheProvinceNameManuallyAsForBillingAddress($provinceName) |
||
99 | { |
||
100 | $this->addressPage->specifyBillingAddressProvince($provinceName); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @When I try to open checkout addressing page |
||
105 | */ |
||
106 | public function iTryToOpenCheckoutAddressingPage() |
||
107 | { |
||
108 | $this->addressPage->tryToOpen(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @When /^I choose ("[^"]+" street) for shipping address$/ |
||
113 | */ |
||
114 | public function iChooseForShippingAddress(AddressInterface $address) |
||
115 | { |
||
116 | $this->addressPage->selectShippingAddressFromAddressBook($address); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @When /^I choose ("[^"]+" street) for billing address$/ |
||
121 | */ |
||
122 | public function iChooseForBillingAddress(AddressInterface $address) |
||
123 | { |
||
124 | $this->addressPage->chooseDifferentBillingAddress(); |
||
125 | $this->addressPage->selectBillingAddressFromAddressBook($address); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @When /^I specify the shipping (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
130 | * @When /^I specify the shipping (address for "[^"]+" from "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+")$/ |
||
131 | * @When /^I (do not specify any shipping address) information$/ |
||
132 | * @When /^I change the shipping (address to "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
133 | */ |
||
134 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
135 | { |
||
136 | $key = sprintf( |
||
137 | 'shipping_address_%s_%s', |
||
138 | strtolower($address->getFirstName()), |
||
139 | strtolower($address->getLastName()) |
||
140 | ); |
||
141 | $this->sharedStorage->set($key, $address); |
||
142 | |||
143 | $this->addressPage->specifyShippingAddress($address); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @When I specify shipping country province as :province |
||
148 | */ |
||
149 | public function iSpecifyShippingCountryProvinceAs($province) |
||
150 | { |
||
151 | $this->addressPage->selectShippingAddressProvince($province); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @When I specify billing country province as :province |
||
156 | */ |
||
157 | public function iSpecifyBillingCountryProvinceAs($province) |
||
158 | { |
||
159 | $this->addressPage->selectBillingAddressProvince($province); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
164 | * @When /^I specify the billing (address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
165 | * @When /^I (do not specify any billing address) information$/ |
||
166 | */ |
||
167 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
168 | { |
||
169 | $this->addressPage->chooseDifferentBillingAddress(); |
||
170 | |||
171 | $key = sprintf( |
||
172 | 'billing_address_%s_%s', |
||
173 | strtolower($address->getFirstName()), |
||
174 | strtolower($address->getLastName()) |
||
175 | ); |
||
176 | $this->sharedStorage->set($key, $address); |
||
177 | |||
178 | $this->addressPage->specifyBillingAddress($address); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @When /^I specified the shipping (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
183 | */ |
||
184 | public function iSpecifiedTheShippingAddress(AddressInterface $address = null) |
||
185 | { |
||
186 | if (null === $address) { |
||
187 | $address = $this->createDefaultAddress(); |
||
188 | } |
||
189 | |||
190 | $this->addressPage->open(); |
||
191 | $this->iSpecifyTheShippingAddressAs($address); |
||
192 | |||
193 | $key = sprintf('billing_address_%s_%s', strtolower($address->getFirstName()), strtolower($address->getLastName())); |
||
194 | $this->sharedStorage->set($key, $address); |
||
195 | |||
196 | $this->iCompleteTheAddressingStep(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @When I specify the email as :email |
||
201 | * @When I do not specify the email |
||
202 | */ |
||
203 | public function iSpecifyTheEmail($email = null) |
||
204 | { |
||
205 | $this->addressPage->specifyEmail($email); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @When I complete the addressing step |
||
210 | * @When I try to complete the addressing step |
||
211 | */ |
||
212 | public function iCompleteTheAddressingStep() |
||
213 | { |
||
214 | $this->addressPage->nextStep(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @When I go back to store |
||
219 | */ |
||
220 | public function iGoBackToStore() |
||
221 | { |
||
222 | $this->addressPage->backToStore(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @When /^I proceed selecting ("[^"]+" as shipping country)$/ |
||
227 | */ |
||
228 | public function iProceedSelectingShippingCountry(CountryInterface $shippingCountry = null) |
||
229 | { |
||
230 | $this->addressPage->open(); |
||
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 proceed as guest "([^"]*)" with ("[^"]+" as shipping country)$/ |
||
242 | */ |
||
243 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, CountryInterface $shippingCountry = null) |
||
244 | { |
||
245 | $this->addressPage->open(); |
||
246 | $this->addressPage->specifyEmail($email); |
||
247 | $shippingAddress = $this->createDefaultAddress(); |
||
248 | if (null !== $shippingCountry) { |
||
249 | $shippingAddress->setCountryCode($shippingCountry->getCode()); |
||
250 | } |
||
251 | |||
252 | $this->addressPage->specifyShippingAddress($shippingAddress); |
||
253 | $this->addressPage->nextStep(); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @When I specify the password as :password |
||
258 | */ |
||
259 | public function iSpecifyThePasswordAs($password) |
||
260 | { |
||
261 | $this->addressPage->specifyPassword($password); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @When I sign in |
||
266 | */ |
||
267 | public function iSignIn() |
||
268 | { |
||
269 | $this->addressPage->signIn(); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @Then I should have :countryName selected as country |
||
274 | */ |
||
275 | public function iShouldHaveSelectedAsCountry($countryName) |
||
276 | { |
||
277 | Assert::same($this->addressPage->getShippingAddressCountry(), $countryName); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @Then I should have no country selected |
||
282 | */ |
||
283 | public function iShouldHaveNoCountrySelected() |
||
284 | { |
||
285 | Assert::same($this->addressPage->getShippingAddressCountry(), 'Select'); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @Then I should be able to log in |
||
290 | */ |
||
291 | public function iShouldBeAbleToLogIn() |
||
292 | { |
||
293 | Assert::true($this->addressPage->canSignIn()); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @Then the login form should no longer be accessible |
||
298 | */ |
||
299 | public function theLoginFormShouldNoLongerBeAccessible() |
||
300 | { |
||
301 | Assert::false($this->addressPage->canSignIn()); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @Then I should be notified about bad credentials |
||
306 | */ |
||
307 | public function iShouldBeNotifiedAboutBadCredentials() |
||
308 | { |
||
309 | Assert::true($this->addressPage->checkInvalidCredentialsValidation()); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @Then I should be redirected to the addressing step |
||
314 | * @Then I should be on the checkout addressing step |
||
315 | */ |
||
316 | public function iShouldBeRedirectedToTheAddressingStep() |
||
317 | { |
||
318 | Assert::true($this->addressPage->isOpen()); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @Then I should be able to go to the shipping step again |
||
323 | */ |
||
324 | public function iShouldBeAbleToGoToTheShippingStepAgain() |
||
325 | { |
||
326 | $this->addressPage->nextStep(); |
||
327 | |||
328 | Assert::true($this->selectShippingPage->isOpen()); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @Then I should not be able to specify province name manually for shipping address |
||
333 | */ |
||
334 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForShippingAddress() |
||
335 | { |
||
336 | Assert::false($this->addressPage->hasShippingAddressInput()); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @Then I should not be able to specify province name manually for billing address |
||
341 | */ |
||
342 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForBillingAddress() |
||
343 | { |
||
344 | Assert::false($this->addressPage->hasBillingAddressInput()); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as shipping address$/ |
||
349 | */ |
||
350 | public function addressShouldBeFilledAsShippingAddress(AddressInterface $address) |
||
351 | { |
||
352 | Assert::true($this->addressComparator->equal($address); |
||
|
|||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as billing address$/ |
||
357 | */ |
||
358 | public function addressShouldBeFilledAsBillingAddress(AddressInterface $address) |
||
359 | { |
||
360 | Assert::true($this->addressComparator->equal($address); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
365 | */ |
||
366 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
367 | { |
||
368 | $this->assertElementValidationMessage($type, $firstElement, sprintf('Please enter %s.', $firstElement)); |
||
369 | $this->assertElementValidationMessage($type, $secondElement, sprintf('Please enter %s.', $secondElement)); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return AddressInterface |
||
374 | */ |
||
375 | private function createDefaultAddress() |
||
376 | { |
||
377 | /** @var AddressInterface $address */ |
||
378 | $address = $this->addressFactory->createNew(); |
||
379 | $address->setFirstName('John'); |
||
380 | $address->setLastName('Doe'); |
||
381 | $address->setCountryCode('US'); |
||
382 | $address->setCity('North Bridget'); |
||
383 | $address->setPostcode('93-554'); |
||
384 | $address->setStreet('0635 Myron Hollow Apt. 711'); |
||
385 | $address->setPhoneNumber('321123456'); |
||
386 | |||
387 | return $address; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param string $type |
||
392 | * @param string $element |
||
393 | * @param string $expectedMessage |
||
394 | * |
||
395 | * @throws \InvalidArgumentException |
||
396 | */ |
||
397 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
398 | { |
||
399 | $element = sprintf('%s_%s', $type, implode('_', explode(' ', $element))); |
||
400 | Assert::true($this->addressPage->checkValidationMessageFor($element, $expectedMessage)); |
||
401 | } |
||
402 | } |
||
403 |