Add a validator interface, UpperRangeValidator and OkValidator.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
4430b04f11
commit
a4c0b3463b
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\ValidationInterface;
|
||||
|
||||
class OkValidator implements ValidationInterface {
|
||||
public function isValid(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\ValidationInterface;
|
||||
use App\Models\Validation\Validators\ValidationTrait;
|
||||
|
||||
class UpperRangeValidator implements ValidationInterface {
|
||||
private ValidationInterface $nextValidator;
|
||||
private int $value;
|
||||
private int $rangeLimit;
|
||||
|
||||
public function __construct(int $value, int $rangeLimit, ValidationInterface $nextValidator)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->rangeLimit = $rangeLimit;
|
||||
$this->nextValidator = $nextValidator;
|
||||
}
|
||||
|
||||
public function isCurrentValid(): bool
|
||||
{
|
||||
if($this->value > $this->rangeLimit)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->isCurrentValid() && $this->nextValidator->isValid();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
interface ValidationInterface {
|
||||
public function isValid(): bool;
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class OkValidatorTest extends TestCase
|
||||
{
|
||||
public function testIsValid(): void
|
||||
{
|
||||
$validator = new OkValidator();
|
||||
|
||||
$this->assertTrue($validator->isValid());
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class UpperRangeValidatorTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Valid Case for 10' => [
|
||||
'value' => 10,
|
||||
'limit' => 10,
|
||||
'isValid' => true,
|
||||
],
|
||||
'Valid Case for 255' => [
|
||||
'value' => 0,
|
||||
'limit' => 255,
|
||||
'isValid' => true,
|
||||
],
|
||||
'Invalid Case for 255' => [
|
||||
'value' => 11,
|
||||
'limit' => 10,
|
||||
'isValid' => false,
|
||||
],
|
||||
'Invalid Case for 255' => [
|
||||
'value' => 256,
|
||||
'limit' => 255,
|
||||
'isValid' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function setUpValidator(int $value, int $rangeLimit): UpperRangeValidator
|
||||
{
|
||||
return new UpperRangeValidator($value, $rangeLimit, new OkValidator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage(int $value, int $limit, bool $isValid): void
|
||||
{
|
||||
$validator = $this->setUpValidator($value, $limit);
|
||||
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue