Add EmptyNameValidator and ItemValidatorByName.

This commit is contained in:
2023-08-27 20:31:26 +03:00
parent 3ffc82ee7c
commit e057d2de27
5 changed files with 128 additions and 45 deletions
@@ -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);
}
}
@@ -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);
}
}