parent
36a803131e
commit
a37992c2e9
@ -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\CompanyValidatorByName;
|
||||
|
||||
class CompanyInformativeValidatorByNameFactory extends InformativeValidatorFactory
|
||||
{
|
||||
protected array $messages;
|
||||
protected string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->messages = [
|
||||
'found' => 'A company with the name is valid.',
|
||||
'not_found' => 'A company with the name does not exist!!!',
|
||||
'invalid_name' => 'The company 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 CompanyValidatorByName($this->name, $okValidator),
|
||||
$okValidator
|
||||
);
|
||||
|
||||
return new NextInformativeValidator(
|
||||
$this->messages['invalid_name'],
|
||||
new EmptyNameValidator($this->name, $nameValidator),
|
||||
$nameValidator
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\Validator;
|
||||
use App\Models\Company;
|
||||
|
||||
class CompanyValidatorByName 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 = Company::where('name', $this->name)->count();
|
||||
|
||||
return $count != 0;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\Messages\Factories\CompanyInformativeValidatorByNameFactory;
|
||||
|
||||
class CompanyInformativeValidatorByNameFactoryTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Found Case' => [
|
||||
'name' => 'testCompany',
|
||||
'key' => 'ok',
|
||||
'message' => 'A company with the name is valid.',
|
||||
'isValid' => true,
|
||||
],
|
||||
'Not Found Case' => [
|
||||
'name' => '404 Company',
|
||||
'key' => 'error',
|
||||
'message' => 'A company with the name does not exist!!!',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Invalid Case' => [
|
||||
'name' => '',
|
||||
'key' => 'error',
|
||||
'message' => 'The company name is empty, please, write the name!!!',
|
||||
'isValid' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
|
||||
{
|
||||
$factory = new CompanyInformativeValidatorByNameFactory($name);
|
||||
$validator = $factory->create();
|
||||
|
||||
$this->assertEquals($validator->getMessage(), $message);
|
||||
$this->assertEquals($validator->getOkStatus(), [$key => $message]);
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue