Add Informative Validators.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
da847273b8
commit
7662d98bce
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\Validators\Validator;
|
||||
|
||||
abstract class InformativeValidator extends Validator {
|
||||
protected Validator $validator;
|
||||
protected string $message;
|
||||
|
||||
public function isCurrentValid(): bool
|
||||
{
|
||||
return $this->validator->isCurrentValid();
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->validator->isValid();
|
||||
}
|
||||
|
||||
public function setValidator($validator): void
|
||||
{
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
public function setMessage($message): void
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
if(!$this->isCurrentValid())
|
||||
return $this->message;
|
||||
|
||||
return $this->nextValidator->getMessage();
|
||||
}
|
||||
|
||||
public function okStatus(): array
|
||||
{
|
||||
if(!$this->isCurrentValid())
|
||||
return ['error' => $this->message];
|
||||
|
||||
return $this->nextValidator->okStatus();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\Messages\InformativeValidator;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class OkInformativeValidator extends InformativeValidator {
|
||||
public function __construct($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->validator = new OkValidator();
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function okStatus(): array
|
||||
{
|
||||
return ['ok' => $this->getMessage()];
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\Messages\InformativeValidator;
|
||||
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||
|
||||
class UpperRangeInformativeValidator extends InformativeValidator {
|
||||
protected InformativeValidator $nextValidator;
|
||||
|
||||
public function __construct(string $message, UpperRangeValidator $validator, InformativeValidator $nextValidator)
|
||||
{
|
||||
$this->setMessage($message);
|
||||
$this->setValidator($validator);
|
||||
$this->nextValidator = $nextValidator;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Messages\OkInformativeValidator;
|
||||
|
||||
class OkInformativeValidatorTest extends TestCase
|
||||
{
|
||||
public function testIsValid(): void
|
||||
{
|
||||
$message = 'All fine';
|
||||
$validator = new OkInformativeValidator($message);
|
||||
|
||||
$this->assertTrue($validator->isValid());
|
||||
}
|
||||
|
||||
public function testGetMessage(): void
|
||||
{
|
||||
$message = 'All fine';
|
||||
$validator = new OkInformativeValidator($message);
|
||||
|
||||
$this->assertEquals($validator->getMessage(), $message);
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Messages\UpperRangeInformativeValidator;
|
||||
use App\Models\Validation\Messages\OkInformativeValidator;
|
||||
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class UpperRangeInformativeValidatorTest extends TestCase
|
||||
{
|
||||
protected $messages = [
|
||||
'ok' => 'valid',
|
||||
'error' => 'invalid',
|
||||
];
|
||||
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Valid Case for 10' => [
|
||||
'value' => 10,
|
||||
'limit' => 10,
|
||||
'isValid' => true,
|
||||
'message' => $this->messages['ok'],
|
||||
],
|
||||
'Valid Case for 255' => [
|
||||
'value' => 0,
|
||||
'limit' => 255,
|
||||
'isValid' => true,
|
||||
'message' => $this->messages['ok'],
|
||||
],
|
||||
'Invalid Case for 10' => [
|
||||
'value' => 11,
|
||||
'limit' => 10,
|
||||
'isValid' => false,
|
||||
'message' => $this->messages['error'],
|
||||
],
|
||||
'Invalid Case for 255' => [
|
||||
'value' => 256,
|
||||
'limit' => 255,
|
||||
'isValid' => false,
|
||||
'message' => $this->messages['error'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function setUpValidator(int $value, int $rangeLimit): UpperRangeInformativeValidator
|
||||
{
|
||||
$upperRangeValidator = new UpperRangeValidator($value, $rangeLimit, new OkValidator());
|
||||
|
||||
return new UpperRangeInformativeValidator(
|
||||
$this->messages['error'],
|
||||
$upperRangeValidator,
|
||||
new OkInformativeValidator($this->messages['ok']),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage(int $value, int $limit, bool $isValid, string $message): void
|
||||
{
|
||||
$validator = $this->setUpValidator($value, $limit);
|
||||
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
$this->assertEquals($validator->getMessage(), $message);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue