Complex classes like WebContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class WebContext extends BaseWebContext implements SnippetAcceptingContext |
||
29 | { |
||
30 | /** |
||
31 | * @Given /^I am on user login page?$/ |
||
32 | * @When /^I go to user login page?$/ |
||
33 | */ |
||
34 | public function iAmOnTheLoginPage() |
||
38 | |||
39 | /** |
||
40 | * @Then /^I should (?:|still )be on user login page$/ |
||
41 | * @Then /^I should be redirected to user login page$/ |
||
42 | */ |
||
43 | public function iShouldBeOnTheLoginPage() |
||
47 | |||
48 | /** |
||
49 | * @Given /^(?:|I )go to "([^""]*)" tab$/ |
||
50 | */ |
||
51 | public function goToTab($tabLabel) |
||
52 | { |
||
53 | $tabContainer = $this->getSession()->getPage()->find('css', sprintf('.nav-tabs li:contains("%s")', $tabLabel)); |
||
54 | $tabContainer->find('css', 'a')->click(); |
||
55 | |||
56 | $this->waitForTabToActivate($tabContainer); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param NodeElement $tabContainer |
||
61 | */ |
||
62 | protected function waitForTabToActivate($tabContainer) |
||
63 | { |
||
64 | $this->waitFor(function () use ($tabContainer) { |
||
65 | return false !== strpos($tabContainer->getAttribute('class'), 'active'); |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @Then /^the page title should be "([^""]*)"$/ |
||
71 | */ |
||
72 | public function thePageTitleShouldBe($title) |
||
76 | |||
77 | /** |
||
78 | * @When /^I go to the website root$/ |
||
79 | */ |
||
80 | public function iGoToTheWebsiteRoot() |
||
84 | |||
85 | /** |
||
86 | * @Then /^I should (?:|still )be on the store homepage$/ |
||
87 | * @Then /^I should be redirected to the store homepage$/ |
||
88 | */ |
||
89 | public function iShouldBeOnTheStoreHomepage() |
||
93 | |||
94 | /** |
||
95 | * @Given /^I am on the store homepage$/ |
||
96 | */ |
||
97 | public function iAmOnTheStoreHomepage() |
||
101 | |||
102 | /** |
||
103 | * @Given /^I am on my account homepage$/ |
||
104 | */ |
||
105 | public function iAmOnMyAccountHomepage() |
||
106 | { |
||
107 | $this->getSession()->visit($this->generatePageUrl('sylius_account_profile_show')); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @Then /^I should (?:|still )be on my account homepage$/ |
||
112 | */ |
||
113 | public function iShouldBeOnMyAccountHomepage() |
||
117 | |||
118 | /** |
||
119 | * @Given /^I am on my account password page$/ |
||
120 | */ |
||
121 | public function iAmOnMyAccountPasswordPage() |
||
125 | |||
126 | /** |
||
127 | * @Then /^I should (?:|still )be on my account password page$/ |
||
128 | */ |
||
129 | public function iShouldBeOnMyAccountPasswordPage() |
||
133 | |||
134 | /** |
||
135 | * @Given /^I am on my account profile edition page$/ |
||
136 | */ |
||
137 | public function iAmOnMyAccountProfileEditionPage() |
||
141 | |||
142 | /** |
||
143 | * @Then /^I should (?:|still )be on my account profile edition page$/ |
||
144 | */ |
||
145 | public function iShouldBeOnMyProfileEditionPage() |
||
149 | |||
150 | /** |
||
151 | * @Then /^I should (?:|still )be on my account profile page$/ |
||
152 | */ |
||
153 | public function iShouldBeOnMyProfilePage() |
||
157 | |||
158 | /** |
||
159 | * @Then /^I should be on my account orders page$/ |
||
160 | */ |
||
161 | public function iShouldBeOnMyAccountOrdersPage() |
||
165 | |||
166 | /** |
||
167 | * @Given /^I am on my account orders page$/ |
||
168 | */ |
||
169 | public function iAmOnMyAccountOrdersPage() |
||
173 | |||
174 | /** |
||
175 | * @Given /^I am on my account addresses page$/ |
||
176 | */ |
||
177 | public function iAmOnMyAccountAddressesPage() |
||
181 | |||
182 | /** |
||
183 | * @Then /^I should (?:|still )be on my account addresses page$/ |
||
184 | */ |
||
185 | public function iShouldBeOnMyAccountAddressesPage() |
||
189 | |||
190 | /** |
||
191 | * @Given /^I am on my account address creation page$/ |
||
192 | */ |
||
193 | public function iAmOnMyAccountAddressCreationPage() |
||
197 | |||
198 | /** |
||
199 | * @Then /^I should (?:|still )be on my account address creation page$/ |
||
200 | */ |
||
201 | public function iShouldBeOnMyAccountAddressCreationPage() |
||
205 | |||
206 | /** |
||
207 | * @Then /^I should (?:|still )be on registration page$/ |
||
208 | */ |
||
209 | public function iShouldBeOnRegistrationPage() |
||
213 | |||
214 | /** |
||
215 | * @Given /^I am on the shipment page with method "([^""]*)"$/ |
||
216 | */ |
||
217 | public function iAmOnTheShipmentPage($value) |
||
218 | { |
||
219 | $shippingMethod = $this->findOneBy('shipping_method', ['name' => $value]); |
||
220 | $shipment = $this->findOneBy('shipment', ['method' => $shippingMethod]); |
||
221 | |||
222 | $this->getSession()->visit($this->generatePageUrl('backend_shipment_show', ['id' => $shipment->getId()])); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @Then /^I should be on the shipment page with method "([^"]*)"$/ |
||
227 | */ |
||
228 | public function iShouldBeOnTheShipmentPageWithMethod($value) |
||
229 | { |
||
230 | $shippingMethod = $this->findOneBy('shipping_method', ['name' => $value]); |
||
231 | $shipment = $this->findOneBy('shipment', ['method' => $shippingMethod]); |
||
232 | |||
233 | $this->assertSession()->addressEquals($this->generatePageUrl('backend_shipment_show', ['id' => $shipment->getId()])); |
||
234 | $this->assertStatusCodeEquals(200); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @Given /^I remove property choice number (\d+)$/ |
||
239 | */ |
||
240 | public function iRemovePropertyChoiceInput($number) |
||
241 | { |
||
242 | $this |
||
243 | ->getSession() |
||
244 | ->getPage() |
||
245 | ->find('css', sprintf('.sylius_property_choices_%d_delete', $number)) |
||
246 | ->click() |
||
247 | ; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @Given /^I am creating variant of "([^""]*)"$/ |
||
252 | */ |
||
253 | public function iAmCreatingVariantOf($name) |
||
254 | { |
||
255 | $product = $this->findOneByName('product', $name); |
||
256 | |||
257 | $this->getSession()->visit($this->generatePageUrl('sylius_backend_product_variant_create', ['productId' => $product->getId()])); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @Given /^I should be creating variant of "([^""]*)"$/ |
||
262 | */ |
||
263 | public function iShouldBeCreatingVariantOf($name) |
||
264 | { |
||
265 | $product = $this->findOneByName('product', $name); |
||
266 | |||
267 | $this->assertSession()->addressEquals($this->generatePageUrl('sylius_backend_product_variant_create', ['productId' => $product->getId()])); |
||
268 | $this->assertStatusCodeEquals(200); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @Given /^I added product "([^""]*)" to cart$/ |
||
273 | */ |
||
274 | public function iAddedProductToCart($productName) |
||
275 | { |
||
276 | $this->iAmOnTheProductPage($productName); |
||
277 | $this->pressButton('Add to cart'); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @Given /^I fill in province name with "([^"]*)"$/ |
||
282 | */ |
||
283 | public function iFillInProvinceNameWith($value) |
||
287 | |||
288 | /** |
||
289 | * @Given /^I fill in the (billing|shipping) address to (.+)$/ |
||
290 | */ |
||
291 | public function iFillInCheckoutAddress($type, $country) |
||
292 | { |
||
293 | $base = sprintf('sylius_checkout_addressing[%sAddress]', $type); |
||
294 | |||
295 | $this->iFillInAddressFields($base, $country); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @Given /^I fill in the users (billing|shipping) address to (.+)$/ |
||
300 | */ |
||
301 | public function iFillInUserAddress($type, $country) |
||
302 | { |
||
303 | $base = sprintf('%s[%sAddress]', 'sylius_user', $type); |
||
304 | $this->iFillInAddressFields($base, $country); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @Given /^I fill in the users account address to (.+)$/ |
||
309 | */ |
||
310 | public function iFillInUserAccountAddress($country) |
||
314 | |||
315 | protected function iFillInAddressFields($base, $country) |
||
316 | { |
||
317 | $this->fillField($base.'[firstName]', 'John'); |
||
318 | $this->fillField($base.'[lastName]', 'Doe'); |
||
319 | $this->fillField($base.'[street]', 'Pvt. Street 15'); |
||
320 | $this->fillField($base.'[city]', 'Lodz'); |
||
321 | $this->fillField($base.'[postcode]', '95-253'); |
||
322 | $this->selectOption($base.'[countryCode]', $country); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @Given /^I select the "(?P<field>([^""]|\\")*)" radio button$/ |
||
327 | */ |
||
328 | public function iSelectTheRadioButton($field) |
||
329 | { |
||
330 | $field = str_replace('\\"', '"', $field); |
||
331 | $radio = $this->getSession()->getPage()->findField($field); |
||
332 | |||
333 | if (null === $radio) { |
||
334 | throw new ElementNotFoundException( |
||
335 | $this->getSession(), 'form field', 'id|name|label|value', $field |
||
336 | ); |
||
337 | } |
||
338 | |||
339 | $this->fillField($radio->getAttribute('name'), $radio->getAttribute('value')); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @Then I should not see :position in the menu |
||
344 | */ |
||
345 | public function iShouldNotSeeInTheMenu($position) |
||
349 | |||
350 | /** |
||
351 | * @Then I should see :position in the menu |
||
352 | */ |
||
353 | public function iShouldSeeInTheMenu($position) |
||
357 | |||
358 | /** |
||
359 | * @Then I should not see :button button |
||
360 | */ |
||
361 | public function iShouldNotSeeButton($button) |
||
365 | |||
366 | /** |
||
367 | * @Then I should not see :button button near :user in :table table |
||
368 | */ |
||
369 | public function iShouldNotSeeButtonInColumnInTable($button, $customer, $table) |
||
370 | { |
||
371 | $this->assertSession()->elementExists('css', '#'.$table." tr[data-customer='$customer']"); |
||
372 | $this->assertSession()->elementNotExists('css', '#'.$table." tr[data-customer='$customer'] form input[value=".strtoupper($button).']'); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @Then /^I should see product prices in "([^"]*)"$/ |
||
377 | */ |
||
378 | public function iShouldSeeProductPricesIn($code) |
||
379 | { |
||
380 | $symbol = Intl::getCurrencyBundle()->getCurrencySymbol($code); |
||
381 | $this->assertSession()->pageTextContains($symbol); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @Then I should see :count available currencies |
||
386 | */ |
||
387 | public function iShouldSeeAvailableCurrencies($count) |
||
391 | |||
392 | /** |
||
393 | * @When I change the currency to :currency |
||
394 | */ |
||
395 | public function iChangeTheCurrencyTo($code) |
||
396 | { |
||
397 | $symbol = Intl::getCurrencyBundle()->getCurrencySymbol($code); |
||
398 | $this->clickLink($symbol); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @Then I should see :count available locales |
||
403 | */ |
||
404 | public function iShouldSeeAvailableLocales($count) |
||
408 | |||
409 | /** |
||
410 | * @When I change the locale to :locale |
||
411 | */ |
||
412 | public function iChangeTheLocaleTo($name) |
||
416 | |||
417 | /** |
||
418 | * @Then I should browse the store in :locale |
||
419 | */ |
||
420 | public function iShouldBrowseTheStoreInLocale($name) |
||
421 | { |
||
422 | $text = 'Welcome to Sylius'; |
||
423 | |||
424 | switch ($name) { |
||
425 | case 'Polish': |
||
426 | case 'Polish (Poland)': |
||
427 | $text = 'Witaj w Sylius'; |
||
428 | break; |
||
429 | case 'German': |
||
430 | case 'German (Germany)': |
||
431 | $text = 'Englisch'; |
||
432 | break; |
||
433 | } |
||
434 | |||
435 | $this->assertSession()->pageTextContains($text); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * For example: I should see 10 products. |
||
440 | * |
||
441 | * @Then /^I should see there (\d+) products/ |
||
442 | */ |
||
443 | public function iShouldSeeThatMuchProducts($amount) |
||
447 | |||
448 | /** |
||
449 | * @Given /^I am on the product page for "([^"]*)"$/ |
||
450 | * @Given /^I go to the product page for "([^"]*)"$/ |
||
451 | */ |
||
452 | public function iAmOnTheProductPage($name) |
||
453 | { |
||
454 | $product = $this->findOneBy('product', ['name' => $name]); |
||
455 | |||
456 | $this->getSession()->visit($this->generatePageUrl($product)); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @Then /^I should be on the product page for "([^"]*)"$/ |
||
461 | * @Then /^I should still be on the product page for "([^"]*)"$/ |
||
462 | */ |
||
463 | public function iShouldBeOnTheProductPage($name) |
||
464 | { |
||
465 | $product = $this->findOneBy('product', ['name' => $name]); |
||
466 | |||
467 | $this->assertSession()->addressEquals($this->generateUrl($product)); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @Given /^I am on the order ([^""]*) page for (\d+)$/ |
||
472 | * @Given /^I go to the order ([^""]*) page for (\d+)$/ |
||
473 | */ |
||
474 | public function iAmOnTheOrderPage($action, $number) |
||
475 | { |
||
476 | $order = $this->findOneBy('order', ['number' => $number]); |
||
477 | |||
478 | $this->getSession()->visit($this->generatePageUrl('sylius_account_order_'.$action, ['number' => $order->getNumber()])); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @Then /^I should be on the order ([^""]*) page for (\d+)$/ |
||
483 | * @Then /^I should still be on the order ([^""]*) page for (\d+)$/ |
||
484 | */ |
||
485 | public function iShouldBeOnTheOrderPage($action, $number) |
||
489 | |||
490 | /** |
||
491 | * @Given /^I am not authenticated$/ |
||
492 | * @Given /^I am not logged in anymore$/ |
||
493 | */ |
||
494 | public function iAmNotAuthenticated() |
||
495 | { |
||
496 | $this->getTokenStorage()->setToken(null); |
||
497 | $this->getContainer()->get('session')->invalidate(); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @Given /^I log in with "([^""]*)" and "([^""]*)"$/ |
||
502 | */ |
||
503 | public function iLogInWith($email, $password) |
||
511 | |||
512 | /** |
||
513 | * @Given /^I add following option values:$/ |
||
514 | */ |
||
515 | public function iAddFollowingOptionValues(TableNode $table) |
||
516 | { |
||
517 | foreach ($table->getRows() as $value) { |
||
518 | $newItem = $this->addNewItemToFormCollection($this->getSession()->getPage(), 'option_values'); |
||
519 | $newItem->fillField('Value', $value[1]); |
||
520 | $newItem->fillField('Code', $value[0]); |
||
521 | } |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * @When /^I add option value "([^""]*)"$/ |
||
526 | */ |
||
527 | public function iAddOptionValue($optionValue) |
||
528 | { |
||
529 | $newItem = $this->addNewItemToFormCollection($this->getSession()->getPage(), 'option_values'); |
||
530 | $newItem->fillField('Value', $optionValue); |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @Given /^I add choice "([^""]*)"$/ |
||
535 | */ |
||
536 | public function iAddChoice($value) |
||
541 | |||
542 | /** |
||
543 | * @When I want to create a :type zone |
||
544 | */ |
||
545 | public function iWantToCreateZoneOfType($type) |
||
546 | { |
||
547 | $this->selectOption('zone_type', $type); |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * @When /^I add zone member "([^"]+)"$/ |
||
552 | */ |
||
553 | public function iAddZoneMember($zoneMember) |
||
564 | |||
565 | /** |
||
566 | * @Given /^I remove first choice$/ |
||
567 | */ |
||
568 | public function iRemoveFirstChoice() |
||
577 | |||
578 | /** |
||
579 | * @When /^I click the login with (.+) button$/ |
||
580 | * @When /^I press the login with (.+) button$/ |
||
581 | */ |
||
582 | public function iClickTheLoginWithButton($provider) |
||
592 | |||
593 | /** |
||
594 | * @Given /^I added product "([^""]*)" to cart, with quantity "([^""]*)"$/ |
||
595 | * @When /^I add product "([^""]*)" to cart, with quantity "([^""]*)"$/ |
||
596 | */ |
||
597 | public function iAddedProductToCartWithQuantity($productName, $quantity) |
||
603 | |||
604 | /** |
||
605 | * @Given /^I finish the checkout process$/ |
||
606 | */ |
||
607 | public function iFinishTheCheckoutProcess() |
||
618 | |||
619 | /** |
||
620 | * @Then /^I should see ([^""]*) "([^""]*)" for "([^""]*)"$/ |
||
621 | */ |
||
622 | public function iShouldSeeQuantityFor($property, $expectedValue, $item) |
||
639 | |||
640 | /** |
||
641 | * @Given I view deleted elements |
||
642 | */ |
||
643 | public function iViewDeletedElements() |
||
647 | |||
648 | /** |
||
649 | * @Given I registered with email :email and password :password |
||
650 | */ |
||
651 | public function iRegisteredWithEmailAndPassword($email, $password) |
||
652 | { |
||
653 | $this->getSession()->visit($this->generatePageUrl('sylius_user_registration')); |
||
654 | |||
655 | $this->fillField('First name', $this->faker->firstName); |
||
656 | $this->fillField('Last name', $this->faker->lastName); |
||
657 | $this->fillField('Email', $email); |
||
662 | |||
663 | /** |
||
664 | * @When /^(?:|I )fill in guest email with "(?P<value>(?:[^"]|\\")*)"$/ |
||
665 | */ |
||
666 | public function fillGuestEmail($value) |
||
671 | |||
672 | /** |
||
673 | * @When /^I display ([^""]*)$/ |
||
674 | */ |
||
675 | public function iDisplayPage($page) |
||
684 | |||
685 | /** |
||
686 | * @When I restart my browser |
||
687 | */ |
||
688 | public function iRestartMyBrowser() |
||
696 | |||
697 | /** |
||
698 | * @Given /^I go to page for product with empty slug$/ |
||
699 | */ |
||
700 | public function iGoToPageForProductWithEmptySlug() |
||
704 | |||
705 | /** |
||
706 | * @Given /^I have changed my account password to "([^""]*)"$/ |
||
707 | * @When /^I change my account password to "([^""]*)"$/ |
||
708 | */ |
||
709 | public function iChangeMyPasswordTo($newPassword) |
||
717 | |||
718 | /** |
||
719 | * @Then the code field should be disabled |
||
720 | * @Then I should see disabled code field |
||
721 | */ |
||
722 | public function theCodeFieldShouldBeDisabled() |
||
726 | |||
727 | /** |
||
728 | * @Then the customer should have username :username |
||
729 | */ |
||
730 | public function theCustomerWithEmailShouldHaveUsername($username) |
||
734 | |||
735 | /** |
||
736 | * @Then the customer should be enabled |
||
737 | */ |
||
738 | public function theCustomerShouldBeEnabled() |
||
742 | |||
743 | private function assertRoute($route) |
||
752 | |||
753 | /** |
||
754 | * @param DocumentElement $page |
||
755 | * @param string $collectionName |
||
756 | * |
||
757 | * @return NodeElement |
||
758 | */ |
||
759 | protected function getFormCollectionDiv(DocumentElement $page, $collectionName) |
||
763 | |||
764 | /** |
||
765 | * @param DocumentElement $page |
||
766 | * @param string $collectionName |
||
767 | * |
||
768 | * @return NodeElement |
||
769 | */ |
||
770 | protected function addNewItemToFormCollection(DocumentElement $page, $collectionName) |
||
780 | } |
||
781 |