diff --git a/app/Models/Validation/Validators/EmptyNameValidator.php b/app/Models/Validation/Validators/EmptyNameValidator.php new file mode 100644 index 0000000..fb48d6c --- /dev/null +++ b/app/Models/Validation/Validators/EmptyNameValidator.php @@ -0,0 +1,21 @@ +name = $name; + $this->nextValidator = $nextValidator; + } + + public function isCurrentValid(): bool + { + return $this->name != ""; + } +} diff --git a/app/Models/Validation/Validators/ItemValidatorByName.php b/app/Models/Validation/Validators/ItemValidatorByName.php new file mode 100644 index 0000000..333981d --- /dev/null +++ b/app/Models/Validation/Validators/ItemValidatorByName.php @@ -0,0 +1,25 @@ +name = $name; + $this->nextValidator = $nextValidator; + } + + public function isCurrentValid(): bool + { + $count = Item::where('name', $this->name)->count(); + + return $count != 0; + } +} + diff --git a/tests/Feature/Validation/ItemValidationByNameTest.php b/tests/Feature/Validation/ItemValidationByNameTest.php deleted file mode 100644 index a89f776..0000000 --- a/tests/Feature/Validation/ItemValidationByNameTest.php +++ /dev/null @@ -1,45 +0,0 @@ - [ - '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); - } -} diff --git a/tests/Feature/Validation/Validators/ItemValidatorByNameTest.php b/tests/Feature/Validation/Validators/ItemValidatorByNameTest.php new file mode 100644 index 0000000..e6a8aac --- /dev/null +++ b/tests/Feature/Validation/Validators/ItemValidatorByNameTest.php @@ -0,0 +1,43 @@ + [ + '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); + } +} diff --git a/tests/Unit/Validation/Validators/EmptyNameValidatorTest.php b/tests/Unit/Validation/Validators/EmptyNameValidatorTest.php new file mode 100644 index 0000000..8f809d9 --- /dev/null +++ b/tests/Unit/Validation/Validators/EmptyNameValidatorTest.php @@ -0,0 +1,39 @@ + [ + '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); + } +}