From 3bfb29a51c0b4e2cd2635a069cf96a7e9be3d8f4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 3 Mar 2024 08:47:31 +0100 Subject: [PATCH] Test new methods for RedirectCondition and ShortUrlRedirectRule --- .../Entity/ShortUrlRedirectRuleTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/module/Core/test/RedirectRule/Entity/ShortUrlRedirectRuleTest.php b/module/Core/test/RedirectRule/Entity/ShortUrlRedirectRuleTest.php index 19e431db..15df256f 100644 --- a/module/Core/test/RedirectRule/Entity/ShortUrlRedirectRuleTest.php +++ b/module/Core/test/RedirectRule/Entity/ShortUrlRedirectRuleTest.php @@ -6,9 +6,11 @@ use Doctrine\Common\Collections\ArrayCollection; use Laminas\Diactoros\ServerRequestFactory; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; 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\ShortUrl\Entity\ShortUrl; class ShortUrlRedirectRuleTest extends TestCase @@ -51,6 +53,39 @@ class ShortUrlRedirectRuleTest extends TestCase self::assertEmpty($conditions); } + #[Test, DataProvider('provideConditionMappingCallbacks')] + public function conditionsCanBeMapped(callable $callback, array $expectedResult): void + { + $conditions = new ArrayCollection( + [RedirectCondition::forLanguage('en-UK'), RedirectCondition::forQueryParam('foo', 'bar')], + ); + $rule = $this->createRule($conditions); + + $result = $rule->mapConditions($callback); + + self::assertEquals($expectedResult, $result); + } + + public static function provideConditionMappingCallbacks(): iterable + { + yield 'json-serialized conditions' => [fn (RedirectCondition $cond) => $cond->jsonSerialize(), [ + [ + 'type' => RedirectConditionType::LANGUAGE->value, + 'matchKey' => null, + 'matchValue' => 'en-UK', + ], + [ + 'type' => RedirectConditionType::QUERY_PARAM->value, + 'matchKey' => 'foo', + 'matchValue' => 'bar', + ], + ]]; + yield 'human-friendly conditions' => [fn (RedirectCondition $cond) => $cond->toHumanFriendly(), [ + 'en-UK language is accepted', + 'query string contains foo=bar', + ]]; + } + /** * @param ArrayCollection $conditions */