mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-25 08:21:11 -06:00
Add test for ShortUrlRedirectResolver rule matching
This commit is contained in:
parent
175712d4a9
commit
07ae92943d
@ -2,7 +2,9 @@
|
||||
|
||||
namespace RedirectRule;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
@ -10,6 +12,8 @@ use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
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\ShortUrlRedirectionResolver;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
||||
@ -30,8 +34,11 @@ class ShortUrlRedirectionResolverTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideData')]
|
||||
public function resolveLongUrlReturnsExpectedValue(ServerRequestInterface $request, string $expectedUrl): void
|
||||
{
|
||||
public function resolveLongUrlReturnsExpectedValue(
|
||||
ServerRequestInterface $request,
|
||||
?RedirectCondition $condition,
|
||||
string $expectedUrl,
|
||||
): void {
|
||||
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
|
||||
'longUrl' => 'https://example.com/foo/bar',
|
||||
'deviceLongUrls' => [
|
||||
@ -40,6 +47,16 @@ class ShortUrlRedirectionResolverTest extends TestCase
|
||||
],
|
||||
]));
|
||||
|
||||
$repo = $this->createMock(EntityRepository::class);
|
||||
$repo->expects($this->once())->method('findBy')->willReturn($condition !== null ? [
|
||||
new ShortUrlRedirectRule($shortUrl, 1, 'https://example.com/from-rule', new ArrayCollection([
|
||||
$condition,
|
||||
])),
|
||||
] : []);
|
||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrlRedirectRule::class)->willReturn(
|
||||
$repo,
|
||||
);
|
||||
|
||||
$result = $this->resolver->resolveLongUrl($shortUrl, $request);
|
||||
|
||||
self::assertEquals($expectedUrl, $result);
|
||||
@ -52,9 +69,27 @@ class ShortUrlRedirectionResolverTest extends TestCase
|
||||
$userAgent,
|
||||
);
|
||||
|
||||
yield 'unknown user agent' => [$request('Unknown'), 'https://example.com/foo/bar'];
|
||||
yield 'desktop user agent' => [$request(DESKTOP_USER_AGENT), 'https://example.com/foo/bar'];
|
||||
yield 'android user agent' => [$request(ANDROID_USER_AGENT), 'https://example.com/android'];
|
||||
yield 'ios user agent' => [$request(IOS_USER_AGENT), 'https://example.com/ios'];
|
||||
yield 'unknown user agent' => [
|
||||
$request('Unknown'), // This user agent won't match any device
|
||||
RedirectCondition::forLanguage('es-ES'), // This condition won't match
|
||||
'https://example.com/foo/bar',
|
||||
];
|
||||
yield 'desktop user agent' => [$request(DESKTOP_USER_AGENT), null, 'https://example.com/foo/bar'];
|
||||
yield 'android user agent' => [
|
||||
$request(ANDROID_USER_AGENT),
|
||||
RedirectCondition::forQueryParam('foo', 'bar'), // This condition won't match
|
||||
'https://example.com/android',
|
||||
];
|
||||
yield 'ios user agent' => [$request(IOS_USER_AGENT), null, 'https://example.com/ios'];
|
||||
yield 'matching language' => [
|
||||
$request()->withHeader('Accept-Language', 'es-ES'),
|
||||
RedirectCondition::forLanguage('es-ES'),
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
yield 'matching query params' => [
|
||||
$request()->withQueryParams(['foo' => 'bar']),
|
||||
RedirectCondition::forQueryParam('foo', 'bar'),
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user