From ab7824aa85143267d227bc7f6b962579597246b0 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 28 Feb 2024 09:14:27 +0100 Subject: [PATCH] Add unit test for ListRedirectRulesAction --- .../RedirectRule/ListRedirectRulesAction.php | 4 +- .../ListRedirectRulesActionTest.php | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 module/Rest/test/Action/RedirectRule/ListRedirectRulesActionTest.php diff --git a/module/Rest/src/Action/RedirectRule/ListRedirectRulesAction.php b/module/Rest/src/Action/RedirectRule/ListRedirectRulesAction.php index c4d9754e..c6c12fd9 100644 --- a/module/Rest/src/Action/RedirectRule/ListRedirectRulesAction.php +++ b/module/Rest/src/Action/RedirectRule/ListRedirectRulesAction.php @@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Rest\Action\RedirectRule; use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleService; +use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleServiceInterface; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; @@ -18,7 +18,7 @@ class ListRedirectRulesAction extends AbstractRestAction public function __construct( private readonly ShortUrlResolverInterface $urlResolver, - private readonly ShortUrlRedirectRuleService $ruleService, + private readonly ShortUrlRedirectRuleServiceInterface $ruleService, ) { } diff --git a/module/Rest/test/Action/RedirectRule/ListRedirectRulesActionTest.php b/module/Rest/test/Action/RedirectRule/ListRedirectRulesActionTest.php new file mode 100644 index 00000000..d2e92240 --- /dev/null +++ b/module/Rest/test/Action/RedirectRule/ListRedirectRulesActionTest.php @@ -0,0 +1,58 @@ +urlResolver = $this->createMock(ShortUrlResolverInterface::class); + $this->ruleService = $this->createMock(ShortUrlRedirectRuleServiceInterface::class); + + $this->action = new ListRedirectRulesAction($this->urlResolver, $this->ruleService); + } + + #[Test] + public function requestIsHandledAndRulesAreReturned(): void + { + $shortUrl = ShortUrl::withLongUrl('https://example.com'); + $request = ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, ApiKey::create()); + $conditions = [RedirectCondition::forDevice(DeviceType::ANDROID), RedirectCondition::forLanguage('en-US')]; + $redirectRules = [ + new ShortUrlRedirectRule($shortUrl, 1, 'https://example.com/rule', new ArrayCollection($conditions)), + ]; + + $this->urlResolver->expects($this->once())->method('resolveShortUrl')->willReturn($shortUrl); + $this->ruleService->expects($this->once())->method('rulesForShortUrl')->willReturn($redirectRules); + + /** @var JsonResponse $response */ + $response = $this->action->handle($request); + $payload = $response->getPayload(); + + self::assertEquals([ + 'defaultLongUrl' => $shortUrl->getLongUrl(), + 'redirectRules' => $redirectRules, + ], $payload); + } +}