From 7662d98bcefff674c8971c3bcab1dbf76cb3d414 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 26 Aug 2023 20:30:25 +0300 Subject: [PATCH] Add Informative Validators. --- .../Messages/InformativeValidator.php | 47 +++++++++++++ .../Messages/OkInformativeValidator.php | 25 +++++++ .../UpperRangeInformativeValidator.php | 18 +++++ .../Messages/OkInformativeValidatorTest.php | 26 +++++++ .../UpperRangeInformativeValidatorTest.php | 69 +++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 app/Models/Validation/Messages/InformativeValidator.php create mode 100644 app/Models/Validation/Messages/OkInformativeValidator.php create mode 100644 app/Models/Validation/Messages/UpperRangeInformativeValidator.php create mode 100644 tests/Unit/Validation/Messages/OkInformativeValidatorTest.php create mode 100644 tests/Unit/Validation/Messages/UpperRangeInformativeValidatorTest.php diff --git a/app/Models/Validation/Messages/InformativeValidator.php b/app/Models/Validation/Messages/InformativeValidator.php new file mode 100644 index 0000000..3a7290c --- /dev/null +++ b/app/Models/Validation/Messages/InformativeValidator.php @@ -0,0 +1,47 @@ +validator->isCurrentValid(); + } + + public function isValid(): bool + { + return $this->validator->isValid(); + } + + public function setValidator($validator): void + { + $this->validator = $validator; + } + + public function setMessage($message): void + { + $this->message = $message; + } + + public function getMessage(): string + { + if(!$this->isCurrentValid()) + return $this->message; + + return $this->nextValidator->getMessage(); + } + + public function okStatus(): array + { + if(!$this->isCurrentValid()) + return ['error' => $this->message]; + + return $this->nextValidator->okStatus(); + } +} + diff --git a/app/Models/Validation/Messages/OkInformativeValidator.php b/app/Models/Validation/Messages/OkInformativeValidator.php new file mode 100644 index 0000000..f06027d --- /dev/null +++ b/app/Models/Validation/Messages/OkInformativeValidator.php @@ -0,0 +1,25 @@ +message = $message; + $this->validator = new OkValidator(); + } + + public function getMessage(): string + { + return $this->message; + } + + public function okStatus(): array + { + return ['ok' => $this->getMessage()]; + } +} + diff --git a/app/Models/Validation/Messages/UpperRangeInformativeValidator.php b/app/Models/Validation/Messages/UpperRangeInformativeValidator.php new file mode 100644 index 0000000..77999ca --- /dev/null +++ b/app/Models/Validation/Messages/UpperRangeInformativeValidator.php @@ -0,0 +1,18 @@ +setMessage($message); + $this->setValidator($validator); + $this->nextValidator = $nextValidator; + } +} + diff --git a/tests/Unit/Validation/Messages/OkInformativeValidatorTest.php b/tests/Unit/Validation/Messages/OkInformativeValidatorTest.php new file mode 100644 index 0000000..409bf10 --- /dev/null +++ b/tests/Unit/Validation/Messages/OkInformativeValidatorTest.php @@ -0,0 +1,26 @@ +assertTrue($validator->isValid()); + } + + public function testGetMessage(): void + { + $message = 'All fine'; + $validator = new OkInformativeValidator($message); + + $this->assertEquals($validator->getMessage(), $message); + } +} diff --git a/tests/Unit/Validation/Messages/UpperRangeInformativeValidatorTest.php b/tests/Unit/Validation/Messages/UpperRangeInformativeValidatorTest.php new file mode 100644 index 0000000..670a351 --- /dev/null +++ b/tests/Unit/Validation/Messages/UpperRangeInformativeValidatorTest.php @@ -0,0 +1,69 @@ + 'valid', + 'error' => 'invalid', + ]; + + public function dataProvider() { + return [ + 'Valid Case for 10' => [ + 'value' => 10, + 'limit' => 10, + 'isValid' => true, + 'message' => $this->messages['ok'], + ], + 'Valid Case for 255' => [ + 'value' => 0, + 'limit' => 255, + 'isValid' => true, + 'message' => $this->messages['ok'], + ], + 'Invalid Case for 10' => [ + 'value' => 11, + 'limit' => 10, + 'isValid' => false, + 'message' => $this->messages['error'], + ], + 'Invalid Case for 255' => [ + 'value' => 256, + 'limit' => 255, + 'isValid' => false, + 'message' => $this->messages['error'], + ], + ]; + } + + public function setUpValidator(int $value, int $rangeLimit): UpperRangeInformativeValidator + { + $upperRangeValidator = new UpperRangeValidator($value, $rangeLimit, new OkValidator()); + + return new UpperRangeInformativeValidator( + $this->messages['error'], + $upperRangeValidator, + new OkInformativeValidator($this->messages['ok']), + ); + } + + /** + * @dataProvider dataProvider + */ + public function testGetMessage(int $value, int $limit, bool $isValid, string $message): void + { + $validator = $this->setUpValidator($value, $limit); + + $this->assertEquals($validator->isValid(), $isValid); + $this->assertEquals($validator->getMessage(), $message); + } +}