From 59695b195c5c36ee168ccdad58d6419355775163 Mon Sep 17 00:00:00 2001 From: Wojdylak Date: Tue, 14 May 2024 12:04:36 +0200 Subject: [PATCH] [Taxon][Behat] Extract management of image to separate form element. --- .../Ui/Admin/ManagingTaxonsContext.php | 20 ++-- .../Behat/Element/Admin/Taxon/FormElement.php | 66 ------------- .../Admin/Taxon/FormElementInterface.php | 14 --- .../Element/Admin/Taxon/FormImageElement.php | 94 +++++++++++++++++++ .../Admin/Taxon/FormImageElementInterface.php | 33 +++++++ .../Resources/config/services/contexts/ui.xml | 1 + .../config/services/elements/admin.xml | 8 +- 7 files changed, 146 insertions(+), 90 deletions(-) create mode 100644 src/Sylius/Behat/Element/Admin/Taxon/FormImageElement.php create mode 100644 src/Sylius/Behat/Element/Admin/Taxon/FormImageElementInterface.php diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php index 6206b153715..e13e7318d29 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php @@ -17,6 +17,7 @@ use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface; use Sylius\Behat\Context\Ui\Admin\Helper\ValidationTrait; use Sylius\Behat\Element\Admin\Taxon\FormElementInterface; +use Sylius\Behat\Element\Admin\Taxon\FormImageElementInterface; use Sylius\Behat\NotificationType; use Sylius\Behat\Page\Admin\Product\UpdateSimpleProductPageInterface; use Sylius\Behat\Page\Admin\Taxon\CreateForParentPageInterface; @@ -40,6 +41,7 @@ public function __construct( private readonly CreateForParentPageInterface $createForParentPage, private readonly UpdatePageInterface $updatePage, private readonly FormElementInterface $formElement, + private readonly FormImageElementInterface $formImageElement, private readonly CurrentPageResolverInterface $currentPageResolver, private readonly NotificationCheckerInterface $notificationChecker, private readonly JavaScriptTestHelper $testHelper, @@ -307,7 +309,7 @@ public function iShouldSeeTaxonsInTheList($number) */ public function iAttachImageWithType(string $path, ?string $type = null): void { - $this->formElement->attachImage($path, $type); + $this->formImageElement->attachImage($path, $type); } /** @@ -323,7 +325,7 @@ public function iShouldSeeTheTaxonNamedInTheList($name) */ public function thisTaxonShouldHaveAnImageWithType(string $type): void { - Assert::true($this->formElement->isImageWithTypeDisplayed($type)); + Assert::true($this->formImageElement->isImageWithTypeDisplayed($type)); } /** @@ -331,7 +333,7 @@ public function thisTaxonShouldHaveAnImageWithType(string $type): void */ public function thisTaxonShouldNotHaveAnImageWithType($code) { - Assert::false($this->formElement->isImageWithTypeDisplayed($code)); + Assert::false($this->formImageElement->isImageWithTypeDisplayed($code)); } /** @@ -339,7 +341,7 @@ public function thisTaxonShouldNotHaveAnImageWithType($code) */ public function iRemoveAnImageWithType(string $type): void { - $this->formElement->removeImageWithType($type); + $this->formImageElement->removeImageWithType($type); } /** @@ -347,7 +349,7 @@ public function iRemoveAnImageWithType(string $type): void */ public function iRemoveTheFirstImage(): void { - $this->formElement->removeFirstImage(); + $this->formImageElement->removeFirstImage(); } /** @@ -357,7 +359,7 @@ public function thisTaxonShouldNotHaveAnyImages(TaxonInterface $taxon): void { $this->iWantToModifyATaxon($taxon); - Assert::same($this->formElement->countImages(), 0); + Assert::same($this->formImageElement->countImages(), 0); } /** @@ -365,7 +367,7 @@ public function thisTaxonShouldNotHaveAnyImages(TaxonInterface $taxon): void */ public function iChangeItsImageToPathForTheType($path, $type): void { - $this->formElement->changeImageWithType($type, $path); + $this->formImageElement->changeImageWithType($type, $path); } /** @@ -373,7 +375,7 @@ public function iChangeItsImageToPathForTheType($path, $type): void */ public function iChangeTheFirstImageTypeTo($type): void { - $this->formElement->modifyFirstImageType($type); + $this->formImageElement->modifyFirstImageType($type); } /** @@ -384,7 +386,7 @@ public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon, { $this->iWantToModifyATaxon($taxon); - Assert::same($this->formElement->countImages(), (int) $count); + Assert::same($this->formImageElement->countImages(), (int) $count); } /** diff --git a/src/Sylius/Behat/Element/Admin/Taxon/FormElement.php b/src/Sylius/Behat/Element/Admin/Taxon/FormElement.php index c136a83f738..ae38b38c99b 100644 --- a/src/Sylius/Behat/Element/Admin/Taxon/FormElement.php +++ b/src/Sylius/Behat/Element/Admin/Taxon/FormElement.php @@ -13,7 +13,6 @@ namespace Sylius\Behat\Element\Admin\Taxon; -use Behat\Mink\Exception\ElementNotFoundException; use Sylius\Behat\Element\Admin\Crud\FormElement as BaseFormElement; final class FormElement extends BaseFormElement implements FormElementInterface @@ -32,77 +31,12 @@ public function slugIt(string $slug, string $localeCode): void { $this->getElement('slug', ['%locale_code%' => $localeCode])->setValue($slug); } - public function attachImage(string $path, ?string $type = null): void - { - $this->getElement('add_image')->press(); - $this->waitForFormUpdate(); - - $lastImage = $this->getElement('last_image'); - - if (null !== $type) { - $lastImage->fillField('Type', $type); - } - - $filesPath = $this->getParameter('files_path'); - $lastImage->find('css', '[data-test-file]')->attachFile($filesPath . $path); - } - - public function changeImageWithType(string $type, string $path): void - { - $image = $this->getElement('image_with_type', ['%type%' => $type]); - - $filesPath = $this->getParameter('files_path'); - $image->find('css', '[data-test-file]')->attachFile($filesPath . $path); - } - - public function modifyFirstImageType(string $type): void - { - $this->getElement('first_image')->fillField('Type', $type); - } - - public function removeImageWithType(string $type): void - { - $this->getElement('delete_image', ['%type%' => $type])->press(); - $this->waitForFormUpdate(); - } - - public function removeFirstImage(): void - { - $this->getElement('first_image')->find('css', '[data-test-delete-image]')->press(); - $this->waitForFormUpdate(); - } - - public function isImageWithTypeDisplayed(string $type): bool - { - try { - $image = $this->getElement('image_with_type', ['%type%' => $type]); - } catch (ElementNotFoundException) { - return false; - } - - $imageUrl = $image->getAttribute('data-test-image-url'); - $this->getDriver()->visit($imageUrl); - $statusCode = $this->getDriver()->getStatusCode(); - $this->getDriver()->back(); - - return in_array($statusCode, [200, 304], true); - } - public function countImages(): int - { - return count($this->getElement('images')->findAll('css', '[data-test-image]')); - } protected function getDefinedElements(): array { return array_merge(parent::getDefinedElements(), [ - 'add_image' => '[data-test-images] [data-test-add-image]', 'code' => '[data-test-code]', - 'delete_image' => '[data-test-images] [data-test-image][data-test-type="%type%"] [data-test-delete-image]', - 'first_image' => '[data-test-images] [data-test-image]:first-child', 'form' => '[data-live-name-value="sylius_admin:taxon:form"]', - 'images' => '[data-test-images]', - 'image_with_type' => '[data-test-images] [data-test-image][data-test-type="%type%"]', - 'last_image' => '[data-test-images] [data-test-image]:last-child', 'name' => '[name="taxon[translations][%locale_code%][name]"]', 'slug' => '[name="taxon[translations][%locale_code%][slug]"]', ]); diff --git a/src/Sylius/Behat/Element/Admin/Taxon/FormElementInterface.php b/src/Sylius/Behat/Element/Admin/Taxon/FormElementInterface.php index 5c2099cbe32..f65fd9f766b 100644 --- a/src/Sylius/Behat/Element/Admin/Taxon/FormElementInterface.php +++ b/src/Sylius/Behat/Element/Admin/Taxon/FormElementInterface.php @@ -22,18 +22,4 @@ public function getCode(): string; public function nameIt(string $name, string $localeCode): void; public function slugIt(string $slug, string $localeCode): void; - - public function attachImage(string $path, ?string $type = null): void; - - public function changeImageWithType(string $type, string $path): void; - - public function modifyFirstImageType(string $type): void; - - public function removeImageWithType(string $type): void; - - public function removeFirstImage(): void; - - public function isImageWithTypeDisplayed(string $type): bool; - - public function countImages(): int; } diff --git a/src/Sylius/Behat/Element/Admin/Taxon/FormImageElement.php b/src/Sylius/Behat/Element/Admin/Taxon/FormImageElement.php new file mode 100644 index 00000000000..4bfb3aa9b26 --- /dev/null +++ b/src/Sylius/Behat/Element/Admin/Taxon/FormImageElement.php @@ -0,0 +1,94 @@ +getElement('add_image')->press(); + $this->waitForFormUpdate(); + + $lastImage = $this->getElement('last_image'); + + if (null !== $type) { + $lastImage->fillField('Type', $type); + } + + $filesPath = $this->getParameter('files_path'); + $lastImage->find('css', '[data-test-file]')->attachFile($filesPath . $path); + } + + public function changeImageWithType(string $type, string $path): void + { + $image = $this->getElement('image_with_type', ['%type%' => $type]); + + $filesPath = $this->getParameter('files_path'); + $image->find('css', '[data-test-file]')->attachFile($filesPath . $path); + } + + public function modifyFirstImageType(string $type): void + { + $this->getElement('first_image')->fillField('Type', $type); + } + + public function removeImageWithType(string $type): void + { + $this->getElement('delete_image', ['%type%' => $type])->press(); + $this->waitForFormUpdate(); + } + + public function removeFirstImage(): void + { + $this->getElement('first_image')->find('css', '[data-test-delete-image]')->press(); + $this->waitForFormUpdate(); + } + + public function isImageWithTypeDisplayed(string $type): bool + { + try { + $image = $this->getElement('image_with_type', ['%type%' => $type]); + } catch (ElementNotFoundException) { + return false; + } + + $imageUrl = $image->getAttribute('data-test-image-url'); + $this->getDriver()->visit($imageUrl); + $statusCode = $this->getDriver()->getStatusCode(); + $this->getDriver()->back(); + + return in_array($statusCode, [200, 304], true); + } + + public function countImages(): int + { + return count($this->getElement('images')->findAll('css', '[data-test-image]')); + } + + protected function getDefinedElements(): array + { + return array_merge(parent::getDefinedElements(), [ + 'add_image' => '[data-test-images] [data-test-add-image]', + 'delete_image' => '[data-test-images] [data-test-image][data-test-type="%type%"] [data-test-delete-image]', + 'first_image' => '[data-test-images] [data-test-image]:first-child', + 'form' => '[data-live-name-value="sylius_admin:taxon:form"]', + 'image_with_type' => '[data-test-images] [data-test-image][data-test-type="%type%"]', + 'images' => '[data-test-images]', + 'last_image' => '[data-test-images] [data-test-image]:last-child', + ]); + } +} diff --git a/src/Sylius/Behat/Element/Admin/Taxon/FormImageElementInterface.php b/src/Sylius/Behat/Element/Admin/Taxon/FormImageElementInterface.php new file mode 100644 index 00000000000..c16e3a31e49 --- /dev/null +++ b/src/Sylius/Behat/Element/Admin/Taxon/FormImageElementInterface.php @@ -0,0 +1,33 @@ + + diff --git a/src/Sylius/Behat/Resources/config/services/elements/admin.xml b/src/Sylius/Behat/Resources/config/services/elements/admin.xml index ca041a9790a..19e849d5ec5 100644 --- a/src/Sylius/Behat/Resources/config/services/elements/admin.xml +++ b/src/Sylius/Behat/Resources/config/services/elements/admin.xml @@ -57,7 +57,7 @@ @@ -87,5 +87,11 @@ class="Sylius\Behat\Element\Admin\Taxon\FormElement" parent="sylius.behat.element.admin.crud.form" /> + +