Add API test for redirect rules list

This commit is contained in:
Alejandro Celaya 2024-02-28 08:53:35 +01:00
parent c4805b8152
commit 67bafbe44e
2 changed files with 131 additions and 26 deletions

View file

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
use function sprintf;
class ListRedirectRulesTest extends ApiTestCase
{
private const LANGUAGE_EN_CONDITION = [
'name' => 'language-en',
'type' => 'language',
'matchKey' => null,
'matchValue' => 'en',
];
private const QUERY_FOO_BAR_CONDITION = [
'name' => 'query-foo-bar',
'type' => 'query',
'matchKey' => 'foo',
'matchValue' => 'bar',
];
#[Test]
public function errorIsReturnedWhenInvalidUrlIsFetched(): void
{
$response = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid/redirect-rules');
$payload = $this->getJsonResponsePayload($response);
self::assertEquals(404, $response->getStatusCode());
self::assertEquals(404, $payload['status']);
self::assertEquals('invalid', $payload['shortCode']);
self::assertEquals('No URL found with short code "invalid"', $payload['detail']);
self::assertEquals('Short URL not found', $payload['title']);
self::assertEquals('https://shlink.io/api/error/short-url-not-found', $payload['type']);
}
#[Test]
#[TestWith(['abc123', []])]
#[TestWith(['def456', [
[
'longUrl' => 'https://example.com/english-and-foo-query',
'priority' => 1,
'conditions' => [
self::LANGUAGE_EN_CONDITION,
self::QUERY_FOO_BAR_CONDITION,
],
],
[
'longUrl' => 'https://example.com/multiple-query-params',
'priority' => 2,
'conditions' => [
self::QUERY_FOO_BAR_CONDITION,
[
'name' => 'query-hello-world',
'type' => 'query',
'matchKey' => 'hello',
'matchValue' => 'world',
],
],
],
[
'longUrl' => 'https://example.com/only-english',
'priority' => 3,
'conditions' => [self::LANGUAGE_EN_CONDITION],
],
[
'longUrl' => 'https://blog.alejandrocelaya.com/android',
'priority' => 4,
'conditions' => [
[
'name' => 'device-android',
'type' => 'device',
'matchKey' => null,
'matchValue' => 'android',
],
],
],
[
'longUrl' => 'https://blog.alejandrocelaya.com/ios',
'priority' => 5,
'conditions' => [
[
'name' => 'device-ios',
'type' => 'device',
'matchKey' => null,
'matchValue' => 'ios',
],
],
],
]])]
public function returnsListOfRulesForShortUrl(string $shortCode, array $expectedRules): void
{
$response = $this->callApiWithKey(self::METHOD_GET, sprintf('/short-urls/%s/redirect-rules', $shortCode));
$payload = $this->getJsonResponsePayload($response);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals($expectedRules, $payload['redirectRules']);
}
}

View file

@ -40,43 +40,44 @@ class ShortUrlRedirectRulesFixture extends AbstractFixture implements DependentF
$iosCondition = RedirectCondition::forDevice(DeviceType::IOS);
$manager->persist($iosCondition);
$englishAndFooQueryRule = new ShortUrlRedirectRule(
$defShortUrl,
1,
'https://example.com/english-and-foo-query',
new ArrayCollection([$englishCondition, $fooQueryCondition]),
);
$manager->persist($englishAndFooQueryRule);
// Create rules disordered to make sure the order by priority works
$multipleQueryParamsRule = new ShortUrlRedirectRule(
$defShortUrl,
2,
'https://example.com/multiple-query-params',
new ArrayCollection([$helloQueryCondition, $fooQueryCondition]),
shortUrl: $defShortUrl,
priority: 2,
longUrl: 'https://example.com/multiple-query-params',
conditions: new ArrayCollection([$helloQueryCondition, $fooQueryCondition]),
);
$manager->persist($multipleQueryParamsRule);
$onlyEnglishRule = new ShortUrlRedirectRule(
$defShortUrl,
3,
'https://example.com/only-english',
new ArrayCollection([$englishCondition]),
$englishAndFooQueryRule = new ShortUrlRedirectRule(
shortUrl: $defShortUrl,
priority: 1,
longUrl: 'https://example.com/english-and-foo-query',
conditions: new ArrayCollection([$englishCondition, $fooQueryCondition]),
);
$manager->persist($onlyEnglishRule);
$manager->persist($englishAndFooQueryRule);
$androidRule = new ShortUrlRedirectRule(
$defShortUrl,
4,
'https://blog.alejandrocelaya.com/android',
new ArrayCollection([$androidCondition]),
shortUrl: $defShortUrl,
priority: 4,
longUrl: 'https://blog.alejandrocelaya.com/android',
conditions: new ArrayCollection([$androidCondition]),
);
$manager->persist($androidRule);
$onlyEnglishRule = new ShortUrlRedirectRule(
shortUrl: $defShortUrl,
priority: 3,
longUrl: 'https://example.com/only-english',
conditions: new ArrayCollection([$englishCondition]),
);
$manager->persist($onlyEnglishRule);
$iosRule = new ShortUrlRedirectRule(
$defShortUrl,
5,
'https://blog.alejandrocelaya.com/ios',
new ArrayCollection([$iosCondition]),
shortUrl: $defShortUrl,
priority: 5,
longUrl: 'https://blog.alejandrocelaya.com/ios',
conditions: new ArrayCollection([$iosCondition]),
);
$manager->persist($iosRule);