From bdbf5d95782d9ace9f6a7c7606baf7ef6eab8eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kali=C5=84ski?= Date: Thu, 25 Jun 2026 13:31:50 +0200 Subject: [PATCH] Add unit tests for PerChannelPromotionActionCommand and PerChannelRuleChecker --- .../PerChannelPromotionActionCommandTest.php | 126 ++++++++++++++++++ .../Rule/PerChannelRuleCheckerTest.php | 99 ++++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 src/Sylius/Component/Core/tests/Promotion/Action/PerChannelPromotionActionCommandTest.php create mode 100644 src/Sylius/Component/Core/tests/Promotion/Checker/Rule/PerChannelRuleCheckerTest.php diff --git a/src/Sylius/Component/Core/tests/Promotion/Action/PerChannelPromotionActionCommandTest.php b/src/Sylius/Component/Core/tests/Promotion/Action/PerChannelPromotionActionCommandTest.php new file mode 100644 index 0000000000..b132cda400 --- /dev/null +++ b/src/Sylius/Component/Core/tests/Promotion/Action/PerChannelPromotionActionCommandTest.php @@ -0,0 +1,126 @@ +decorated = $this->createMock(PromotionActionCommandInterface::class); + $this->channel = $this->createMock(ChannelInterface::class); + $this->order = $this->createMock(OrderInterface::class); + $this->promotion = $this->createMock(PromotionInterface::class); + $this->command = new PerChannelPromotionActionCommand($this->decorated); + } + + public function testShouldImplementPromotionActionCommandInterface(): void + { + $this->assertInstanceOf(PromotionActionCommandInterface::class, $this->command); + } + + public function testShouldExecuteDecoratedCommandWithTheChannelConfiguration(): 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->once()) + ->method('execute') + ->with($this->order, ['percentage' => 0.2], $this->promotion) + ->willReturn(true) + ; + + $this->assertTrue($this->command->execute($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion)); + } + + public function testShouldNotExecuteDecoratedCommandIfThereIsNoConfigurationForTheOrderChannel(): 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_EU' => ['percentage' => 0.2]], $this->promotion)); + } + + public function testShouldNotExecuteDecoratedCommandWhenOrderHasNoChannel(): void + { + $this->order->expects($this->once())->method('getChannel')->willReturn(null); + $this->decorated->expects($this->never())->method('execute'); + + $this->assertFalse($this->command->execute($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion)); + } + + public function testShouldThrowExceptionWhenExecutingIfPromotionSubjectIsNotOrder(): void + { + $this->expectException(UnexpectedTypeException::class); + + $this->command->execute($this->createMock(PromotionSubjectInterface::class), [], $this->promotion); + } + + public function testShouldRevertDecoratedCommandWithTheChannelConfiguration(): 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->once()) + ->method('revert') + ->with($this->order, ['percentage' => 0.2], $this->promotion) + ; + + $this->command->revert($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion); + } + + public function testShouldNotRevertDecoratedCommandIfThereIsNoConfigurationForTheOrderChannel(): 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_EU' => ['percentage' => 0.2]], $this->promotion); + } + + public function testShouldNotRevertDecoratedCommandWhenOrderHasNoChannel(): void + { + $this->order->expects($this->once())->method('getChannel')->willReturn(null); + $this->decorated->expects($this->never())->method('revert'); + + $this->command->revert($this->order, ['WEB_US' => ['percentage' => 0.2]], $this->promotion); + } + + public function testShouldThrowExceptionWhenRevertingIfPromotionSubjectIsNotOrder(): void + { + $this->expectException(UnexpectedTypeException::class); + + $this->command->revert($this->createMock(PromotionSubjectInterface::class), [], $this->promotion); + } +} diff --git a/src/Sylius/Component/Core/tests/Promotion/Checker/Rule/PerChannelRuleCheckerTest.php b/src/Sylius/Component/Core/tests/Promotion/Checker/Rule/PerChannelRuleCheckerTest.php new file mode 100644 index 0000000000..2729374de0 --- /dev/null +++ b/src/Sylius/Component/Core/tests/Promotion/Checker/Rule/PerChannelRuleCheckerTest.php @@ -0,0 +1,99 @@ +decorated = $this->createMock(RuleCheckerInterface::class); + $this->channel = $this->createMock(ChannelInterface::class); + $this->order = $this->createMock(OrderInterface::class); + $this->ruleChecker = new PerChannelRuleChecker($this->decorated); + } + + public function testShouldImplementRuleCheckerInterface(): void + { + $this->assertInstanceOf(RuleCheckerInterface::class, $this->ruleChecker); + } + + public function testShouldDelegateToDecoratedCheckerWithTheChannelConfiguration(): 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->once()) + ->method('isEligible') + ->with($this->order, ['nth' => 5]) + ->willReturn(true) + ; + + $this->assertTrue($this->ruleChecker->isEligible($this->order, ['WEB_US' => ['nth' => 5]])); + } + + public function testShouldReturnDecoratedCheckerResultForTheOrderChannel(): 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->once()) + ->method('isEligible') + ->with($this->order, ['nth' => 5]) + ->willReturn(false) + ; + + $this->assertFalse($this->ruleChecker->isEligible($this->order, ['WEB_US' => ['nth' => 5]])); + } + + public function testShouldReturnFalseAndNotDelegateIfThereIsNoConfigurationForTheOrderChannel(): 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_EU' => ['nth' => 5]])); + } + + public function testShouldReturnFalseAndNotDelegateWhenOrderHasNoChannel(): void + { + $this->order->expects($this->once())->method('getChannel')->willReturn(null); + $this->decorated->expects($this->never())->method('isEligible'); + + $this->assertFalse($this->ruleChecker->isEligible($this->order, ['WEB_US' => ['nth' => 5]])); + } + + public function testShouldThrowExceptionIfPromotionSubjectIsNotOrder(): void + { + $this->expectException(UnsupportedTypeException::class); + + $this->ruleChecker->isEligible($this->createMock(PromotionSubjectInterface::class), []); + } +}