Skip to content

Commit

Permalink
[Catalog Promotion] Move catalog promotion processing after the fixtu…
Browse files Browse the repository at this point in the history
…re execution
  • Loading branch information
lchrusciel committed Feb 3, 2022
1 parent 66ab195 commit ee3a2da
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class CatalogPromotionExampleFactory extends AbstractExampleFactory implements E

private ExampleFactoryInterface $catalogPromotionActionExampleFactory;

private AllProductVariantsCatalogPromotionsProcessorInterface $allProductVariantsCatalogPromotionsProcessor;

private Generator $faker;

private OptionsResolver $optionsResolver;
Expand All @@ -51,15 +49,13 @@ public function __construct(
RepositoryInterface $localeRepository,
ChannelRepositoryInterface $channelRepository,
ExampleFactoryInterface $catalogPromotionScopeExampleFactory,
ExampleFactoryInterface $catalogPromotionActionExampleFactory,
AllProductVariantsCatalogPromotionsProcessorInterface $allProductVariantsCatalogPromotionsProcessor
ExampleFactoryInterface $catalogPromotionActionExampleFactory
) {
$this->catalogPromotionFactory = $catalogPromotionFactory;
$this->localeRepository = $localeRepository;
$this->channelRepository = $channelRepository;
$this->catalogPromotionScopeExampleFactory = $catalogPromotionScopeExampleFactory;
$this->catalogPromotionActionExampleFactory = $catalogPromotionActionExampleFactory;
$this->allProductVariantsCatalogPromotionsProcessor = $allProductVariantsCatalogPromotionsProcessor;
$this->faker = Factory::create();
$this->optionsResolver = new OptionsResolver();

Expand Down Expand Up @@ -110,8 +106,6 @@ public function create(array $options = []): CatalogPromotionInterface
}
}

$this->allProductVariantsCatalogPromotionsProcessor->process();

return $catalogPromotion;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Fixture\Listener;

use Sylius\Bundle\CoreBundle\Fixture\CatalogPromotionFixture;
use Sylius\Bundle\CoreBundle\Processor\AllProductVariantsCatalogPromotionsProcessorInterface;
use Sylius\Bundle\FixturesBundle\Listener\AbstractListener;
use Sylius\Bundle\FixturesBundle\Listener\AfterFixtureListenerInterface;
use Sylius\Bundle\FixturesBundle\Listener\FixtureEvent;

final class CatalogPromotionExecutorListener extends AbstractListener implements AfterFixtureListenerInterface
{
public function __construct(private AllProductVariantsCatalogPromotionsProcessorInterface $allCatalogPromotionsProcessor)
{
}

public function afterFixture(FixtureEvent $fixtureEvent, array $options): void
{
if ($fixtureEvent->fixture() instanceof CatalogPromotionFixture) {
$this->allCatalogPromotionsProcessor->process();
}
}

public function getName(): string
{
return 'catalog_promotion_processor_executor';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
sylius_fixtures:
suites:
default:
listeners:
catalog_promotion_processor_executor: ~
fixtures:
promotion:
options:
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<import resource="services/dashboard.xml" />
<import resource="services/emails.xml" />
<import resource="services/fixtures.xml" />
<import resource="services/fixtures_listeners.xml" />
<import resource="services/fixtures_factories.xml" />
<import resource="services/form.xml" />
<import resource="services/installer.xml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<argument type="service" id="sylius.repository.channel" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionScopeExampleFactory" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionActionExampleFactory" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\AllProductVariantsCatalogPromotionsProcessorInterface" />
</service>

<service id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionScopeExampleFactory">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="true" />

<service id="sylius_fixtures.listener.catalog_promotion_executor" class="Sylius\Bundle\CoreBundle\Fixture\Listener\CatalogPromotionExecutorListener" public="false">
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\AllProductVariantsCatalogPromotionsProcessorInterface" />
<tag name="sylius_fixtures.listener" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 spec\Sylius\Bundle\CoreBundle\Fixture\Listener;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\CoreBundle\Fixture\CatalogPromotionFixture;
use Sylius\Bundle\CoreBundle\Processor\AllProductVariantsCatalogPromotionsProcessorInterface;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Bundle\FixturesBundle\Listener\AfterFixtureListenerInterface;
use Sylius\Bundle\FixturesBundle\Listener\FixtureEvent;
use Sylius\Bundle\FixturesBundle\Listener\ListenerInterface;
use Sylius\Bundle\FixturesBundle\Suite\SuiteInterface;

final class CatalogPromotionExecutorListenerSpec extends ObjectBehavior
{
function let(AllProductVariantsCatalogPromotionsProcessorInterface $allCatalogPromotionsProcessor): void
{
$this->beConstructedWith($allCatalogPromotionsProcessor);
}

function it_implements_listener_interface(): void
{
$this->shouldImplement(ListenerInterface::class);
}

function it_listens_for_after_fixture_events(): void
{
$this->shouldImplement(AfterFixtureListenerInterface::class);
}

function it_triggers_catalog_promotion_processing_after_catalog_promotion_fixture_execution(
AllProductVariantsCatalogPromotionsProcessorInterface $allCatalogPromotionsProcessor,
SuiteInterface $suite,
CatalogPromotionFixture $catalogPromotionFixture
): void {
$this->afterFixture(new FixtureEvent($suite->getWrappedObject(), $catalogPromotionFixture->getWrappedObject(), []), []);

$allCatalogPromotionsProcessor->process()->shouldBeCalled();
}

function it_does_not_trigger_catalog_promotion_processing_after_any_other_fixture_execution(
AllProductVariantsCatalogPromotionsProcessorInterface $allCatalogPromotionsProcessor,
SuiteInterface $suite,
FixtureInterface $fixture
): void {
$this->afterFixture(new FixtureEvent($suite->getWrappedObject(), $fixture->getWrappedObject(), []), []);

$allCatalogPromotionsProcessor->process()->shouldNotBeCalled();
}
}

0 comments on commit ee3a2da

Please sign in to comment.