mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Add tests for ShortUrlRedirectionRuleService::setRulesForShortUrl
This commit is contained in:
@@ -14,18 +14,13 @@ class RedirectRulesDataTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
#[TestWith([['redirectRules' => ['foo']]])]
|
||||
#[TestWith([['redirectRules' => [
|
||||
['priority' => 'foo'],
|
||||
]]])]
|
||||
#[TestWith([['redirectRules' => [
|
||||
[
|
||||
'priority' => 4,
|
||||
'longUrl' => 34,
|
||||
],
|
||||
]]])]
|
||||
#[TestWith([['redirectRules' => [
|
||||
[
|
||||
'priority' => 4,
|
||||
'longUrl' => 'https://example.com',
|
||||
'conditions' => [
|
||||
[
|
||||
@@ -36,7 +31,6 @@ class RedirectRulesDataTest extends TestCase
|
||||
]]])]
|
||||
#[TestWith([['redirectRules' => [
|
||||
[
|
||||
'priority' => 4,
|
||||
'longUrl' => 'https://example.com',
|
||||
'conditions' => [
|
||||
[
|
||||
@@ -49,7 +43,6 @@ class RedirectRulesDataTest extends TestCase
|
||||
]]])]
|
||||
#[TestWith([['redirectRules' => [
|
||||
[
|
||||
'priority' => 4,
|
||||
'longUrl' => 'https://example.com',
|
||||
'conditions' => [
|
||||
[
|
||||
|
||||
@@ -11,6 +11,9 @@ 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\Model\RedirectConditionType;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectRulesData;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\Model\Validation\RedirectRulesInputFilter;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleService;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
|
||||
@@ -26,7 +29,7 @@ class ShortUrlRedirectRuleServiceTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function delegatesToRepository(): void
|
||||
public function rulesForShortUrlDelegatesToRepository(): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://shlink.io');
|
||||
$rules = [
|
||||
@@ -52,4 +55,81 @@ class ShortUrlRedirectRuleServiceTest extends TestCase
|
||||
|
||||
self::assertSame($rules, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function setRulesForShortUrlParsesProvidedData(): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://example.com');
|
||||
$data = RedirectRulesData::fromRawData([
|
||||
RedirectRulesInputFilter::REDIRECT_RULES => [
|
||||
[
|
||||
RedirectRulesInputFilter::RULE_LONG_URL => 'https://example.com/first',
|
||||
RedirectRulesInputFilter::RULE_CONDITIONS => [
|
||||
[
|
||||
RedirectRulesInputFilter::CONDITION_TYPE => RedirectConditionType::DEVICE->value,
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_KEY => null,
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_VALUE => DeviceType::ANDROID->value,
|
||||
],
|
||||
[
|
||||
RedirectRulesInputFilter::CONDITION_TYPE => RedirectConditionType::QUERY_PARAM->value,
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_KEY => 'foo',
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_VALUE => 'bar',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
RedirectRulesInputFilter::RULE_LONG_URL => 'https://example.com/second',
|
||||
RedirectRulesInputFilter::RULE_CONDITIONS => [
|
||||
[
|
||||
RedirectRulesInputFilter::CONDITION_TYPE => RedirectConditionType::DEVICE->value,
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_KEY => null,
|
||||
RedirectRulesInputFilter::CONDITION_MATCH_VALUE => DeviceType::IOS->value,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->em->expects($this->once())->method('wrapInTransaction')->willReturnCallback(
|
||||
fn (callable $callback) => $callback(),
|
||||
);
|
||||
$this->em->expects($this->exactly(2))->method('persist');
|
||||
$this->em->expects($this->never())->method('remove');
|
||||
|
||||
$result = $this->ruleService->setRulesForShortUrl($shortUrl, $data);
|
||||
|
||||
self::assertCount(2, $result);
|
||||
self::assertInstanceOf(ShortUrlRedirectRule::class, $result[0]);
|
||||
self::assertInstanceOf(ShortUrlRedirectRule::class, $result[1]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function setRulesForShortUrlRemovesOldRules(): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://example.com');
|
||||
$data = RedirectRulesData::fromRawData([
|
||||
RedirectRulesInputFilter::REDIRECT_RULES => [],
|
||||
]);
|
||||
|
||||
$repo = $this->createMock(EntityRepository::class);
|
||||
$repo->expects($this->once())->method('findBy')->with(
|
||||
['shortUrl' => $shortUrl],
|
||||
['priority' => 'ASC'],
|
||||
)->willReturn([
|
||||
new ShortUrlRedirectRule($shortUrl, 1, 'https://example.com'),
|
||||
new ShortUrlRedirectRule($shortUrl, 2, 'https://example.com'),
|
||||
]);
|
||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrlRedirectRule::class)->willReturn(
|
||||
$repo,
|
||||
);
|
||||
$this->em->expects($this->once())->method('wrapInTransaction')->willReturnCallback(
|
||||
fn (callable $callback) => $callback(),
|
||||
);
|
||||
$this->em->expects($this->never())->method('persist');
|
||||
$this->em->expects($this->exactly(2))->method('remove');
|
||||
|
||||
$result = $this->ruleService->setRulesForShortUrl($shortUrl, $data);
|
||||
|
||||
self::assertCount(0, $result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user