[Behat] Refactor to use PHP 7.4 syntax

This commit is contained in:
Grzegorz Sadowski 2022-05-31 13:18:52 +02:00
parent e083ab026b
commit 5477cd090d
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
35 changed files with 59 additions and 140 deletions

View file

@ -10,6 +10,7 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
return static function (ContainerConfigurator $containerConfigurator): void
{
$containerConfigurator->import(SetList::PHP_74);
$containerConfigurator->import(SetList::PHP_80);
$parameters = $containerConfigurator->parameters();

View file

@ -21,16 +21,13 @@ final class Request implements RequestInterface
private string $method;
/** @var array */
private $headers = ['HTTP_ACCEPT' => 'application/ld+json'];
private array $headers = ['HTTP_ACCEPT' => 'application/ld+json'];
private array $content = [];
/** @var array */
private $parameters = [];
private array $parameters = [];
/** @var array */
private $files = [];
private array $files = [];
private function __construct(string $url, string $method, array $headers = [])
{

View file

@ -36,9 +36,7 @@ final class ResponseChecker implements ResponseCheckerInterface
public function getCollectionItemsWithValue(Response $response, string $key, string $value): array
{
$items = array_filter($this->getCollection($response), function (array $item) use ($key, $value): bool {
return $item[$key] === $value;
});
$items = array_filter($this->getCollection($response), fn(array $item): bool => $item[$key] === $value);
return $items;
}

View file

@ -63,9 +63,7 @@ final class AjaxContext implements Context
*/
public function iShouldSeeTheProductVariantNamedAnd(...$names): void
{
$itemsNames = array_map(static function ($item) {
return strstr($item['descriptor'], ' ', true);
}, $this->getJSONResponse());
$itemsNames = array_map(static fn($item) => strstr($item['descriptor'], ' ', true), $this->getJSONResponse());
Assert::allOneOf($itemsNames, $names);
}

View file

@ -714,7 +714,7 @@ final class CartContext implements Context
private function putProductToCart(ProductInterface $product, ?string $tokenValue, int $quantity = 1): void
{
$tokenValue = $tokenValue ?? $this->pickupCart();
$tokenValue ??= $this->pickupCart();
$request = Request::customItemAction('shop', 'orders', $tokenValue, HttpRequest::METHOD_POST, 'items');
@ -728,7 +728,7 @@ final class CartContext implements Context
private function putProductVariantToCart(ProductVariantInterface $productVariant, ?string $tokenValue, int $quantity = 1): void
{
$tokenValue = $tokenValue ?? $this->pickupCart();
$tokenValue ??= $this->pickupCart();
$request = Request::customItemAction('shop', 'orders', $tokenValue, HttpRequest::METHOD_POST, 'items');

View file

@ -21,11 +21,9 @@ use Webmozart\Assert\Assert;
final class LocaleContext implements Context
{
/** @var ApiClientInterface */
private $client;
private ApiClientInterface $client;
/** @var ResponseCheckerInterface */
private $responseChecker;
private ResponseCheckerInterface $responseChecker;
public function __construct(ApiClientInterface $client, ResponseCheckerInterface $responseChecker)
{

View file

@ -807,7 +807,7 @@ final class CatalogPromotionContext implements Context
$channels = [$this->sharedStorage->get('channel')];
}
$code = $code ?? StringInflector::nameToCode($name);
$code ??= StringInflector::nameToCode($name);
/** @var CatalogPromotionInterface $catalogPromotion */
$catalogPromotion = $this->catalogPromotionExampleFactory->create([

View file

@ -73,8 +73,7 @@ final class ProductContext implements Context
private SlugGeneratorInterface $slugGenerator;
/** @var array */
private $minkParameters;
private array $minkParameters;
public function __construct(
SharedStorageInterface $sharedStorage,
@ -677,9 +676,7 @@ final class ProductContext implements Context
{
$code = sprintf('%s_%s', $product->getCode(), $optionValueName);
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->filter(function ($variant) use ($code) {
return $code === $variant->getCode();
})->first();
$productVariant = $product->getVariants()->filter(fn($variant) => $code === $variant->getCode())->first();
Assert::notNull($productVariant, sprintf('Product variant with given code %s not exists!', $code));

View file

@ -42,8 +42,7 @@ final class TaxonomyContext implements Context
private TaxonSlugGeneratorInterface $taxonSlugGenerator;
/** @var array */
private $minkParameters;
private array $minkParameters;
public function __construct(
RepositoryInterface $taxonRepository,

View file

@ -54,8 +54,6 @@ final class ProductContext implements Context
*/
public function getProductsByNames(...$productsNames)
{
return array_map(function ($productName) {
return $this->getProductByName($productName);
}, $productsNames);
return array_map(fn($productName) => $this->getProductByName($productName), $productsNames);
}
}

View file

@ -34,7 +34,7 @@ final class ShippingCalculatorContext implements Context
public function getShippingCalculatorByName(string $shippingCalculator): string
{
$flippedCalculators = array_flip(array_map(
function (string $translationKey): string { return $this->translator->trans($translationKey); },
fn(string $translationKey): string => $this->translator->trans($translationKey),
$this->shippingCalculators
));

View file

@ -233,9 +233,7 @@ final class ManagingCatalogPromotionsContext implements Context
*/
public function iAddScopeThatAppliesOnVariants(ProductVariantInterface ...$variants): void
{
$variantCodes = array_map(function (ProductVariantInterface $variant) {
return $variant->getCode();
}, $variants);
$variantCodes = array_map(fn(ProductVariantInterface $variant) => $variant->getCode(), $variants);
$this->formElement->addScope();
$this->formElement->chooseScopeType('For variants');
@ -247,9 +245,7 @@ final class ManagingCatalogPromotionsContext implements Context
*/
public function iAddScopeThatAppliesOnTaxons(TaxonInterface ...$taxons): void
{
$taxonsCodes = array_map(function (TaxonInterface $taxon) {
return $taxon->getCode();
}, $taxons);
$taxonsCodes = array_map(fn(TaxonInterface $taxon) => $taxon->getCode(), $taxons);
$this->formElement->addScope();
$this->formElement->chooseScopeType('For taxons');

View file

@ -25,8 +25,7 @@ use Webmozart\Assert\Assert;
final class ManagingCustomersContext implements Context
{
/** @var CustomerIndexPageInterface */
private $indexPage;
private CustomerIndexPageInterface $indexPage;
private CreatePageInterface $createPage;

View file

@ -18,8 +18,7 @@ use Sylius\Behat\Element\BrowserElementInterface;
final class BrowserContext implements Context
{
/** @var BrowserElementInterface */
private $browserElement;
private BrowserElementInterface $browserElement;
public function __construct(BrowserElementInterface $browserElement)
{

View file

@ -228,9 +228,7 @@ final class FormElement extends Element implements FormElementInterface
$this->getElement($buttonElement)->click();
$this->getDocument()->waitFor(5, function () use ($count, $collectionElement) {
return $count + 1 === count($this->getCollectionItems($collectionElement));
});
$this->getDocument()->waitFor(5, fn() => $count + 1 === count($this->getCollectionItems($collectionElement)));
}
/** @return NodeElement[] */

View file

@ -22,9 +22,7 @@ final class VerticalMenuElement extends Element implements VerticalMenuElementIn
{
$menu = $this->getElement('vertical-menu');
return array_map(function (NodeElement $element): string {
return $element->getText();
}, $menu->findAll('css', '[data-test-vertical-menu-item]'));
return array_map(fn(NodeElement $element): string => $element->getText(), $menu->findAll('css', '[data-test-vertical-menu-item]'));
}
public function canNavigateToParentTaxon(): bool

View file

@ -22,9 +22,7 @@ final class MenuElement extends Element implements MenuElementInterface
{
$menu = $this->getElement('menu');
return array_map(function (NodeElement $element): string {
return $element->getText();
}, $menu->findAll('css', '[data-test-menu-item]'));
return array_map(fn(NodeElement $element): string => $element->getText(), $menu->findAll('css', '[data-test-menu-item]'));
}
protected function getDefinedElements(): array

View file

@ -186,9 +186,7 @@ class ShowPage extends SymfonyPage implements ShowPageInterface
->findAll('css', '.row > .column > .statistic > .sylius-channel-name')
;
$statisticsRibs = array_filter($statisticsRibs, function (NodeElement $statistic) use ($channelName) {
return $channelName === trim($statistic->getText());
});
$statisticsRibs = array_filter($statisticsRibs, fn(NodeElement $statistic) => $channelName === trim($statistic->getText()));
$actualStatisticsCount = count($statisticsRibs);
Assert::same(

View file

@ -169,12 +169,10 @@ class CreateSimpleProductPage extends BaseCreatePage implements CreateSimpleProd
$dropdown->click();
foreach ($productsNames as $productName) {
$dropdown->waitFor(5, function () use ($productName, $productAssociationType) {
return $this->hasElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),
'%item%' => $productName,
]);
});
$dropdown->waitFor(5, fn() => $this->hasElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),
'%item%' => $productName,
]));
$item = $this->getElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),
@ -294,9 +292,7 @@ class CreateSimpleProductPage extends BaseCreatePage implements CreateSimpleProd
private function waitForFormElement(int $timeout = 5): void
{
$form = $this->getElement('form');
$this->getDocument()->waitFor($timeout, function () use ($form) {
return false === strpos($form->getAttribute('class'), 'loading');
});
$this->getDocument()->waitFor($timeout, fn() => false === strpos($form->getAttribute('class'), 'loading'));
}
private function clickTabIfItsNotActive(string $tabName): void

View file

@ -46,9 +46,7 @@ class IndexPerTaxonPage extends CrudIndexPage implements IndexPerTaxonPageInterf
$saveConfigurationButton = $this->getElement('save_configuration_button');
$saveConfigurationButton->press();
$this->getDocument()->waitFor(5, function () use ($saveConfigurationButton) {
return null === $saveConfigurationButton->find('css', '.loading');
});
$this->getDocument()->waitFor(5, fn() => null === $saveConfigurationButton->find('css', '.loading'));
}
protected function getDefinedElements(): array

View file

@ -57,9 +57,7 @@ class UpdateConfigurableProductPage extends BaseUpdatePage implements UpdateConf
$this->getDriver()->executeScript(sprintf('$(\'input.search\').val(\'%s\')', $taxon->getName()));
$this->getElement('search')->click();
$this->getElement('search')->waitFor(10, function () {
return $this->hasElement('search_item_selected');
});
$this->getElement('search')->waitFor(10, fn() => $this->hasElement('search_item_selected'));
$itemSelected = $this->getElement('search_item_selected');
$itemSelected->click();
}

View file

@ -62,9 +62,7 @@ class UpdateSimpleProductPage extends BaseUpdatePage implements UpdateSimpleProd
$form = $this->getDocument()->find('css', 'form');
$this->getDocument()->waitFor(1, function () use ($form) {
return $form->hasClass('loading');
});
$this->getDocument()->waitFor(1, fn() => $form->hasClass('loading'));
}
public function removeAttribute(string $attributeName, string $localeCode): void
@ -286,12 +284,10 @@ class UpdateSimpleProductPage extends BaseUpdatePage implements UpdateSimpleProd
$dropdown->click();
foreach ($productsNames as $productName) {
$dropdown->waitFor(5, function () use ($productName, $productAssociationType) {
return $this->hasElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),
'%item%' => $productName,
]);
});
$dropdown->waitFor(5, fn() => $this->hasElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),
'%item%' => $productName,
]));
$item = $this->getElement('association_dropdown_item', [
'%association%' => $productAssociationType->getName(),

View file

@ -50,9 +50,7 @@ class IndexPage extends BaseIndexPage implements IndexPageInterface
{
$this->getElement('save_configuration_button')->press();
$this->getDocument()->waitFor(5, function () {
return null === $this->getElement('save_configuration_button')->find('css', '.loading');
});
$this->getDocument()->waitFor(5, fn() => null === $this->getElement('save_configuration_button')->find('css', '.loading'));
}
protected function getDefinedElements(): array

View file

@ -32,9 +32,7 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
$this->getDocument()->clickLink('Add rule');
$this->getDocument()->waitFor(5, function () use ($count) {
return $count + 1 === count($this->getCollectionItems('rules'));
});
$this->getDocument()->waitFor(5, fn() => $count + 1 === count($this->getCollectionItems('rules')));
if (null !== $ruleName) {
$this->selectRuleOption('Type', $ruleName);
@ -82,9 +80,7 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
$this->getDocument()->clickLink('Add action');
$this->getDocument()->waitFor(5, function () use ($count) {
return $count + 1 === count($this->getCollectionItems('actions'));
});
$this->getDocument()->waitFor(5, fn() => $count + 1 === count($this->getCollectionItems('actions')));
if (null !== $actionName) {
$this->selectActionOption('Type', $actionName);

View file

@ -96,9 +96,7 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
$this->getDocument()->clickLink('Add rule');
$this->getDocument()->waitFor(5, function () use ($count) {
return $count + 1 === count($this->getCollectionItems('rules'));
});
$this->getDocument()->waitFor(5, fn() => $count + 1 === count($this->getCollectionItems('rules')));
$this->selectRuleOption('Type', $ruleName);
}

View file

@ -199,9 +199,7 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
$languageTabTitle->click();
}
$this->getDocument()->waitFor(10, function () use ($languageTabTitle) {
return $languageTabTitle->hasClass('active');
});
$this->getDocument()->waitFor(10, fn() => $languageTabTitle->hasClass('active'));
}
public function enable(): void

View file

@ -40,9 +40,7 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
public function chooseMember(string $name): void
{
$selectItems = $this->getDocument()->waitFor(2, function () {
return $this->getDocument()->findAll('css', 'div[data-form-type="collection"] select');
});
$selectItems = $this->getDocument()->waitFor(2, fn() => $this->getDocument()->findAll('css', 'div[data-form-type="collection"] select'));
$lastSelectItem = end($selectItems);
if (false === $lastSelectItem) {

View file

@ -94,8 +94,6 @@ class UpdatePage extends SymfonyPage implements UpdatePageInterface
private function waitForElement(int $timeout, string $elementName): void
{
$this->getDocument()->waitFor($timeout, function () use ($elementName) {
return $this->hasElement($elementName);
});
$this->getDocument()->waitFor($timeout, fn() => $this->hasElement($elementName));
}
}

View file

@ -224,9 +224,7 @@ class SummaryPage extends SymfonyPage implements SummaryPageInterface
public function waitForRedirect(int $timeout): void
{
$this->getDocument()->waitFor($timeout, function () {
return $this->isOpen();
});
$this->getDocument()->waitFor($timeout, fn() => $this->isOpen());
}
public function getPromotionCouponValidationMessage(): string

View file

@ -169,9 +169,7 @@ class AddressPage extends SymfonyPage implements AddressPageInterface
public function specifyPassword(string $password): void
{
$this->getDocument()->waitFor(5, function () {
return $this->getElement('login_password')->isVisible();
});
$this->getDocument()->waitFor(5, fn() => $this->getElement('login_password')->isVisible());
$this->getElement('login_password')->setValue($password);
}
@ -233,9 +231,7 @@ class AddressPage extends SymfonyPage implements AddressPageInterface
$addressBookSelect = $this->getElement('shipping_address_book');
$addressBookSelect->click();
$addressOption = $addressBookSelect->waitFor(5, function () use ($address, $addressBookSelect) {
return $addressBookSelect->find('css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId()));
});
$addressOption = $addressBookSelect->waitFor(5, fn() => $addressBookSelect->find('css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId())));
if (null === $addressOption) {
throw new ElementNotFoundException($this->getDriver(), 'option', 'css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId()));
@ -250,9 +246,7 @@ class AddressPage extends SymfonyPage implements AddressPageInterface
$addressBookSelect = $this->getElement('billing_address_book');
$addressBookSelect->click();
$addressOption = $addressBookSelect->waitFor(5, function () use ($address, $addressBookSelect) {
return $addressBookSelect->find('css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId()));
});
$addressOption = $addressBookSelect->waitFor(5, fn() => $addressBookSelect->find('css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId())));
if (null === $addressOption) {
throw new ElementNotFoundException($this->getDriver(), 'option', 'css', sprintf('[data-test-address-book-item][data-id="%s"]', $address->getId()));
@ -325,7 +319,7 @@ class AddressPage extends SymfonyPage implements AddressPageInterface
{
return array_map(
/** @return string[] */
static function (NodeElement $element): string { return $element->getText(); },
static fn(NodeElement $element): string => $element->getText(),
$element->findAll('css', 'option[value!=""]')
);
}
@ -389,16 +383,12 @@ class AddressPage extends SymfonyPage implements AddressPageInterface
private function waitForLoginAction(): bool
{
return $this->getDocument()->waitFor(5, function () {
return !$this->hasElement('login_password');
});
return $this->getDocument()->waitFor(5, fn() => !$this->hasElement('login_password'));
}
private function waitForElement(int $timeout, string $elementName): bool
{
return $this->getDocument()->waitFor($timeout, function () use ($elementName) {
return $this->hasElement($elementName);
});
return $this->getDocument()->waitFor($timeout, fn() => $this->hasElement($elementName));
}
private function assertAddressType(string $type): void

View file

@ -56,9 +56,7 @@ class HomePage extends SymfonyPage implements HomePageInterface
public function getAvailableCurrencies(): array
{
return array_map(
function (NodeElement $element) {
return $element->getText();
},
fn(NodeElement $element) => $element->getText(),
$this->getElement('currency_selector')->findAll('css', '[data-test-available-currency]')
);
}
@ -81,9 +79,7 @@ class HomePage extends SymfonyPage implements HomePageInterface
public function getAvailableLocales(): array
{
return array_map(
function (NodeElement $element) {
return $element->getText();
},
fn(NodeElement $element) => $element->getText(),
$this->getElement('locale_selector')->findAll('css', '[data-test-available-locale]')
);
}
@ -96,9 +92,7 @@ class HomePage extends SymfonyPage implements HomePageInterface
public function getLatestProductsNames(): array
{
return array_map(
function (NodeElement $element) {
return $element->getText();
},
fn(NodeElement $element) => $element->getText(),
$this->getElement('latest_products')->findAll('css', '[data-test-product-name]')
);
}

View file

@ -151,9 +151,7 @@ class ShowPage extends SymfonyPage implements ShowPageInterface
$productPriceContent = $this->getElement('product_price_content');
$catalogPromotions = $productPriceContent->findAll('css', '.promotion_label');
return array_map(function (NodeElement $element): string {
return $element->getText();
}, $catalogPromotions);
return array_map(fn(NodeElement $element): string => $element->getText(), $catalogPromotions);
}
public function getCurrentUrl(): string
@ -341,9 +339,7 @@ class ShowPage extends SymfonyPage implements ShowPageInterface
$optionElement = $this->getElement('option_select', ['%optionCode%' => strtoupper($optionCode)]);
return array_map(
function (NodeElement $element) {
return $element->getText();
},
fn(NodeElement $element) => $element->getText(),
$optionElement->findAll('css', 'option')
);
}
@ -387,9 +383,7 @@ class ShowPage extends SymfonyPage implements ShowPageInterface
{
if ($this->getDriver() instanceof Selenium2Driver || $this->getDriver() instanceof ChromeDriver) {
JQueryHelper::waitForAsynchronousActionsToFinish($this->getSession());
$this->getDocument()->waitFor(3, function (): bool {
return $this->summaryPage->isOpen();
});
$this->getDocument()->waitFor(3, fn(): bool => $this->summaryPage->isOpen());
}
}
}

View file

@ -61,7 +61,7 @@ final class JavaScriptTestHelper implements JavaScriptTestHelperInterface
private function waitUntilExceptionDisappears(callable $callable, string $exceptionClass, ?int $timeout = null): void
{
$start = microtime(true);
$timeout = $timeout ?? $this->defaultTimeout;
$timeout ??= $this->defaultTimeout;
$end = $start + $timeout;
do {

View file

@ -26,8 +26,6 @@ abstract class JQueryHelper
public static function waitForFormToStopLoading(DocumentElement $document, int $timeout = 10): void
{
$form = $document->find('css', 'form');
$document->waitFor($timeout, function () use ($form) {
return !$form->hasClass('loading');
});
$document->waitFor($timeout, fn() => !$form->hasClass('loading'));
}
}

View file

@ -23,8 +23,7 @@ final class CookieSetter implements CookieSetterInterface
{
private Session $minkSession;
/** @var array */
private $minkParameters;
private array $minkParameters;
public function __construct(Session $minkSession, $minkParameters)
{