Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Feature: Creating a catalog promotion
And it should have "winter_sale" code and "Winter sale" name
And this catalog promotion should be usable

@api
@api @ui @javascript
Scenario: Creating a catalog promotion
Given the store has a "T-Shirt" configurable product
And this product has "PHP T-Shirt" variant priced at "$20.00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\CatalogPromotionRuleInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionRuleInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -194,8 +194,6 @@ public function iBrowseCatalogPromotions(): void
}

/**
* @When I add the "contains variants" rule configured with :firstVariant and :secondVariant
* @When /^I add the "contains variants" rule configured with ("[^"]+" variant) and ("[^"]+" variant)$/
* @When /^it applies on variants ("[^"]+" variant) and ("[^"]+" variant)$/
*/
public function iAddTheRuleConfiguredWithProductAnd(ProductVariantInterface $firstVariant, ProductVariantInterface $secondVariant): void
Expand Down
5 changes: 2 additions & 3 deletions src/Sylius/Behat/Context/Setup/CatalogPromotionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\CatalogPromotionRuleInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionRule;
use Sylius\Component\Promotion\Model\CatalogPromotionRuleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class CatalogPromotionContext implements Context
Expand Down Expand Up @@ -92,7 +91,7 @@ public function itWillBeAppliedOnVariant(CatalogPromotionInterface $catalogPromo
{
/** @var CatalogPromotionRuleInterface $catalogPromotionRule */
$catalogPromotionRule = $this->catalogPromotionRuleFactory->createNew();
$catalogPromotionRule->setType(CatalogPromotionRule::TYPE_FOR_VARIANTS);
$catalogPromotionRule->setType(CatalogPromotionRuleInterface::TYPE_FOR_VARIANTS);
$catalogPromotionRule->setConfiguration([$variant->getCode()]);

$catalogPromotion->addRule($catalogPromotionRule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Sylius\Behat\Page\Admin\CatalogPromotion\CreatePageInterface;
use Sylius\Behat\Page\Admin\CatalogPromotion\UpdatePageInterface;
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webmozart\Assert\Assert;

final class ManagingCatalogPromotionsContext implements Context
Expand All @@ -31,16 +33,20 @@ final class ManagingCatalogPromotionsContext implements Context

private FormElementInterface $formElement;

private SharedStorageInterface $sharedStorage;

public function __construct(
IndexPageInterface $indexPage,
CreatePageInterface $createPage,
UpdatePageInterface $updatePage,
FormElementInterface $formElement
FormElementInterface $formElement,
SharedStorageInterface $sharedStorage
) {
$this->indexPage = $indexPage;
$this->createPage = $createPage;
$this->updatePage = $updatePage;
$this->formElement = $formElement;
$this->sharedStorage = $sharedStorage;
}

/**
Expand Down Expand Up @@ -127,6 +133,28 @@ public function iMakeItUnavailableInChannel(string $channelName): void
$this->formElement->uncheckChannel($channelName);
}

/**
* @When /^it applies on variants ("[^"]+" variant) and ("[^"]+" variant)$/
*/
public function itAppliesOnVariants(...$variants): void
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function itAppliesOnVariants(...$variants): void
public function itAppliesOnVariants(ProductVariantInterface ...$variants): void

{
$variantCodes = array_map(function(ProductVariantInterface $variant) {
return $variant->getCode();
}, $variants);

$this->formElement->addRule();
$this->formElement->chooseLastRuleVariants($variantCodes);
}

/**
* @When /^it gives the "([^"]+)%" percentage discount$/
*/
public function catalogPromotionGivesDiscount(string $discount): void
{
$this->formElement->addAction();
$this->formElement->specifyLastActionDiscount($discount);
}

/**
* @When I add it
*/
Expand Down Expand Up @@ -194,6 +222,28 @@ public function itShouldHaveCodeAndName(string $code, string $name): void
$this->indexPage->isSingleResourceOnPage(['name' => $name, 'code' => $code]),
sprintf('Cannot find catalog promotions with code "%s" and name "%s" in the list', $code, $name)
);

$actions = $this->indexPage->getActionsForResource(['name' => $name]);
$actions->clickLink('Edit');
}

/**
* @Then /^it should apply to ("[^"]+" variant) and ("[^"]+" variant)$/
*/
public function itShouldHaveRule(ProductVariantInterface $firstVariant, ProductVariantInterface $secondVariant): void
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function itShouldHaveRule(ProductVariantInterface $firstVariant, ProductVariantInterface $secondVariant): void
public function itShouldApplyToVariants(ProductVariantInterface ...$variants): void

and of course adjust the content of this method

{
$selectedVariants = $this->formElement->getLastRuleVariantCodes();

Assert::inArray($firstVariant->getCode(), $selectedVariants);
Assert::inArray($secondVariant->getCode(), $selectedVariants);
}

/**
* @Then /^it should have "([^"]+)%" discount$/
*/
public function itShouldHaveDiscount(string $amount): void
{
Assert::same($this->formElement->getLastActionDiscount(), $amount);
}

/**
Expand Down
67 changes: 67 additions & 0 deletions src/Sylius/Behat/Element/Admin/CatalogPromotion/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Sylius\Behat\Element\Admin\CatalogPromotion;

use Behat\Mink\Element\NodeElement;
use FriendsOfBehat\PageObjectExtension\Element\Element;
use Webmozart\Assert\Assert;

final class FormElement extends Element implements FormElementInterface
{
Expand Down Expand Up @@ -42,18 +44,83 @@ public function uncheckChannel(string $channelName): void
$this->getDocument()->uncheckField($channelName);
}

public function addRule(): void
{
$this->addCollectionElement('rules', 'add_rule_button');
}

public function addAction(): void
{
$this->addCollectionElement('actions', 'add_action_button');
}

public function chooseLastRuleVariants(array $variantCodes): void
{
$lastRule = $this->getElement('last_rule');

$lastRule->find('css', 'input[type="hidden"]')->setValue(implode(',', $variantCodes));
}

public function specifyLastActionDiscount(string $discount): void
{
$lastAction = $this->getElement('last_action');

$lastAction->find('css', 'input')->setValue($discount);
}

public function getFieldValueInLocale(string $field, string $localeCode): string
{
return $this->getElement($field, ['%localeCode%' => $localeCode])->getValue();
}

public function getLastRuleVariantCodes(): array
{
$lastRule = $this->getElement('last_rule');

return explode(',', $lastRule->find('css', 'input[type="hidden"]')->getValue());
}

public function getLastActionDiscount(): string
{
$lastAction = $this->getElement('last_action');

return $lastAction->find('css', 'input')->getValue();
}

protected function getDefinedElements(): array
{
return array_merge(parent::getDefinedElements(), [
'actions' => '#actions',
'add_action_button' => '#actions [data-form-collection="add"]',
'add_rule_button' => '#rules [data-form-collection="add"]',
'channel' => '#sylius_catalog_promotion_code',
'description' => '#sylius_catalog_promotion_translations_%localeCode%_description',
'label' => '#sylius_catalog_promotion_translations_%localeCode%_label',
'last_action' => '#actions [data-form-collection="item"]:last-child',
'last_rule' => '#rules [data-form-collection="item"]:last-child',
'name' => '#sylius_catalog_promotion_name',
'rules' => '#rules',
]);
}

private function addCollectionElement(string $collectionElement, string $buttonElement): void
{
$count = count($this->getCollectionItems($collectionElement));

$this->getElement($buttonElement)->click();

$this->getDocument()->waitFor(5, function () use ($count, $collectionElement) {
return $count + 1 === count($this->getCollectionItems($collectionElement));
});
}

/** @return NodeElement[] */
private function getCollectionItems(string $collection): array
{
$items = $this->getElement($collection)->findAll('css', 'div[data-form-collection="item"]');

Assert::isArray($items);

return $items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@ public function checkChannel(string $channelName): void;

public function uncheckChannel(string $channelName): void;

public function addRule(): void;

public function addAction(): void;

public function chooseLastRuleVariants(array $variantCodes): void;

public function specifyLastActionDiscount(string $discount): void;

public function getFieldValueInLocale(string $field, string $localeCode): string;

public function getLastRuleVariantCodes(): array;

public function getLastActionDiscount(): string;
}
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 @@ -50,6 +50,7 @@
<argument type="service" id="sylius.behat.page.admin.catalog_promotion.create" />
<argument type="service" id="sylius.behat.page.admin.catalog_promotion.update" />
<argument type="service" id="Sylius\Behat\Element\Admin\CatalogPromotion\FormElement" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>

<service id="sylius.behat.context.ui.admin.managing_channels" class="Sylius\Behat\Context\Ui\Admin\ManagingChannelsContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ sylius_admin_ajax_product_variants_by_phrase:
locale: expr:service('sylius.context.locale').getLocaleCode()
productCode: $productCode

sylius_admin_ajax_all_product_variants_by_phrase:
path: /search-all
methods: [GET]
defaults:
_controller: sylius.controller.product_variant:indexAction
_format: json
_sylius:
serialization_groups: [Autocomplete]
permission: true
repository:
method: findByPhrase
arguments:
phrase: $phrase
locale: expr:service('sylius.context.locale').getLocaleCode()

sylius_admin_ajax_product_variants_by_codes:
path: /
methods: [GET]
Expand All @@ -35,3 +50,16 @@ sylius_admin_ajax_product_variants_by_codes:
repository:
method: findByCodesAndProductCode
arguments: [$code, $productCode]

sylius_admin_ajax_all_product_variants_by_codes:
path: /all
methods: [GET]
defaults:
_controller: sylius.controller.product_variant:indexAction
_format: json
_sylius:
serialization_groups: [Autocomplete]
permission: true
repository:
method: findByCodes
arguments: [$code]
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
{% from '@SyliusAdmin/Macro/translationForm.html.twig' import translationForm %}

<div class="ui segment">
{% form_theme form '@SyliusAdmin/CatalogPromotion/theme.html.twig' %}

<div class="ui two column stackable grid">
{{ form_errors(form) }}
<div class="three fields">
{{ form_row(form.code) }}
{{ form_row(form.name) }}
{{ form_row(form.channels) }}
<div class="column">
<div class="ui segment">
{{ form_row(form.code) }}
{{ form_row(form.name) }}
{{ form_row(form.channels) }}
</div>
<div class="ui segment" id="rules">
{{ form_row(form.rules) }}
</div>
<div class="ui segment" id="actions">
{{ form_row(form.actions) }}
</div>
</div>
<div class="column">
{{ translationForm(form.translations) }}
</div>
{{ translationForm(form.translations) }}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends '@SyliusAdmin/Form/theme.html.twig' %}

