From 1a7c585aa7df9448ef64395ea0cac27db7bed586 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 18 Aug 2023 13:20:09 +0300 Subject: [PATCH] Add CityValidationByName. --- .../Controllers/API/v2/CartController.php | 9 ++-- app/Models/City.php | 26 ++++------- .../Validation/CityValidationByName.php | 18 ++++++++ .../Messages/CityMessagesFactory.php | 20 +++++++++ .../Validation/CityValidationByNameTest.php | 45 +++++++++++++++++++ .../Feature/Validation/CityValidationTest.php | 37 --------------- 6 files changed, 96 insertions(+), 59 deletions(-) create mode 100644 app/Models/Validation/CityValidationByName.php create mode 100644 app/Models/Validation/Messages/CityMessagesFactory.php create mode 100644 tests/Feature/Validation/CityValidationByNameTest.php delete mode 100644 tests/Feature/Validation/CityValidationTest.php diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index fea8938..43de7b8 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -21,6 +21,7 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher; use App\DotsAPI\Fetcher\v2\AuthApiSender; use App\Http\Resources\API\v2\CartItemCollection; +use App\Models\Validation\CityValidationByName; use App\Models\Validation\CompanyValidationByName; class CartController extends Controller @@ -121,10 +122,9 @@ class CartController extends Controller if(array_key_exists('error', $validation)) return response()->json($validation); - // check for not valid city - $validation = City::validateWithName($cityName); - if(array_key_exists('error', $validation)) - return response()->json($validation); + $validator = new CityValidationByName($cityName); + if(!$validator->isValid()) + return response()->json($validator->getMessageMap()); // Get objects $user = User::where('matrix_username', $matrixUsername)->first(); @@ -166,7 +166,6 @@ class CartController extends Controller if(array_key_exists('error', $validation)) return response()->json($validation); - // check for not valid company $validator = new CompanyValidationByName($companyName); if(!$validator->isValid()) return response()->json($validator->getMessageMap()); diff --git a/app/Models/City.php b/app/Models/City.php index f9f44e7..ce6ced7 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -6,9 +6,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use App\Models\Validation\ValidationByNameInterface; use App\Models\Company; -class City extends Model +class City extends Model implements ValidationByNameInterface { use HasFactory; @@ -58,24 +59,15 @@ class City extends Model $this->companies()->detach($company_ids); } - public static function validateWithName(string $name) + public static function isExistByName(string $name): bool { - $name = $name ?? ''; + $count = City::where('name', $name)->count(); - if($name == '') - return [ - 'error' => 'The city name is empty, please, write the name!!!' - ]; - - $city = City::where('name', $name)->first(); - - if(!$city) - return [ - 'error' => 'A city with the name does not exist!!!' - ]; + return $count != 0; + } - return [ - 'ok' => 'A city with the name is valid.' - ]; + public static function isNameValid(string $name): bool + { + return $name != ''; } } diff --git a/app/Models/Validation/CityValidationByName.php b/app/Models/Validation/CityValidationByName.php new file mode 100644 index 0000000..6ce2b6c --- /dev/null +++ b/app/Models/Validation/CityValidationByName.php @@ -0,0 +1,18 @@ +create(), + ); + } +} diff --git a/app/Models/Validation/Messages/CityMessagesFactory.php b/app/Models/Validation/Messages/CityMessagesFactory.php new file mode 100644 index 0000000..86ad608 --- /dev/null +++ b/app/Models/Validation/Messages/CityMessagesFactory.php @@ -0,0 +1,20 @@ + 'A city with the name is valid.', + 'not_found' => 'A city with the name does not exist!!!', + 'invalid_name' => 'The city name is empty, please, write the name!!!', + ]; + + public function create() + { + return new BaseMessages($this->messages); + } +} + diff --git a/tests/Feature/Validation/CityValidationByNameTest.php b/tests/Feature/Validation/CityValidationByNameTest.php new file mode 100644 index 0000000..c1b9500 --- /dev/null +++ b/tests/Feature/Validation/CityValidationByNameTest.php @@ -0,0 +1,45 @@ + [ + 'name' => '', + 'key' => 'error', + 'message' => 'The city name is empty, please, write the name!!!', + 'isValid' => false, + ], + 'Not Found Case' => [ + 'name' => '404 City', + 'key' => 'error', + 'message' => 'A city with the name does not exist!!!', + 'isValid' => false, + ], + 'Found Case' => [ + 'name' => 'testCity', + 'key' => 'ok', + 'message' => 'A city with the name is valid.', + 'isValid' => true, + ] + ]; + } + + /** + * @dataProvider dataProvider + */ + public function testCityValidationWithName(string $name, string $key, string $message, bool $isValid): void + { + $validator = new CityValidationByName($name); + $json = $validator->getMessageMap(); + + $this->assertEquals($json[$key], $message); + $this->assertEquals($validator->isValid(), $isValid); + } +} diff --git a/tests/Feature/Validation/CityValidationTest.php b/tests/Feature/Validation/CityValidationTest.php deleted file mode 100644 index 4dadfcb..0000000 --- a/tests/Feature/Validation/CityValidationTest.php +++ /dev/null @@ -1,37 +0,0 @@ -assertEquals($json['error'], 'The city name is empty, please, write the name!!!'); - } - - public function testNotExistingCityWithName(): void - { - $name = '404 City'; - - $json = City::validateWithName($name); - - $this->assertEquals($json['error'], 'A city with the name does not exist!!!'); - } - - public function testValidCityWithName(): void - { - $name = 'testCity'; - - $json = City::validateWithName($name); - - $this->assertEquals($json['ok'], 'A city with the name is valid.'); - } -}