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

[CatalogPromotions] Add fixtures for catalog promotions rules and actions #13055

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Fixture/CatalogPromotionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
->scalarNode('label')->cannotBeEmpty()->end()
->scalarNode('description')->cannotBeEmpty()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->arrayNode('rules')
->requiresAtLeastOneElement()
->arrayPrototype()
->children()
->scalarNode('type')->cannotBeEmpty()->end()
->scalarNode('catalogPromotion')->cannotBeEmpty()->end()
->variableNode('configuration')->end()
->end()
->end()
->end()
->arrayNode('actions')
->requiresAtLeastOneElement()
->arrayPrototype()
->children()
->scalarNode('type')->cannotBeEmpty()->end()
->scalarNode('catalogPromotion')->cannotBeEmpty()->end()
->variableNode('configuration')->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Factory;

use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class CatalogPromotionActionExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
{
private FactoryInterface $catalogPromotionActionFactory;

private OptionsResolver $optionsResolver;

public function __construct(FactoryInterface $catalogPromotionActionFactory)
{
$this->catalogPromotionActionFactory = $catalogPromotionActionFactory;

$this->optionsResolver = new OptionsResolver();

$this->configureOptions($this->optionsResolver);
}

public function create(array $options = []): CatalogPromotionActionInterface
{
$options = $this->optionsResolver->resolve($options);

/** @var CatalogPromotionActionInterface $catalogPromotionAction */
$catalogPromotionAction = $this->catalogPromotionActionFactory->createNew();
$catalogPromotionAction->setType($options['type']);
$catalogPromotionAction->setConfiguration($options['configuration']);

return $catalogPromotionAction;
}

protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('type', CatalogPromotionActionInterface::TYPE_PERCENTAGE_DISCOUNT)
->setAllowedTypes('type', 'string')
->setDefault('configuration', [])
Copy link
Member

Choose a reason for hiding this comment

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

I would be for setting some amount as a default configuration because now, the default action in invalid

->setAllowedTypes('configuration', 'array')
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionRuleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\OptionsResolver\Options;
Expand All @@ -32,18 +34,26 @@ class CatalogPromotionExampleFactory extends AbstractExampleFactory implements E

private ChannelRepositoryInterface $channelRepository;

private ExampleFactoryInterface $catalogPromotionRuleExampleFactory;

private ExampleFactoryInterface $catalogPromotionActionExampleFactory;

private Generator $faker;

private OptionsResolver $optionsResolver;

public function __construct(
FactoryInterface $catalogPromotionFactory,
RepositoryInterface $localeRepository,
ChannelRepositoryInterface $channelRepository
ChannelRepositoryInterface $channelRepository,
ExampleFactoryInterface $catalogPromotionRuleExampleFactory,
ExampleFactoryInterface $catalogPromotionActionExampleFactory
) {
$this->catalogPromotionFactory = $catalogPromotionFactory;
$this->localeRepository = $localeRepository;
$this->channelRepository = $channelRepository;
$this->catalogPromotionRuleExampleFactory = $catalogPromotionRuleExampleFactory;
$this->catalogPromotionActionExampleFactory = $catalogPromotionActionExampleFactory;

$this->faker = \Faker\Factory::create();
$this->optionsResolver = new OptionsResolver();
Expand Down Expand Up @@ -72,6 +82,24 @@ public function create(array $options = []): CatalogPromotionInterface
$catalogPromotion->addChannel($channel);
}

if (isset($options['rules'])) {
foreach ($options['rules'] as $rule) {
/** @var CatalogPromotionRuleInterface $catalogPromotionRule */
$catalogPromotionRule = $this->catalogPromotionRuleExampleFactory->create($rule);
$catalogPromotionRule->setCatalogPromotion($catalogPromotion);
$catalogPromotion->addRule($catalogPromotionRule);
}
}

if (isset($options['actions'])) {
foreach ($options['actions'] as $action) {
/** @var CatalogPromotionActionInterface $catalogPromotionAction */
$catalogPromotionAction = $this->catalogPromotionActionExampleFactory->create($action);
$catalogPromotionAction->setCatalogPromotion($catalogPromotion);
$catalogPromotion->addAction($catalogPromotionAction);
}
}

return $catalogPromotion;
}

