Skip to content

Commit

Permalink
Remove unneded logic and hcange method name
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Sep 28, 2021
1 parent 547f3b6 commit 7d8b2f1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ Feature: Reapplying catalog promotion after editing its action
When I modify a catalog promotion "Summer sale"
And I add action that gives "25%" percentage discount
And I save my changes
Then the visitor view "Python T-Shirt" variant
And this product variant price should be "$7.50"
And the visitor view "Python T-Shirt" variant
Then this product variant price should be "$7.50"
And this product original price should be "$10.00"

@api
Scenario: Reapplying catalog promotion after editing its action
When I modify a catalog promotion "Winter sale"
And I edit its action so that it reduces price by "25%"
And I save my changes
Then the visitor view "PHP T-Shirt" variant
And this product variant price should be "$15.00"
And the visitor view "PHP T-Shirt" variant
Then this product variant price should be "$15.00"
And this product original price should be "$20.00"

@api
Expand All @@ -39,24 +39,24 @@ Feature: Reapplying catalog promotion after editing its action
And I save my changes
And I add action that gives "10%" percentage discount
And I save my changes
Then the visitor view "PHP T-Shirt" variant
And this product variant price should be "$18.00"
And the visitor view "PHP T-Shirt" variant
Then this product variant price should be "$18.00"
And this product original price should be "$20.00"

@api
Scenario: Reapplying catalog promotion after adding another action
When I modify a catalog promotion "Winter sale"
And I add another action that gives "10%" percentage discount
And I save my changes
Then the visitor view "PHP T-Shirt" variant
And this product variant price should be "$9.00"
And the visitor view "PHP T-Shirt" variant
Then this product variant price should be "$9.00"
And this product original price should be "$20.00"

@api
Scenario: Restoring original price after removing action from catalog promotion configuration
When I modify a catalog promotion "Winter sale"
And I remove its every action
And I save my changes
Then the visitor view "PHP T-Shirt" variant
And the product variant price should be "$20.00"
And the visitor view "PHP T-Shirt" variant
Then the product variant price should be "$20.00"
And the product original price should be "$20.00"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ApiPlatform\Core\EventListener\EventPriorities;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
Expand All @@ -42,17 +41,15 @@ public static function getSubscribedEvents(): array
public function postWrite(ViewEvent $event): void
{
$entity = $event->getControllerResult();

if (!$entity instanceof CatalogPromotionInterface) {
return;
}

$method = $event->getRequest()->getMethod();

if ($method === Request::METHOD_PUT || $method === Request::METHOD_PATCH) {
if ($entity instanceof CatalogPromotionInterface) {
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCode()));
}

if ($entity instanceof CatalogPromotionActionInterface) {
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCatalogPromotion()->getCode()));
}
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCode()));
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ function it_dispatches_catalog_promotion_updated_after_writing_catalog_promotion
));
}

function it_dispatches_catalog_promotion_updated_after_writing_catalog_promotion_action(
MessageBusInterface $eventBus,
CatalogPromotionActionInterface $catalogPromotionAction,
CatalogPromotionInterface $catalogPromotion,
HttpKernelInterface $kernel,
Request $request
): void {
$request->getMethod()->willReturn(Request::METHOD_PUT);

$catalogPromotionAction->getCatalogPromotion()->willReturn($catalogPromotion);
$catalogPromotion->getCode()->willReturn('Winter_sale');

$message = new CatalogPromotionUpdated('Winter_sale');
$eventBus->dispatch($message)->willReturn(new Envelope($message))->shouldBeCalled();

$this->postWrite(new ViewEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
HttpKernelInterface::MASTER_REQUEST,
$catalogPromotionAction->getWrappedObject()
));
}

