From e79a18bdf913b0cac445f56252f70383790a1645 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Sat, 18 May 2024 14:48:59 +0200 Subject: [PATCH] Cleanup and remove no more required codes --- .../Context/Admin/ManagingOrdersContext.php | 105 ++++++------------ .../Element/Admin/OrderCreateFormElement.php | 65 +++++------ tests/Behat/Element/Element.php | 19 ++-- tests/Behat/Page/Admin/OrderPreviewPage.php | 37 +++--- tests/Behat/Resources/services.xml | 1 - 5 files changed, 84 insertions(+), 143 deletions(-) diff --git a/tests/Behat/Context/Admin/ManagingOrdersContext.php b/tests/Behat/Context/Admin/ManagingOrdersContext.php index c328e189..ae1cd0a2 100644 --- a/tests/Behat/Context/Admin/ManagingOrdersContext.php +++ b/tests/Behat/Context/Admin/ManagingOrdersContext.php @@ -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; @@ -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; } /** @@ -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(); } @@ -127,7 +90,9 @@ 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()); } /** @@ -135,7 +100,9 @@ public function addProductToThisOrder(ProductInterface $product): void */ 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); } /** @@ -143,11 +110,13 @@ public function addMultipleProductsToThisOrder(int $quantity, ProductInterface $ */ 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()); } } @@ -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) ); } @@ -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); } } @@ -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') ); } @@ -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); } /** diff --git a/tests/Behat/Element/Admin/OrderCreateFormElement.php b/tests/Behat/Element/Admin/OrderCreateFormElement.php index 9235cfba..3e754790 100644 --- a/tests/Behat/Element/Admin/OrderCreateFormElement.php +++ b/tests/Behat/Element/Admin/OrderCreateFormElement.php @@ -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; @@ -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 @@ -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 ); } @@ -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) { @@ -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 @@ -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 @@ -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 @@ -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; } } @@ -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')) { @@ -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'); @@ -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(); } } diff --git a/tests/Behat/Element/Element.php b/tests/Behat/Element/Element.php index 693e8516..ae12e950 100644 --- a/tests/Behat/Element/Element.php +++ b/tests/Behat/Element/Element.php @@ -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; @@ -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 @@ -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] ); diff --git a/tests/Behat/Page/Admin/OrderPreviewPage.php b/tests/Behat/Page/Admin/OrderPreviewPage.php index 5a650653..3ad0c6b9 100644 --- a/tests/Behat/Page/Admin/OrderPreviewPage.php +++ b/tests/Behat/Page/Admin/OrderPreviewPage.php @@ -4,10 +4,7 @@ namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Page\Admin; -use Behat\Mink\Driver\PantherDriver; -use Behat\Mink\Driver\Selenium2Driver; use Behat\Mink\Element\NodeElement; -use DMore\ChromeDriver\ChromeDriver; use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage; final class OrderPreviewPage extends SymfonyPage implements OrderPreviewPageInterface @@ -19,12 +16,16 @@ public function getRouteName(): string public function getTotal(): string { - return str_replace('Order total: ', '', $this->getDocument()->find('css', 'td#total')->getText()); + /** @var NodeElement $totalCell */ + $totalCell = $this->getDocument()->find('css', 'td#total'); + return str_replace('Order total: ', '', $totalCell->getText()); } public function getShippingTotal(): string { - return str_replace('Shipping total: ', '', $this->getDocument()->find('css', 'td#shipping-total')->getText()); + /** @var NodeElement $shippingTotalCell */ + $shippingTotalCell = $this->getDocument()->find('css', 'td#shipping-total'); + return str_replace('Shipping total: ', '', $shippingTotalCell->getText()); } public function hasProduct(string $productName): bool @@ -57,6 +58,7 @@ public function hasOrderDiscountValidationMessage(string $message): bool public function hasItemDiscountValidationMessage(string $productCode, string $message): bool { + /** @var NodeElement $item */ $item = $this->getDocument()->find('css', sprintf('table tr:contains("%s") + tr', $productCode)); return null !== $item->find('css', sprintf('.sylius-validation-error:contains("%s")', $message)); @@ -67,7 +69,7 @@ public function hasLocale(string $localeName): bool /** @var NodeElement $localeElement */ $localeElement = $this->getDocument()->find('css', '#sylius-order-locale-code'); - return strpos($localeElement->getText(), $localeName) !== false; + return str_contains($localeElement->getText(), $localeName); } public function hasCurrency(string $currencyName): bool @@ -75,11 +77,12 @@ public function hasCurrency(string $currencyName): bool /** @var NodeElement $localeElement */ $localeElement = $this->getDocument()->find('css', '#sylius-order-currency'); - return strpos($localeElement->getText(), $currencyName) !== false; + return str_contains($localeElement->getText(), $currencyName); } public function lowerOrderPriceBy(string $discount): void { + /** @var NodeElement $discountCollection */ $discountCollection = $this->getDocument()->find('css', '#sylius_admin_order_creation_new_order_adjustments'); $discountCollection->clickLink('Add discount'); @@ -92,9 +95,11 @@ public function lowerOrderPriceBy(string $discount): void public function lowerItemWithProductPriceBy(string $productCode, string $discount): void { + /** @var NodeElement $item */ $item = $this->getDocument()->find('css', sprintf('table tr:contains("%s") + tr', $productCode)); $item->clickLink('Add discount'); + /** @var NodeElement $discountCollection */ $discountCollection = $item->find('css', '[data-form-type="collection"]'); $this->getDocument()->waitFor(1, function () use ($discountCollection) { @@ -106,25 +111,11 @@ public function lowerItemWithProductPriceBy(string $productCode, string $discoun public function confirm(): void { - $confirmButton = $this->getDocument()->findButton('Confirm'); - - $driver = $this->getDriver(); - if ($driver instanceof Selenium2Driver || $driver instanceof ChromeDriver || $driver instanceof PantherDriver) { - $confirmButton->focus(); - } - - $confirmButton->press(); + $this->getDocument()->pressButton('Confirm'); } public function goBack(): void { - $backButton = $this->getDocument()->findButton('Back'); - - $driver = $this->getDriver(); - if ($driver instanceof Selenium2Driver || $driver instanceof ChromeDriver || $driver instanceof PantherDriver) { - $backButton->focus(); - } - - $backButton->press(); + $this->getDocument()->pressButton('Back'); } } diff --git a/tests/Behat/Resources/services.xml b/tests/Behat/Resources/services.xml index 6af58e09..69ec3b9b 100644 --- a/tests/Behat/Resources/services.xml +++ b/tests/Behat/Resources/services.xml @@ -24,7 +24,6 @@ -