|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file was created by developers working at BitBag |
| 5 | + * Do you need more information about us and what we do? Visit our https://bitbag.io website! |
| 6 | + * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
| 7 | +*/ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Tests\BitBag\SyliusCmsPlugin\Behat\Context\Ui\Admin; |
| 12 | + |
| 13 | +use Behat\Behat\Context\Context; |
| 14 | +use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface; |
| 15 | +use Sylius\Behat\NotificationType; |
| 16 | +use Sylius\Behat\Service\NotificationCheckerInterface; |
| 17 | +use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface; |
| 18 | +use Sylius\Behat\Service\SharedStorageInterface; |
| 19 | +use Tests\BitBag\SyliusCmsPlugin\Behat\Page\Admin\Section\CreatePageInterface; |
| 20 | +use Tests\BitBag\SyliusCmsPlugin\Behat\Page\Admin\Section\IndexPageInterface; |
| 21 | +use Tests\BitBag\SyliusCmsPlugin\Behat\Page\Admin\Section\UpdatePageInterface; |
| 22 | +use Tests\BitBag\SyliusCmsPlugin\Behat\Service\RandomStringGeneratorInterface; |
| 23 | +use Webmozart\Assert\Assert; |
| 24 | + |
| 25 | +final class SectionContext implements Context |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private SharedStorageInterface $sharedStorage, |
| 29 | + private CurrentPageResolverInterface $currentPageResolver, |
| 30 | + private NotificationCheckerInterface $notificationChecker, |
| 31 | + private IndexPageInterface $indexPage, |
| 32 | + private CreatePageInterface $createPage, |
| 33 | + private UpdatePageInterface $updatePage, |
| 34 | + private RandomStringGeneratorInterface $randomStringGenerator, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @When I go to the sections page |
| 40 | + */ |
| 41 | + public function iGoToTheSectionsPage(): void |
| 42 | + { |
| 43 | + $this->indexPage->open(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @When I go to the create section page |
| 48 | + */ |
| 49 | + public function iGoToTheCreateSectionPage(): void |
| 50 | + { |
| 51 | + $this->createPage->open(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @When I delete this section |
| 56 | + */ |
| 57 | + public function iDeleteThisSection(): void |
| 58 | + { |
| 59 | + $section = $this->sharedStorage->get('section'); |
| 60 | + |
| 61 | + $this->indexPage->deleteSection($section->getCode()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @When I want to edit this section |
| 66 | + */ |
| 67 | + public function iWantToEditThisSection(): void |
| 68 | + { |
| 69 | + $section = $this->sharedStorage->get('section'); |
| 70 | + |
| 71 | + $this->updatePage->open(['id' => $section->getId()]); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @When I fill the code with :code |
| 76 | + */ |
| 77 | + public function iFillTheCodeWith(string $code): void |
| 78 | + { |
| 79 | + $this->resolveCurrentPage()->fillCode($code); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @When I fill the name with :name |
| 84 | + */ |
| 85 | + public function iFillTheNameWith(string $name): void |
| 86 | + { |
| 87 | + $this->resolveCurrentPage()->fillName($name); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @When I add it |
| 92 | + * @When I try to add it |
| 93 | + */ |
| 94 | + public function iAddIt(): void |
| 95 | + { |
| 96 | + $this->createPage->create(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @When /^I fill "([^"]*)" fields with (\d+) (?:character|characters)$/ |
| 101 | + */ |
| 102 | + public function iFillFieldsWithCharacters(string $fields, int $length): void |
| 103 | + { |
| 104 | + $fields = explode(',', $fields); |
| 105 | + |
| 106 | + foreach ($fields as $field) { |
| 107 | + $this->resolveCurrentPage()->fillField(trim($field), $this->randomStringGenerator->generate($length)); |
| 108 | + sleep(5); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @Then I should be notified that :fields fields cannot be blank |
| 114 | + */ |
| 115 | + public function iShouldBeNotifiedThatFieldsCannotBeBlank(string $fields): void |
| 116 | + { |
| 117 | + $fields = explode(',', $fields); |
| 118 | + |
| 119 | + foreach ($fields as $field) { |
| 120 | + Assert::true($this->resolveCurrentPage()->containsErrorWithMessage(sprintf( |
| 121 | + '%s cannot be blank.', |
| 122 | + trim($field), |
| 123 | + ))); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @Then I should be notified that :fields fields are too short |
| 129 | + */ |
| 130 | + public function iShouldBeNotifiedThatFieldsAreTooShort(string $fields): void |
| 131 | + { |
| 132 | + $fields = explode(',', $fields); |
| 133 | + |
| 134 | + foreach ($fields as $field) { |
| 135 | + Assert::true($this->resolveCurrentPage()->containsErrorWithMessage(sprintf( |
| 136 | + '%s must be at least %d characters long.', |
| 137 | + trim($field), |
| 138 | + 2, |
| 139 | + ))); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @Then I should be notified that :fields fields are too long |
| 145 | + */ |
| 146 | + public function iShouldBeNotifiedThatFieldsAreTooLong(string $fields): void |
| 147 | + { |
| 148 | + $fields = explode(',', $fields); |
| 149 | + |
| 150 | + foreach ($fields as $field) { |
| 151 | + Assert::true($this->resolveCurrentPage()->containsErrorWithMessage(sprintf( |
| 152 | + '%s can not be longer than', |
| 153 | + trim($field), |
| 154 | + ), false)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @Then I should be notified that there is already an existing section with provided code |
| 160 | + */ |
| 161 | + public function iShouldBeNotifiedThatThereIsAlreadyAnExistingSectionWithCode(): void |
| 162 | + { |
| 163 | + Assert::true($this->resolveCurrentPage()->containsErrorWithMessage( |
| 164 | + 'There is an existing section with this code.', |
| 165 | + false, |
| 166 | + )); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @Then I should be notified that new section has been created |
| 171 | + */ |
| 172 | + public function iShouldBeNotifiedThatNewSectionHasBeenCreated(): void |
| 173 | + { |
| 174 | + $this->notificationChecker->checkNotification( |
| 175 | + 'Section has been successfully created.', |
| 176 | + NotificationType::success(), |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @Then I should be notified that the section has been deleted |
| 182 | + */ |
| 183 | + public function iShouldBeNotifiedThatTheSectionHasBeenDeleted(): void |
| 184 | + { |
| 185 | + $this->notificationChecker->checkNotification( |
| 186 | + 'Section has been successfully deleted.', |
| 187 | + NotificationType::success(), |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @Then the code field should be disabled |
| 193 | + */ |
| 194 | + public function theCodeFieldShouldBeDisabled(): void |
| 195 | + { |
| 196 | + Assert::true($this->resolveCurrentPage()->isCodeDisabled()); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @Then I should see empty list of sections |
| 201 | + */ |
| 202 | + public function iShouldSeeEmptyListOfSections(): void |
| 203 | + { |
| 204 | + $this->resolveCurrentPage()->isEmpty(); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @return IndexPageInterface|CreatePageInterface|UpdatePageInterface|SymfonyPageInterface |
| 209 | + */ |
| 210 | + private function resolveCurrentPage(): SymfonyPageInterface |
| 211 | + { |
| 212 | + return $this->currentPageResolver->getCurrentPageWithForm([ |
| 213 | + $this->indexPage, |
| 214 | + $this->createPage, |
| 215 | + $this->updatePage, |
| 216 | + ]); |
| 217 | + } |
| 218 | +} |
0 commit comments