function it_does_nothing_after_writing_other_entity(
MessageBusInterface $eventBus,
HttpKernelInterface $kernel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
namespace Sylius\Bundle\CoreBundle\Listener;

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessorInterface;
use Sylius\Bundle\CoreBundle\Processor\AllCatalogPromotionsProcessorInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class CatalogPromotionUpdateListener
{
private CatalogPromotionReprocessorInterface $catalogPromotionReprocessor;
private AllCatalogPromotionsProcessorInterface $catalogPromotionsProcessor;

private RepositoryInterface $catalogPromotionRepository;

private EntityManagerInterface $entityManager;

public function __construct(
CatalogPromotionReprocessorInterface $catalogPromotionReprocessor,
AllCatalogPromotionsProcessorInterface $catalogPromotionsProcessor,
RepositoryInterface $catalogPromotionRepository,
EntityManagerInterface $entityManager
) {
$this->catalogPromotionReprocessor = $catalogPromotionReprocessor;
$this->catalogPromotionsProcessor = $catalogPromotionsProcessor;
$this->catalogPromotionRepository = $catalogPromotionRepository;
$this->entityManager = $entityManager;
}
Expand All @@ -42,7 +42,7 @@ public function __invoke(CatalogPromotionUpdated $event): void
return;
}

$this->catalogPromotionReprocessor->reprocess();
$this->catalogPromotionsProcessor->process();

$this->entityManager->flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sylius\Component\Resource\Repository\RepositoryInterface;

class CatalogPromotionReprocessor implements CatalogPromotionReprocessorInterface
final class AllCatalogPromotionsProcessor implements AllCatalogPromotionsProcessorInterface
{
private CatalogPromotionClearerInterface $catalogPromotionClearer;

Expand All @@ -33,7 +33,7 @@ public function __construct(
$this->catalogPromotionRepository = $catalogPromotionRepository;
}

public function reprocess(): void
public function process(): void
{
$this->catalogPromotionClearer->clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Bundle\CoreBundle\Processor;

interface CatalogPromotionReprocessorInterface
interface AllCatalogPromotionsProcessorInterface
{
public function reprocess(): void;
public function process(): void;
}
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@
</service>

<service
id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessorInterface"
class="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessor"
id="Sylius\Bundle\CoreBundle\Processor\AllCatalogPromotionsProcessor"
class="Sylius\Bundle\CoreBundle\Processor\AllCatalogPromotionsProcessor"
>
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
</service>

<service id="Sylius\Bundle\CoreBundle\Listener\CatalogPromotionUpdateListener">
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessorInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\AllCatalogPromotionsProcessor" />
<argument type="service" id="sylius.repository.catalog_promotion" />
<argument type="service" id="doctrine.orm.entity_manager" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessorInterface;
use Sylius\Bundle\CoreBundle\Processor\AllCatalogPromotionsProcessorInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class CatalogPromotionUpdateListenerSpec extends ObjectBehavior
{
function let(
CatalogPromotionReprocessorInterface $catalogPromotionReprocessor,
AllCatalogPromotionsProcessorInterface $catalogPromotionReprocessor,
RepositoryInterface $catalogPromotionRepository,
EntityManagerInterface $entityManager
): void {
Expand All @@ -38,28 +35,28 @@ function let(
}

function it_processes_catalog_promotion_that_has_just_been_updated(
CatalogPromotionReprocessorInterface $catalogPromotionReprocessor,
AllCatalogPromotionsProcessorInterface $catalogPromotionReprocessor,
EntityManagerInterface $entityManager,
RepositoryInterface $catalogPromotionRepository,
CatalogPromotionInterface $catalogPromotion
): void {
$catalogPromotionRepository->findOneBy(['code' => 'WINTER_MUGS_SALE'])->willReturn($catalogPromotion);

$catalogPromotionReprocessor->reprocess()->shouldBeCalled();
$catalogPromotionReprocessor->process()->shouldBeCalled();

$entityManager->flush()->shouldBeCalled();

$this->__invoke(new CatalogPromotionUpdated('WINTER_MUGS_SALE'));
}

function it_does_nothing_if_there_is_not_catalog_promotion_with_given_code(
CatalogPromotionReprocessorInterface $catalogPromotionReprocessor,
AllCatalogPromotionsProcessorInterface $catalogPromotionReprocessor,
RepositoryInterface $catalogPromotionRepository
): void {
$catalogPromotionRepository->findOneBy(['code' => 'WINTER_MUGS_SALE'])->willReturn(null);
$catalogPromotionRepository->findAll()->shouldNotBeCalled();

$catalogPromotionReprocessor->reprocess()->shouldNotBeCalled();
$catalogPromotionReprocessor->process()->shouldNotBeCalled();

$this->__invoke(new CatalogPromotionUpdated('WINTER_MUGS_SALE'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

class CatalogPromotionReprocessorSpec extends ObjectBehavior
final class AllCatalogPromotionsProcessorSpec extends ObjectBehavior
{
function let(
CatalogPromotionClearerInterface $catalogPromotionClearer,
Expand All @@ -33,7 +33,7 @@ function let(
);
}

function it_should_clear_and_calculate_catalog_promotions(
function it_clears_and_processes_catalog_promotions(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
RepositoryInterface $catalogPromotionRepository,
Expand All @@ -47,6 +47,6 @@ function it_should_clear_and_calculate_catalog_promotions(
$catalogPromotionProcessor->process($firstCatalogPromotion)->shouldBeCalled();
$catalogPromotionProcessor->process($secondCatalogPromotion)->shouldBeCalled();

$this->reprocess();
$this->process();
}
}

0 comments on commit 7d8b2f1

Please sign in to comment.