Skip to content

Commit

Permalink
Cleanup and remove no more required codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed May 18, 2024
1 parent a35c891 commit e79a18b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 143 deletions.
105 changes: 34 additions & 71 deletions tests/Behat/Context/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use Behat\Behat\Context\Context;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Service\Checker\EmailCheckerInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Bundle\CoreBundle\Application\Kernel;
use Sylius\Component\Addressing\Comparator\AddressComparatorInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Behat\Service\Checker\EmailCheckerInterface as BehatEmailCheckerInterface;
use Sylius\Component\Core\Test\Services\EmailCheckerInterface as CoreEmailCheckerInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Element\Admin\OrderCreateFormElementInterface;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Page\Admin\NewOrderCustomerPageInterface;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Page\Admin\OrderIndexPageInterface;
Expand All @@ -25,53 +23,16 @@

final class ManagingOrdersContext implements Context
{
/** @var OrderIndexPageInterface */
private $orderIndexPage;

/** @var NewOrderCustomerPageInterface */
private $newOrderCustomerPage;

/** @var OrderPreviewPageInterface */
private $orderPreviewPage;

/** @var OrderShowPageInterface */
private $orderShowPage;

/** @var ReorderPageInterface */
private $reorderPage;

/** @var OrderCreateFormElementInterface */
private $orderCreateFormElement;

/** @var NotificationCheckerInterface */
private $notificationChecker;

/** @var BehatEmailCheckerInterface|CoreEmailCheckerInterface */
private $emailChecker;

/** @var AddressComparatorInterface */
private $addressComparator;

public function __construct(
OrderIndexPageInterface $orderIndexPage,
NewOrderCustomerPageInterface $newOrderCustomerPage,
OrderPreviewPageInterface $orderPreviewPage,
OrderShowPageInterface $orderShowPage,
ReorderPageInterface $reorderPage,
OrderCreateFormElementInterface $orderCreateFormElement,
NotificationCheckerInterface $notificationChecker,
BehatEmailCheckerInterface|CoreEmailCheckerInterface $emailChecker,
AddressComparatorInterface $addressComparator
private OrderIndexPageInterface $orderIndexPage,
private NewOrderCustomerPageInterface $newOrderCustomerPage,
private OrderPreviewPageInterface $orderPreviewPage,
private OrderShowPageInterface $orderShowPage,
private ReorderPageInterface $reorderPage,
private OrderCreateFormElementInterface $orderCreateFormElement,
private NotificationCheckerInterface $notificationChecker,
private EmailCheckerInterface $emailChecker,
) {
$this->orderIndexPage = $orderIndexPage;
$this->newOrderCustomerPage = $newOrderCustomerPage;
$this->orderPreviewPage = $orderPreviewPage;
$this->orderShowPage = $orderShowPage;
$this->reorderPage = $reorderPage;
$this->orderCreateFormElement = $orderCreateFormElement;
$this->notificationChecker = $notificationChecker;
$this->emailChecker = $emailChecker;
$this->addressComparator = $addressComparator;
}

/**
Expand All @@ -84,7 +45,9 @@ public function createNewOrderFor(CustomerInterface $customer, string $channelNa

$this->newOrderCustomerPage->selectChannel($channelName);

$this->newOrderCustomerPage->selectCustomer($customer->getEmail());
/** @var string $customerEmail */
$customerEmail = $customer->getEmail();
$this->newOrderCustomerPage->selectCustomer($customerEmail);
$this->newOrderCustomerPage->next();
}

Expand Down Expand Up @@ -127,27 +90,33 @@ public function tryToCreateNewOrderForNewCustomerWithoutEmail(): void
*/
public function addProductToThisOrder(ProductInterface $product): void
{
$this->orderCreateFormElement->addProduct($product->getVariants()->first()->getDescriptor());
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();
$this->orderCreateFormElement->addProduct($productVariant->getDescriptor());
}

/**
* @When I add :quantity of :product to this order
*/
public function addMultipleProductsToThisOrder(int $quantity, ProductInterface $product): void
{
$this->orderCreateFormElement->addMultipleProducts($product->getVariants()->first()->getDescriptor(), $quantity);
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();
$this->orderCreateFormElement->addMultipleProducts($productVariant->getDescriptor(), $quantity);
}

/**
* @When I remove :product from this order
*/
public function removeProductFromThisOrder(ProductInterface $product): void
{
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();
try {
$this->orderCreateFormElement->removeProduct($product->getVariants()->first()->getDescriptor());
$this->orderCreateFormElement->removeProduct($productVariant->getDescriptor());
} catch (\InvalidArgumentException $exception) {
// TODO: Currently the autocomplete include product variant code instead of its descriptor when rendering a form with existing items
$this->orderCreateFormElement->removeProduct($product->getVariants()->first()->getCode());
$this->orderCreateFormElement->removeProduct($productVariant->getCode());
}
}