Expand Down Expand Up @@ -103,6 +131,22 @@ protected function configureOptions(OptionsResolver $resolver): void
->setDefault('channels', LazyOption::all($this->channelRepository))
->setAllowedTypes('channels', 'array')
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
->setDefined('rules')
->setNormalizer('rules', function (Options $options, array $rules): array {
if (empty($rules)) {
return [[]];
}

return $rules;
})
->setDefined('actions')
->setNormalizer('actions', function (Options $options, array $actions): array {
if (empty($actions)) {
return [[]];
}

return $actions;
})
;
}

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

use Sylius\Component\Core\Model\CatalogPromotionRuleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class CatalogPromotionRuleExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
{
private FactoryInterface $catalogPromotionRuleFactory;

private OptionsResolver $optionsResolver;

public function __construct(FactoryInterface $catalogPromotionRuleFactory)
{
$this->catalogPromotionRuleFactory = $catalogPromotionRuleFactory;

$this->optionsResolver = new OptionsResolver();

$this->configureOptions($this->optionsResolver);
}

public function create(array $options = []): CatalogPromotionRuleInterface
{
$options = $this->optionsResolver->resolve($options);

/** @var CatalogPromotionRuleInterface $catalogPromotionRule */
$catalogPromotionRule = $this->catalogPromotionRuleFactory->createNew();
$catalogPromotionRule->setType($options['type']);
$catalogPromotionRule->setConfiguration($options['configuration']);

return $catalogPromotionRule;
}

protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('type', CatalogPromotionRuleInterface::TYPE_FOR_VARIANTS)
->setAllowedTypes('type', 'string')
->setDefault('configuration', [])
Copy link
Member

Choose a reason for hiding this comment

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

Similar as above

->setAllowedTypes('configuration', 'array')
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,12 @@ sylius_fixtures:
name: 'Winter sale'
channels:
- 'FASHION_WEB'
rules:
- type: 'for_variants'
configuration:
- 't_shirt_size_s'
- 't_shirt_size_l'
actions:
- type: 'percentage_discount'
configuration:
- amount: 0.5
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- amount: 0.5
amount: 0.5

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
<argument type="service" id="sylius.factory.catalog_promotion" />
<argument type="service" id="sylius.repository.locale" />
<argument type="service" id="sylius.repository.channel" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionRuleExampleFactory" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionActionExampleFactory" />
</service>

<service id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionRuleExampleFactory">
<argument type="service" id="sylius.factory.catalog_promotion_rule" />
</service>

<service id="Sylius\Bundle\CoreBundle\Fixture\Factory\CatalogPromotionActionExampleFactory">
<argument type="service" id="sylius.factory.catalog_promotion_action" />
</service>

<service id="sylius.fixture.example_factory.payment_method" class="Sylius\Bundle\CoreBundle\Fixture\Factory\PaymentMethodExampleFactory">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public function catalog_promotion_channels_can_be_set(): void
$this->assertConfigurationIsValid([['custom' => [['channels' => ['first_channel', 'second_channel']]]]], 'custom.*.channels');
}

/** @test */
public function catalog_promotion_rules_can_be_set(): void
{
$this->assertConfigurationIsValid([['custom' => [['rules' => [['type' => 'some_rule_type', 'configuration' => ['first_variant', 'second_variant']]]]]]], 'custom.*.rules');
}

/** @test */
public function catalog_promotion_actions_can_be_set(): void
{
$this->assertConfigurationIsValid([['custom' => [['actions' => [['type' => 'some_action_type', 'configuration' => ['amount' => 500]]]]]]], 'custom.*.actions');
}

protected function getConfiguration(): CatalogPromotionFixture
{
return new CatalogPromotionFixture(
Expand Down