From 31ebf1bb68bc1821701d39c2f4d0aae6f498869c Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Wed, 8 May 2019 08:43:57 +0200 Subject: [PATCH 1/8] [Admin][Promotion] Add scenarios for accessing a promotion with a rule that contains a removed taxon --- ...h_rule_that_contains_removed_taxon.feature | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature diff --git a/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature new file mode 100644 index 00000000000..2eb3c0a5e3e --- /dev/null +++ b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature @@ -0,0 +1,24 @@ +@managing_promotions +Feature: Accessing an edit page of a promotion with a rule that contains a removed taxon + In order to change promotion details + As an Administrator + I want to be able to access an edit page of a promotion with a rule that contains a removed taxon + + Background: + Given the store operates on a single channel in "United States" + And the store classifies its products as "T-Shirts" and "Mugs" + And I am logged in as an administrator + + @ui @todo + Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon + Given there is a promotion "Christmas sale" with "Has at least one from taxons" rule configured with "T-Shirts" and "Mugs" + When I remove taxon named "Mugs" + Then I should be able to modify a "Christmas sale" promotion + And the "Christmas sale" promotion should have "Has at least one from taxons" rule configured + + @ui @todo + Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon + Given there is a promotion "Christmas sale" with "Total price of items from taxon" rule configured with "Mugs" taxon and $100 amount for "United States" channel + When I remove taxon named "Mugs" + Then I should be able to modify a "Christmas sale" promotion + And the "Christmas sale" promotion should not have any rule configured From 42b957e77f89e63f6fd63d558156f1348e1d3d1b Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Wed, 8 May 2019 14:59:53 +0200 Subject: [PATCH 2/8] [Admin][Promotion] Remove deleted taxon from existing promotion rules --- ...h_rule_that_contains_removed_taxon.feature | 4 +- .../Behat/Context/Setup/PromotionContext.php | 55 ++++++++-- .../Behat/Context/Transform/TaxonContext.php | 7 +- .../Ui/Admin/ManagingPromotionsContext.php | 21 ++++ .../Ui/Admin/ManagingTaxonsContext.php | 9 -- .../Context/Ui/Admin/RemovingTaxonContext.php | 38 +++++++ .../Behat/Page/Admin/Promotion/UpdatePage.php | 24 +++++ .../Admin/Promotion/UpdatePageInterface.php | 4 + .../Resources/config/services/contexts/ui.xml | 5 + .../ui/promotion/managing_promotions.yml | 2 + .../suites/ui/taxonomy/managing_taxons.yml | 1 + .../EventListener/TaxonDeletionListener.php | 91 ++++++++++++++++ .../Resources/config/services/listeners.xml | 6 ++ .../TaxonDeletionListenerSpec.php | 100 ++++++++++++++++++ 14 files changed, 342 insertions(+), 25 deletions(-) create mode 100644 src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php create mode 100644 src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php create mode 100644 src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php diff --git a/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature index 2eb3c0a5e3e..9c6a03056f9 100644 --- a/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature +++ b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature @@ -9,14 +9,14 @@ Feature: Accessing an edit page of a promotion with a rule that contains a remov And the store classifies its products as "T-Shirts" and "Mugs" And I am logged in as an administrator - @ui @todo + @ui Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon Given there is a promotion "Christmas sale" with "Has at least one from taxons" rule configured with "T-Shirts" and "Mugs" When I remove taxon named "Mugs" Then I should be able to modify a "Christmas sale" promotion And the "Christmas sale" promotion should have "Has at least one from taxons" rule configured - @ui @todo + @ui Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon Given there is a promotion "Christmas sale" with "Total price of items from taxon" rule configured with "Mugs" taxon and $100 amount for "United States" channel When I remove taxon named "Mugs" diff --git a/src/Sylius/Behat/Context/Setup/PromotionContext.php b/src/Sylius/Behat/Context/Setup/PromotionContext.php index 9fc1e46cc36..34955eb5ebb 100644 --- a/src/Sylius/Behat/Context/Setup/PromotionContext.php +++ b/src/Sylius/Behat/Context/Setup/PromotionContext.php @@ -73,21 +73,40 @@ public function __construct( } /** - * @Given there is (also) a promotion :promotionName - * @Given there is a promotion :promotionName identified by :promotionCode code + * @Given there is (also) a promotion :name + * @Given there is a promotion :name identified by :code code */ - public function thereIsPromotion(string $promotionName, ?string $promotionCode = null): void + public function thereIsPromotion(string $name, ?string $code = null): void { - $promotion = $this->testPromotionFactory - ->createForChannel($promotionName, $this->sharedStorage->get('channel')) - ; + $this->createPromotion($name, $code); + } - if (null !== $promotionCode) { - $promotion->setCode($promotionCode); - } + /** + * @Given /^there is a promotion "([^"]+)" with "Has at least one from taxons" rule (configured with "[^"]+" and "[^"]+")$/ + */ + public function thereIsAPromotionWithHasAtLeastOneFromTaxonsRuleConfiguredWith(string $name, array $taxons): void + { + $promotion = $this->createPromotion($name); + $rule = $this->ruleFactory->createHasTaxon([$taxons[0]->getCode(), $taxons[1]->getCode()]); + $promotion->addRule($rule); - $this->promotionRepository->add($promotion); - $this->sharedStorage->set('promotion', $promotion); + $this->objectManager->flush(); + } + + /** + * @Given /^there is a promotion "([^"]+)" with "Total price of items from taxon" rule configured with ("[^"]+" taxon) and (?:€|£|\$)([^"]+) amount for ("[^"]+" channel)$/ + */ + public function thereIsAPromotionWithTotalPriceOfItemsFromTaxonRuleConfiguredWithTaxonAndAmountForChannel( + string $name, + TaxonInterface $taxon, + int $amount, + ChannelInterface $channel + ): void { + $promotion = $this->createPromotion($name); + $rule = $this->ruleFactory->createItemsFromTaxonTotal($channel->getCode(), $taxon->getCode(), $amount); + $promotion->addRule($rule); + + $this->objectManager->flush(); } /** @@ -730,6 +749,20 @@ private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) return $configuration; } + private function createPromotion(string $name, ?string $code = null): PromotionInterface + { + $promotion = $this->testPromotionFactory->createForChannel($name, $this->sharedStorage->get('channel')); + + if (null !== $code) { + $promotion->setCode($code); + } + + $this->promotionRepository->add($promotion); + $this->sharedStorage->set('promotion', $promotion); + + return $promotion; + } + /** * @param int $discount */ diff --git a/src/Sylius/Behat/Context/Transform/TaxonContext.php b/src/Sylius/Behat/Context/Transform/TaxonContext.php index 270c81445bb..6ed70e1c4e4 100644 --- a/src/Sylius/Behat/Context/Transform/TaxonContext.php +++ b/src/Sylius/Behat/Context/Transform/TaxonContext.php @@ -64,12 +64,13 @@ public function getTaxonByCode($code) /** * @Transform /^classified as "([^"]+)" or "([^"]+)"$/ + * @Transform /^configured with "([^"]+)" and "([^"]+)"$/ */ - public function getTaxonsByNames($firstTaxon, $secondTaxon) + public function getTaxonsByNames(string $firstTaxonName, string $secondTaxonName): array { return [ - $this->getTaxonByName($firstTaxon), - $this->getTaxonByName($secondTaxon), + $this->getTaxonByName($firstTaxonName), + $this->getTaxonByName($secondTaxonName), ]; } } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php index f69e163a8a6..82691a66aa0 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php @@ -617,6 +617,27 @@ public function thePromotionsShouldHavePriority(PromotionInterface $promotion, i Assert::same($this->updatePage->getPriority(), $priority); } + /** + * @Then the :promotion promotion should have :ruleName rule configured + */ + public function thePromotionShouldHaveRuleConfigured(PromotionInterface $promotion, string $ruleName): void + { + $this->iWantToModifyAPromotion($promotion); + + Assert::true($this->updatePage->hasRule($ruleName)); + } + + /** + * @Then the :promotion promotion should not have any rule configured + */ + public function thePromotionShouldNotHaveAnyRuleConfigured(PromotionInterface $promotion): void + { + $this->iWantToModifyAPromotion($promotion); + + Assert::false($this->updatePage->hasAnyRule()); + } + + /** * @param string $element * @param string $expectedMessage diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php index e384fe1baf9..03c79597488 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php @@ -156,15 +156,6 @@ public function iChangeItsParentTaxonTo(TaxonInterface $taxon) $this->updatePage->chooseParent($taxon); } - /** - * @When I delete taxon named :name - */ - public function iDeleteTaxonNamed($name) - { - $this->createPage->open(); - $this->createPage->deleteTaxonOnPageByName($name); - } - /** * @When I add it * @When I try to add it diff --git a/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php b/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php new file mode 100644 index 00000000000..af434fa277f --- /dev/null +++ b/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php @@ -0,0 +1,38 @@ +createPage = $createPage; + } + + /** + * @When I remove taxon named :name + * @When I delete taxon named :name + */ + public function iRemoveTaxonNamed(string $name): void + { + $this->createPage->open(); + $this->createPage->deleteTaxonOnPageByName($name); + } +} diff --git a/src/Sylius/Behat/Page/Admin/Promotion/UpdatePage.php b/src/Sylius/Behat/Page/Admin/Promotion/UpdatePage.php index e72645642ee..63dde8f6157 100644 --- a/src/Sylius/Behat/Page/Admin/Promotion/UpdatePage.php +++ b/src/Sylius/Behat/Page/Admin/Promotion/UpdatePage.php @@ -92,6 +92,29 @@ public function hasEndsAt(\DateTimeInterface $dateTime): bool && $this->getElement('ends_at_time')->getValue() === date('H:i', $timestamp); } + public function hasAnyRule(): bool + { + $items = $this->getElement('rules')->findAll('css', 'div[data-form-collection="item"]'); + + return 0 < count($items); + } + + public function hasRule(string $name): bool + { + $items = $this->getElement('rules')->findAll('css', 'div[data-form-collection="item"]'); + + foreach ($items as $item) { + $selectedOption = $item->find('css', 'option[selected="selected"]'); + + /** @var NodeElement $selectedOption */ + if ($selectedOption->getText() === $name) { + return true; + } + } + + return false; + } + protected function getCodeElement(): NodeElement { return $this->getElement('code'); @@ -108,6 +131,7 @@ protected function getDefinedElements(): array 'ends_at_time' => '#sylius_promotion_endsAt_time', 'exclusive' => '#sylius_promotion_exclusive', 'name' => '#sylius_promotion_name', + 'rules' => '#rules', 'starts_at' => '#sylius_promotion_startsAt', 'starts_at_date' => '#sylius_promotion_startsAt_date', 'starts_at_time' => '#sylius_promotion_startsAt_time', diff --git a/src/Sylius/Behat/Page/Admin/Promotion/UpdatePageInterface.php b/src/Sylius/Behat/Page/Admin/Promotion/UpdatePageInterface.php index 5a77f5311e6..47655ea85a7 100644 --- a/src/Sylius/Behat/Page/Admin/Promotion/UpdatePageInterface.php +++ b/src/Sylius/Behat/Page/Admin/Promotion/UpdatePageInterface.php @@ -42,4 +42,8 @@ public function setEndsAt(\DateTimeInterface $dateTime): void; public function hasStartsAt(\DateTimeInterface $dateTime): bool; public function hasEndsAt(\DateTimeInterface $dateTime): bool; + + public function hasAnyRule(): bool; + + public function hasRule(string $name): bool; } diff --git a/src/Sylius/Behat/Resources/config/services/contexts/ui.xml b/src/Sylius/Behat/Resources/config/services/contexts/ui.xml index 1d7a2aaf237..5b7d2606191 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/ui.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/ui.xml @@ -274,6 +274,11 @@ + + + + + diff --git a/src/Sylius/Behat/Resources/config/suites/ui/promotion/managing_promotions.yml b/src/Sylius/Behat/Resources/config/suites/ui/promotion/managing_promotions.yml index b20716df6e4..2e3d76cebbb 100644 --- a/src/Sylius/Behat/Resources/config/suites/ui/promotion/managing_promotions.yml +++ b/src/Sylius/Behat/Resources/config/suites/ui/promotion/managing_promotions.yml @@ -8,6 +8,7 @@ default: - sylius.behat.context.hook.doctrine_orm - sylius.behat.context.transform.address + - sylius.behat.context.transform.channel - sylius.behat.context.transform.customer - sylius.behat.context.transform.date_time - sylius.behat.context.transform.lexical @@ -31,6 +32,7 @@ default: - sylius.behat.context.setup.taxonomy - sylius.behat.context.ui.admin.managing_promotions + - sylius.behat.context.ui.admin.removing_taxons - sylius.behat.context.ui.admin.notification filters: tags: "@managing_promotions && @ui" diff --git a/src/Sylius/Behat/Resources/config/suites/ui/taxonomy/managing_taxons.yml b/src/Sylius/Behat/Resources/config/suites/ui/taxonomy/managing_taxons.yml index 0c21d11e27c..79eecd665f5 100644 --- a/src/Sylius/Behat/Resources/config/suites/ui/taxonomy/managing_taxons.yml +++ b/src/Sylius/Behat/Resources/config/suites/ui/taxonomy/managing_taxons.yml @@ -16,6 +16,7 @@ default: - sylius.behat.context.setup.taxonomy - sylius.behat.context.ui.admin.managing_taxons + - sylius.behat.context.ui.admin.removing_taxons - sylius.behat.context.ui.admin.notification filters: tags: "@managing_taxons && @ui" diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php new file mode 100644 index 00000000000..cfe62a78835 --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -0,0 +1,91 @@ +promotionRuleRepository = $promotionRuleRepository; + $this->manager = $manager; + } + + public function removeTaxonFromPromotionRules(GenericEvent $event): void + { + $taxon = $event->getSubject(); + Assert::isInstanceOf($taxon, TaxonInterface::class); + + $this->resolveHasTaxonRules($taxon->getCode()); + $this->resolveTotalOfItemsFromTaxonRules($taxon->getCode()); + + $this->manager->flush(); + } + + private function resolveHasTaxonRules(string $taxonCode): void + { + $promotionRules = $this->promotionRuleRepository->findBy(['type' => HasTaxonRuleChecker::TYPE]); + + /** @var PromotionRuleInterface $promotionRule */ + foreach ($promotionRules as $promotionRule) { + $configuration = $promotionRule->getConfiguration(); + if (in_array($taxonCode, $configuration['taxons'])) { + $taxons = array_filter( + $configuration['taxons'], + function (string $code) use ($taxonCode) { + return $taxonCode !== $code; + } + ); + + $configuration['taxons'] = array_values($taxons); + $promotionRule->setConfiguration($configuration); + } + } + } + + private function resolveTotalOfItemsFromTaxonRules(string $taxonCode): void + { + $promotionRules = $this->promotionRuleRepository->findBy(['type' => TotalOfItemsFromTaxonRuleChecker::TYPE]); + + /** @var PromotionRuleInterface $promotionRule */ + foreach ($promotionRules as $promotionRule) { + $this->removePromotionRuleIfNecessary($promotionRule, $taxonCode); + } + } + + private function removePromotionRuleIfNecessary(PromotionRuleInterface $promotionRule, string $taxonCode): void + { + foreach ($promotionRule->getConfiguration() as $configuration) { + if ($taxonCode === $configuration['taxon']) { + $this->promotionRuleRepository->remove($promotionRule); + + return; + } + } + } +} diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index 60a8eccd449..2a2b886d33a 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -86,5 +86,11 @@ + + + + + + diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php new file mode 100644 index 00000000000..0c0f6bcc81e --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -0,0 +1,100 @@ +beConstructedWith($promotionRuleRepository, $manager); + } + + function it_removes_rules_that_using_deleted_taxon( + RepositoryInterface $promotionRuleRepository, + EntityManagerInterface $manager, + GenericEvent $event, + TaxonInterface $taxon, + PromotionRuleInterface $firstPromotionRule, + PromotionRuleInterface $secondPromotionRule + ): void { + $event->getSubject()->willReturn($taxon); + $taxon->getCode()->willReturn('toys'); + + $promotionRuleRepository + ->findBy(['type' => 'has_taxon']) + ->willReturn([$firstPromotionRule, $secondPromotionRule]) + ; + $firstPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs', 'toys']]); + $secondPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs']]); + + $firstPromotionRule->setConfiguration(['taxons' => ['mugs']])->shouldBeCalled(); + $secondPromotionRule->setConfiguration(Argument::any())->shouldNotBeCalled(); + + $promotionRuleRepository->findBy(['type' => 'total_of_items_from_taxon'])->willReturn([]); + + $manager->flush()->shouldBeCalled(); + + $this->removeTaxonFromPromotionRules($event); + } + + function it_removes_deleted_taxon_from_rules_configurations( + RepositoryInterface $promotionRuleRepository, + EntityManagerInterface $manager, + GenericEvent $event, + TaxonInterface $taxon, + PromotionRuleInterface $firstPromotionRule, + PromotionRuleInterface $secondPromotionRule + ): void { + $event->getSubject()->willReturn($taxon); + $taxon->getCode()->willReturn('toys'); + + $promotionRuleRepository->findBy(['type' => 'has_taxon'])->willReturn([]); + + $promotionRuleRepository + ->findBy(['type' => 'total_of_items_from_taxon']) + ->willReturn([$firstPromotionRule, $secondPromotionRule]) + ; + $firstPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'mugs', 'amount' => 500]]); + $secondPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'toys', 'amount' => 300]]); + + $promotionRuleRepository->remove($firstPromotionRule)->shouldNotBeCalled(); + $promotionRuleRepository->remove($secondPromotionRule)->shouldBeCalled(); + + $manager->flush()->shouldBeCalled(); + + $this->removeTaxonFromPromotionRules($event); + } + + function it_does_nothing_if_there_is_no_rule_with_given_types( + RepositoryInterface $promotionRuleRepository, + GenericEvent $event, + TaxonInterface $taxon + ): void { + $event->getSubject()->willReturn($taxon); + $taxon->getCode()->willReturn('toys'); + + $promotionRuleRepository->findBy(['type' => 'has_taxon'])->willReturn([]); + $promotionRuleRepository->findBy(['type' => 'total_of_items_from_taxon'])->willReturn([]); + + $this->removeTaxonFromPromotionRules($event); + } +} From 40835021b7d17508fb03dbac6c968746186b2830 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 9 May 2019 09:26:54 +0200 Subject: [PATCH 3/8] [Promotion] Fixes after PR review --- .../Ui/Admin/ManagingPromotionsContext.php | 21 ++++++++++++++----- .../EventListener/TaxonDeletionListener.php | 9 +------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php index 82691a66aa0..a2a20a23582 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php @@ -213,10 +213,13 @@ public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonN } /** - * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+" channel)$/ + * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+") channel$/ */ - public function iAddTheActionConfiguredWithAPercentageValueForChannel($actionType, $percentage = null, $channelName) - { + public function iAddTheActionConfiguredWithAPercentageValueForChannel( + string $actionType, + string $percentage = null, + string $channelName + ): void { $this->createPage->addAction($actionType); $this->createPage->fillActionOptionForChannel($channelName, 'Percentage', $percentage); } @@ -408,7 +411,6 @@ public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $ /** * @Given I want to modify a :promotion promotion * @Given /^I want to modify (this promotion)$/ - * @Then I should be able to modify a :promotion promotion */ public function iWantToModifyAPromotion(PromotionInterface $promotion) { @@ -617,12 +619,22 @@ public function thePromotionsShouldHavePriority(PromotionInterface $promotion, i Assert::same($this->updatePage->getPriority(), $priority); } + /** + * @Then I should be able to modify a :promotion promotion + */ + public function iShouldBeAbleToModifyAPromotion(PromotionInterface $promotion): void + { + $this->iWantToModifyAPromotion($promotion); + $this->updatePage->saveChanges(); + } + /** * @Then the :promotion promotion should have :ruleName rule configured */ public function thePromotionShouldHaveRuleConfigured(PromotionInterface $promotion, string $ruleName): void { $this->iWantToModifyAPromotion($promotion); + $this->updatePage->saveChanges(); Assert::true($this->updatePage->hasRule($ruleName)); } @@ -637,7 +649,6 @@ public function thePromotionShouldNotHaveAnyRuleConfigured(PromotionInterface $p Assert::false($this->updatePage->hasAnyRule()); } - /** * @param string $element * @param string $expectedMessage diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index cfe62a78835..0d8b3eb8251 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -55,14 +55,7 @@ private function resolveHasTaxonRules(string $taxonCode): void foreach ($promotionRules as $promotionRule) { $configuration = $promotionRule->getConfiguration(); if (in_array($taxonCode, $configuration['taxons'])) { - $taxons = array_filter( - $configuration['taxons'], - function (string $code) use ($taxonCode) { - return $taxonCode !== $code; - } - ); - - $configuration['taxons'] = array_values($taxons); + $configuration['taxons'] = array_values(array_diff($configuration['taxons'], [$taxonCode])); $promotionRule->setConfiguration($configuration); } } From ed2d9a18d07556f2df1f5ca497f3eeda9ff831fe Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 10 May 2019 11:29:23 +0200 Subject: [PATCH 4/8] [Promotion] Split update rule configuration after deleting taxon to separate services --- .../EventListener/TaxonDeletionListener.php | 69 ++++------------- .../Resources/config/services/listeners.xml | 4 +- .../Resources/config/services/promotion.xml | 9 +++ .../TaxonDeletionListenerSpec.php | 76 +++---------------- .../Updater/Rule/HasTaxonRuleUpdater.php | 55 ++++++++++++++ .../Rule/TaxonAwareRuleUpdaterInterface.php | 19 +++++ .../Rule/TotalOfItemsFromTaxonRuleUpdater.php | 59 ++++++++++++++ .../Updater/Rule/HasTaxonRuleUpdaterSpec.php | 54 +++++++++++++ .../TotalOfItemsFromTaxonRuleUpdaterSpec.php | 49 ++++++++++++ 9 files changed, 272 insertions(+), 122 deletions(-) create mode 100644 src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php create mode 100644 src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php create mode 100644 src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php create mode 100644 src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php create mode 100644 src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index 0d8b3eb8251..1f13840dbdf 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -13,27 +13,25 @@ namespace Sylius\Bundle\CoreBundle\EventListener; -use Doctrine\ORM\EntityManagerInterface; use Sylius\Component\Core\Model\TaxonInterface; -use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker; -use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker; -use Sylius\Component\Promotion\Model\PromotionRuleInterface; -use Sylius\Component\Resource\Repository\RepositoryInterface; +use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface; use Symfony\Component\EventDispatcher\GenericEvent; use Webmozart\Assert\Assert; final class TaxonDeletionListener { - /** @var RepositoryInterface */ - private $promotionRuleRepository; - - /** @var EntityManagerInterface */ - private $manager; - - public function __construct(RepositoryInterface $promotionRuleRepository, EntityManagerInterface $manager) - { - $this->promotionRuleRepository = $promotionRuleRepository; - $this->manager = $manager; + /** @var TaxonAwareRuleUpdaterInterface */ + private $hasTaxonRuleUpdater; + + /** @var TaxonAwareRuleUpdaterInterface */ + private $totalOfItemsFromTaxonRuleUpdater; + + public function __construct( + TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater + ) { + $this->hasTaxonRuleUpdater = $hasTaxonRuleUpdater; + $this->totalOfItemsFromTaxonRuleUpdater = $totalOfItemsFromTaxonRuleUpdater; } public function removeTaxonFromPromotionRules(GenericEvent $event): void @@ -41,44 +39,7 @@ public function removeTaxonFromPromotionRules(GenericEvent $event): void $taxon = $event->getSubject(); Assert::isInstanceOf($taxon, TaxonInterface::class); - $this->resolveHasTaxonRules($taxon->getCode()); - $this->resolveTotalOfItemsFromTaxonRules($taxon->getCode()); - - $this->manager->flush(); - } - - private function resolveHasTaxonRules(string $taxonCode): void - { - $promotionRules = $this->promotionRuleRepository->findBy(['type' => HasTaxonRuleChecker::TYPE]); - - /** @var PromotionRuleInterface $promotionRule */ - foreach ($promotionRules as $promotionRule) { - $configuration = $promotionRule->getConfiguration(); - if (in_array($taxonCode, $configuration['taxons'])) { - $configuration['taxons'] = array_values(array_diff($configuration['taxons'], [$taxonCode])); - $promotionRule->setConfiguration($configuration); - } - } - } - - private function resolveTotalOfItemsFromTaxonRules(string $taxonCode): void - { - $promotionRules = $this->promotionRuleRepository->findBy(['type' => TotalOfItemsFromTaxonRuleChecker::TYPE]); - - /** @var PromotionRuleInterface $promotionRule */ - foreach ($promotionRules as $promotionRule) { - $this->removePromotionRuleIfNecessary($promotionRule, $taxonCode); - } - } - - private function removePromotionRuleIfNecessary(PromotionRuleInterface $promotionRule, string $taxonCode): void - { - foreach ($promotionRule->getConfiguration() as $configuration) { - if ($taxonCode === $configuration['taxon']) { - $this->promotionRuleRepository->remove($promotionRule); - - return; - } - } + $this->hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); + $this->totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index 2a2b886d33a..481558d23cc 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -88,8 +88,8 @@ - - + + diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/promotion.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/promotion.xml index 8c50108a8ed..2546e3b4938 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/promotion.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/promotion.xml @@ -106,5 +106,14 @@ + + + + + + + + + diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index 0c0f6bcc81e..4fe9ea43f69 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -13,87 +13,31 @@ namespace spec\Sylius\Bundle\CoreBundle\EventListener; -use Doctrine\ORM\EntityManagerInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use Sylius\Component\Core\Model\TaxonInterface; -use Sylius\Component\Promotion\Model\PromotionRuleInterface; -use Sylius\Component\Resource\Repository\RepositoryInterface; +use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface; use Symfony\Component\EventDispatcher\GenericEvent; final class TaxonDeletionListenerSpec extends ObjectBehavior { - function let(RepositoryInterface $promotionRuleRepository, EntityManagerInterface $manager): void - { - $this->beConstructedWith($promotionRuleRepository, $manager); - } - - function it_removes_rules_that_using_deleted_taxon( - RepositoryInterface $promotionRuleRepository, - EntityManagerInterface $manager, - GenericEvent $event, - TaxonInterface $taxon, - PromotionRuleInterface $firstPromotionRule, - PromotionRuleInterface $secondPromotionRule + function let( + TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater ): void { - $event->getSubject()->willReturn($taxon); - $taxon->getCode()->willReturn('toys'); - - $promotionRuleRepository - ->findBy(['type' => 'has_taxon']) - ->willReturn([$firstPromotionRule, $secondPromotionRule]) - ; - $firstPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs', 'toys']]); - $secondPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs']]); - - $firstPromotionRule->setConfiguration(['taxons' => ['mugs']])->shouldBeCalled(); - $secondPromotionRule->setConfiguration(Argument::any())->shouldNotBeCalled(); - - $promotionRuleRepository->findBy(['type' => 'total_of_items_from_taxon'])->willReturn([]); - - $manager->flush()->shouldBeCalled(); - - $this->removeTaxonFromPromotionRules($event); - } - - function it_removes_deleted_taxon_from_rules_configurations( - RepositoryInterface $promotionRuleRepository, - EntityManagerInterface $manager, - GenericEvent $event, - TaxonInterface $taxon, - PromotionRuleInterface $firstPromotionRule, - PromotionRuleInterface $secondPromotionRule - ): void { - $event->getSubject()->willReturn($taxon); - $taxon->getCode()->willReturn('toys'); - - $promotionRuleRepository->findBy(['type' => 'has_taxon'])->willReturn([]); - - $promotionRuleRepository - ->findBy(['type' => 'total_of_items_from_taxon']) - ->willReturn([$firstPromotionRule, $secondPromotionRule]) - ; - $firstPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'mugs', 'amount' => 500]]); - $secondPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'toys', 'amount' => 300]]); - - $promotionRuleRepository->remove($firstPromotionRule)->shouldNotBeCalled(); - $promotionRuleRepository->remove($secondPromotionRule)->shouldBeCalled(); - - $manager->flush()->shouldBeCalled(); - - $this->removeTaxonFromPromotionRules($event); + $this->beConstructedWith($hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater); } - function it_does_nothing_if_there_is_no_rule_with_given_types( - RepositoryInterface $promotionRuleRepository, + function it_adds_flash_that_promotions_have_been_updated( + TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, GenericEvent $event, TaxonInterface $taxon ): void { $event->getSubject()->willReturn($taxon); $taxon->getCode()->willReturn('toys'); - $promotionRuleRepository->findBy(['type' => 'has_taxon'])->willReturn([]); - $promotionRuleRepository->findBy(['type' => 'total_of_items_from_taxon'])->willReturn([]); + $hasTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas', 'holiday']); + $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas']); $this->removeTaxonFromPromotionRules($event); } diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php new file mode 100644 index 00000000000..fa950986400 --- /dev/null +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php @@ -0,0 +1,55 @@ +promotionRuleRepository = $promotionRuleRepository; + $this->manager = $manager; + } + + public function updateAfterDeletingTaxon(string $taxonCode): array + { + $updatedPromotionCodes = []; + $promotionRules = $this->promotionRuleRepository->findBy(['type' => HasTaxonRuleChecker::TYPE]); + + /** @var PromotionRuleInterface $promotionRule */ + foreach ($promotionRules as $promotionRule) { + $configuration = $promotionRule->getConfiguration(); + if (in_array($taxonCode, $configuration['taxons'])) { + $configuration['taxons'] = array_values(array_diff($configuration['taxons'], [$taxonCode])); + $promotionRule->setConfiguration($configuration); + + $updatedPromotionCodes[] = $promotionRule->getPromotion()->getCode(); + } + } + + $this->manager->flush(); + + return $updatedPromotionCodes; + } +} diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php new file mode 100644 index 00000000000..7cf85be0a07 --- /dev/null +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php @@ -0,0 +1,19 @@ +promotionRuleRepository = $ruleUpdater; + } + + public function updateAfterDeletingTaxon(string $taxonCode): array + { + $updatedPromotionCodes = []; + $promotionRules = $this->promotionRuleRepository->findBy(['type' => TotalOfItemsFromTaxonRuleChecker::TYPE]); + + /** @var PromotionRuleInterface $promotionRule */ + foreach ($promotionRules as $promotionRule) { + $promotionCode = $this->removePromotionRuleIfNecessary($promotionRule, $taxonCode); + + if (null !== $promotionCode) { + $updatedPromotionCodes[] = $promotionRule->getPromotion()->getCode(); + } + } + + return $updatedPromotionCodes; + } + + private function removePromotionRuleIfNecessary(PromotionRuleInterface $promotionRule, string $taxonCode): ?string + { + foreach ($promotionRule->getConfiguration() as $configuration) { + if ($taxonCode === $configuration['taxon']) { + $this->promotionRuleRepository->remove($promotionRule); + + return $promotionRule->getPromotion()->getCode(); + } + } + + return null; + } +} diff --git a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php new file mode 100644 index 00000000000..271aae71177 --- /dev/null +++ b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php @@ -0,0 +1,54 @@ +beConstructedWith($promotionRuleRepository, $manager); + } + + function it_removes_deleted_taxon_from_rules_configurations( + RepositoryInterface $promotionRuleRepository, + EntityManagerInterface $manager, + PromotionRuleInterface $firstPromotionRule, + PromotionRuleInterface $secondPromotionRule, + PromotionInterface $promotion + ): void { + $promotionRuleRepository + ->findBy(['type' => 'has_taxon']) + ->willReturn([$firstPromotionRule, $secondPromotionRule]) + ; + $firstPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs', 'toys']]); + $secondPromotionRule->getConfiguration()->willReturn(['taxons' => ['mugs']]); + + $firstPromotionRule->getPromotion()->willReturn($promotion); + $promotion->getCode()->willReturn('christmas'); + + $firstPromotionRule->setConfiguration(['taxons' => ['mugs']])->shouldBeCalled(); + $secondPromotionRule->setConfiguration(Argument::any())->shouldNotBeCalled(); + + $manager->flush()->shouldBeCalled(); + + $this->updateAfterDeletingTaxon('toys')->shouldReturn(['christmas']); + } +} diff --git a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php new file mode 100644 index 00000000000..1eb0b0ff5ed --- /dev/null +++ b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php @@ -0,0 +1,49 @@ +beConstructedWith($promotionRuleRepository); + } + + function it_removes_rules_that_using_deleted_taxon( + RepositoryInterface $promotionRuleRepository, + PromotionRuleInterface $firstPromotionRule, + PromotionRuleInterface $secondPromotionRule, + PromotionInterface $promotion + ): void { + $promotionRuleRepository + ->findBy(['type' => 'total_of_items_from_taxon']) + ->willReturn([$firstPromotionRule, $secondPromotionRule]) + ; + $firstPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'mugs', 'amount' => 500]]); + $secondPromotionRule->getConfiguration()->willReturn(['web' => ['taxon' => 'toys', 'amount' => 300]]); + + $secondPromotionRule->getPromotion()->willReturn($promotion); + $promotion->getCode()->willReturn('christmas'); + + $promotionRuleRepository->remove($firstPromotionRule)->shouldNotBeCalled(); + $promotionRuleRepository->remove($secondPromotionRule)->shouldBeCalled(); + + $this->updateAfterDeletingTaxon('toys')->shouldReturn(['christmas']); + } +} From fd1d220a2cb541d29d3d9316d55e7ab9bea7e377 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 10 May 2019 13:15:43 +0200 Subject: [PATCH 5/8] [Behat] Add info type to notification checker --- src/Sylius/Behat/NotificationType.php | 32 ++++++------------- .../Behat/Service/NotificationChecker.php | 11 ++++++- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/Sylius/Behat/NotificationType.php b/src/Sylius/Behat/NotificationType.php index a3261d096bb..908845f285d 100644 --- a/src/Sylius/Behat/NotificationType.php +++ b/src/Sylius/Behat/NotificationType.php @@ -21,44 +21,32 @@ final class NotificationType /** @var array */ private static $types = []; - /** - * @param string $value - */ - private function __construct($value) + private function __construct(string $value) { $this->value = $value; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->value; } - /** - * @return NotificationType - */ - public static function failure() + public static function failure(): NotificationType { return static::getTyped('failure'); } - /** - * @return NotificationType - */ - public static function success() + public static function success(): NotificationType { return static::getTyped('success'); } - /** - * @param string $type - * - * @return NotificationType - */ - private static function getTyped($type) + public static function info(): NotificationType + { + return static::getTyped('info'); + } + + private static function getTyped(string $type): NotificationType { if (!isset(static::$types[$type])) { static::$types[$type] = new self($type); diff --git a/src/Sylius/Behat/Service/NotificationChecker.php b/src/Sylius/Behat/Service/NotificationChecker.php index adbf9f8e066..f6d6c58abb0 100644 --- a/src/Sylius/Behat/Service/NotificationChecker.php +++ b/src/Sylius/Behat/Service/NotificationChecker.php @@ -35,7 +35,7 @@ public function checkNotification(string $message, NotificationType $type): void foreach ($this->notificationAccessor->getMessageElements() as $messageElement) { if ( false !== strpos($messageElement->getText(), $message) && - $messageElement->hasClass($type->__toString() === 'success' ? 'positive' : 'negative') + $messageElement->hasClass($this->resolveClass($type)) ) { return; } @@ -43,4 +43,13 @@ public function checkNotification(string $message, NotificationType $type): void throw new NotificationExpectationMismatchException($type, $message); } + + private function resolveClass(NotificationType $type): string + { + if ($type->__toString() === 'info') { + return 'info'; + } + + return $type->__toString() === 'success' ? 'positive' : 'negative'; + } } From 060e96952d19ab02422fc6358d65c0e393cda38e Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 10 May 2019 12:14:57 +0200 Subject: [PATCH 6/8] [Admin] Add flash to taxon deletion listener --- ...h_rule_that_contains_removed_taxon.feature | 6 ++-- .../Context/Ui/Admin/RemovingTaxonContext.php | 20 ++++++++++- .../Resources/config/services/contexts/ui.xml | 1 + .../EventListener/TaxonDeletionListener.php | 23 ++++++++++-- .../Resources/config/services/listeners.xml | 1 + .../TaxonDeletionListenerSpec.php | 36 +++++++++++++++++-- .../Resources/translations/flashes.en.yml | 2 ++ .../Rule/TotalOfItemsFromTaxonRuleUpdater.php | 4 +-- 8 files changed, 83 insertions(+), 10 deletions(-) diff --git a/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature index 9c6a03056f9..92da192df9a 100644 --- a/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature +++ b/features/promotion/managing_promotions/accessing_promotion_with_rule_that_contains_removed_taxon.feature @@ -13,12 +13,14 @@ Feature: Accessing an edit page of a promotion with a rule that contains a remov Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon Given there is a promotion "Christmas sale" with "Has at least one from taxons" rule configured with "T-Shirts" and "Mugs" When I remove taxon named "Mugs" - Then I should be able to modify a "Christmas sale" promotion + Then I should be notified that "Christmas sale" promotion has been updated + And I should be able to modify a "Christmas sale" promotion And the "Christmas sale" promotion should have "Has at least one from taxons" rule configured @ui Scenario: Accessing an edit page of a promotion with a rule that contains a removed taxon Given there is a promotion "Christmas sale" with "Total price of items from taxon" rule configured with "Mugs" taxon and $100 amount for "United States" channel When I remove taxon named "Mugs" - Then I should be able to modify a "Christmas sale" promotion + Then I should be notified that "Christmas sale" promotion has been updated + And I should be able to modify a "Christmas sale" promotion And the "Christmas sale" promotion should not have any rule configured diff --git a/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php b/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php index af434fa277f..3924c9fed0d 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/RemovingTaxonContext.php @@ -14,16 +14,23 @@ namespace Sylius\Behat\Context\Ui\Admin; use Behat\Behat\Context\Context; +use Sylius\Behat\NotificationType; use Sylius\Behat\Page\Admin\Taxon\CreatePageInterface; +use Sylius\Behat\Service\NotificationCheckerInterface; +use Sylius\Component\Core\Model\PromotionInterface; final class RemovingTaxonContext implements Context { /** @var CreatePageInterface */ private $createPage; - public function __construct(CreatePageInterface $createPage) + /** @var NotificationCheckerInterface */ + private $notificationChecker; + + public function __construct(CreatePageInterface $createPage, NotificationCheckerInterface $notificationChecker) { $this->createPage = $createPage; + $this->notificationChecker = $notificationChecker; } /** @@ -35,4 +42,15 @@ public function iRemoveTaxonNamed(string $name): void $this->createPage->open(); $this->createPage->deleteTaxonOnPageByName($name); } + + /** + * @Then I should be notified that :promotion promotion has been updated + */ + public function iShouldBeNotifiedThatPromotionHasBeenUpdated(PromotionInterface $promotion): void + { + $this->notificationChecker->checkNotification( + sprintf('Some rules of the promotions with codes %s have been updated.', $promotion->getCode()), + NotificationType::info() + ); + } } diff --git a/src/Sylius/Behat/Resources/config/services/contexts/ui.xml b/src/Sylius/Behat/Resources/config/services/contexts/ui.xml index 5b7d2606191..51404b5c98c 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/ui.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/ui.xml @@ -276,6 +276,7 @@ + diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index 1f13840dbdf..f52d5ff59f8 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -16,6 +16,8 @@ use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface; use Symfony\Component\EventDispatcher\GenericEvent; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Webmozart\Assert\Assert; final class TaxonDeletionListener @@ -26,12 +28,17 @@ final class TaxonDeletionListener /** @var TaxonAwareRuleUpdaterInterface */ private $totalOfItemsFromTaxonRuleUpdater; + /** @var SessionInterface */ + private $session; + public function __construct( TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, + SessionInterface $session ) { $this->hasTaxonRuleUpdater = $hasTaxonRuleUpdater; $this->totalOfItemsFromTaxonRuleUpdater = $totalOfItemsFromTaxonRuleUpdater; + $this->session = $session; } public function removeTaxonFromPromotionRules(GenericEvent $event): void @@ -39,7 +46,17 @@ public function removeTaxonFromPromotionRules(GenericEvent $event): void $taxon = $event->getSubject(); Assert::isInstanceOf($taxon, TaxonInterface::class); - $this->hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); - $this->totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); + $firstUpdatedPromotionCodes = $this->hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); + $secondUpdatedPromotionCodes = $this->totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); + $updatedPromotionCodes = array_unique(array_merge($firstUpdatedPromotionCodes, $secondUpdatedPromotionCodes)); + + if (!empty($updatedPromotionCodes)) { + /** @var FlashBagInterface $flashes */ + $flashes = $this->session->getBag('flashes'); + $flashes->add('info', [ + 'message' => 'sylius.promotion.update_rules', + 'parameters' => ['%codes%' => implode(', ', $updatedPromotionCodes)], + ]); + } } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index 481558d23cc..a595409342b 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -90,6 +90,7 @@ + diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index 4fe9ea43f69..b0c6fdcb4dd 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -17,19 +17,24 @@ use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface; use Symfony\Component\EventDispatcher\GenericEvent; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; final class TaxonDeletionListenerSpec extends ObjectBehavior { function let( TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, + SessionInterface $session ): void { - $this->beConstructedWith($hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater); + $this->beConstructedWith($hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater, $session); } function it_adds_flash_that_promotions_have_been_updated( TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, + SessionInterface $session, + FlashBagInterface $flashes, GenericEvent $event, TaxonInterface $taxon ): void { @@ -39,6 +44,33 @@ function it_adds_flash_that_promotions_have_been_updated( $hasTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas', 'holiday']); $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas']); + $session->getBag('flashes')->willReturn($flashes); + $flashes + ->add('info', [ + 'message' => 'sylius.promotion.update_rules', + 'parameters' => ['%codes%' => 'christmas, holiday'], + ]) + ->shouldBeCalled() + ; + + $this->removeTaxonFromPromotionRules($event); + } + + function it_does_nothing_if_no_promotion_has_been_updated( + TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, + SessionInterface $session, + GenericEvent $event, + TaxonInterface $taxon + ): void { + $event->getSubject()->willReturn($taxon); + $taxon->getCode()->willReturn('toys'); + + $hasTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn([]); + $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn([]); + + $session->getBag('flashes')->shouldNotBeCalled(); + $this->removeTaxonFromPromotionRules($event); } } diff --git a/src/Sylius/Bundle/UiBundle/Resources/translations/flashes.en.yml b/src/Sylius/Bundle/UiBundle/Resources/translations/flashes.en.yml index 58958f5c211..0fd9e41ac79 100644 --- a/src/Sylius/Bundle/UiBundle/Resources/translations/flashes.en.yml +++ b/src/Sylius/Bundle/UiBundle/Resources/translations/flashes.en.yml @@ -24,3 +24,5 @@ sylius: out_of_stock: 'There is only %quantity% of this product in the stock.' product_variant: update_error: 'There was an unexpected problem with updating a product variant. Please, try to update product variant again.' + promotion: + update_rules: 'Some rules of the promotions with codes %codes% have been updated.' diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php index 14fb572b693..cf6469a9771 100644 --- a/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php @@ -22,9 +22,9 @@ final class TotalOfItemsFromTaxonRuleUpdater implements TaxonAwareRuleUpdaterInt /** @var RepositoryInterface */ private $promotionRuleRepository; - public function __construct(RepositoryInterface $ruleUpdater) + public function __construct(RepositoryInterface $promotionRuleRepository) { - $this->promotionRuleRepository = $ruleUpdater; + $this->promotionRuleRepository = $promotionRuleRepository; } public function updateAfterDeletingTaxon(string $taxonCode): array From 1e82d3cc4177cf51aa3126c7d902cc3c9e1359af Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 10 May 2019 16:03:43 +0200 Subject: [PATCH 7/8] Add variadics to taxon deletion listener --- .../EventListener/TaxonDeletionListener.php | 27 +++++++++---------- .../Resources/config/services/listeners.xml | 2 +- .../TaxonDeletionListenerSpec.php | 10 +++---- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index f52d5ff59f8..8f0dcdcda03 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -22,23 +22,19 @@ final class TaxonDeletionListener { - /** @var TaxonAwareRuleUpdaterInterface */ - private $hasTaxonRuleUpdater; - - /** @var TaxonAwareRuleUpdaterInterface */ - private $totalOfItemsFromTaxonRuleUpdater; - /** @var SessionInterface */ private $session; + /** @var TaxonAwareRuleUpdaterInterface[] */ + private $ruleUpdaters; + public function __construct( - TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session + SessionInterface $session, + TaxonAwareRuleUpdaterInterface ...$ruleUpdaters + ) { - $this->hasTaxonRuleUpdater = $hasTaxonRuleUpdater; - $this->totalOfItemsFromTaxonRuleUpdater = $totalOfItemsFromTaxonRuleUpdater; $this->session = $session; + $this->ruleUpdaters = $ruleUpdaters; } public function removeTaxonFromPromotionRules(GenericEvent $event): void @@ -46,16 +42,17 @@ public function removeTaxonFromPromotionRules(GenericEvent $event): void $taxon = $event->getSubject(); Assert::isInstanceOf($taxon, TaxonInterface::class); - $firstUpdatedPromotionCodes = $this->hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); - $secondUpdatedPromotionCodes = $this->totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); - $updatedPromotionCodes = array_unique(array_merge($firstUpdatedPromotionCodes, $secondUpdatedPromotionCodes)); + $updatedPromotionCodes = []; + foreach ($this->ruleUpdaters as $ruleUpdater) { + $updatedPromotionCodes = array_merge($updatedPromotionCodes, $ruleUpdater->updateAfterDeletingTaxon($taxon->getCode())); + } if (!empty($updatedPromotionCodes)) { /** @var FlashBagInterface $flashes */ $flashes = $this->session->getBag('flashes'); $flashes->add('info', [ 'message' => 'sylius.promotion.update_rules', - 'parameters' => ['%codes%' => implode(', ', $updatedPromotionCodes)], + 'parameters' => ['%codes%' => implode(', ', array_unique($updatedPromotionCodes))], ]); } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index a595409342b..aa3bbb2cfe5 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -88,9 +88,9 @@ + - diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index b0c6fdcb4dd..02d9a988374 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -23,17 +23,17 @@ final class TaxonDeletionListenerSpec extends ObjectBehavior { function let( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater ): void { - $this->beConstructedWith($hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater, $session); + $this->beConstructedWith($session, $hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater); } function it_adds_flash_that_promotions_have_been_updated( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session, FlashBagInterface $flashes, GenericEvent $event, TaxonInterface $taxon @@ -57,9 +57,9 @@ function it_adds_flash_that_promotions_have_been_updated( } function it_does_nothing_if_no_promotion_has_been_updated( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session, GenericEvent $event, TaxonInterface $taxon ): void { From 93cd924d839a69ced21084dc0c00481207440e46 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Sun, 12 May 2019 10:53:45 +0200 Subject: [PATCH 8/8] Fixes after PR review --- src/Sylius/Behat/Service/NotificationChecker.php | 13 +++++++++---- .../EventListener/TaxonDeletionListener.php | 3 +-- .../EventListener/TaxonDeletionListenerSpec.php | 10 ++++------ .../Promotion/Updater/Rule/HasTaxonRuleUpdater.php | 7 ++++--- .../Updater/Rule/TaxonAwareRuleUpdaterInterface.php | 4 +++- .../Rule/TotalOfItemsFromTaxonRuleUpdater.php | 5 +++-- .../Updater/Rule/HasTaxonRuleUpdaterSpec.php | 8 ++++++-- .../Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php | 8 ++++++-- 8 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/Sylius/Behat/Service/NotificationChecker.php b/src/Sylius/Behat/Service/NotificationChecker.php index f6d6c58abb0..35eb3af4fcb 100644 --- a/src/Sylius/Behat/Service/NotificationChecker.php +++ b/src/Sylius/Behat/Service/NotificationChecker.php @@ -16,6 +16,7 @@ use Sylius\Behat\Exception\NotificationExpectationMismatchException; use Sylius\Behat\NotificationType; use Sylius\Behat\Service\Accessor\NotificationAccessorInterface; +use Webmozart\Assert\Assert; final class NotificationChecker implements NotificationCheckerInterface { @@ -46,10 +47,14 @@ public function checkNotification(string $message, NotificationType $type): void private function resolveClass(NotificationType $type): string { - if ($type->__toString() === 'info') { - return 'info'; - } + $typeClassMap = [ + 'failure' => 'negative', + 'info' => 'info', + 'success' => 'positive', + ]; + + Assert::keyExists($typeClassMap, $type->__toString()); - return $type->__toString() === 'success' ? 'positive' : 'negative'; + return $typeClassMap[$type->__toString()]; } } diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index 8f0dcdcda03..487041f2145 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -31,7 +31,6 @@ final class TaxonDeletionListener public function __construct( SessionInterface $session, TaxonAwareRuleUpdaterInterface ...$ruleUpdaters - ) { $this->session = $session; $this->ruleUpdaters = $ruleUpdaters; @@ -44,7 +43,7 @@ public function removeTaxonFromPromotionRules(GenericEvent $event): void $updatedPromotionCodes = []; foreach ($this->ruleUpdaters as $ruleUpdater) { - $updatedPromotionCodes = array_merge($updatedPromotionCodes, $ruleUpdater->updateAfterDeletingTaxon($taxon->getCode())); + $updatedPromotionCodes = array_merge($updatedPromotionCodes, $ruleUpdater->updateAfterDeletingTaxon($taxon)); } if (!empty($updatedPromotionCodes)) { diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index 02d9a988374..59d9634cdfd 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -39,10 +39,9 @@ function it_adds_flash_that_promotions_have_been_updated( TaxonInterface $taxon ): void { $event->getSubject()->willReturn($taxon); - $taxon->getCode()->willReturn('toys'); - $hasTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas', 'holiday']); - $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn(['christmas']); + $hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon)->willReturn(['christmas', 'holiday']); + $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon)->willReturn(['christmas']); $session->getBag('flashes')->willReturn($flashes); $flashes @@ -64,10 +63,9 @@ function it_does_nothing_if_no_promotion_has_been_updated( TaxonInterface $taxon ): void { $event->getSubject()->willReturn($taxon); - $taxon->getCode()->willReturn('toys'); - $hasTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn([]); - $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon('toys')->willReturn([]); + $hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon)->willReturn([]); + $totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon)->willReturn([]); $session->getBag('flashes')->shouldNotBeCalled(); diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php index fa950986400..da3d406f3a3 100644 --- a/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/HasTaxonRuleUpdater.php @@ -14,6 +14,7 @@ namespace Sylius\Component\Core\Promotion\Updater\Rule; use Doctrine\ORM\EntityManagerInterface; +use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker; use Sylius\Component\Promotion\Model\PromotionRuleInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -32,7 +33,7 @@ public function __construct(RepositoryInterface $promotionRuleRepository, Entity $this->manager = $manager; } - public function updateAfterDeletingTaxon(string $taxonCode): array + public function updateAfterDeletingTaxon(TaxonInterface $taxon): array { $updatedPromotionCodes = []; $promotionRules = $this->promotionRuleRepository->findBy(['type' => HasTaxonRuleChecker::TYPE]); @@ -40,8 +41,8 @@ public function updateAfterDeletingTaxon(string $taxonCode): array /** @var PromotionRuleInterface $promotionRule */ foreach ($promotionRules as $promotionRule) { $configuration = $promotionRule->getConfiguration(); - if (in_array($taxonCode, $configuration['taxons'])) { - $configuration['taxons'] = array_values(array_diff($configuration['taxons'], [$taxonCode])); + if (in_array($taxon->getCode(), $configuration['taxons'])) { + $configuration['taxons'] = array_values(array_diff($configuration['taxons'], [$taxon->getCode()])); $promotionRule->setConfiguration($configuration); $updatedPromotionCodes[] = $promotionRule->getPromotion()->getCode(); diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php index 7cf85be0a07..b4c76f62340 100644 --- a/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/TaxonAwareRuleUpdaterInterface.php @@ -13,7 +13,9 @@ namespace Sylius\Component\Core\Promotion\Updater\Rule; +use Sylius\Component\Core\Model\TaxonInterface; + interface TaxonAwareRuleUpdaterInterface { - public function updateAfterDeletingTaxon(string $taxonCode): array; + public function updateAfterDeletingTaxon(TaxonInterface $taxon): array; } diff --git a/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php b/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php index cf6469a9771..5d2cca4d454 100644 --- a/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php +++ b/src/Sylius/Component/Core/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdater.php @@ -13,6 +13,7 @@ namespace Sylius\Component\Core\Promotion\Updater\Rule; +use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker; use Sylius\Component\Promotion\Model\PromotionRuleInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -27,14 +28,14 @@ public function __construct(RepositoryInterface $promotionRuleRepository) $this->promotionRuleRepository = $promotionRuleRepository; } - public function updateAfterDeletingTaxon(string $taxonCode): array + public function updateAfterDeletingTaxon(TaxonInterface $taxon): array { $updatedPromotionCodes = []; $promotionRules = $this->promotionRuleRepository->findBy(['type' => TotalOfItemsFromTaxonRuleChecker::TYPE]); /** @var PromotionRuleInterface $promotionRule */ foreach ($promotionRules as $promotionRule) { - $promotionCode = $this->removePromotionRuleIfNecessary($promotionRule, $taxonCode); + $promotionCode = $this->removePromotionRuleIfNecessary($promotionRule, $taxon->getCode()); if (null !== $promotionCode) { $updatedPromotionCodes[] = $promotionRule->getPromotion()->getCode(); diff --git a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php index 271aae71177..d17c3a1caae 100644 --- a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php +++ b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/HasTaxonRuleUpdaterSpec.php @@ -17,6 +17,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Sylius\Component\Core\Model\PromotionInterface; +use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Promotion\Model\PromotionRuleInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -32,8 +33,11 @@ function it_removes_deleted_taxon_from_rules_configurations( EntityManagerInterface $manager, PromotionRuleInterface $firstPromotionRule, PromotionRuleInterface $secondPromotionRule, - PromotionInterface $promotion + PromotionInterface $promotion, + TaxonInterface $taxon ): void { + $taxon->getCode()->willReturn('toys'); + $promotionRuleRepository ->findBy(['type' => 'has_taxon']) ->willReturn([$firstPromotionRule, $secondPromotionRule]) @@ -49,6 +53,6 @@ function it_removes_deleted_taxon_from_rules_configurations( $manager->flush()->shouldBeCalled(); - $this->updateAfterDeletingTaxon('toys')->shouldReturn(['christmas']); + $this->updateAfterDeletingTaxon($taxon)->shouldReturn(['christmas']); } } diff --git a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php index 1eb0b0ff5ed..90e67d68eba 100644 --- a/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php +++ b/src/Sylius/Component/Core/spec/Promotion/Updater/Rule/TotalOfItemsFromTaxonRuleUpdaterSpec.php @@ -15,6 +15,7 @@ use PhpSpec\ObjectBehavior; use Sylius\Component\Core\Model\PromotionInterface; +use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Promotion\Model\PromotionRuleInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -29,8 +30,11 @@ function it_removes_rules_that_using_deleted_taxon( RepositoryInterface $promotionRuleRepository, PromotionRuleInterface $firstPromotionRule, PromotionRuleInterface $secondPromotionRule, - PromotionInterface $promotion + PromotionInterface $promotion, + TaxonInterface $taxon ): void { + $taxon->getCode()->willReturn('toys'); + $promotionRuleRepository ->findBy(['type' => 'total_of_items_from_taxon']) ->willReturn([$firstPromotionRule, $secondPromotionRule]) @@ -44,6 +48,6 @@ function it_removes_rules_that_using_deleted_taxon( $promotionRuleRepository->remove($firstPromotionRule)->shouldNotBeCalled(); $promotionRuleRepository->remove($secondPromotionRule)->shouldBeCalled(); - $this->updateAfterDeletingTaxon('toys')->shouldReturn(['christmas']); + $this->updateAfterDeletingTaxon($taxon)->shouldReturn(['christmas']); } }