diff --git a/app/Models/Validation/Messages/Factories/CompanyInformativeValidatorByNameFactory.php b/app/Models/Validation/Messages/Factories/CompanyInformativeValidatorByNameFactory.php new file mode 100644 index 0000000..289621d --- /dev/null +++ b/app/Models/Validation/Messages/Factories/CompanyInformativeValidatorByNameFactory.php @@ -0,0 +1,46 @@ +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 + ); + } +} + diff --git a/app/Models/Validation/Validators/CompanyValidatorByName.php b/app/Models/Validation/Validators/CompanyValidatorByName.php new file mode 100644 index 0000000..c827d51 --- /dev/null +++ b/app/Models/Validation/Validators/CompanyValidatorByName.php @@ -0,0 +1,25 @@ +name = $name; + $this->nextValidator = $nextValidator; + } + + public function isCurrentValid(): bool + { + $count = Company::where('name', $this->name)->count(); + + return $count != 0; + } +} + diff --git a/tests/Feature/Validation/Factories/CompanyInformativeValidatorByNameFactoryTest.php b/tests/Feature/Validation/Factories/CompanyInformativeValidatorByNameFactoryTest.php new file mode 100644 index 0000000..8c41169 --- /dev/null +++ b/tests/Feature/Validation/Factories/CompanyInformativeValidatorByNameFactoryTest.php @@ -0,0 +1,46 @@ + [ + '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); + } +}