From 8f233221e5da9ba619e114501db083450f34e6bf Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 29 Feb 2024 20:14:15 +0100 Subject: [PATCH] Add SetRedirectRulesAction unit test --- .../Validation/RedirectRulesInputFilter.php | 2 +- .../SetRedirectRulesActionTest.php | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 module/Rest/test/Action/RedirectRule/SetRedirectRulesActionTest.php diff --git a/module/Core/src/RedirectRule/Model/Validation/RedirectRulesInputFilter.php b/module/Core/src/RedirectRule/Model/Validation/RedirectRulesInputFilter.php index 42b83c76..5decaf4c 100644 --- a/module/Core/src/RedirectRule/Model/Validation/RedirectRulesInputFilter.php +++ b/module/Core/src/RedirectRule/Model/Validation/RedirectRulesInputFilter.php @@ -48,7 +48,7 @@ class RedirectRulesInputFilter extends InputFilter $redirectRuleInputFilter = new InputFilter(); $longUrl = InputFactory::basic(self::RULE_LONG_URL, required: true); - $longUrl->setValidatorChain(ShortUrlInputFilter::longUrlValidators()); + $longUrl->getValidatorChain()->merge(ShortUrlInputFilter::longUrlValidators()); $redirectRuleInputFilter->add($longUrl); $conditionsInputFilter = new CollectionInputFilter(); diff --git a/module/Rest/test/Action/RedirectRule/SetRedirectRulesActionTest.php b/module/Rest/test/Action/RedirectRule/SetRedirectRulesActionTest.php new file mode 100644 index 00000000..e330839c --- /dev/null +++ b/module/Rest/test/Action/RedirectRule/SetRedirectRulesActionTest.php @@ -0,0 +1,58 @@ +urlResolver = $this->createMock(ShortUrlResolverInterface::class); + $this->ruleService = $this->createMock(ShortUrlRedirectRuleServiceInterface::class); + + $this->action = new SetRedirectRulesAction($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('setRulesForShortUrl')->willReturn($redirectRules); + + /** @var JsonResponse $response */ + $response = $this->action->handle($request); + $payload = $response->getPayload(); + + self::assertEquals([ + 'defaultLongUrl' => $shortUrl->getLongUrl(), + 'redirectRules' => $redirectRules, + ], $payload); + } +}