-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented text length range validation rule
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\Validation\Rule\Text; | ||
|
||
use Amp\Promise; | ||
use Amp\Success; | ||
use HarmonyIO\Validation\Exception\InvalidNumericalRange; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
use function Amp\call; | ||
|
||
final class LengthRange implements Rule | ||
{ | ||
/** @var int */ | ||
private $minimumLength; | ||
|
||
/** @var int */ | ||
private $maximumLength; | ||
|
||
public function __construct(int $minimumLength, int $maximumLength) | ||
{ | ||
if ($minimumLength > $maximumLength) { | ||
throw new InvalidNumericalRange($minimumLength, $maximumLength); | ||
} | ||
|
||
$this->minimumLength = $minimumLength; | ||
$this->maximumLength = $maximumLength; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value): Promise | ||
{ | ||
if (!is_string($value)) { | ||
return new Success(false); | ||
} | ||
|
||
return call(function () use ($value) { | ||
if (!yield (new MinimumLength($this->minimumLength))->validate($value)) { | ||
return false; | ||
} | ||
|
||
return (new MaximumLength($this->maximumLength))->validate($value); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\ValidationTest\Unit\Rule\Text; | ||
|
||
use HarmonyIO\PHPUnitExtension\TestCase; | ||
use HarmonyIO\Validation\Exception\InvalidNumericalRange; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
use HarmonyIO\Validation\Rule\Text\LengthRange; | ||
|
||
class LengthRangeTest extends TestCase | ||
{ | ||
public function testConstructorThrowsUpWhenMinimumLengthIsGreaterThanTheMaximumLength(): void | ||
{ | ||
$this->expectException(InvalidNumericalRange::class); | ||
$this->expectExceptionMessage('The minimum (`12`) can not be greater than the maximum (`10`).'); | ||
|
||
new LengthRange(12, 10); | ||
} | ||
|
||
public function testRuleImplementsInterface(): void | ||
{ | ||
$this->assertInstanceOf(Rule::class, new LengthRange(10, 12)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnInteger(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAFloat(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(1.1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingABoolean(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(true)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnArray(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate([])); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnObject(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(new \DateTimeImmutable())); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingNull(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(null)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAResource(): void | ||
{ | ||
$resource = fopen('php://memory', 'r'); | ||
|
||
if ($resource === false) { | ||
$this->fail('Could not open the memory stream used for the test'); | ||
|
||
return; | ||
} | ||
|
||
$this->assertFalse((new LengthRange(10, 12))->validate($resource)); | ||
|
||
fclose($resource); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingACallable(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate(static function (): void { | ||
})); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAStringSmallerThanTheMinimumLength(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate('€€€€€€€€€')); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAStringLargerThanTheMaximumLength(): void | ||
{ | ||
$this->assertFalse((new LengthRange(10, 12))->validate('€€€€€€€€€€€€€')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAStringLargerThanTheMinimumLengthAndSmallerThanMaximumLength(): void | ||
{ | ||
$this->assertTrue((new LengthRange(10, 12))->validate('€€€€€€€€€€€')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAStringWithExactlyTheMinimumLength(): void | ||
{ | ||
$this->assertTrue((new LengthRange(10, 12))->validate('€€€€€€€€€€')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAStringExactlyTheMaximumLength(): void | ||
{ | ||
$this->assertTrue((new LengthRange(10, 12))->validate('€€€€€€€€€€€€')); | ||
} | ||
} |