Add unit test for ListRedirectRulesAction

This commit is contained in:
Alejandro Celaya 2024-02-28 09:14:27 +01:00
parent 67bafbe44e
commit ab7824aa85
2 changed files with 60 additions and 2 deletions

View File

@ -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,
) {
}

View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\RedirectRule;
use Doctrine\Common\Collections\ArrayCollection;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Model\DeviceType;
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
use Shlinkio\Shlink\Core\RedirectRule\Entity\ShortUrlRedirectRule;
use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Rest\Action\RedirectRule\ListRedirectRulesAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ListRedirectRulesActionTest extends TestCase
{
private ShortUrlResolverInterface & MockObject $urlResolver;
private ShortUrlRedirectRuleServiceInterface & MockObject $ruleService;
private ListRedirectRulesAction $action;
protected function setUp(): void
{
$this->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);
}
}