Expand Down Expand Up @@ -237,8 +206,10 @@ public function lowerOrderPriceBy(string $discount): void
*/
public function lowerItemWithProductPriceBy(ProductInterface $product, string $discount): void
{
/** @var string $productCode */
$productCode = $product->getCode();
$this->orderPreviewPage->lowerItemWithProductPriceBy(
$product->getCode(),
$productCode,
str_replace(['$', '', '£'], '', $discount)
);
}
Expand All @@ -248,11 +219,13 @@ public function lowerItemWithProductPriceBy(ProductInterface $product, string $d
*/
public function iChangeQuantityOfItemTo(ProductInterface $product, int $quantity): void
{
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();
try {
$this->orderCreateFormElement->specifyQuantity($product->getVariants()->first()->getDescriptor(), $quantity);
$this->orderCreateFormElement->specifyQuantity($productVariant->getDescriptor(), $quantity);
} catch (\InvalidArgumentException $exception) {
// TODO: Currently the autocomplete include product variant code instead of its descriptor when rendering a form with existing items
$this->orderCreateFormElement->specifyQuantity($product->getVariants()->first()->getCode(), $quantity);
$this->orderCreateFormElement->specifyQuantity($productVariant->getCode(), $quantity);
}
}

Expand Down Expand Up @@ -352,8 +325,10 @@ public function shouldBeNotifiedThatOrderDiscountCannotBeBelow0(): void
*/
public function shouldBeNotifiedThatItemWithProductDiscountCannotBeBelow0(ProductInterface $product): void
{
/** @var string $productCode */
$productCode = $product->getCode();
Assert::true(
$this->orderPreviewPage->hasItemDiscountValidationMessage($product->getCode(), 'Discount cannot be below 0')
$this->orderPreviewPage->hasItemDiscountValidationMessage($productCode, 'Discount cannot be below 0')
);
}

Expand Down Expand Up @@ -397,19 +372,7 @@ public function thereShouldBePaymentLinkSentTo(string $email): void
*/
public function thereShouldBeNoPaymentLinkSentTo(string $email): void
{
if (Kernel::VERSION_ID < 11200) {
try {
$this->emailChecker->countMessagesTo($email);
} catch (\InvalidArgumentException $exception) {
return;
}

throw new \Exception('There should be no messages exception thrown');
}

if (11200 <= Kernel::VERSION_ID) {
Assert::same($this->emailChecker->countMessagesTo($email), 0);
}
Assert::same($this->emailChecker->countMessagesTo($email), 0);
}

/**
Expand Down
65 changes: 29 additions & 36 deletions tests/Behat/Element/Admin/OrderCreateFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Element\Admin;

use Behat\Mink\Driver\PantherDriver;
use Behat\Mink\Driver\Selenium2Driver;
use ArrayAccess;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\Exception;
use Behat\Mink\Session;
use DMore\ChromeDriver\ChromeDriver;
use Sylius\Component\Core\Model\AddressInterface;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Element\Element;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Service\AutoCompleteSelector;
Expand All @@ -19,16 +17,12 @@ class OrderCreateFormElement extends Element implements OrderCreateFormElementIn
public const TYPE_BILLING = 'billing';
public const TYPE_SHIPPING = 'shipping';

/** @var AutoCompleteSelector */
private $autoCompleteSelector;
public function __construct(
Session $session,
$parameters,
AutoCompleteSelector $autoCompleteSelector
ArrayAccess $parameters,
private AutoCompleteSelector $autoCompleteSelector,
) {
parent::__construct($session, $parameters);

$this->autoCompleteSelector = $autoCompleteSelector;
}

public function addProduct(string $productVariantDescriptor): void
Expand Down Expand Up @@ -70,16 +64,20 @@ public function specifyShippingAddress(AddressInterface $address): void
{
$this->clickOnTabAndWait('Shipping address & Billing address');

/** @var NodeElement $addressForm */
$addressForm = $this->getDocument()->find('css', 'div[id*="shippingAddress"]');
$this->fillAddressData(
$this->getDocument()->find('css', 'div[id*="shippingAddress"]'),
$addressForm,
$address
);
}

public function specifyBillingAddress(AddressInterface $address): void
{
/** @var NodeElement $addressForm */
$addressForm = $this->getDocument()->find('css', 'div[id*="billingAddress"]');
$this->fillAddressData(
$this->getDocument()->find('css', 'div[id*="billingAddress"]'),
$addressForm,
$address
);
}
Expand All @@ -88,6 +86,7 @@ public function getAvailableShippingMethods(): array
{
$this->clickOnTabAndWait('Shipments & Payments');

/** @var NodeElement $shipmentsCollection */
$shipmentsCollection = $this->getDocument()->find('css', '#sylius_admin_order_creation_new_order_shipments');

if (count($shipmentsCollection->findAll('css', '[data-form-collection="item"]')) === 0) {
Expand All @@ -100,11 +99,9 @@ public function getAvailableShippingMethods(): array
'css', '#sylius_admin_order_creation_new_order_shipments [data-form-collection="item"]:last-child select option'
);

$shippingMethods = array_map(function(NodeElement $option) : string {
return array_map(static function(NodeElement $option) : string {
return $option->getText();
}, $shippingMethods);

return $shippingMethods;
}

public function moveToShippingAndPaymentsSection(): void
Expand Down Expand Up @@ -169,11 +166,9 @@ public function selectCurrency(string $currencyName): void

public function getShippingMethodsValidationMessage(): string
{
return $this
->getDocument()
->find('css', '#shipmentsAndPayments .invalid-data-message')
->getText()
;
/** @var NodeElement $invalidMessage */
$invalidMessage = $this->getDocument()->find('css', '#shipmentsAndPayments .invalid-data-message');
return $invalidMessage->getText();
}

protected function getDefinedElements(): array
Expand Down Expand Up @@ -252,7 +247,9 @@ private function addItemAndWaitForIt(): NodeElement
return $this->countItems() > $itemsCount;
});

return $this->getDocument()->find('css', '#items [data-form-collection="item"]:last-child');
/** @var NodeElement $lastItem */
$lastItem = $this->getDocument()->find('css', '#items [data-form-collection="item"]:last-child');
return $lastItem;
}

