Compare commits

..
2 Commits
Author SHA1 Message Date
KKlochko e057d2de27 Add EmptyNameValidator and ItemValidatorByName. 2023-08-27 20:31:26 +03:00
KKlochko 3ffc82ee7c Refactor InformativeValidator. 2023-08-27 20:25:43 +03:00
7 changed files with 135 additions and 52 deletions
@@ -3,16 +3,15 @@
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Validators\UpperRangeValidator;
use App\Models\Validation\Validators\Validator;
class UpperRangeInformativeValidator extends InformativeValidator {
class NextInformativeValidator extends InformativeValidator {
protected InformativeValidator $nextValidator;
public function __construct(string $message, UpperRangeValidator $validator, InformativeValidator $nextValidator)
public function __construct(string $message, Validator $validator, InformativeValidator $nextValidator)
{
$this->setMessage($message);
$this->setValidator($validator);
$this->nextValidator = $nextValidator;
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\Validator;
class EmptyNameValidator extends Validator {
private string $name;
protected Validator $nextValidator;
public function __construct(string $name, Validator $nextValidator)
{
$this->name = $name;
$this->nextValidator = $nextValidator;
}
public function isCurrentValid(): bool
{
return $this->name != "";
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\Validator;
use App\Models\Item;
class ItemValidatorByName extends Validator {
private string $name;
protected Validator $nextValidator;
public function __construct(string $name, Validator $nextValidator)
{
$this->name = $name;
$this->nextValidator = $nextValidator;
}
public function isCurrentValid(): bool
{
$count = Item::where('name', $this->name)->count();
return $count != 0;
}
}
@@ -1,45 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\ItemValidationByName;
class ItemValidationByNameTest extends TestCase
{
public function dataProvider() {
return [
'Invalid Case' => [
'name' => '',
'key' => 'error',
'message' => 'The item name is empty, please, write the name!!!',
'isValid' => false,
],
'Not Found Case' => [
'name' => '404 Item',
'key' => 'error',
'message' => 'A item with the name does not exist!!!',
'isValid' => false,
],
'Found Case' => [
'name' => 'Pizza Polo',
'key' => 'ok',
'message' => 'A item with the name is valid.',
'isValid' => true,
]
];
}
/**
* @dataProvider dataProvider
*/
public function testItemValidationWithName(string $name, string $key, string $message, bool $isValid): void
{
$validator = new ItemValidationByName($name);
$json = $validator->getMessageMap();
$this->assertEquals($json[$key], $message);
$this->assertEquals($validator->isValid(), $isValid);
}
}
@@ -0,0 +1,43 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Validators\ItemValidatorByName;
use App\Models\Validation\Validators\OkValidator;
class ItemValidatorByNameTest extends TestCase
{
public function dataProvider() {
return [
'Invalid Case' => [
'name' => '',
'isValid' => false,
],
'Not Found Case' => [
'name' => '404 Item',
'isValid' => false,
],
'Found Case' => [
'name' => 'Pizza Polo',
'isValid' => true,
]
];
}
public function setUpValidator(string $name): ItemValidatorByName
{
return new ItemValidatorByName($name, new OkValidator());
}
/**
* @dataProvider dataProvider
*/
public function testGetMessage(string $name, bool $isValid): void
{
$validator = $this->setUpValidator($name);
$this->assertEquals($validator->isValid(), $isValid);
}
}
@@ -4,10 +4,11 @@ namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Validation\Messages\UpperRangeInformativeValidator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator;
use App\Models\Validation\Validators\UpperRangeValidator;
use App\Models\Validation\Validators\OkValidator;
use App\Models\Validation\Validators\Validator;
class UpperRangeInformativeValidatorTest extends TestCase
{
@@ -45,11 +46,11 @@ class UpperRangeInformativeValidatorTest extends TestCase
];
}
public function setUpValidator(int $value, int $rangeLimit): UpperRangeInformativeValidator
public function setUpValidator(int $value, int $rangeLimit): NextInformativeValidator
{
$upperRangeValidator = new UpperRangeValidator($value, $rangeLimit, new OkValidator());
return new UpperRangeInformativeValidator(
return new NextInformativeValidator(
$this->messages['error'],
$upperRangeValidator,
new OkInformativeValidator($this->messages['ok']),
@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\OkValidator;
class EmptyNameValidatorTest extends TestCase
{
public function dataProvider() {
return [
'Valid Case' => [
'name' => 'name',
'isValid' => true,
],
'Invalid Case' => [
'name' => '',
'isValid' => false,
],
];
}
public function setUpValidator(string $name): EmptyNameValidator
{
return new EmptyNameValidator($name, new OkValidator());
}
/**
* @dataProvider dataProvider
*/
public function testGetMessage(string $name, bool $isValid): void
{
$validator = $this->setUpValidator($name);
$this->assertEquals($validator->isValid(), $isValid);
}
}