Validate channel-based promotion configuration and add tests

This commit is contained in:
Tomasz Kaliński 2026-06-25 17:35:08 +02:00
parent f40c4e5d7e
commit fc5ae5e69f
6 changed files with 32 additions and 5 deletions

View file

@ -38,7 +38,7 @@ final class PerChannelPromotionActionCommand implements PromotionActionCommandIn
}
$channelCode = $channel->getCode();
if (!isset($configuration[$channelCode])) {
if (!isset($configuration[$channelCode]) || !is_array($configuration[$channelCode])) {
return false;
}
@ -58,7 +58,7 @@ final class PerChannelPromotionActionCommand implements PromotionActionCommandIn
}
$channelCode = $channel->getCode();
if (!isset($configuration[$channelCode])) {
if (!isset($configuration[$channelCode]) || !is_array($configuration[$channelCode])) {
return;
}

View file

@ -41,7 +41,7 @@ final class PerChannelRuleChecker implements RuleCheckerInterface
}
$channelCode = $channel->getCode();
if (!isset($configuration[$channelCode])) {
if (!isset($configuration[$channelCode]) || !is_array($configuration[$channelCode])) {
return false;
}

View file

@ -81,6 +81,15 @@ final class PerChannelPromotionActionCommandTest extends TestCase
$this->assertFalse($this->command->execute($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion));
}
public function testShouldNotExecuteDecoratedCommandWhenChannelConfigurationIsNotAnArray(): void
{
$this->order->expects($this->once())->method('getChannel')->willReturn($this->channel);
$this->channel->expects($this->once())->method('getCode')->willReturn('WEB_US');
$this->decorated->expects($this->never())->method('execute');
$this->assertFalse($this->command->execute($this->order, ['WEB_US' => 'not-an-array'], $this->promotion));
}
public function testShouldThrowExceptionWhenExecutingIfPromotionSubjectIsNotOrder(): void
{
$this->expectException(UnexpectedTypeException::class);
@ -117,6 +126,15 @@ final class PerChannelPromotionActionCommandTest extends TestCase
$this->command->revert($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion);
}
public function testShouldNotRevertDecoratedCommandWhenChannelConfigurationIsNotAnArray(): void
{
$this->order->expects($this->once())->method('getChannel')->willReturn($this->channel);
$this->channel->expects($this->once())->method('getCode')->willReturn('WEB_US');
$this->decorated->expects($this->never())->method('revert');
$this->command->revert($this->order, ['WEB_US' => 'not-an-array'], $this->promotion);
}
public function testShouldThrowExceptionWhenRevertingIfPromotionSubjectIsNotOrder(): void
{
$this->expectException(UnexpectedTypeException::class);

View file

@ -90,6 +90,15 @@ final class PerChannelRuleCheckerTest extends TestCase
$this->assertFalse($this->ruleChecker->isEligible($this->order, ['WEB_US' => ['nth' => 5]]));
}
public function testShouldReturnFalseAndNotDelegateWhenChannelConfigurationIsNotAnArray(): void
{
$this->order->expects($this->once())->method('getChannel')->willReturn($this->channel);
$this->channel->expects($this->once())->method('getCode')->willReturn('WEB_US');
$this->decorated->expects($this->never())->method('isEligible');
$this->assertFalse($this->ruleChecker->isEligible($this->order, ['WEB_US' => 'not-an-array']));
}
public function testShouldThrowExceptionIfPromotionSubjectIsNotOrder(): void
{
$this->expectException(UnsupportedTypeException::class);

View file

@ -38,7 +38,7 @@
},
{
"propertyPath": "actions[5].type",
"message": "Promotion action type is invalid. Available action types are order_fixed_discount, unit_fixed_discount, order_percentage_discount, unit_percentage_discount, shipping_percentage_discount.",
"message": "Promotion action type is invalid. Available action types are order_fixed_discount, unit_fixed_discount, order_percentage_discount, order_percentage_discount_per_channel, unit_percentage_discount, shipping_percentage_discount, shipping_percentage_discount_per_channel.",
"code": null
}
],

View file

@ -53,7 +53,7 @@
},
{
"propertyPath": "rules[8].type",
"message": "Promotion rule type is invalid. Available rule types are customer_group, nth_order, shipping_country, has_taxon, total_of_items_from_taxon, contains_product, item_total, cart_quantity.",
"message": "Promotion rule type is invalid. Available rule types are customer_group, customer_group_per_channel, nth_order, nth_order_per_channel, shipping_country, shipping_country_per_channel, has_taxon, has_taxon_per_channel, total_of_items_from_taxon, contains_product, contains_product_per_channel, item_total, cart_quantity, cart_quantity_per_channel.",
"code": null
}
],