Enforced short URLs length to be 4 at least

This commit is contained in:
Alejandro Celaya 2020-02-18 18:57:24 +01:00
parent 13555366e3
commit 9372d1739a
3 changed files with 7 additions and 4 deletions

View File

@ -43,7 +43,7 @@ class ShortUrlMetaInputFilter extends InputFilter
$this->add($customSlug); $this->add($customSlug);
$this->add($this->createPositiveNumberInput(self::MAX_VISITS)); $this->add($this->createPositiveNumberInput(self::MAX_VISITS));
$this->add($this->createPositiveNumberInput(self::SHORT_CODE_LENGTH)); $this->add($this->createPositiveNumberInput(self::SHORT_CODE_LENGTH, 4));
$this->add($this->createBooleanInput(self::FIND_IF_EXISTS, false)); $this->add($this->createBooleanInput(self::FIND_IF_EXISTS, false));
@ -52,11 +52,11 @@ class ShortUrlMetaInputFilter extends InputFilter
$this->add($domain); $this->add($domain);
} }
private function createPositiveNumberInput(string $name): Input private function createPositiveNumberInput(string $name, int $min = 1): Input
{ {
$input = $this->createInput($name, false); $input = $this->createInput($name, false);
$input->getValidatorChain()->attach(new Validator\Digits()) $input->getValidatorChain()->attach(new Validator\Digits())
->attach(new Validator\GreaterThan(['min' => 1, 'inclusive' => true])); ->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true]));
return $input; return $input;
} }

View File

@ -72,6 +72,6 @@ class ShortUrlTest extends TestCase
public function provideLengths(): iterable public function provideLengths(): iterable
{ {
yield [null, DEFAULT_SHORT_CODES_LENGTH]; yield [null, DEFAULT_SHORT_CODES_LENGTH];
yield from map(range(1, 10), fn (int $value) => [$value, $value]); yield from map(range(4, 10), fn (int $value) => [$value, $value]);
} }
} }

View File

@ -44,6 +44,9 @@ class ShortUrlMetaTest extends TestCase
ShortUrlMetaInputFilter::VALID_UNTIL => 500, ShortUrlMetaInputFilter::VALID_UNTIL => 500,
ShortUrlMetaInputFilter::DOMAIN => 4, ShortUrlMetaInputFilter::DOMAIN => 4,
]]; ]];
yield [[
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 3,
]];
} }
/** @test */ /** @test */