diff --git a/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php b/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php new file mode 100644 index 00000000..79859d23 --- /dev/null +++ b/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php @@ -0,0 +1,95 @@ +shortUrlResolver = $this->createMock(ShortUrlResolverInterface::class); + $this->ruleService = $this->createMock(ShortUrlRedirectRuleServiceInterface::class); + $this->ruleHandler = $this->createMock(RedirectRuleHandlerInterface::class); + + $this->commandTester = CliTestUtils::testerForCommand(new ManageRedirectRulesCommand( + $this->shortUrlResolver, + $this->ruleService, + $this->ruleHandler, + )); + } + + #[Test] + public function errorIsReturnedIfShortUrlCannotBeFound(): void + { + $this->shortUrlResolver->expects($this->once())->method('resolveShortUrl')->with( + ShortUrlIdentifier::fromShortCodeAndDomain('foo'), + )->willThrowException(new ShortUrlNotFoundException('')); + $this->ruleService->expects($this->never())->method('rulesForShortUrl'); + $this->ruleService->expects($this->never())->method('saveRulesForShortUrl'); + $this->ruleHandler->expects($this->never())->method('manageRules'); + + $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $output = $this->commandTester->getDisplay(); + + self::assertEquals(ExitCode::EXIT_FAILURE, $exitCode); + self::assertStringContainsString('Short URL for foo not found', $output); + } + + #[Test] + public function savesNoRulesIfManageResultIsNull(): void + { + $shortUrl = ShortUrl::withLongUrl('https://example.com'); + + $this->shortUrlResolver->expects($this->once())->method('resolveShortUrl')->with( + ShortUrlIdentifier::fromShortCodeAndDomain('foo'), + )->willReturn($shortUrl); + $this->ruleService->expects($this->once())->method('rulesForShortUrl')->with($shortUrl)->willReturn([]); + $this->ruleHandler->expects($this->once())->method('manageRules')->willReturn(null); + $this->ruleService->expects($this->never())->method('saveRulesForShortUrl'); + + $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $output = $this->commandTester->getDisplay(); + + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + self::assertStringNotContainsString('Rules properly saved', $output); + } + + #[Test] + public function savesRulesIfManageResultIsAnArray(): void + { + $shortUrl = ShortUrl::withLongUrl('https://example.com'); + + $this->shortUrlResolver->expects($this->once())->method('resolveShortUrl')->with( + ShortUrlIdentifier::fromShortCodeAndDomain('foo'), + )->willReturn($shortUrl); + $this->ruleService->expects($this->once())->method('rulesForShortUrl')->with($shortUrl)->willReturn([]); + $this->ruleHandler->expects($this->once())->method('manageRules')->willReturn([]); + $this->ruleService->expects($this->once())->method('saveRulesForShortUrl')->with($shortUrl, []); + + $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $output = $this->commandTester->getDisplay(); + + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + self::assertStringContainsString('Rules properly saved', $output); + } +}