private function countItems(): int
Expand All @@ -262,11 +259,12 @@ private function countItems(): int

private function getItemWithProductSelected(string $productVariantDescriptor): NodeElement
{
/** @var NodeElement $item */
foreach ($this->getDocument()->findAll('css', '#items [data-form-collection="item"]') as $item) {
$selectedProduct = $item->find('css', '.sylius-autocomplete .text')->getText();
/** @var NodeElement $autocompleteText */
$autocompleteText = $item->find('css', '.sylius-autocomplete .text');
$selectedProduct = $autocompleteText->getText();

if (strpos($selectedProduct, $productVariantDescriptor) !== false) {
if (str_contains($selectedProduct, $productVariantDescriptor)) {
return $item;
}
}
Expand All @@ -276,11 +274,7 @@ private function getItemWithProductSelected(string $productVariantDescriptor): N

private function clickOnTabAndWait(string $tabName): void
{
$driver = $this->getDriver();
if (!$driver instanceof Selenium2Driver && !$driver instanceof ChromeDriver && !$driver instanceof PantherDriver) {
return;
}

/** @var NodeElement $tab */
$tab = $this->getDocument()->find('css', sprintf('.title:contains("%s")', $tabName));

if ($tab->hasClass('active')) {
Expand All @@ -290,16 +284,15 @@ private function clickOnTabAndWait(string $tabName): void
$tab->click();

$this->getDocument()->waitFor(5, function () use ($tabName) {
return $this
->getDocument()
->find('css', sprintf('.title:contains("%s") + .content', $tabName))
->hasClass('active')
;
/** @var NodeElement $title */
$title = $this->getDocument()->find('css', sprintf('.title:contains("%s") + .content', $tabName));
return $title->hasClass('active');
});
}

private function waitForFormToLoad(): void
{
/** @var NodeElement $form */
$form = $this->getDocument()->find('css', '[name="sylius_admin_order_creation_new_order"]');
$this->getDocument()->waitFor(1000, function () use ($form) {
return !$form->hasClass('loading');
Expand All @@ -308,8 +301,8 @@ private function waitForFormToLoad(): void

public function isAddPaymentButtonVisible(): bool
{
return
$this->getElement('payments')->find('css', '[data-form-collection="add"]')->isVisible()
;
/** @var NodeElement $paymentButton */
$paymentButton = $this->getElement('payments')->find('css', '[data-form-collection="add"]');
return $paymentButton->isVisible();
}
}
19 changes: 7 additions & 12 deletions tests/Behat/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Element;

use ArrayAccess;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
Expand All @@ -13,18 +14,12 @@

abstract class Element
{
/** @var Session */
private $session;

private $parameters;
private ?DocumentElement $document = null;

/** @var DocumentElement|null */
private $document;

public function __construct(Session $session, $parameters = [])
{
$this->session = $session;
$this->parameters = $parameters;
public function __construct(
private Session $session,
private ArrayAccess $parameters,
) {
}

protected function getParameter(string $name): NodeElement
Expand Down Expand Up @@ -115,7 +110,7 @@ private function resolveParameters(string $name, array $parameters, array $defin
}

array_map(
function ($definedElement) use ($parameters): string {
static function ($definedElement) use ($parameters): string {
return strtr($definedElement, $parameters);
}, $definedElements[$name]
);
Expand Down
Loading

0 comments on commit e79a18b

Please sign in to comment.