{% block sylius_catalog_promotion_rule_widget %}
{{ form_row(form.type) }}
{{ form_row(form.configuration, {'remote_url': path('sylius_admin_ajax_all_product_variants_by_phrase'), 'remote_criteria_type': 'contains', 'remote_criteria_name': 'phrase', 'load_edit_url': path('sylius_admin_ajax_all_product_variants_by_codes')}) }}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Bundle\CoreBundle\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Webmozart\Assert\Assert;

final class ProductVariantsToCodesTransformer implements DataTransformerInterface
{
private ProductVariantRepositoryInterface $productVariantRepository;

public function __construct(ProductVariantRepositoryInterface $productVariantRepository)
{
$this->productVariantRepository = $productVariantRepository;
}

/** @throws \InvalidArgumentException */
public function transform($value): Collection
{
Assert::nullOrIsArray($value);

if (empty($value)) {
return new ArrayCollection();
}

return new ArrayCollection($this->productVariantRepository->findBy(['code' => $value]));
}

/** @throws \InvalidArgumentException */
public function reverseTransform($productVariants): array
{
Assert::isInstanceOf($productVariants, Collection::class);

return array_map(function (ProductVariantInterface $productVariant) {
return $productVariant->getCode();
}, $productVariants->toArray());
}
}
Loading