mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-26 02:40:41 -06:00
Add test for ShortUrlRedirectRule request matching
This commit is contained in:
parent
3f1b253c31
commit
175712d4a9
@ -28,7 +28,7 @@ class ShortUrlRedirectRule extends AbstractEntity
|
|||||||
*/
|
*/
|
||||||
public function matchesRequest(ServerRequestInterface $request): bool
|
public function matchesRequest(ServerRequestInterface $request): bool
|
||||||
{
|
{
|
||||||
return every(
|
return $this->conditions->count() > 0 && every(
|
||||||
$this->conditions,
|
$this->conditions,
|
||||||
static fn (RedirectCondition $condition) => $condition->matchesRequest($request),
|
static fn (RedirectCondition $condition) => $condition->matchesRequest($request),
|
||||||
);
|
);
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace RedirectRule\Entity;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
|
||||||
|
use Shlinkio\Shlink\Core\RedirectRule\Entity\ShortUrlRedirectRule;
|
||||||
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
|
|
||||||
|
class ShortUrlRedirectRuleTest extends TestCase
|
||||||
|
{
|
||||||
|
#[Test, DataProvider('provideConditions')]
|
||||||
|
public function matchesRequestIfAllConditionsMatch(array $conditions, bool $expectedResult): void
|
||||||
|
{
|
||||||
|
$request = ServerRequestFactory::fromGlobals()
|
||||||
|
->withHeader('Accept-Language', 'en-UK')
|
||||||
|
->withQueryParams(['foo' => 'bar']);
|
||||||
|
|
||||||
|
$result = $this->createRule($conditions)->matchesRequest($request);
|
||||||
|
|
||||||
|
self::assertEquals($expectedResult, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideConditions(): iterable
|
||||||
|
{
|
||||||
|
yield 'no conditions' => [[], false];
|
||||||
|
yield 'not all conditions match' => [
|
||||||
|
[RedirectCondition::forLanguage('en-UK'), RedirectCondition::forQueryParam('foo', 'foo')],
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
yield 'all conditions match' => [
|
||||||
|
[RedirectCondition::forLanguage('en-UK'), RedirectCondition::forQueryParam('foo', 'bar')],
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param RedirectCondition[] $conditions
|
||||||
|
*/
|
||||||
|
private function createRule(array $conditions): ShortUrlRedirectRule
|
||||||
|
{
|
||||||
|
$shortUrl = ShortUrl::withLongUrl('https://s.test');
|
||||||
|
return new ShortUrlRedirectRule($shortUrl, 1, '', new ArrayCollection($conditions));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user