Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin][Promotion] Fix removing taxon used in promotion rule #10365

Merged
merged 8 commits into from
May 13, 2019
Prev Previous commit
Fixes after PR review
  • Loading branch information
GSadee committed May 13, 2019
commit 93cd924d839a69ced21084dc0c00481207440e46
13 changes: 9 additions & 4 deletions src/Sylius/Behat/Service/NotificationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

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

I'm just wondering, what is better

Suggested change
Assert::keyExists($typeClassMap, $type->__toString());
Assert::keyExists($typeClassMap, (string) $type);


return $type->__toString() === 'success' ? 'positive' : 'negative';
return $typeClassMap[$type->__toString()];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ final class TaxonDeletionListener
public function __construct(
SessionInterface $session,
TaxonAwareRuleUpdaterInterface ...$ruleUpdaters

) {
$this->session = $session;
$this->ruleUpdaters = $ruleUpdaters;
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,16 +33,16 @@ 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]);

/** @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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])
Expand All @@ -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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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])
Expand All @@ -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']);
}
}