Add ItemInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing Details

main
KKlochko 2 years ago
parent e057d2de27
commit 9a83b398c0

@ -0,0 +1,11 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\InformativeValidator;
abstract class InformativeValidatorFactory
{
abstract function create(): InformativeValidator;
}

@ -0,0 +1,46 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\InformativeValidatorFactory;
use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\ItemValidatorByName;
class ItemInformativeValidatorByNameFactory extends InformativeValidatorFactory
{
protected array $messages;
protected string $name;
public function __construct(string $name)
{
$this->messages = [
'found' => 'A item with the name is valid.',
'not_found' => 'A item with the name does not exist!!!',
'invalid_name' => 'The item name is empty, please, write the name!!!',
];
$this->name = $name;
}
function create(): InformativeValidator
{
$okValidator = new OkInformativeValidator($this->messages['found']);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
new ItemValidatorByName($this->name, $okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
}
}

@ -0,0 +1,46 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Messages\Factories\ItemInformativeValidatorByNameFactory;
class ItemValidatorByNameFactoryTest 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 testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
{
$factory = new ItemInformativeValidatorByNameFactory($name);
$validator = $factory->create();
$this->assertEquals($validator->getMessage(), $message);
$this->assertEquals($validator->okStatus(), [$key => $message]);
$this->assertEquals($validator->isValid(), $isValid);
}
}
Loading…
Cancel
Save