From a72e22e046c76e33bcac7dc046107700285b11ad Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 27 Feb 2024 19:22:30 +0100 Subject: [PATCH] Unit-test RedirectCondition for devices --- .../Entity/RedirectConditionTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/module/Core/test/RedirectRule/Entity/RedirectConditionTest.php b/module/Core/test/RedirectRule/Entity/RedirectConditionTest.php index da1e28e8..1eb7e0f8 100644 --- a/module/Core/test/RedirectRule/Entity/RedirectConditionTest.php +++ b/module/Core/test/RedirectRule/Entity/RedirectConditionTest.php @@ -7,8 +7,13 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; +use Shlinkio\Shlink\Core\Model\DeviceType; use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition; +use const ShlinkioTest\Shlink\ANDROID_USER_AGENT; +use const ShlinkioTest\Shlink\DESKTOP_USER_AGENT; +use const ShlinkioTest\Shlink\IOS_USER_AGENT; + class RedirectConditionTest extends TestCase { #[Test] @@ -47,6 +52,26 @@ class RedirectConditionTest extends TestCase self::assertEquals($expected, $result); } + #[Test] + #[TestWith([null, DeviceType::ANDROID, false])] + #[TestWith(['unknown', DeviceType::ANDROID, false])] + #[TestWith([ANDROID_USER_AGENT, DeviceType::ANDROID, true])] + #[TestWith([DESKTOP_USER_AGENT, DeviceType::DESKTOP, true])] + #[TestWith([IOS_USER_AGENT, DeviceType::IOS, true])] + #[TestWith([IOS_USER_AGENT, DeviceType::ANDROID, false])] + #[TestWith([DESKTOP_USER_AGENT, DeviceType::IOS, false])] + public function matchesDevice(?string $userAgent, DeviceType $value, bool $expected): void + { + $request = ServerRequestFactory::fromGlobals(); + if ($userAgent !== null) { + $request = $request->withHeader('User-Agent', $userAgent); + } + + $result = RedirectCondition::forDevice($value)->matchesRequest($request); + + self::assertEquals($expected, $result); + } + #[Test, DataProvider('provideNames')] public function generatesExpectedName(RedirectCondition $condition, string $expectedName): void { @@ -59,5 +84,8 @@ class RedirectConditionTest extends TestCase yield [RedirectCondition::forLanguage('en_UK'), 'language-en_UK']; yield [RedirectCondition::forQueryParam('foo', 'bar'), 'query-foo-bar']; yield [RedirectCondition::forQueryParam('baz', 'foo'), 'query-baz-foo']; + yield [RedirectCondition::forDevice(DeviceType::ANDROID), 'device-android']; + yield [RedirectCondition::forDevice(DeviceType::IOS), 'device-ios']; + yield [RedirectCondition::forDevice(DeviceType::DESKTOP), 'device-desktop']; } }