Skip to content

Commit

Permalink
[Taxon][Behat] Extract management of image to separate form element.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojdylak authored and GSadee committed May 15, 2024
1 parent e3909ab commit 59695b1
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 90 deletions.
20 changes: 11 additions & 9 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -323,31 +325,31 @@ public function iShouldSeeTheTaxonNamedInTheList($name)
*/
public function thisTaxonShouldHaveAnImageWithType(string $type): void
{
Assert::true($this->formElement->isImageWithTypeDisplayed($type));
Assert::true($this->formImageElement->isImageWithTypeDisplayed($type));
}

/**
* @Then /^(?:this taxon|it) should not have(?:| also) any images with "([^"]*)" type$/
*/
public function thisTaxonShouldNotHaveAnImageWithType($code)
{
Assert::false($this->formElement->isImageWithTypeDisplayed($code));
Assert::false($this->formImageElement->isImageWithTypeDisplayed($code));
}

/**
* @When /^I(?:| also) remove an image with "([^"]*)" type$/
*/
public function iRemoveAnImageWithType(string $type): void
{
$this->formElement->removeImageWithType($type);
$this->formImageElement->removeImageWithType($type);
}

/**
* @When I remove the first image
*/
public function iRemoveTheFirstImage(): void
{
$this->formElement->removeFirstImage();
$this->formImageElement->removeFirstImage();
}

/**
Expand All @@ -357,23 +359,23 @@ public function thisTaxonShouldNotHaveAnyImages(TaxonInterface $taxon): void
{
$this->iWantToModifyATaxon($taxon);

Assert::same($this->formElement->countImages(), 0);
Assert::same($this->formImageElement->countImages(), 0);
}

/**
* @When I change the image with the :type type to :path
*/
public function iChangeItsImageToPathForTheType($path, $type): void
{
$this->formElement->changeImageWithType($type, $path);
$this->formImageElement->changeImageWithType($type, $path);
}

/**
* @When I change the first image type to :type
*/
public function iChangeTheFirstImageTypeTo($type): void
{
$this->formElement->modifyFirstImageType($type);
$this->formImageElement->modifyFirstImageType($type);
}

/**
Expand All @@ -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);
}

/**
Expand Down
66 changes: 0 additions & 66 deletions src/Sylius/Behat/Element/Admin/Taxon/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]"]',
]);
Expand Down
14 changes: 0 additions & 14 deletions src/Sylius/Behat/Element/Admin/Taxon/FormElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
94 changes: 94 additions & 0 deletions src/Sylius/Behat/Element/Admin/Taxon/FormImageElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Element\Admin\Taxon;

use Behat\Mink\Exception\ElementNotFoundException;
use Sylius\Behat\Element\Admin\Crud\FormElement as BaseFormElement;

final class FormImageElement extends BaseFormElement implements FormImageElementInterface
{
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]',
'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',
]);
}
}
33 changes: 33 additions & 0 deletions src/Sylius/Behat/Element/Admin/Taxon/FormImageElementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Element\Admin\Taxon;

use Sylius\Behat\Element\Admin\Crud\FormElementInterface as BaseFormElementInterface;

interface FormImageElementInterface extends BaseFormElementInterface
{
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;
}
1 change: 1 addition & 0 deletions src/Sylius/Behat/Resources/config/services/contexts/ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
<argument type="service" id="sylius.behat.page.admin.taxon.create_for_parent" />
<argument type="service" id="sylius.behat.page.admin.taxon.update" />
<argument type="service" id="sylius.behat.element.admin.taxon.form" />
<argument type="service" id="sylius.behat.element.admin.taxon.form_image" />
<argument type="service" id="sylius.behat.current_page_resolver" />
<argument type="service" id="sylius.behat.notification_checker.admin" />
<argument type="service" id="sylius.behat.java_script_test_helper" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<service
id="Sylius\Behat\Element\Admin\Channel\ExcludeTaxonsFromShowingLowestPriceInputElementInterface"
class="Sylius\Behat\Element\Admin\Channel\ExcludeTaxonsFromShowingLowestPriceInputElement"
parent="sylius.behat.element"
parent="sylius.behat.element.admin.crud.form"
>
<argument type="service" id="Sylius\Behat\Service\Helper\AutocompleteHelperInterface" />
</service>
Expand Down Expand Up @@ -87,5 +87,11 @@
class="Sylius\Behat\Element\Admin\Taxon\FormElement"
parent="sylius.behat.element.admin.crud.form"
/>

<service
id="sylius.behat.element.admin.taxon.form_image"
class="Sylius\Behat\Element\Admin\Taxon\FormImageElement"
parent="sylius.behat.element.admin.crud.form"
/>
</services>
</container>

0 comments on commit 59695b1

Please sign in to comment.